var Class = require('../utils/Class'); var List = new Class({ initialize: function List(parent){ this.parent = parent; this.list = [] ; this.position = 0; } , add: function (child){ if (this.getIndex(child) === -1) { this.list.push(child); } return child; } , addAt: function (child, index){ if (index === undefined) { index = 0; } if ((_AN_Read_length('length', this.list)) === 0) { return this.add(child); } if (index >= 0 && index <= (_AN_Read_length('length', this.list))) { if (this.getIndex(child) === -1) { this.list.splice(index, 0, child); } } return child; } , addMultiple: function (children){ if (Array.isArray(children)) { for (var i = 0; i < (_AN_Read_length('length', children)); i++ ){ this.add(children[i]); } } return children; } , getAt: function (index){ return this.list[index]; } , getIndex: function (child){ return this.list.indexOf(child); } , sort: function (children){ if (children === undefined) { children = this.list; } return children.sort(this.sortIndexHandler.bind(this)); } , sortIndexHandler: function (childA, childB){ var indexA = this.getIndex(childA); var indexB = this.getIndex(childB); if (indexA < indexB) { return -1; } else if (indexA > indexB) { return 1; } return 0; } , getByKey: function (property, value){ for (var i = 0; i < (_AN_Read_length('length', this.list)); i++ ){ if (this.list[i][property] === value) { return this.list[i]; } } return null ; } , getByName: function (name){ return this.getByKey('name', name); } , getRandom: function (startIndex, length){ if (startIndex === undefined) { startIndex = 0; } if (length === undefined) { length = _AN_Read_length('length', this.list); } if (length === 0 || length > (_AN_Read_length('length', this.list))) { return null ; } var randomIndex = startIndex + Math.floor(Math.random() * length); return this.list[randomIndex]; } , getFirst: function (property, value, startIndex, endIndex){ if (startIndex === undefined) { startIndex = 0; } if (endIndex === undefined) { endIndex = _AN_Read_length('length', this.list); } for (var i = startIndex; i < endIndex; i++ ){ var child = this.list[i]; if (child[property] === value) { return child; } } return null ; } , getAll: function (property, value, startIndex, endIndex){ if (startIndex === undefined) { startIndex = 0; } if (endIndex === undefined) { endIndex = _AN_Read_length('length', this.list); } var output = [] ; for (var i = startIndex; i < endIndex; i++ ){ var child = this.list[i]; if (property) { if (child[property] === value) { output.push(child); } } else { output.push(child); } } return output; } , count: function (property, value){ var total = 0; for (var i = 0; i < (_AN_Read_length('length', this.list)); i++ ){ var child = this.list[i]; if (child[property] === value) { total++ ; } } return total; } , swap: function (child1, child2){ if (child1 === child2) { return ; } var index1 = this.getIndex(child1); var index2 = this.getIndex(child2); if (index1 < 0 || index2 < 0) { throw new Error('List.swap: Supplied objects must be children of the same list') } this.list[index1] = child2; this.list[index2] = child1; } , moveTo: function (child, index){ var currentIndex = this.getIndex(child); if (currentIndex === -1 || index < 0 || index >= (_AN_Read_length('length', this.list))) { throw new Error('List.moveTo: The supplied index is out of bounds') } this.list.splice(currentIndex, 1); this.list.splice(index, 0, child); return child; } , remove: function (child){ var index = this.list.indexOf(child); if (index !== -1) { this.list.splice(index, 1); } return child; } , removeAt: function (index){ var child = this.list[index]; if (child) { this.children.splice(index, 1); } return child; } , removeBetween: function (beginIndex, endIndex){ if (beginIndex === undefined) { beginIndex = 0; } if (endIndex === undefined) { endIndex = _AN_Read_length('length', this.list); } var range = endIndex - beginIndex; if (range > 0 && range <= endIndex) { var removed = this.list.splice(beginIndex, range); return removed; } else if (range === 0 && (_AN_Read_length('length', this.list)) === 0) { return [] ; } else { throw new Error('List.removeBetween: Range Error, numeric values are outside the acceptable range') } } , removeAll: function (){ var i = _AN_Read_length('length', this.list); while (i-- ){ this.remove(this.list[i]); } return this; } , shutdown: function (){ this.removeAll(); } , bringToTop: function (child){ if (this.getIndex(child) < (_AN_Read_length('length', this.list))) { this.remove(child); this.add(child); } return child; } , sendToBack: function (child){ if (this.getIndex(child) > 0) { this.remove(child); this.addAt(child, 0); } return child; } , moveUp: function (child){ var a = this.getIndex(child); if (a !== -1 && a < (_AN_Read_length('length', this.list)) - 1) { var b = this.getAt(a + 1); if (b) { this.swap(child, b); } } return child; } , moveDown: function (child){ var a = this.getIndex(child); if (a > 0) { var b = this.getAt(a - 1); if (b) { this.swap(child, b); } } return child; } , reverse: function (){ this.list.reverse(); return this; } , shuffle: function (){ for (var i = _AN_Read_length('length', this.list) - 1; i > 0; i-- ){ var j = Math.floor(Math.random() * (i + 1)); var temp = this.list[i]; this.list[i] = this.list[j]; this.list[j] = temp; } return this; } , replace: function (oldChild, newChild){ var index = this.getIndex(oldChild); if (index !== -1) { this.remove(oldChild); this.addAt(newChild, index); return oldChild; } } , exists: function (child){ return (this.list.indexOf(child) > -1); } , setAll: function (key, value){ for (var i = 0; i < (_AN_Read_length('length', this.list)); i++ ){ if (this.list[i]) { this.list[i][key] = value; } } } , each: function (callback, thisArg){ var args = [null ] ; for (var i = 1; i < (_AN_Read_length('length', arguments)); i++ ){ args.push(arguments[i]); } for (i = 0; i < (_AN_Read_length('length', this.list)); i++ ){ args[0] = this.list[i]; callback.apply(thisArg, args); } } , length: { get: function (){ return (_AN_Read_length('length', this.list)); } } , first: { get: function (){ this.position = 0; if ((_AN_Read_length('length', this.list)) > 0) { return this.list[0]; } else { return null ; } } } , last: { get: function (){ if ((_AN_Read_length('length', this.list)) > 0) { this.position = _AN_Read_length('length', this.list) - 1; return this.list[this.position]; } else { return null ; } } } , next: { get: function (){ if (this.position < (_AN_Read_length('length', this.list))) { this.position++ ; return this.list[this.position]; } else { return null ; } } } , previous: { get: function (){ if (this.position > 0) { this.position-- ; return this.list[this.position]; } else { return null ; } } } } ); module.exports = List;