UNPKG

@aurigma/design-atoms-model

Version:

Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.

452 lines 14.6 kB
import { EventObject } from "./EventObject"; import { itemNotFoundInCollection, itemBelongsCollection } from "./Utils/Exceptions"; import Enumerable from "linq"; import "linq-iterator"; import { NotImplementedException, OutOfRangeException, ArgumentException } from "./Exception"; import { CollectionChangeType } from "./ICollectionChangeEventArgs"; let origEnumerableFrom = null; patch(Enumerable); export class Collection { constructor(...args) { this._collection = []; this["constructor"] = (getEnumerator) => { throw new NotImplementedException(); }; this._itemAddedEvent = new EventObject(); this._itemRemovedEvent = new EventObject(); this._itemMovedEvent = new EventObject(); this._enumerable = EnumerableFrom(this._collection); if (args.length === 0) return; this.setRange(args); } setRange(items) { this.clear(); this.addRange(items); } /** Adds the specified items to the end of the collection.*/ addRange(items) { if (items instanceof Collection) items = items._collection; if (isIEnumerable(items)) items = items.toArray(); for (var i = 0; i < items.length; i++) this.add(items[i]); function isIEnumerable(obj) { return "getEnumerator" in obj && "toArray" in obj; } } add(...items) { for (let item of items) this.insertAt(this._collection.length, item); } push(item) { /// <summary>Pushes the specified item to the collection.</summary> /// <param name="item" type="Type">The item to push.</param> this.add(item); return this.length; } /** * Inserts the specified item into the collection at the specified index. */ insertAt(index, items) { if (!Array.isArray(items)) items = [items]; for (let item of items) this._checkIfItemExists(item); if (index < 0 || index > this._collection.length) throw new OutOfRangeException(); this._collection.splice(index, 0, ...items); for (let i = 0; i < items.length; i++) { const item = items[i]; this._onItemAdded(this._collection.indexOf(item), item); } } remove(item) { if (!this.contains(item)) throw new ArgumentException(itemNotFoundInCollection); return this.removeAt(this.indexOf(item)); } /** * Removes the item at the specified index of the collection. */ removeAt(index) { /// <summary>Removes the item at the specified index of the collection.</summary> /// <param name="index" type="Number">The zero-based index of the item to remove.</param> if (index < 0 || index > this._collection.length - 1) throw new OutOfRangeException(); const item = this._collection[index]; this._collection.splice(index, 1); this._onItemRemoved(index, item); return item; } removeRange(from, to) { var prevCollection = this._collection.slice(); var deletedItems = this._collection.splice(from, to - from + 1); for (var i = 0; i < deletedItems.length; i++) { var item = deletedItems[i]; this._onItemRemoved(prevCollection.indexOf(item), item); } return deletedItems; } move(oldIndex, newIndex, supressEvent) { if (!(typeof supressEvent == "boolean")) supressEvent = false; var item = this._collection[oldIndex]; if (item && oldIndex >= 0 && newIndex >= 0 && oldIndex != newIndex) { this._collection.splice(oldIndex, 1); this._collection.splice(newIndex, 0, item); if (!supressEvent) this._onItemMoved(oldIndex, newIndex); } return item; } /** Removes all items from the collection */ clear() { while (this.length > 0) { this.removeAt(this.length - 1); } } replaceAt(item, index) { if (index < 0 || index > this._collection.length - 1) throw new OutOfRangeException(); this._onItemRemoved(index, this._collection[index]); this._collection[index] = item; this._onItemAdded(index, this._collection[index]); } get length() { return this._collection.length; } get empty() { return this.length === 0; } //[index: number]: T; get(index) { return this.getItem(index); } set(index, value) { throw new NotImplementedException(); } getItem(index) { /// <summary>Gets the item at the specified index.</summary> /// <param name="index" type="Number">The zero-based index of the item to get.</param> /// <value type="Type">The item at the specified index.</value> /// <remarks><para>This property corresponds to <see cref="P:Aurigma.GraphicsMill.AjaxControls.VectorObjects.Collection`1.Item(System.Int32)">Item(Int32)</see> server-side member.</para></remarks> return this._collection[index]; } _checkIfItemExists(item) { if (this.contains(item)) throw new ArgumentException(itemBelongsCollection); } _onItemAdded(index, item) { this._itemAddedEvent.notify({ item: item, index: index, type: CollectionChangeType.Add }); } add_itemAdded(h) { this._itemAddedEvent.add(h); } remove_itemAdded(h) { this._itemAddedEvent.remove(h); } _onItemRemoved(index, item) { this._itemRemovedEvent.notify({ item: item, index: index, type: CollectionChangeType.Remove }); } add_itemRemoved(h) { this._itemRemovedEvent.add(h); } remove_itemRemoved(h) { this._itemRemovedEvent.remove(h); } _onItemMoved(oldIndex, newIndex) { this._itemMovedEvent.notify({ oldIndex: oldIndex, newIndex: newIndex, type: CollectionChangeType.Move }); } add_itemMoved(h) { this._itemMovedEvent.add(h); } remove_itemMoved(h) { this._itemMovedEvent.remove(h); } add_collectionChanged(h) { this.add_itemMoved(h); this.add_itemRemoved(h); this.add_itemAdded(h); } remove_collectionChanged(h) { this.remove_itemMoved(h); this.remove_itemRemoved(h); this.remove_itemAdded(h); } //#region Iterable implementation [Symbol.iterator]() { return this._collection.values(); } //#endregion toArray() { return this._collection.slice(); } //#region IEnumerable implementation getEnumerator() { return this._enumerable.getEnumerator(); } traverseBreadthFirst(...args) { return this._enumerable.traverseBreadthFirst(...args); } traverseDepthFirst(...args) { return this._enumerable.traverseDepthFirst(...args); } flatten() { return this._enumerable.flatten(); } pairwise(selector) { return this._enumerable.pairwise(selector); } scan(...args) { return this._enumerable.scan(...args); } select(selector) { return this._enumerable.select(selector); } selectMany(...args) { return this._enumerable.selectMany(...args); } where(predicate) { return this._enumerable.where(predicate); } choose(selector) { return this._enumerable.choose(selector); } ofType(type) { return this._enumerable.ofType(type); } zip(...args) { return this._enumerable.zip(...args); } merge(...args) { return this._enumerable.merge(...args); } join(...args) { return this._enumerable.join(...args); } groupJoin(...args) { return this._enumerable.groupJoin(...args); } all(predicate) { return this._enumerable.all(predicate); } any(predicate) { return this._enumerable.any(predicate); } isEmpty() { return this._enumerable.isEmpty(); } concat(...args) { return this._enumerable.concat(...args); } alternate(...args) { return this._enumerable.alternate(...args); } defaultIfEmpty(defaultValue) { return this._enumerable.defaultIfEmpty(defaultValue); } distinct(...args) { return this._enumerable.distinct(...args); } distinctUntilChanged(...args) { return this._enumerable.distinctUntilChanged(...args); } except(...args) { return this._enumerable.except(...args); } intersect(...args) { return this._enumerable.intersect(...args); } union(...args) { return this._enumerable.union(...args); } sequenceEqual(...args) { return this._enumerable.sequenceEqual(...args); } orderBy(keySelector) { return this._enumerable.orderBy(keySelector); } orderByDescending(keySelector) { return this._enumerable.orderByDescending(keySelector); } reverse() { return this._enumerable.reverse(); } shuffle() { return this._enumerable.shuffle(); } weightedSample(weightSelector) { return this._enumerable.weightedSample(weightSelector); } groupBy(...args) { return this._enumerable.groupBy(...args); } partitionBy(...args) { return this._enumerable.partitionBy(...args); } buffer(count) { return this._enumerable.buffer(count); } aggregate(...args) { return this._enumerable.aggregate(...args); } average(selector) { return this._enumerable.average(selector); } count(predicate) { return this._enumerable.count(predicate); } max(selector) { return this._enumerable.max(selector); } min(selector) { return this._enumerable.min(selector); } maxBy(keySelector) { return this._enumerable.maxBy(keySelector); } minBy(keySelector) { return this._enumerable.minBy(keySelector); } sum(selector) { return this._enumerable.sum(selector); } elementAt(index) { return this._enumerable.elementAt(index); } elementAtOrDefault(index, defaultValue) { return this._enumerable.elementAtOrDefault(index, defaultValue); } first(predicate) { return this._enumerable.first(predicate); } firstOrDefault(predicate, defaultValue) { if (typeof predicate === "function") return this._enumerable.firstOrDefault(predicate, defaultValue); return this._enumerable.firstOrDefault(defaultValue); } last(predicate) { return this._enumerable.last(predicate); } lastOrDefault(predicate, defaultValue) { if (typeof predicate === "function") return this._enumerable.lastOrDefault(predicate, defaultValue); return this._enumerable.lastOrDefault(defaultValue); } single(predicate) { return this._enumerable.single(predicate); } singleOrDefault(predicate, defaultValue) { return this._enumerable.singleOrDefault(predicate, defaultValue); } skip(count) { return this._enumerable.skip(count); } skipWhile(predicate) { return this._enumerable.skipWhile(predicate); } take(count) { return this._enumerable.take(count); } takeWhile(predicate) { return this._enumerable.takeWhile(predicate); } takeExceptLast(count) { return this._enumerable.takeExceptLast(count); } takeFromLast(count) { return this._enumerable.takeFromLast(count); } indexOf(...args) { return this._enumerable.indexOf(...args); } lastIndexOf(...args) { return this._enumerable.lastIndexOf(...args); } asEnumerable() { return this._enumerable; } cast() { return this._enumerable.cast(); } toLookup(...args) { return this._enumerable.toLookup(...args); } toObject(keySelector, elementSelector) { return this._enumerable.toObject(keySelector, elementSelector); } toDictionary(...args) { return this._enumerable.toDictionary(...args); } toJSONString(...args) { return this._enumerable.toJSONString(...args); } toJoinedString(...args) { return this._enumerable.toJoinedString(...args); } doAction(...args) { return this._enumerable.doAction(...args); } forEach(...args) { return this._enumerable.forEach(...args); } write(...args) { return this._enumerable.write(...args); } writeLine(...args) { return this._enumerable.writeLine(...args); } force() { return this._enumerable.force(); } letBind(...args) { return this._enumerable.letBind(...args); } share() { return this._enumerable.share(); } memoize() { return this._enumerable.memoize(); } catchError(handler) { return this._enumerable.catchError(handler); } finallyAction(finallyAction) { return this._enumerable.finallyAction(finallyAction); } log(...args) { return this._enumerable.log(...args); } trace(...args) { return this._enumerable.trace(...args); } insert(...args) { return this._enumerable.insert(...args); } contains(...args) { return this._enumerable.contains(...args); } } function patch(enumerable) { origEnumerableFrom = Enumerable.from; try { enumerable.from = (obj) => { if (obj instanceof Collection) return obj; return origEnumerableFrom(obj); }; } catch (ex) { console.error(`unable to patch Enumerable`, ex); } } function EnumerableFrom(enumerable) { if (enumerable instanceof Collection) return enumerable; else if (origEnumerableFrom) return origEnumerableFrom(enumerable); return Enumerable.from(enumerable); } //# sourceMappingURL=Collection.js.map