@web-atoms/core
Version:
213 lines (212 loc) • 5.98 kB
JavaScript
System.register(["./AtomBinder"], function (_export, _context) {
"use strict";
var AtomBinder, AtomList, ap;
_export("AtomList", void 0);
return {
setters: [function (_AtomBinder) {
AtomBinder = _AtomBinder.AtomBinder;
}],
execute: function () {
_export("AtomList", AtomList = class AtomList extends Array {
constructor() {
super();
this.startValue = 0;
this.totalValue = 0;
this.sizeValue = 10;
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.refreshValue(this, "start");
}
get total() {
return this.totalValue;
}
set total(v) {
if (v === this.totalValue) {
return;
}
this.totalValue = v;
AtomBinder.refreshValue(this, "total");
}
get size() {
return this.sizeValue;
}
set size(v) {
if (v === this.sizeValue) {
return;
}
this.sizeValue = v;
AtomBinder.refreshValue(this, "size");
}
add(item) {
const i = this.length;
const n = this.push(item);
AtomBinder.invokeItemsEvent(this, "add", i, item);
AtomBinder.refreshValue(this, "length");
return n;
}
addAll(items) {
for (const item of items) {
const i = this.length;
this.push(item);
AtomBinder.invokeItemsEvent(this, "add", i, item);
AtomBinder.refreshValue(this, "length");
}
const t = items["total"];
if (t) {
this.total = t;
}
}
replace(items, start, size) {
this.length = items.length;
for (let i = 0; i < items.length; i++) {
this[i] = items[i];
}
this.refresh();
const t = items["total"];
if (t) {
this.total = t;
}
if (start !== undefined) {
this.start = start;
}
if (size !== undefined) {
this.size = size;
}
}
insert(i, item) {
const n = this.splice(i, 0, item);
AtomBinder.invokeItemsEvent(this, "add", i, item);
AtomBinder.refreshValue(this, "length");
}
set(i, item) {
const old = this[i];
this[i] = item;
AtomBinder.invokeItemsEvent(this, "replace", i, item, old);
AtomBinder.refreshValue(this, "length");
}
removeAt(i) {
const item = this[i];
this.splice(i, 1);
AtomBinder.invokeItemsEvent(this, "remove", i, item);
AtomBinder.refreshValue(this, "length");
return item;
}
remove(item) {
if (item instanceof Function) {
let removed = false;
for (let index = 0; index < this.length;) {
const it = this[index];
if (item(it)) {
this.splice(index, 1);
AtomBinder.invokeItemsEvent(this, "remove", index, it);
AtomBinder.refreshValue(this, "length");
removed = true;
continue;
}
index++;
}
return removed;
}
const n = this.indexOf(item);
if (n !== -1) {
this.removeAt(n);
return true;
}
return false;
}
clear() {
this.length = 0;
this.refresh();
}
refresh() {
AtomBinder.invokeItemsEvent(this, "refresh", -1, null);
AtomBinder.refreshValue(this, "length");
}
watch(f, wrap) {
if (wrap) {
const fx = f;
f = function () {
const p = [];
for (let i = 0; i < arguments.length; i++) {
const iterator = arguments[i];
p.push(iterator);
}
return fx.call(this, p);
};
}
return AtomBinder.add_CollectionChanged(this, f);
}
count(f) {
let total = 0;
for (const iterator of this) {
if (f(iterator)) {
total++;
}
}
return total;
}
avg(f) {
if (!this.length) {
return 0;
}
let sum = 0;
for (const iterator of this) {
sum += f(iterator);
}
return sum / this.length;
}
includes(item) {
return this.indexOf(item) !== -1;
}
});
ap = Array.prototype;
for (const key of Object.getOwnPropertyNames(AtomList.prototype)) {
const {
value
} = Object.getOwnPropertyDescriptor(AtomList.prototype, key);
switch (key) {
case "add":
case "addAll":
case "clear":
case "refresh":
case "set":
case "remove":
case "removeAt":
case "watch":
case "replace":
case "insert":
case "count":
case "avg":
case "includes":
if (key in ap) {
continue;
}
Object.defineProperty(ap, key, {
enumerable: false,
value,
configurable: true,
writable: true
});
continue;
}
}
}
};
});
//# sourceMappingURL=AtomList.js.map