UNPKG

@web-atoms/core-docs

Version:
219 lines 7.25 kB
(function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "./AtomBinder"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AtomList = void 0; const AtomBinder_1 = require("./AtomBinder"); /** * * * @export * @class AtomList * @extends {Array<T>} * @template T */ class AtomList extends Array { // private version: number = 1; constructor() { super(); this.startValue = 0; this.totalValue = 0; this.sizeValue = 10; // tslint:disable-next-line this["__proto__"] = AtomList.prototype; this.next = () => { this.start = this.start + this.size; }; this.prev = () => { if (this.start >= this.size) { this.start = this.start - this.size; } }; } get start() { return this.startValue; } set start(v) { if (v === this.startValue) { return; } this.startValue = v; AtomBinder_1.AtomBinder.refreshValue(this, "start"); } get total() { return this.totalValue; } set total(v) { if (v === this.totalValue) { return; } this.totalValue = v; AtomBinder_1.AtomBinder.refreshValue(this, "total"); } get size() { return this.sizeValue; } set size(v) { if (v === this.sizeValue) { return; } this.sizeValue = v; AtomBinder_1.AtomBinder.refreshValue(this, "size"); } /** * Adds the item in the list and refresh bindings * @param {T} item * @returns {number} * @memberof AtomList */ add(item) { const i = this.length; const n = this.push(item); AtomBinder_1.AtomBinder.invokeItemsEvent(this, "add", i, item); AtomBinder_1.AtomBinder.refreshValue(this, "length"); // this.version++; return n; } /** * Add given items in the list and refresh bindings * @param {Array<T>} items * @memberof AtomList */ addAll(items) { for (const item of items) { const i = this.length; this.push(item); AtomBinder_1.AtomBinder.invokeItemsEvent(this, "add", i, item); AtomBinder_1.AtomBinder.refreshValue(this, "length"); } // tslint:disable-next-line:no-string-literal const t = items["total"]; if (t) { this.total = t; } // this.version++; } /** * Replaces list with given items, use this * to avoid flickering in screen * @param {T[]} items * @memberof AtomList */ replace(items, start, size) { this.length = items.length; for (let i = 0; i < items.length; i++) { this[i] = items[i]; } this.refresh(); // tslint:disable-next-line:no-string-literal const t = items["total"]; if (t) { this.total = t; } if (start !== undefined) { this.start = start; } if (size !== undefined) { this.size = size; } } /** * Inserts given number in the list at position `i` * and refreshes the bindings. * @param {number} i * @param {T} item * @memberof AtomList */ insert(i, item) { const n = this.splice(i, 0, item); AtomBinder_1.AtomBinder.invokeItemsEvent(this, "add", i, item); AtomBinder_1.AtomBinder.refreshValue(this, "length"); } /** * Removes item at given index i and refresh the bindings * @param {number} i * @memberof AtomList */ removeAt(i) { const item = this[i]; this.splice(i, 1); AtomBinder_1.AtomBinder.invokeItemsEvent(this, "remove", i, item); AtomBinder_1.AtomBinder.refreshValue(this, "length"); } /** * Removes given item or removes all items that match * given lambda as true and refresh the bindings * @param {(T | ((i:T) => boolean))} item * @returns {boolean} `true` if any item was removed * @memberof AtomList */ remove(item) { if (item instanceof Function) { let index = 0; let removed = false; for (const it of this) { if (item(it)) { this.removeAt(index); removed = true; continue; } index++; } return removed; } const n = this.indexOf(item); if (n !== -1) { this.removeAt(n); return true; } return false; } /** * Removes all items from the list and refreshes the bindings * @memberof AtomList */ clear() { this.length = 0; this.refresh(); } refresh() { AtomBinder_1.AtomBinder.invokeItemsEvent(this, "refresh", -1, null); AtomBinder_1.AtomBinder.refreshValue(this, "length"); // this.version++; } watch(f, wrap) { if (wrap) { const fx = f; f = (function () { const p = []; // tslint:disable-next-line:prefer-for-of for (let i = 0; i < arguments.length; i++) { const iterator = arguments[i]; p.push(iterator); } return fx.call(this, p); }); } return AtomBinder_1.AtomBinder.add_CollectionChanged(this, f); } } exports.AtomList = AtomList; // tslint:disable Array.prototype["add"] = AtomList.prototype.add; Array.prototype["addAll"] = AtomList.prototype.addAll; Array.prototype["clear"] = AtomList.prototype.clear; Array.prototype["refresh"] = AtomList.prototype.refresh; Array.prototype["remove"] = AtomList.prototype.remove; Array.prototype["removeAt"] = AtomList.prototype.removeAt; Array.prototype["watch"] = AtomList.prototype.watch; Array.prototype["replace"] = AtomList.prototype.replace; Array.prototype["insert"] = AtomList.prototype.insert; }); //# sourceMappingURL=AtomList.js.map