UNPKG

rb-data-table

Version:
257 lines 9.64 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import { LocalSorter } from './local.sorter'; import { LocalFilter } from './local.filter'; import { LocalPager } from './local.pager'; import { DataSource } from '../data-source'; import { deepExtend } from '../../helpers'; var LocalDataSource = /** @class */ (function (_super) { __extends(LocalDataSource, _super); function LocalDataSource(data) { if (data === void 0) { data = []; } var _this = _super.call(this) || this; _this.data = []; _this.filteredAndSorted = []; _this.sortConf = []; _this.filterConf = { filters: [], andOperator: true, }; _this.pagingConf = {}; _this.data = data; return _this; } LocalDataSource.prototype.load = function (data) { this.data = data; return _super.prototype.load.call(this, data); }; LocalDataSource.prototype.prepend = function (element) { this.reset(true); this.data.unshift(element); return _super.prototype.prepend.call(this, element); }; LocalDataSource.prototype.append = function (element) { this.reset(true); this.data.push(element); return _super.prototype.append.call(this, element); }; LocalDataSource.prototype.add = function (element) { this.data.push(element); return _super.prototype.add.call(this, element); }; LocalDataSource.prototype.remove = function (element) { this.data = this.data.filter(function (el) { return el !== element; }); return _super.prototype.remove.call(this, element); }; LocalDataSource.prototype.update = function (element, values) { var _this = this; return new Promise(function (resolve, reject) { _this.find(element).then(function (found) { found = deepExtend(found, values); _super.prototype.update.call(_this, found, values).then(resolve).catch(reject); }).catch(reject); }); }; LocalDataSource.prototype.find = function (element) { var found = this.data.find(function (el) { return el === element; }); if (found) { return Promise.resolve(found); } return Promise.reject(new Error('Element was not found in the dataset')); }; LocalDataSource.prototype.getElements = function () { var data = this.data.slice(0); return Promise.resolve(this.prepareData(data)); }; LocalDataSource.prototype.getFilteredAndSorted = function () { var data = this.data.slice(0); this.prepareData(data); return Promise.resolve(this.filteredAndSorted); }; LocalDataSource.prototype.getAll = function () { var data = this.data.slice(0); return Promise.resolve(data); }; LocalDataSource.prototype.reset = function (silent) { if (silent === void 0) { silent = false; } if (silent) { this.filterConf = { filters: [], andOperator: true, }; this.sortConf = []; this.pagingConf['page'] = 1; } else { this.setFilter([], true, false); this.setSort([], false); this.setPage(1); } }; LocalDataSource.prototype.empty = function () { this.data = []; return _super.prototype.empty.call(this); }; LocalDataSource.prototype.count = function () { return this.filteredAndSorted.length; }; /** * * Array of conf objects * [ * {field: string, direction: asc|desc|null, compare: Function|null}, * ] * @param conf * @param doEmit * @returns {LocalDataSource} */ LocalDataSource.prototype.setSort = function (conf, doEmit) { if (doEmit === void 0) { doEmit = true; } if (conf !== null) { conf.forEach(function (fieldConf) { if (!fieldConf['field'] || typeof fieldConf['direction'] === 'undefined') { throw new Error('Sort configuration object is not valid'); } }); this.sortConf = conf; } _super.prototype.setSort.call(this, conf, doEmit); return this; }; /** * * Array of conf objects * [ * {field: string, search: string, filter: Function|null}, * ] * @param conf * @param andOperator * @param doEmit * @returns {LocalDataSource} */ LocalDataSource.prototype.setFilter = function (conf, andOperator, doEmit) { var _this = this; if (andOperator === void 0) { andOperator = true; } if (doEmit === void 0) { doEmit = true; } if (conf && conf.length > 0) { conf.forEach(function (fieldConf) { _this.addFilter(fieldConf, andOperator, false); }); } else { this.filterConf = { filters: [], andOperator: true, }; } this.filterConf.andOperator = andOperator; this.pagingConf['page'] = 1; _super.prototype.setFilter.call(this, conf, andOperator, doEmit); return this; }; LocalDataSource.prototype.addFilter = function (fieldConf, andOperator, doEmit) { var _this = this; if (andOperator === void 0) { andOperator = true; } if (doEmit === void 0) { doEmit = true; } if (!fieldConf['field'] || typeof fieldConf['search'] === 'undefined') { throw new Error('Filter configuration object is not valid'); } var found = false; this.filterConf.filters.forEach(function (currentFieldConf, index) { if (currentFieldConf['field'] === fieldConf['field']) { _this.filterConf.filters[index] = fieldConf; found = true; } }); if (!found) { this.filterConf.filters.push(fieldConf); } this.filterConf.andOperator = andOperator; _super.prototype.addFilter.call(this, fieldConf, andOperator, doEmit); return this; }; LocalDataSource.prototype.setPaging = function (page, perPage, doEmit) { if (doEmit === void 0) { doEmit = true; } this.pagingConf['page'] = page; this.pagingConf['perPage'] = perPage; _super.prototype.setPaging.call(this, page, perPage, doEmit); return this; }; LocalDataSource.prototype.setPage = function (page, doEmit) { if (doEmit === void 0) { doEmit = true; } this.pagingConf['page'] = page; _super.prototype.setPage.call(this, page, doEmit); return this; }; LocalDataSource.prototype.getSort = function () { return this.sortConf; }; LocalDataSource.prototype.getFilter = function () { return this.filterConf; }; LocalDataSource.prototype.getPaging = function () { return this.pagingConf; }; LocalDataSource.prototype.prepareData = function (data) { data = this.filter(data); data = this.sort(data); this.filteredAndSorted = data.slice(0); return this.paginate(data); }; LocalDataSource.prototype.sort = function (data) { if (this.sortConf) { this.sortConf.forEach(function (fieldConf) { data = LocalSorter .sort(data, fieldConf['field'], fieldConf['direction'], fieldConf['compare']); }); } return data; }; // TODO: refactor? LocalDataSource.prototype.filter = function (data) { if (this.filterConf.filters) { if (this.filterConf.andOperator) { this.filterConf.filters.forEach(function (fieldConf) { if (fieldConf['search'].length > 0) { data = LocalFilter .filter(data, fieldConf['field'], fieldConf['search'], fieldConf['filter']); } }); } else { var mergedData_1 = []; this.filterConf.filters.forEach(function (fieldConf) { if (fieldConf['search'].length > 0) { mergedData_1 = mergedData_1.concat(LocalFilter .filter(data, fieldConf['field'], fieldConf['search'], fieldConf['filter'])); } }); // remove non unique items data = mergedData_1.filter(function (elem, pos, arr) { return arr.indexOf(elem) === pos; }); } } return data; }; LocalDataSource.prototype.paginate = function (data) { if (this.pagingConf && this.pagingConf['page'] && this.pagingConf['perPage']) { data = LocalPager.paginate(data, this.pagingConf['page'], this.pagingConf['perPage']); } return data; }; return LocalDataSource; }(DataSource)); export { LocalDataSource }; //# sourceMappingURL=local.data-source.js.map