ListModel.js 1.8 KB

123456789101112131415161718192021
  1. export default class ListModel extends Common.Object{constructor(items){super();this._items=items||[];}
  2. [Symbol.iterator](){return this._items[Symbol.iterator]();}
  3. get length(){return this._items.length;}
  4. at(index){return this._items[index];}
  5. every(callback){return this._items.every(callback);}
  6. filter(callback){return this._items.filter(callback);}
  7. find(callback){return this._items.find(callback);}
  8. findIndex(callback){return this._items.findIndex(callback);}
  9. indexOf(value,fromIndex){return this._items.indexOf(value,fromIndex);}
  10. insert(index,value){this._items.splice(index,0,value);this._replaced(index,[],1);}
  11. insertWithComparator(value,comparator){this.insert(this._items.lowerBound(value,comparator),value);}
  12. join(separator){return this._items.join(separator);}
  13. remove(index){const result=this._items[index];this._items.splice(index,1);this._replaced(index,[result],0);return result;}
  14. replace(index,value){const oldValue=this._items[index];this._items[index]=value;this._replaced(index,[oldValue],1);return oldValue;}
  15. replaceRange(from,to,items){let removed;if(items.length<10000){removed=this._items.splice(from,to-from,...items);}else{removed=this._items.slice(from,to);const before=this._items.slice(0,from);const after=this._items.slice(to);this._items=[].concat(before,items,after);}
  16. this._replaced(from,removed,items.length);return removed;}
  17. replaceAll(items){const oldItems=this._items.slice();this._items=items;this._replaced(0,oldItems,items.length);return oldItems;}
  18. slice(from,to){return this._items.slice(from,to);}
  19. some(callback){return this._items.some(callback);}
  20. _replaced(index,removed,inserted){this.dispatchEventToListeners(Events.ItemsReplaced,{index:index,removed:removed,inserted:inserted});}}
  21. export const Events={ItemsReplaced:Symbol('ItemsReplaced'),};self.UI=self.UI||{};UI=UI||{};UI.ListModel=ListModel;UI.ListModel.Events=Events;