Skip to content

Commit

Permalink
issue/2808 convert to es6 classes
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverfoster authored Nov 22, 2021
2 parents 9aa2016 + 5bcba15 commit a879d7f
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 293 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ The alternative text for this image. Assign [alt text](https://github.com/adaptl
<div float align=right><a href="#top">Back to Top</a></div>

----------------------------
**Version number:** 5.4.1 <a href="https://community.adaptlearning.org/" target="_blank"><img src="https://github.com/adaptlearning/documentation/blob/master/04_wiki_assets/plug-ins/images/adapt-logo-mrgn-lft.jpg" alt="adapt learning logo" align="right"></a>
**Version number:** 5.5.0 <a href="https://community.adaptlearning.org/" target="_blank"><img src="https://github.com/adaptlearning/documentation/blob/master/04_wiki_assets/plug-ins/images/adapt-logo-mrgn-lft.jpg" alt="adapt learning logo" align="right"></a>
**Framework versions:** 5.17.2+
**Author / maintainer:** Adapt Core Team with [contributors](https://github.com/adaptlearning/adapt-contrib-hotgraphic/graphs/contributors)
**Accessibility support:** WAI AA
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "adapt-contrib-hotgraphic",
"version": "5.4.1",
"version": "5.5.0",
"framework": ">=5.17.2",
"homepage": "https://github.com/adaptlearning/adapt-contrib-hotgraphic",
"bugs": "https://github.com/adaptlearning/adapt-contrib-hotgraphic/issues",
Expand Down
16 changes: 6 additions & 10 deletions js/adapt-contrib-hotgraphic.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
define([
'core/js/adapt',
'./hotgraphicView',
'core/js/models/itemsComponentModel'
], function(Adapt, HotgraphicView, ItemsComponentModel) {

return Adapt.register('hotgraphic', {
model: ItemsComponentModel.extend({}),
view: HotgraphicView
});
import Adapt from 'core/js/adapt';
import HotgraphicView from './hotgraphicView';
import ItemsComponentModel from 'core/js/models/itemsComponentModel';

export default Adapt.register('hotgraphic', {
model: ItemsComponentModel.extend({}),
view: HotgraphicView
});
284 changes: 140 additions & 144 deletions js/hotgraphicPopupView.js
Original file line number Diff line number Diff line change
@@ -1,144 +1,140 @@
define([
'core/js/adapt'
], function(Adapt) {

class HotgraphicPopupView extends Backbone.View {

className() {
return 'hotgraphic-popup';
}

events() {
return {
'click .js-hotgraphic-popup-close': 'closePopup',
'click .js-hotgraphic-controls-click': 'onControlClick'
};
}

initialize(...args) {
super.initialize(...args);
// Debounce required as a second (bad) click event is dispatched on iOS causing a jump of two items.
this.onControlClick = _.debounce(this.onControlClick.bind(this), 100);
this.listenToOnce(Adapt, 'notify:opened', this.onOpened);
this.listenTo(this.model.get('_children'), {
'change:_isActive': this.onItemsActiveChange,
'change:_isVisited': this.onItemsVisitedChange
});
this.render();
}

onOpened() {
this.applyNavigationClasses(this.model.getActiveItem().get('_index'));
this.updatePageCount();
this.handleTabs();
}

applyNavigationClasses (index) {
const itemCount = this.model.get('_items').length;
const canCycleThroughPagination = this.model.get('_canCycleThroughPagination');

const shouldEnableBack = index > 0 || canCycleThroughPagination;
const shouldEnableNext = index < itemCount - 1 || canCycleThroughPagination;
const $controls = this.$('.hotgraphic-popup__controls');

this.$('hotgraphic-popup__nav')
.toggleClass('first', !shouldEnableBack)
.toggleClass('last', !shouldEnableNext);

Adapt.a11y.toggleAccessibleEnabled($controls.filter('.back'), shouldEnableBack);
Adapt.a11y.toggleAccessibleEnabled($controls.filter('.next'), shouldEnableNext);
}

updatePageCount() {
const template = Adapt.course.get('_globals')._components._hotgraphic.popupPagination || '{{itemNumber}} / {{totalItems}}';
const labelText = Handlebars.compile(template)({
itemNumber: this.model.getActiveItem().get('_index') + 1,
totalItems: this.model.get('_items').length
});
this.$('.hotgraphic-popup__count').html(labelText);
}

handleTabs() {
Adapt.a11y.toggleHidden(this.$('.hotgraphic-popup__item:not(.is-active)'), true);
Adapt.a11y.toggleHidden(this.$('.hotgraphic-popup__item.is-active'), false);
}

onItemsActiveChange(item, _isActive) {
if (!_isActive) return;
const index = item.get('_index');
this.updatePageCount();
this.applyItemClasses(index);
this.handleTabs();
this.handleFocus(index);
}

applyItemClasses(index) {
this.$(`.hotgraphic-popup__item[data-index="${index}"]`).addClass('is-active').removeAttr('aria-hidden');
this.$(`.hotgraphic-popup__item[data-index="${index}"] .hotgraphic-popup__item-title`).attr('id', 'notify-heading');
this.$(`.hotgraphic-popup__item:not([data-index="${index}"])`).removeClass('is-active').attr('aria-hidden', 'true');
this.$(`.hotgraphic-popup__item:not([data-index="${index}"]) .hotgraphic-popup__item-title`).removeAttr('id');
}

handleFocus(index) {
Adapt.a11y.focusFirst(this.$('.hotgraphic-popup__inner .is-active'));
this.applyNavigationClasses(index);
}

onItemsVisitedChange(item, _isVisited) {
if (!_isVisited) return;

this.$('.hotgraphic-popup__item')
.filter(`[data-index="${item.get('_index')}"]`)
.addClass('is-visited');
}

render() {
const data = this.model.toJSON();
data.view = this;
const template = Handlebars.templates[this.constructor.template];
this.$el.html(template(data));
}

closePopup() {
Adapt.trigger('notify:close');
}

onControlClick(event) {
const direction = $(event.currentTarget).data('direction');
const index = this.getNextIndex(direction);
if (index === -1) return;

this.setItemState(index);
}

getNextIndex(direction) {
let index = this.model.getActiveItem().get('_index');
const lastIndex = this.model.get('_items').length - 1;

switch (direction) {
case 'back':
if (index > 0) return --index;
if (this.model.get('_canCycleThroughPagination')) return lastIndex;
break;
case 'next':
if (index < lastIndex) return ++index;
if (this.model.get('_canCycleThroughPagination')) return 0;
}
return -1;
}

setItemState(index) {
this.model.getActiveItem().toggleActive();

const nextItem = this.model.getItem(index);
nextItem.toggleActive();
nextItem.toggleVisited(true);
}

};

HotgraphicPopupView.template = 'hotgraphicPopup';

return HotgraphicPopupView;

});
import Adapt from 'core/js/adapt';

class HotgraphicPopupView extends Backbone.View {

className() {
return 'hotgraphic-popup';
}

events() {
return {
'click .js-hotgraphic-popup-close': 'closePopup',
'click .js-hotgraphic-controls-click': 'onControlClick'
};
}

initialize(...args) {
super.initialize(...args);
// Debounce required as a second (bad) click event is dispatched on iOS causing a jump of two items.
this.onControlClick = _.debounce(this.onControlClick.bind(this), 100);
this.listenToOnce(Adapt, 'notify:opened', this.onOpened);
this.listenTo(this.model.get('_children'), {
'change:_isActive': this.onItemsActiveChange,
'change:_isVisited': this.onItemsVisitedChange
});
this.render();
}

onOpened() {
this.applyNavigationClasses(this.model.getActiveItem().get('_index'));
this.updatePageCount();
this.handleTabs();
}

applyNavigationClasses (index) {
const itemCount = this.model.get('_items').length;
const canCycleThroughPagination = this.model.get('_canCycleThroughPagination');

const shouldEnableBack = index > 0 || canCycleThroughPagination;
const shouldEnableNext = index < itemCount - 1 || canCycleThroughPagination;
const $controls = this.$('.hotgraphic-popup__controls');

this.$('hotgraphic-popup__nav')
.toggleClass('first', !shouldEnableBack)
.toggleClass('last', !shouldEnableNext);

Adapt.a11y.toggleAccessibleEnabled($controls.filter('.back'), shouldEnableBack);
Adapt.a11y.toggleAccessibleEnabled($controls.filter('.next'), shouldEnableNext);
}

updatePageCount() {
const template = Adapt.course.get('_globals')._components._hotgraphic.popupPagination || '{{itemNumber}} / {{totalItems}}';
const labelText = Handlebars.compile(template)({
itemNumber: this.model.getActiveItem().get('_index') + 1,
totalItems: this.model.get('_items').length
});
this.$('.hotgraphic-popup__count').html(labelText);
}

handleTabs() {
Adapt.a11y.toggleHidden(this.$('.hotgraphic-popup__item:not(.is-active)'), true);
Adapt.a11y.toggleHidden(this.$('.hotgraphic-popup__item.is-active'), false);
}

onItemsActiveChange(item, _isActive) {
if (!_isActive) return;
const index = item.get('_index');
this.updatePageCount();
this.applyItemClasses(index);
this.handleTabs();
this.handleFocus(index);
}

applyItemClasses(index) {
this.$(`.hotgraphic-popup__item[data-index="${index}"]`).addClass('is-active').removeAttr('aria-hidden');
this.$(`.hotgraphic-popup__item[data-index="${index}"] .hotgraphic-popup__item-title`).attr('id', 'notify-heading');
this.$(`.hotgraphic-popup__item:not([data-index="${index}"])`).removeClass('is-active').attr('aria-hidden', 'true');
this.$(`.hotgraphic-popup__item:not([data-index="${index}"]) .hotgraphic-popup__item-title`).removeAttr('id');
}

handleFocus(index) {
Adapt.a11y.focusFirst(this.$('.hotgraphic-popup__inner .is-active'));
this.applyNavigationClasses(index);
}

onItemsVisitedChange(item, _isVisited) {
if (!_isVisited) return;

this.$('.hotgraphic-popup__item')
.filter(`[data-index="${item.get('_index')}"]`)
.addClass('is-visited');
}

render() {
const data = this.model.toJSON();
data.view = this;
const template = Handlebars.templates[this.constructor.template];
this.$el.html(template(data));
}

closePopup() {
Adapt.trigger('notify:close');
}

onControlClick(event) {
const direction = $(event.currentTarget).data('direction');
const index = this.getNextIndex(direction);
if (index === -1) return;

this.setItemState(index);
}

getNextIndex(direction) {
let index = this.model.getActiveItem().get('_index');
const lastIndex = this.model.get('_items').length - 1;

switch (direction) {
case 'back':
if (index > 0) return --index;
if (this.model.get('_canCycleThroughPagination')) return lastIndex;
break;
case 'next':
if (index < lastIndex) return ++index;
if (this.model.get('_canCycleThroughPagination')) return 0;
}
return -1;
}

setItemState(index) {
this.model.getActiveItem().toggleActive();

const nextItem = this.model.getItem(index);
nextItem.toggleActive();
nextItem.toggleVisited(true);
}

};

HotgraphicPopupView.template = 'hotgraphicPopup';

export default HotgraphicPopupView;
Loading

0 comments on commit a879d7f

Please sign in to comment.