spatial-navigation
Version:
A javascript-based implementation of Spatial Navigation.
117 lines (88 loc) • 4.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _EventAggregator = require('./EventAggregator');
var _EventAggregator2 = _interopRequireDefault(_EventAggregator);
var _constants = require('./util/constants');
var _Container = require('./Container');
var _Container2 = _interopRequireDefault(_Container);
var _ContainerNavigation = require('./ContainerNavigation');
var _ContainerNavigation2 = _interopRequireDefault(_ContainerNavigation);
var _ElementNavigation = require('./ElementNavigation');
var _ElementNavigation2 = _interopRequireDefault(_ElementNavigation);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Navigation = function () {
function Navigation() {
_classCallCheck(this, Navigation);
/**
* @type {null|Element}
*/
this.focusedElement = null;
this.bindListeners();
}
_createClass(Navigation, [{
key: 'bindListeners',
value: function bindListeners() {
_EventAggregator2.default.subscribe(_constants.EVENT_PREFIX + 'navigate', this.navigate.bind(this));
_EventAggregator2.default.subscribe(_constants.EVENT_PREFIX + 'focusElement', this.setFocusedElement.bind(this));
}
}, {
key: 'navigate',
value: function navigate(actionName) {
if (!~['up', 'down', 'left', 'right'].indexOf(actionName)) {
return;
}
var instanceToFocus = this.getInstanceToFocus(actionName);
if (!instanceToFocus) {
return;
}
this.focusInstance(instanceToFocus);
}
}, {
key: 'getInstanceToFocus',
value: function getInstanceToFocus(direction) {
var instance = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.focusedElement;
if (!Boolean(instance)) {
return null;
}
var instanceToFocus = _ElementNavigation2.default.getToNavigate(instance, direction);
// if there is no Element to navigate in passed direction
if (!instanceToFocus) {
// if parent of Element is Container
if (instance.parent.constructor.name === 'Container') {
instanceToFocus = _ContainerNavigation2.default.getToNavigate(instance.parent, direction);
}
// if parent of Element is Element
else {
return this.getInstanceToFocus(direction, instance.parent);
}
}
return instanceToFocus;
}
}, {
key: 'focusInstance',
value: function focusInstance(instance) {
var prevFocusedElement = this.focusedElement;
instance.focus();
// dispatch event of collection focused element belongs to
if (instance.parent && instance.parent.collection) {
instance.parent.collection.eventAggregator.dispatchEvent('onNavigate');
}
// dispatch event of parent of collection focused element belongs to
if (prevFocusedElement && instance.parent && prevFocusedElement.parent && instance.parent != prevFocusedElement.parent && instance.parent.parent && instance.parent.parent.collection) {
instance.parent.parent.collection.eventAggregator.dispatchEvent('onNavigate');
}
}
}, {
key: 'setFocusedElement',
value: function setFocusedElement(element) {
this.focusedElement = element;
}
}]);
return Navigation;
}();
exports.default = new Navigation();
module.exports = exports['default'];