Skip to content

Commit

Permalink
JSX conversion
Browse files Browse the repository at this point in the history
- replace .hbs templates with .jsx
- change view template filenames
- create Boxmenu model
- condense view background style functions that are dependant on background image
  • Loading branch information
kirsty-hames committed May 21, 2024
1 parent 1fc32a3 commit 68729e0
Show file tree
Hide file tree
Showing 11 changed files with 301 additions and 196 deletions.
2 changes: 1 addition & 1 deletion js/BoxMenuGroupView.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BoxMenuGroupView extends MenuItemView {
}
}

BoxMenuGroupView.template = 'boxMenuGroup';
BoxMenuGroupView.template = 'boxMenuGroup.jsx';
BoxMenuGroupView.childContainer = '.js-group-children';
BoxMenuGroupView.childView = BoxMenuItemView;

Expand Down
2 changes: 1 addition & 1 deletion js/BoxMenuItemView.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class BoxMenuItemView extends MenuItemView {
}
}

BoxMenuItemView.template = 'boxMenuItem';
BoxMenuItemView.template = 'boxMenuItem.jsx';

export default BoxMenuItemView;
3 changes: 3 additions & 0 deletions js/BoxMenuModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import MenuModel from 'core/js/models/menuModel';

export default class BoxMenuModel extends MenuModel {}
54 changes: 24 additions & 30 deletions js/BoxMenuView.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class BoxMenuView extends MenuView {
nthChild++;
model.set({
_nthChild: nthChild,
_totalChild: totalChild
_totalChild: totalChild,
_isRendered: true
});

const ChildView = (model.get('_type') === 'menu' && model.get('_boxMenu') && model.get('_boxMenu')._renderAsGroup) ?
Expand All @@ -59,31 +60,27 @@ class BoxMenuView extends MenuView {
}

setStyles() {
this.addBackgroundLayer();
this.setBackgroundImage();
this.setBackgroundStyles();
this.processHeader();
}

addBackgroundLayer() {
setBackgroundImage() {
const backgroundImages = this.model.get('_boxmenu')?._backgroundImage;
if (!backgroundImages) return;

// add background layer
if (this.$el.find(' > .background').length) return;
this.$background = $('<div class="background" aria-hidden="true"></div>')
.prependTo(this.$el);
}

setBackgroundImage() {
const config = this.model.get('_boxMenu');
const backgroundImages = config?._backgroundImage;
if (!backgroundImages) return;
// set background image
const backgroundImage = backgroundImages[`_${device.screenSize}`] ?? backgroundImages._small;
this.$el.toggleClass('has-bg-image', Boolean(backgroundImage));
this.$background
.css('background-image', backgroundImage ? 'url(' + backgroundImage + ')' : '');
}

setBackgroundStyles() {
const config = this.model.get('_boxMenu');
const styles = config?._backgroundStyles;
// set background styles
const styles = this.model.get('_boxmenu')._backgroundStyles;
if (!styles) return;
this.$background.css({
'background-repeat': styles._backgroundRepeat,
Expand All @@ -93,42 +90,39 @@ class BoxMenuView extends MenuView {
}

processHeader() {
const config = this.model.get('_boxMenu');
const header = config?._menuHeader;
const header = this.model.get('_boxmenu')?._menuHeader;
if (!header) return;
const $header = this.$('.menu__header');
this.setHeaderTextAlignment(header);
this.addHeaderBackgroundLayer($header);
this.setHeaderBackgroundImage(header, $header);
this.setHeaderBackgroundStyles(header, $header);
this.setHeaderMinimumHeight(header, $header);
}

setHeaderTextAlignment(config) {
const textAlignment = config._textAlignment;
setHeaderTextAlignment(header) {
const textAlignment = header._textAlignment;
if (!textAlignment) return;

if (textAlignment._title) this.$el.addClass(`title-align-${textAlignment._title}`);
if (textAlignment._body) this.$el.addClass(`body-align-${textAlignment._body}`);
if (textAlignment._instruction) this.$el.addClass(`instruction-align-${textAlignment._instruction}`);
}

addHeaderBackgroundLayer($header) {
setHeaderBackgroundImage(header, $header) {
const backgroundImages = header._backgroundImage;
if (!backgroundImages) return;

// add header background layer
if ($header.find(' > .background').length) return;
this.$headerBackground = $('<div class="background" aria-hidden="true"></div>')
.prependTo($header);
}

setHeaderBackgroundImage(config, $header) {
const backgroundImages = config._backgroundImage;
if (!backgroundImages) return;
// add header background image
const backgroundImage = backgroundImages[`_${device.screenSize}`] ?? backgroundImages._small;
$header.toggleClass('has-bg-image', Boolean(backgroundImage));
this.$headerBackground.css('background-image', backgroundImage ? 'url(' + backgroundImage + ')' : '');
}

setHeaderBackgroundStyles(config, $header) {
const styles = config._backgroundStyles;
// set header background styles
const styles = header._backgroundStyles;
if (!styles) return;
this.$headerBackground.css({
'background-repeat': styles._backgroundRepeat,
Expand All @@ -137,8 +131,8 @@ class BoxMenuView extends MenuView {
});
}

setHeaderMinimumHeight(config, $header) {
const minimumHeights = config._minimumHeights;
setHeaderMinimumHeight(header, $header) {
const minimumHeights = header._minimumHeights;
if (!minimumHeights) return;
const minimumHeight = minimumHeights[`_${device.screenSize}`] ?? minimumHeights._small;
$header
Expand All @@ -148,6 +142,6 @@ class BoxMenuView extends MenuView {

}

BoxMenuView.template = 'boxMenu';
BoxMenuView.template = 'boxMenu.jsx';

export default BoxMenuView;
7 changes: 4 additions & 3 deletions js/adapt-contrib-boxMenu.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import components from 'core/js/components';
import MenuModel from 'core/js/models/menuModel';
import BoxMenuView from './BoxMenuView';
import BoxMenuModel from './BoxMenuModel';

// Use as default "_type": "course" or "_type": "menu" view.
// Note: This is necessary to maintain legacy behaviour in the AAT where
// only one menu is usable per course and the course / menu is assumed to be
// a core model and use the only installed MenuView.
components.register('course menu', {
view: BoxMenuView
view: BoxMenuView,
model: BoxMenuModel
});

// Use for "_component": "boxMenu", or "_view": "boxMenu" and "_model": "boxMenu"
components.register('boxMenu', {
view: BoxMenuView,
model: MenuModel.extend({})
model: BoxMenuModel
});
63 changes: 0 additions & 63 deletions templates/boxMenu.hbs

This file was deleted.

86 changes: 86 additions & 0 deletions templates/boxMenu.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import React from 'react';
import Adapt from 'core/js/adapt';
import { classes, compile } from 'core/js/reactHelpers';

export default function BoxMenu (props) {
const {
displayTitle,
subtitle,
body,
pageBody,
instruction
} = props;

const _graphic = Adapt.course.get('_boxmenu')?._graphic;

return (

<div className="menu__inner boxmenu__inner">

{(displayTitle || subtitle || body || instruction) &&
<div className="menu__header boxmenu__header">
<div className="menu__header-inner boxmenu__header-inner">

{_graphic?._src &&
<div className="menu__image-container boxmenu__image-container">
<img
className="menu__image boxmenu__image"
src={_graphic?._src}
alt={_graphic?.alt}
aria-hidden={!_graphic?.alt ? true : null}
/>
</div>
}

<div className="menu__header-content">

{displayTitle &&
<div className="menu__title boxmenu__title">
<div className={classes([
'menu__title-inner',
'boxmenu__title-inner',
'js-heading'
])} />
</div>
}

{subtitle &&
<div className="menu__subtitle boxmenu__subtitle">
<div className="menu__subtitle-inner boxmenu__subtitle-inner" dangerouslySetInnerHTML={{ __html: compile(subtitle) }}/>
</div>
}

{(body || pageBody) &&
<div className="menu__body boxmenu__body">
<div className="menu__body-inner boxmenu__body-inner" dangerouslySetInnerHTML={{ __html: compile(pageBody || body) }} />
</div>
}

{instruction &&
<div className="menu__instruction boxmenu__instruction">
<div className="menu__instruction-inner boxmenu__instruction-inner" dangerouslySetInnerHTML={{ __html: compile(instruction) }}/>
</div>
}

</div>

</div>
</div>
}

<div className="menu__item-container boxmenu__item-container">
<div
className={classes([
'menu__item-container-inner',
'boxmenu__item-container-inner',
'js-children'
])}
role="list"
>
{/* Menu items render here */}
</div>
</div>

</div>
);
}
43 changes: 0 additions & 43 deletions templates/boxMenuGroup.hbs

This file was deleted.

Loading

0 comments on commit 68729e0

Please sign in to comment.