core-cde
Version:
179 lines • 5.79 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataRowCollection = void 0;
const tslib_1 = require("tslib");
const LinkedList_1 = (0, tslib_1.__importDefault)(require("typescript-dotnet-commonjs/System/Collections/LinkedList"));
const Linq_1 = (0, tslib_1.__importDefault)(require("typescript-dotnet-commonjs/System.Linq/Linq"));
const Data_module_1 = require("../Data.module");
class DataRowCollection {
constructor(table) {
this._list = new LinkedList_1.default();
this._table = table;
}
// static reviver(key: string, value: any): any {
// // if (Utils.isNumber(key)){
// // return DataRow.fromJSON(value);
// // }
// return key === '' ? DataRowCollection.fromJSON(value) : value;
// }
static fromJSON(json, columns) {
if (typeof json === 'string') {
const obj = JSON.parse(json);
return DataRowCollection.fromJSON(obj, columns);
}
else {
const obj = JSON.parse(json.rowItems);
const realRows = [];
obj.forEach(r => {
const row = new Data_module_1.DataRow();
row.id = r.id;
const rowContent = new Array();
const dictRow = new Map();
r.items.forEach(val => dictRow.set(val.guidColumn, val));
const e = (0, Linq_1.default)(columns);
e.forEach((val) => {
var _a;
rowContent.push((_a = dictRow.get(val.id)) === null || _a === void 0 ? void 0 : _a.value);
});
row.silentSetItems(rowContent);
realRows.push(row);
});
const collection = Object.create(DataRowCollection.prototype);
const assign = Object.assign(collection, null, {
_list: new LinkedList_1.default(realRows)
});
return assign;
}
}
toJSON() {
return Object.assign({}, null, {
rowItems: JSON.stringify(this._list.toArray())
});
}
set(index, value) {
// const retVal = this._list.set(index, value);
const retVal = this._list.getNodeAt(index);
retVal.value = value;
if (retVal) {
this.updateIndex(index);
}
return retVal.value ? true : false;
// return retVal;
}
insert(index, value) {
// FIXME
// const nodeInsert = this._list.getNodeAt(index);
// if (!nodeInsert) {
// this._list.addLast(value)
// } else {
// this._list.addAfter(nodeInsert, value);
// }
// FIXME
const nodeInsert = this._list.getNodeAt(index);
if (index <= 0) {
this._list.addFirst(value);
}
else if (index >= this._list.count) {
this._list.addLast(value);
}
else {
this._list.addBefore(nodeInsert, value);
}
// this._list.insert(index, value);
this.updateIndex(index);
}
removeAt(index) {
const retVal = this._list.removeAt(index);
if (retVal) {
this.updateIndex(index);
}
return retVal;
}
add(entry) {
this._list.add(entry);
this.updateIndex(this._list.count - 1);
return this;
}
/**
* Добавление строки без генерации события
*/
silentAdd(entry) {
this._list.add(entry);
this.updateIndex(this._list.count - 1);
return this;
}
remove(entry, max) {
const index = this._list.toArray().indexOf(entry);
// const index = this._list.indexOf(entry);
const retVal = this._list.remove(entry, max);
if (index > -1) {
this.updateIndex(index);
}
return retVal;
}
clear() {
return this._list.clear();
}
importEntries(entries) {
const first = this._list.count;
const retVal = this._list.importEntries(entries);
if (retVal > 0) {
this.updateIndex(0);
this.updateIndex(first);
}
return retVal;
}
toArray() {
return this._list.toArray();
}
get count() {
return this._list.count;
}
get isReadOnly() {
return this._list.isReadOnly;
}
contains(entry) {
return this._list.contains(entry);
}
copyTo(target, index) {
return this._list.copyTo(target, index);
}
getEnumerator() {
return this._list.getEnumerator();
}
get(index) {
const node = this._list.getNodeAt(index);
if (!node) {
return null;
}
return node.value;
// return this._list.get(index);
}
get last() {
return this._list.last;
}
first() {
return this._list.first;
}
getNode(index) {
return this._list.getNodeAt(index);
}
indexOf(item) {
return this._list.toArray().indexOf(item);
// return this._list.indexOf(item);
}
// PRIVATE
/**
* Обновляет значения индекса DataRow после вставки/удаления
* @param start стартовый индекс
*/
updateIndex(start) {
for (let i = start; i < this._list.count; i++) {
const row = this._list.getValueAt(i);
// const row = this._list.get(i);
row.index = i;
}
}
}
exports.DataRowCollection = DataRowCollection;
//# sourceMappingURL=DataRowCollection.js.map