spatial-navigation
Version:
A javascript-based implementation of Spatial Navigation.
101 lines (81 loc) • 2.62 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; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
//import Element from './Element'
//import Container from './Container'
var Collection = function () {
function Collection() {
_classCallCheck(this, Collection);
this.items = [];
this.itemIds = {};
}
_createClass(Collection, [{
key: 'add',
value: function add(item, name) {
this.push(item);
if (item.constructor.name === 'Element') {
return item;
}
//if (item.constructor.name === 'Container' && !name) {
if (item.constructor.name === 'Container' && !name) {
throw Error('You must pass name of item you try add to Collection');
}
if (!this.itemIds) {
this.itemIds = {};
}
if (name in this.itemIds) {
console.warn('Item with name "' + name + '" already exists in Collection');
}
this.itemIds[name] = this.items.length - 1;
return item;
}
}, {
key: 'unshift',
value: function unshift(item) {
this.items.unshift(item);
return item;
}
}, {
key: 'push',
value: function push(item) {
this.items.push(item);
return item;
}
}, {
key: 'indexOf',
value: function indexOf(item) {
return this.items.indexOf(item);
}
}, {
key: 'getByIndex',
value: function getByIndex(index) {
return this.items[index];
}
/**
*
* @param name
* @returns {*}
*/
}, {
key: 'getByName',
value: function getByName(name) {
return this.items[this.itemIds[name]];
}
}, {
key: 'isExists',
value: function isExists(name) {
return Boolean(this.getByName(name));
}
}, {
key: 'length',
get: function get() {
return this.items.length;
}
}]);
return Collection;
}();
exports.default = Collection;
module.exports = exports['default'];