spatial-navigation
Version:
A javascript-based implementation of Spatial Navigation.
138 lines (109 loc) • 5.54 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 _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
var _EventAggregator = require('./EventAggregator');
var _EventAggregator2 = _interopRequireDefault(_EventAggregator);
var _constants = require('./util/constants');
var _Collection2 = require('./Collection');
var _Collection3 = _interopRequireDefault(_Collection2);
var _Container = require('./Container');
var _Container2 = _interopRequireDefault(_Container);
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"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var ElementCollection = function (_Collection) {
_inherits(ElementCollection, _Collection);
function ElementCollection(parent, settings) {
_classCallCheck(this, ElementCollection);
var _this = _possibleConstructorReturn(this, (ElementCollection.__proto__ || Object.getPrototypeOf(ElementCollection)).call(this));
_this.parent = parent;
_this.settings = settings;
_this.focusedIndex = null;
_this.eventAggregator = new _EventAggregator.EventAggregator();
_this.lazyLoading = false;
_this.onLazyLoad = null;
return _this;
}
_createClass(ElementCollection, [{
key: '_extendElement',
value: function _extendElement(element) {
element.parent = this.parent;
//this.eventAggregator.dispatchEvent(`${EVENT_PREFIX}addElement`, item)
//EA.dispatchEvent(`${EVENT_PREFIX}addElement`, this.parent)
}
}, {
key: 'unshift',
value: function unshift(element) {
this._extendElement(element);
_get(ElementCollection.prototype.__proto__ || Object.getPrototypeOf(ElementCollection.prototype), 'unshift', this).call(this, element);
return element;
}
}, {
key: 'push',
value: function push(element) {
this._extendElement(element);
_get(ElementCollection.prototype.__proto__ || Object.getPrototypeOf(ElementCollection.prototype), 'push', this).call(this, element);
return element;
}
}, {
key: 'focus',
value: function focus() {
if (this.parent.constructor.name === 'Container' && !Boolean(this.length)) {
throw Error('You must add at least one element to each container. Check ' + this.parent.name + ' container');
}
var elementIndex = void 0;
if (this.focusedIndex === null) {
this.focusedIndex = elementIndex = 0;
} else {
// TODO this is never used!
switch (this.enterTo) {
case 'first':
elementIndex = 0;
break;
case 'last':
elementIndex = this.length - 1;
break;
default:
elementIndex = this.focusedIndex;
break;
}
}
var element = this.getByIndex(elementIndex);
element.focus();
}
}, {
key: 'getIndex',
value: function getIndex(element) {
return this.indexOf(element);
}
}, {
key: 'setFocusedIndex',
value: function setFocusedIndex(element) {
this.focusedIndex = this.getIndex(element);
}
}, {
key: 'lazyLoad',
value: function lazyLoad() {
var _this2 = this;
if (typeof this.onLazyLoad !== 'function' || this.lazyLoading) {
return;
}
this.lazyLoading = true;
this.onLazyLoad(function () {
_this2.lazyLoading = false;
});
}
}, {
key: 'subscribeLazyLoad',
value: function subscribeLazyLoad(requestWrapper) {
this.onLazyLoad = requestWrapper;
}
}]);
return ElementCollection;
}(_Collection3.default);
exports.default = ElementCollection;
module.exports = exports['default'];