spatial-navigation
Version:
A javascript-based implementation of Spatial Navigation.
111 lines (85 loc) • 3.45 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 _Keyboard = require('./Keyboard');
var _Keyboard2 = _interopRequireDefault(_Keyboard);
var _ElementCollection = require('./ElementCollection');
var _ElementCollection2 = _interopRequireDefault(_ElementCollection);
var _KeyMapNavigation = require('./KeyMapNavigation');
var _KeyMapNavigation2 = _interopRequireDefault(_KeyMapNavigation);
var _constants = require('./util/constants');
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 Container = function () {
function Container(name) {
var _this = this;
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_ref$map = _ref.map,
map = _ref$map === undefined ? {} : _ref$map,
keyBindings = _ref.keyBindings,
autoFocus = _ref.autoFocus;
_classCallCheck(this, Container);
this.name = name;
this.disabled = false;
this.focused = false;
this.collection = new _ElementCollection2.default(this);
this.leaveFor = map; // which Container will be focused on leave this Container
if (autoFocus) {
_EventAggregator2.default.once(_constants.EVENT_PREFIX + 'addElement', function (instance) {
if (_this === instance || _this === instance.getContainer()) {
_this.focus();
return true;
}
return false;
});
}
if (keyBindings) {
this.bindKeyAction(keyBindings);
}
}
/**
* @param name
* @param options
* @returns {Container}
*/
_createClass(Container, [{
key: 'bindKeyAction',
value: function bindKeyAction(mapping) {
var normalizedMap = _Keyboard2.default.addToMap(mapping);
_KeyMapNavigation2.default.addRelation(normalizedMap, this);
}
}, {
key: 'disable',
value: function disable() {
this.disabled = true;
}
}, {
key: 'enable',
value: function enable() {
this.disabled = false;
}
}, {
key: 'focus',
value: function focus() {
this.focused = true;
this.collection.focus();
_EventAggregator2.default.dispatchEvent(_constants.EVENT_PREFIX + 'focusContainer', this);
}
}, {
key: 'blur',
value: function blur() {
this.focused = false;
_EventAggregator2.default.dispatchEvent(_constants.EVENT_PREFIX + 'blurContainer', this);
}
}]);
return Container;
}();
Container.create = function (name, options) {
return new Container(name, options);
};
exports.default = Container;
module.exports = exports['default'];