Skip to content

Commit

Permalink
New: Add support for Tooltip API and Navigation Button API (fixes #16 #…
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverfoster authored Aug 14, 2023
2 parents b3a100c + a70f888 commit 04fbc9e
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 53 deletions.
49 changes: 29 additions & 20 deletions example.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
// Configuration options must be added and amended for all json files where required
// Configuration options must be added and amended for all JSON files where required

// config.json - to disable globally
"_homeButton": {
"_isEnabled": false
}
// config.json - Used to disable globally
"_homeButton": {
"_isEnabled": false
}

// course.json
"_homeButton": {
"_isEnabled": true,
"_hideHomeButton": false,
"_comment": "Amend co-00 to match the ID of the start / landing page",
"_redirectToId": "co-00",
"alt": "Introduction"
// course.json - Use for global settings and navigation order
"_extensions": {
"_homeButton": {
"_navOrder": -1,
"_showLabel": true,
"navLabel": "Home",
"_navTooltip": {
"_isEnabled": false,
"text": "Home"
}
}
}

// contentObjects.json
"_homeButton": {
"_isEnabled": true,
"_hideHomeButton": false,
"_hideBackButton": true,
"_redirectToId": "",
"alt": "Home"
}
// course.json or contentObjects - Use to configure at the menu or content object level
"_homeButton": {
"_isEnabled": true,
"_hideHomeButton": false,
"_comment": "Amend _redirectToId to match the ID of the start / landing page",
"_redirectToId": "",
"_comment": "Option to override navigation button label and tooltip",
"navLabel": "Introduction",
"_navTooltip": {
"_isEnabled": true,
"text": "Introduction"
}
}
36 changes: 36 additions & 0 deletions js/HomeNavigationButtonView.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Adapt from 'core/js/adapt';
import NavigationButtonView from 'core/js/views/NavigationButtonView';
import tooltips from 'core/js/tooltips';

class HomeNavigationButtonView extends NavigationButtonView {

attributes() {
return {
...super.attributes(),
'data-tooltip-id': this.model.get('_id')
};
}

initialize(options) {
super.initialize(options);
this.setupEventListeners();
this.render();
tooltips.register({
_id: this.model.get('_id'),
...this.model.get('_navTooltip') || {}
});
}

setupEventListeners() {
this.listenTo(Adapt, {
remove: this.remove
});
}

static get template() {
return 'HomeNavigationButton.jsx';
}

}

export default HomeNavigationButtonView;
53 changes: 38 additions & 15 deletions js/adapt-homeButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import Adapt from 'core/js/adapt';
import data from 'core/js/data';
import location from 'core/js/location';
import router from 'core/js/router';
import navigation from 'core/js/navigation';
import NavigationButtonModel from 'core/js/models/NavigationButtonModel';
import HomeNavigationButtonView from './HomeNavigationButtonView';

class HomeButton extends Backbone.Controller {

Expand All @@ -11,8 +14,10 @@ class HomeButton extends Backbone.Controller {
}

onDataReady() {
// Do not render if turned off at config level
const config = Adapt.config.get('_homeButton');
if (config?._isEnabled === false) return;

this.$html = $('html');
this.stopListening();
this.listenTo(Adapt, {
Expand All @@ -22,11 +27,15 @@ class HomeButton extends Backbone.Controller {
});
}

static get globalsConfig() {
return Adapt.course.get('_globals')?._extensions?._homeButton;
}

get currentModelConfig() {
return location._currentModel?.get('_homeButton');
}

get courseConfig() {
static get courseConfig() {
return Adapt.course?.get('_homeButton');
}

Expand All @@ -43,30 +52,44 @@ class HomeButton extends Backbone.Controller {

enable() {
const currentModelConfig = this.currentModelConfig;
const courseConfig = this.courseConfig;

// Update <html> classes
this.$html.toggleClass('hide-nav-home-btn', Boolean(currentModelConfig?._hideHomeButton));
// extend functionality to toggle back button display
this.$html.toggleClass('hide-nav-back-btn', Boolean(currentModelConfig?._hideBackButton));
const altText = (currentModelConfig?.alt || courseConfig?.alt || '');
const $backButton = $('button[data-event="backButton"]');
const $icon = $('<div>', { class: 'icon', 'aria-hidden': true });
const $homeButton = $('<button>', {
attr: {
'data-event': currentModelConfig?._redirectToId ? 'redirectedHomeButton' : 'homeButton'
},
class: 'btn-icon nav__btn nav__homebutton-btn js-nav-home-btn',
'aria-label': altText,
role: 'link'
}).append($icon);
// insert immediately after back button (so that tab order is correct)
$homeButton.insertAfter($backButton);

this.renderNavigationView();
}

renderNavigationView() {
const currentModelConfig = this.currentModelConfig;
const {
_navOrder = -1,
_showLabel = true,
navLabel = '',
_navTooltip = {}
} = Object.assign(HomeButton.globalsConfig ?? {}, currentModelConfig);
const model = new NavigationButtonModel({
_id: 'homebutton',
_order: _navOrder,
_showLabel,
_classes: 'btn-icon nav__btn nav__homebutton-btn',
_role: 'link',
ariaLabel: navLabel,
text: navLabel,
_navTooltip,
_event: currentModelConfig?._redirectToId ? 'redirectedHomeButton' : 'homeButton'
});
navigation.addButton(new HomeNavigationButtonView({ model }));
}

redirected() {
const redirectToId = this.currentModelConfig?._redirectToId;
if (!redirectToId) return;

const model = data.findById(redirectToId);
if (!model) return;

switch (model.get('_type')) {
case 'course':
router.navigateToHomeRoute();
Expand Down
15 changes: 5 additions & 10 deletions less/homeButton.less
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
.nav {
// replicate navigation back button float
&__homebutton-btn {
.u-float-left;
}

// hide home button class
.hide-nav-home-btn &__homebutton-btn {
.nav__homebutton-btn {
// Hide home button class
.hide-nav-home-btn & {
.u-display-none;
}

// Change the home button icon to use the back arrow on the menu
.location-menu &__homebutton-btn .icon {
.location-menu & .icon {
.icon-controls-small-left;
}

.location-page &__homebutton-btn .icon {
.location-page & .icon {
.icon-home;
}
}
46 changes: 46 additions & 0 deletions properties.schema
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,52 @@
"$schema": "http://json-schema.org/draft-04/schema",
"id": "http://jsonschema.net",
"required": false,
"globals": {
"_navOrder": {
"type": "number",
"required": true,
"title": "Navigation bar order",
"help": "Determines the order in which the button is displayed in the navigation bar. Negative numbers (e.g. -100) are left-aligned. Positive numbers (e.g. 100) are right-aligned.",
"default": 0,
"inputType": "Text",
"validators": []
},
"_showLabel": {
"type": "boolean",
"required": true,
"default": true,
"title": "Enable navigation bar button label",
"inputType": "Checkbox",
"validators": [],
"help": "Controls whether a label is shown on the navigation bar button."
},
"navLabel": {
"type": "string",
"required": true,
"default": "Home",
"title": "Navigation bar button label",
"inputType": "Text",
"validators": [],
"translatable": true
},
"_navTooltip": {
"type": "object",
"title": "Navigation tooltip",
"properties": {
"_isEnabled": {
"type": "boolean",
"title": "Enable tooltip for navigation button",
"default": true
},
"text": {
"type": "string",
"title": "",
"default": "Home",
"translatable": true
}
}
}
},
"properties": {
"pluginLocations": {
"type": "object",
Expand Down
65 changes: 57 additions & 8 deletions schema/course.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,63 @@
},
"with": {
"properties": {
"_globals": {
"type": "object",
"default": {},
"properties": {
"_extensions": {
"type": "object",
"default": {},
"properties": {
"_homeButton": {
"type": "object",
"title": "Home Button",
"default": {},
"properties": {
"_navOrder": {
"type": "number",
"title": "Navigation bar order",
"description": "Determines the order in which the button is displayed in the navigation bar. Negative numbers (e.g. -100) are left-aligned. Positive numbers (e.g. 100) are right-aligned.",
"default": -1
},
"_showLabel": {
"type": "boolean",
"title": "Enable navigation bar button label",
"default": true
},
"navLabel": {
"type": "string",
"title": "Navigation bar button label",
"default": "Home",
"_adapt": {
"translatable": true
}
},
"_navTooltip": {
"type": "object",
"title": "Navigation tooltip",
"properties": {
"_isEnabled": {
"type": "boolean",
"title": "Enable tooltip for navigation button",
"default": true
},
"text": {
"type": "string",
"title": "",
"default": "Home",
"_adapt": {
"translatable": true
}
}
}
}
}
}
}
}
}
},
"_homeButton": {
"type": "object",
"title": "Home Button",
Expand All @@ -28,14 +85,6 @@
"title": "Redirect the home button to id",
"description": "Enter the Friendly id of the page that the home button should direct the user to (ex. a splash page)",
"default": ""
},
"alt": {
"type": "string",
"title": "Home button alt text - applied globally",
"default": "Home",
"_adapt": {
"translatable": true
}
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions templates/HomeNavigationButton.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';
import { classes, compile } from 'core/js/reactHelpers';

export default function HomeNavigationButton(props) {
const {
text,
_iconClasses
} = props;
return (
<>
<span
className={classes([
'icon',
_iconClasses
])}
aria-hidden="true"
/>
<span
className="nav__btn-label"
aria-hidden="true"
dangerouslySetInnerHTML={{ __html: compile(text, props) }}
/>
</>
);
}

0 comments on commit 04fbc9e

Please sign in to comment.