UNPKG

rb-data-table

Version:
1,345 lines (1,322 loc) 141 kB
/** * @license ng2-smart-table v1.6.5 * Copyright (c) 2017 Akveo. https://akveo.github.io/ng2-smart-table/ * License: MIT */ import { cloneDeep } from 'lodash'; import { Subject } from 'rxjs'; import { Component, Input, Output, EventEmitter, ComponentFactoryResolver, ViewChild, ViewContainerRef, ChangeDetectionStrategy, NgModule, ElementRef } from '@angular/core'; import { CompleterService, Ng2CompleterModule } from 'ng2-completer'; import { CommonModule } from '@angular/common'; import { FormsModule, FormControl, NgControl, ReactiveFormsModule } from '@angular/forms'; import { debounceTime, distinctUntilChanged, map, skip } from 'rxjs/operators'; import { HttpParams } from '@angular/common/http'; /** * Extending object that entered in first argument. * * Returns extended object or false if have no target object or incorrect type. * * If you wish to clone source object (without modify it), just use empty new * object as first argument, like this: * deepExtend({}, yourObj_1, [yourObj_N]); */ const deepExtend = function (...objects) { if (arguments.length < 1 || typeof arguments[0] !== 'object') { return false; } if (arguments.length < 2) { return arguments[0]; } const target = arguments[0]; // convert arguments to array and cut off target object const args = Array.prototype.slice.call(arguments, 1); let val, src; args.forEach((obj) => { // skip argument if it is array or isn't object if (typeof obj !== 'object' || Array.isArray(obj)) { return; } Object.keys(obj).forEach(function (key) { src = target[key]; // source value val = obj[key]; // new value // recursion prevention if (val === target) { return; /** * if new value isn't object then just overwrite by new value * instead of extending. */ } else if (typeof val !== 'object' || val === null) { target[key] = val; return; // just clone arrays (and recursive clone objects inside) } else if (Array.isArray(val)) { target[key] = cloneDeep(val); return; // overwrite by new value if source isn't object or array } else if (typeof src !== 'object' || src === null || Array.isArray(src)) { target[key] = deepExtend({}, val); return; // source value and new value is objects both, extending... } else { target[key] = deepExtend(src, val); return; } }); }); return target; }; class Deferred { constructor() { this.promise = new Promise((resolve, reject) => { this.resolve = resolve; this.reject = reject; }); } } // getDeepFromObject({result: {data: 1}}, 'result.data', 2); // returns 1 function getDeepFromObject(object = {}, name, defaultValue) { const keys = name.split('.'); // clone the object let level = deepExtend({}, object); keys.forEach((k) => { if (level && typeof level[k] !== 'undefined') { level = level[k]; } }); return typeof level === 'undefined' ? defaultValue : level; } class Cell { constructor(value, row, column, dataSet) { this.value = value; this.row = row; this.column = column; this.dataSet = dataSet; this.newValue = ''; this.newValue = value; } getColumn() { return this.column; } getRow() { return this.row; } getValue() { const valid = this.column.getValuePrepareFunction() instanceof Function; const prepare = valid ? this.column.getValuePrepareFunction() : Cell.PREPARE; return prepare.call(null, this.value, this.row.getData(), this); } setValue(value) { this.newValue = value; } getId() { return this.getColumn().id; } getTitle() { return this.getColumn().title; } isEditable() { if (this.getRow().index === -1) { return this.getColumn().isAddable; } else { return this.getColumn().isEditable; } } } Cell.PREPARE = (value) => value; class Row { constructor(index, data, _dataSet) { this.index = index; this.data = data; this._dataSet = _dataSet; this.isSelected = false; this.isInEditing = false; this.cells = []; this.process(); } getCell(column) { return this.cells.find(el => el.getColumn() === column); } getCells() { return this.cells; } getData() { return this.data; } getIsSelected() { return this.isSelected; } getNewData() { const values = Object.assign({}, this.data); this.getCells().forEach((cell) => values[cell.getColumn().id] = cell.newValue); return values; } setData(data) { this.data = data; this.process(); } process() { this.cells = []; this._dataSet.getColumns().forEach((column) => { const cell = this.createCell(column); this.cells.push(cell); }); } createCell(column) { const defValue = column.settings.defaultValue ? column.settings.defaultValue : ''; const value = typeof this.data[column.id] === 'undefined' ? defValue : this.data[column.id]; return new Cell(value, this, column, this._dataSet); } } class Column { constructor(id, settings, dataSet) { this.id = id; this.settings = settings; this.dataSet = dataSet; this.title = ''; this.type = ''; this.class = ''; this.width = ''; this.isSortable = false; this.isEditable = true; this.isAddable = true; this.isFilterable = false; this.sortDirection = ''; this.defaultSortDirection = ''; this.editor = { type: '', config: {}, component: null }; this.filter = { type: '', config: {}, component: null }; this.renderComponent = null; this.process(); } getOnComponentInitFunction() { return this.onComponentInitFunction; } getCompareFunction() { return this.compareFunction; } getValuePrepareFunction() { return this.valuePrepareFunction; } getFilterFunction() { return this.filterFunction; } getConfig() { return this.editor && this.editor.config; } getFilterType() { return this.filter && this.filter.type; } getFilterConfig() { return this.filter && this.filter.config; } process() { this.title = this.settings['title']; this.class = this.settings['class']; this.width = this.settings['width']; this.type = this.prepareType(); this.editor = this.settings['editor']; this.filter = this.settings['filter']; this.renderComponent = this.settings['renderComponent']; this.isFilterable = typeof this.settings['filter'] === 'undefined' ? true : !!this.settings['filter']; this.defaultSortDirection = ['asc', 'desc'] .indexOf(this.settings['sortDirection']) !== -1 ? this.settings['sortDirection'] : ''; this.isSortable = typeof this.settings['sort'] === 'undefined' ? true : !!this.settings['sort']; this.isEditable = typeof this.settings['editable'] === 'undefined' ? true : !!this.settings['editable']; this.isAddable = typeof this.settings['addable'] === 'undefined' ? true : !!this.settings['addable']; this.sortDirection = this.prepareSortDirection(); this.compareFunction = this.settings['compareFunction']; this.valuePrepareFunction = this.settings['valuePrepareFunction']; this.filterFunction = this.settings['filterFunction']; this.onComponentInitFunction = this.settings['onComponentInitFunction']; } prepareType() { return this.settings['type'] || this.determineType(); } prepareSortDirection() { return this.settings['sort'] === 'desc' ? 'desc' : 'asc'; } determineType() { // TODO: determine type by data return 'text'; } } class DataSet { constructor(data = [], columnSettings) { this.columnSettings = columnSettings; this.data = []; this.columns = []; this.rows = []; this.willSelect = 'first'; this.createColumns(columnSettings); this.setData(data); this.createNewRow(); } setData(data) { this.data = data; this.createRows(); } getColumns() { return this.columns; } getRows() { return this.rows; } getFirstRow() { return this.rows[0]; } getLastRow() { return this.rows[this.rows.length - 1]; } findRowByData(data) { return this.rows.find((row) => row.getData() === data); } deselectAll() { this.rows.forEach((row) => { row.isSelected = false; }); } selectRow(row) { const previousIsSelected = row.isSelected; this.deselectAll(); row.isSelected = !previousIsSelected; this.selectedRow = row; return this.selectedRow; } multipleSelectRow(row) { row.isSelected = !row.isSelected; this.selectedRow = row; return this.selectedRow; } multipeSelectNoToggle(row) { row.isSelected = true; this.selectedRow = row; return this.selectedRow; } selectPreviousRow() { if (this.rows.length > 0) { let index = this.selectedRow ? this.selectedRow.index : 0; if (index > this.rows.length - 1) { index = this.rows.length - 1; } this.selectRow(this.rows[index]); return this.selectedRow; } } selectFirstRow() { if (this.rows.length > 0) { this.selectRow(this.rows[0]); return this.selectedRow; } } selectLastRow() { if (this.rows.length > 0) { this.selectRow(this.rows[this.rows.length - 1]); return this.selectedRow; } } willSelectFirstRow() { this.willSelect = 'first'; } willSelectLastRow() { this.willSelect = 'last'; } select() { if (this.getRows().length === 0) { return; } if (this.willSelect) { if (this.willSelect === 'first') { this.selectFirstRow(); } if (this.willSelect === 'last') { this.selectLastRow(); } this.willSelect = ''; } else { this.selectFirstRow(); } return this.selectedRow; } createNewRow() { this.newRow = new Row(-1, {}, this); this.newRow.isInEditing = true; } /** * Create columns by mapping from the settings * @param settings * @private */ createColumns(settings) { for (const id in settings) { if (settings.hasOwnProperty(id)) { this.columns.push(new Column(id, settings[id], this)); } } } /** * Create rows based on current data prepared in data source * @private */ createRows() { this.rows = []; this.data.forEach((el, index) => { this.rows.push(new Row(index, el, this)); }); } } class Grid { constructor(source, settings) { this.createFormShown = false; this.onSelectRowSource = new Subject(); this.setSettings(settings); this.setSource(source); } showActionColumn(position) { return this.isCurrentActionsPosition(position) && this.isActionsVisible(); } isCurrentActionsPosition(position) { return position == this.getSetting('actions.position'); } isActionsVisible() { return this.getSetting('actions.add') || this.getSetting('actions.edit') || this.getSetting('actions.delete') || this.getSetting('actions.custom').length; } isMultiSelectVisible() { return this.getSetting('selectMode') === 'multi'; } getNewRow() { return this.dataSet.newRow; } setSettings(settings) { this.settings = settings; this.dataSet = new DataSet([], this.getSetting('columns')); if (this.source) { this.source.refresh(); } } getDataSet() { return this.dataSet; } setSource(source) { this.source = this.prepareSource(source); this.source.onChanged().subscribe((changes) => this.processDataChange(changes)); this.source.onUpdated().subscribe((data) => { const changedRow = this.dataSet.findRowByData(data); changedRow.setData(data); }); } setPreSelectCriteria(criteria) { this.preSelectCriteria = criteria; this.applyPreSelection(); } applyPreSelection() { if (!this.preSelectCriteria || !this.preSelectCriteria.values || !this.preSelectCriteria.field) { return; } this.getRows().forEach((r) => { let select = this.preSelectCriteria.values.filter(v => { return ((r.getData()[this.preSelectCriteria.field]) === v); }); if (select && select.length > 0) { this.dataSet.multipeSelectNoToggle(r); } }); } getSetting(name, defaultValue) { return getDeepFromObject(this.settings, name, defaultValue); } getColumns() { return this.dataSet.getColumns(); } getRows() { return this.dataSet.getRows(); } selectRow(row) { this.dataSet.selectRow(row); } multipleSelectRow(row) { this.dataSet.multipleSelectRow(row); } onSelectRow() { return this.onSelectRowSource.asObservable(); } edit(row) { row.isInEditing = true; } create(row, confirmEmitter) { const deferred = new Deferred(); deferred.promise.then((newData) => { newData = newData ? newData : row.getNewData(); if (deferred.resolve.skipAdd) { this.createFormShown = false; } else { this.source.prepend(newData).then(() => { this.createFormShown = false; this.dataSet.createNewRow(); }); } }).catch((err) => { // doing nothing }); if (this.getSetting('add.confirmCreate')) { confirmEmitter.emit({ newData: row.getNewData(), source: this.source, confirm: deferred, }); } else { deferred.resolve(); } } save(row, confirmEmitter) { const deferred = new Deferred(); deferred.promise.then((newData) => { newData = newData ? newData : row.getNewData(); if (deferred.resolve.skipEdit) { row.isInEditing = false; } else { this.source.update(row.getData(), newData).then(() => { row.isInEditing = false; }); } }).catch((err) => { // doing nothing }); if (this.getSetting('edit.confirmSave')) { confirmEmitter.emit({ data: row.getData(), newData: row.getNewData(), source: this.source, confirm: deferred, }); } else { deferred.resolve(); } } delete(row, confirmEmitter) { const deferred = new Deferred(); deferred.promise.then(() => { this.source.remove(row.getData()); }).catch((err) => { // doing nothing }); if (this.getSetting('delete.confirmDelete')) { confirmEmitter.emit({ data: row.getData(), source: this.source, confirm: deferred, }); } else { deferred.resolve(); } } processDataChange(changes) { if (this.shouldProcessChange(changes)) { this.dataSet.setData(changes['elements']); this.applyPreSelection(); if (this.getSetting('selectMode') !== 'multi') { const row = this.determineRowToSelect(changes); if (row) { this.onSelectRowSource.next(row); } } } } shouldProcessChange(changes) { if (['filter', 'sort', 'page', 'remove', 'refresh', 'load', 'paging'].indexOf(changes['action']) !== -1) { return true; } else if (['prepend', 'append'].indexOf(changes['action']) !== -1 && !this.getSetting('pager.display')) { return true; } return false; } // TODO: move to selectable? Separate directive determineRowToSelect(changes) { if (['load', 'page', 'filter', 'sort', 'refresh'].indexOf(changes['action']) !== -1) { return this.dataSet.select(); } if (changes['action'] === 'remove') { if (changes['elements'].length === 0) { // we have to store which one to select as the data will be reloaded this.dataSet.willSelectLastRow(); } else { return this.dataSet.selectPreviousRow(); } } if (changes['action'] === 'append') { // we have to store which one to select as the data will be reloaded this.dataSet.willSelectLastRow(); } if (changes['action'] === 'add') { return this.dataSet.selectFirstRow(); } if (changes['action'] === 'update') { return this.dataSet.selectFirstRow(); } if (changes['action'] === 'prepend') { // we have to store which one to select as the data will be reloaded this.dataSet.willSelectFirstRow(); } return null; } prepareSource(source) { const initialSource = this.getInitialSort(); if (initialSource && initialSource['field'] && initialSource['direction']) { source.setSort([initialSource], false); } if (this.getSetting('pager.display') === true) { source.setPaging(1, this.getSetting('pager.perPage'), false); } source.refresh(); return source; } getInitialSort() { const sortConf = {}; this.getColumns().forEach((column) => { if (column.isSortable && column.defaultSortDirection) { sortConf['field'] = column.id; sortConf['direction'] = column.defaultSortDirection; sortConf['compare'] = column.getCompareFunction(); } }); return sortConf; } getSelectedRows() { return this.dataSet.getRows() .filter(r => r.isSelected); } selectAllRows(status) { this.dataSet.getRows() .forEach(r => r.isSelected = status); } getFirstRow() { return this.dataSet.getFirstRow(); } getLastRow() { return this.dataSet.getLastRow(); } } var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; let CellComponent = class CellComponent { constructor() { this.inputClass = ''; this.mode = 'inline'; this.isInEditing = false; this.edited = new EventEmitter(); } onEdited(event) { if (this.isNew) { this.grid.create(this.grid.getNewRow(), this.createConfirm); } else { this.grid.save(this.row, this.editConfirm); } } }; __decorate([ Input(), __metadata("design:type", Grid) ], CellComponent.prototype, "grid", void 0); __decorate([ Input(), __metadata("design:type", Row) ], CellComponent.prototype, "row", void 0); __decorate([ Input(), __metadata("design:type", EventEmitter) ], CellComponent.prototype, "editConfirm", void 0); __decorate([ Input(), __metadata("design:type", EventEmitter) ], CellComponent.prototype, "createConfirm", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], CellComponent.prototype, "isNew", void 0); __decorate([ Input(), __metadata("design:type", Cell) ], CellComponent.prototype, "cell", void 0); __decorate([ Input(), __metadata("design:type", String) ], CellComponent.prototype, "inputClass", void 0); __decorate([ Input(), __metadata("design:type", String) ], CellComponent.prototype, "mode", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], CellComponent.prototype, "isInEditing", void 0); __decorate([ Output(), __metadata("design:type", Object) ], CellComponent.prototype, "edited", void 0); CellComponent = __decorate([ Component({ selector: 'ng2-smart-table-cell', template: ` <table-cell-view-mode *ngIf="!isInEditing" [cell]="cell"></table-cell-view-mode> <table-cell-edit-mode *ngIf="isInEditing" [cell]="cell" [inputClass]="inputClass" (edited)="onEdited($event)"> </table-cell-edit-mode> `, }) ], CellComponent); var __decorate$1 = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata$1 = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; class EditCellDefault { constructor() { this.inputClass = ''; this.edited = new EventEmitter(); } onEdited(event) { this.edited.next(event); return false; } onStopEditing() { this.cell.getRow().isInEditing = false; return false; } onClick(event) { event.stopPropagation(); } } __decorate$1([ Input(), __metadata$1("design:type", Cell) ], EditCellDefault.prototype, "cell", void 0); __decorate$1([ Input(), __metadata$1("design:type", String) ], EditCellDefault.prototype, "inputClass", void 0); __decorate$1([ Output(), __metadata$1("design:type", Object) ], EditCellDefault.prototype, "edited", void 0); var __decorate$2 = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata$2 = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; let CustomEditComponent = class CustomEditComponent extends EditCellDefault { constructor(resolver) { super(); this.resolver = resolver; } ngOnChanges(changes) { if (this.cell && !this.customComponent) { const componentFactory = this.resolver.resolveComponentFactory(this.cell.getColumn().editor.component); this.customComponent = this.dynamicTarget.createComponent(componentFactory); // set @Inputs and @Outputs of custom component this.customComponent.instance.cell = this.cell; this.customComponent.instance.inputClass = this.inputClass; this.customComponent.instance.onStopEditing.subscribe(() => this.onStopEditing()); this.customComponent.instance.onEdited.subscribe((event) => this.onEdited(event)); this.customComponent.instance.onClick.subscribe((event) => this.onClick(event)); } } ngOnDestroy() { if (this.customComponent) { this.customComponent.destroy(); } } }; __decorate$2([ ViewChild('dynamicTarget', { read: ViewContainerRef }), __metadata$2("design:type", Object) ], CustomEditComponent.prototype, "dynamicTarget", void 0); CustomEditComponent = __decorate$2([ Component({ selector: 'table-cell-custom-editor', template: ` <ng-template #dynamicTarget></ng-template> `, }), __metadata$2("design:paramtypes", [ComponentFactoryResolver]) ], CustomEditComponent); var __decorate$3 = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata$3 = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; let DefaultEditComponent = class DefaultEditComponent extends EditCellDefault { constructor() { super(); } getEditorType() { return this.cell.getColumn().editor && this.cell.getColumn().editor.type; } }; DefaultEditComponent = __decorate$3([ Component({ selector: 'table-cell-default-editor', template: "<div [ngSwitch]=\"getEditorType()\"><select-editor *ngSwitchCase=\"'list'\" [cell]=\"cell\" [inputClass]=\"inputClass\" (onClick)=\"onClick($event)\" (onEdited)=\"onEdited($event)\" (onStopEditing)=\"onStopEditing()\"></select-editor><textarea-editor *ngSwitchCase=\"'textarea'\" [cell]=\"cell\" [inputClass]=\"inputClass\" (onClick)=\"onClick($event)\" (onEdited)=\"onEdited($event)\" (onStopEditing)=\"onStopEditing()\"></textarea-editor><checkbox-editor *ngSwitchCase=\"'checkbox'\" [cell]=\"cell\" [inputClass]=\"inputClass\" (onClick)=\"onClick($event)\"></checkbox-editor><completer-editor *ngSwitchCase=\"'completer'\" [cell]=\"cell\"></completer-editor><input-editor *ngSwitchDefault [cell]=\"cell\" [inputClass]=\"inputClass\" (onClick)=\"onClick($event)\" (onEdited)=\"onEdited($event)\" (onStopEditing)=\"onStopEditing()\"></input-editor></div>", }), __metadata$3("design:paramtypes", []) ], DefaultEditComponent); var __decorate$4 = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata$4 = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; let EditCellComponent = class EditCellComponent { constructor() { this.inputClass = ''; this.edited = new EventEmitter(); } onEdited(event) { this.edited.next(event); return false; } getEditorType() { return this.cell.getColumn().editor && this.cell.getColumn().editor.type; } }; __decorate$4([ Input(), __metadata$4("design:type", Cell) ], EditCellComponent.prototype, "cell", void 0); __decorate$4([ Input(), __metadata$4("design:type", String) ], EditCellComponent.prototype, "inputClass", void 0); __decorate$4([ Output(), __metadata$4("design:type", Object) ], EditCellComponent.prototype, "edited", void 0); EditCellComponent = __decorate$4([ Component({ selector: 'table-cell-edit-mode', template: ` <div [ngSwitch]="getEditorType()"> <table-cell-custom-editor *ngSwitchCase="'custom'" [cell]="cell" [inputClass]="inputClass" (edited)="onEdited($event)"> </table-cell-custom-editor> <table-cell-default-editor *ngSwitchDefault [cell]="cell" [inputClass]="inputClass" (edited)="onEdited($event)"> </table-cell-default-editor> </div> `, }) ], EditCellComponent); var __decorate$5 = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata$5 = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; class DefaultEditor { constructor() { this.onStopEditing = new EventEmitter(); this.onEdited = new EventEmitter(); this.onClick = new EventEmitter(); } } __decorate$5([ Input(), __metadata$5("design:type", Cell) ], DefaultEditor.prototype, "cell", void 0); __decorate$5([ Input(), __metadata$5("design:type", String) ], DefaultEditor.prototype, "inputClass", void 0); __decorate$5([ Output(), __metadata$5("design:type", Object) ], DefaultEditor.prototype, "onStopEditing", void 0); __decorate$5([ Output(), __metadata$5("design:type", Object) ], DefaultEditor.prototype, "onEdited", void 0); __decorate$5([ Output(), __metadata$5("design:type", Object) ], DefaultEditor.prototype, "onClick", void 0); var __decorate$6 = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata$6 = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; let CheckboxEditorComponent = class CheckboxEditorComponent extends DefaultEditor { constructor() { super(); } onChange(event) { const trueVal = (this.cell.getColumn().getConfig() && this.cell.getColumn().getConfig().true) || true; const falseVal = (this.cell.getColumn().getConfig() && this.cell.getColumn().getConfig().false) || false; this.cell.newValue = event.target.checked ? trueVal : falseVal; } }; CheckboxEditorComponent = __decorate$6([ Component({ selector: 'checkbox-editor', styles: [":host input,:host textarea{width:100%;line-height:normal;padding:.375em .75em} /*# sourceMappingURL=editor.component.css.map */ "], template: ` <input [ngClass]="inputClass" type="checkbox" class="form-control" [name]="cell.getId()" [disabled]="!cell.isEditable()" [checked]="cell.getValue() == (cell.getColumn().getConfig()?.true || true)" (click)="onClick.emit($event)" (change)="onChange($event)"> `, }), __metadata$6("design:paramtypes", []) ], CheckboxEditorComponent); var __decorate$7 = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata$7 = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; let CompleterEditorComponent = class CompleterEditorComponent extends DefaultEditor { constructor(completerService) { super(); this.completerService = completerService; this.completerStr = ''; } ngOnInit() { if (this.cell.getColumn().editor && this.cell.getColumn().editor.type === 'completer') { const config = this.cell.getColumn().getConfig().completer; config.dataService = this.completerService.local(config.data, config.searchFields, config.titleField); config.dataService.descriptionField(config.descriptionField); } } onEditedCompleter(event) { this.cell.newValue = event.title; return false; } }; CompleterEditorComponent = __decorate$7([ Component({ selector: 'completer-editor', template: ` <ng2-completer [(ngModel)]="completerStr" [dataService]="cell.getColumn().getConfig().completer.dataService" [minSearchLength]="cell.getColumn().getConfig().completer.minSearchLength || 0" [pause]="cell.getColumn().getConfig().completer.pause || 0" [placeholder]="cell.getColumn().getConfig().completer.placeholder || 'Start typing...'" (selected)="onEditedCompleter($event)"> </ng2-completer> `, }), __metadata$7("design:paramtypes", [CompleterService]) ], CompleterEditorComponent); var __decorate$8 = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata$8 = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; let InputEditorComponent = class InputEditorComponent extends DefaultEditor { constructor() { super(); } }; InputEditorComponent = __decorate$8([ Component({ selector: 'input-editor', styles: [":host input,:host textarea{width:100%;line-height:normal;padding:.375em .75em} /*# sourceMappingURL=editor.component.css.map */ "], template: ` <input [ngClass]="inputClass" class="form-control" [(ngModel)]="cell.newValue" [name]="cell.getId()" [placeholder]="cell.getTitle()" [disabled]="!cell.isEditable()" (click)="onClick.emit($event)" (keydown.enter)="onEdited.emit($event)" (keydown.esc)="onStopEditing.emit()"> `, }), __metadata$8("design:paramtypes", []) ], InputEditorComponent); var __decorate$9 = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata$9 = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; let SelectEditorComponent = class SelectEditorComponent extends DefaultEditor { constructor() { super(); } }; SelectEditorComponent = __decorate$9([ Component({ selector: 'select-editor', template: ` <select [ngClass]="inputClass" class="form-control" [(ngModel)]="cell.newValue" [name]="cell.getId()" [disabled]="!cell.isEditable()" (click)="onClick.emit($event)" (keydown.enter)="onEdited.emit($event)" (keydown.esc)="onStopEditing.emit()"> <option *ngFor="let option of cell.getColumn().getConfig()?.list" [value]="option.value" [selected]="option.value === cell.getValue()">{{ option.title }} </option> </select> `, }), __metadata$9("design:paramtypes", []) ], SelectEditorComponent); var __decorate$10 = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata$10 = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; let TextareaEditorComponent = class TextareaEditorComponent extends DefaultEditor { constructor() { super(); } }; TextareaEditorComponent = __decorate$10([ Component({ selector: 'textarea-editor', styles: [":host input,:host textarea{width:100%;line-height:normal;padding:.375em .75em} /*# sourceMappingURL=editor.component.css.map */ "], template: ` <textarea [ngClass]="inputClass" class="form-control" [(ngModel)]="cell.newValue" [name]="cell.getId()" [disabled]="!cell.isEditable()" [placeholder]="cell.getTitle()" (click)="onClick.emit($event)" (keydown.enter)="onEdited.emit($event)" (keydown.esc)="onStopEditing.emit()"> </textarea> `, }), __metadata$10("design:paramtypes", []) ], TextareaEditorComponent); var __decorate$11 = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata$11 = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; let CustomViewComponent = class CustomViewComponent { constructor(resolver) { this.resolver = resolver; } ngOnInit() { if (this.cell && !this.customComponent) { this.createCustomComponent(); this.callOnComponentInit(); this.patchInstance(); } } ngOnDestroy() { if (this.customComponent) { this.customComponent.destroy(); } } createCustomComponent() { const componentFactory = this.resolver.resolveComponentFactory(this.cell.getColumn().renderComponent); this.customComponent = this.dynamicTarget.createComponent(componentFactory); } callOnComponentInit() { const onComponentInitFunction = this.cell.getColumn().getOnComponentInitFunction(); onComponentInitFunction && onComponentInitFunction(this.customComponent.instance); } patchInstance() { Object.assign(this.customComponent.instance, this.getPatch()); } getPatch() { return { value: this.cell.getValue(), rowData: this.cell.getRow().getData() }; } }; __decorate$11([ Input(), __metadata$11("design:type", Cell) ], CustomViewComponent.prototype, "cell", void 0); __decorate$11([ ViewChild('dynamicTarget', { read: ViewContainerRef }), __metadata$11("design:type", Object) ], CustomViewComponent.prototype, "dynamicTarget", void 0); CustomViewComponent = __decorate$11([ Component({ selector: 'custom-view-component', template: ` <ng-template #dynamicTarget></ng-template> `, }), __metadata$11("design:paramtypes", [ComponentFactoryResolver]) ], CustomViewComponent); var __decorate$12 = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata$12 = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; let ViewCellComponent = class ViewCellComponent { }; __decorate$12([ Input(), __metadata$12("design:type", Cell) ], ViewCellComponent.prototype, "cell", void 0); ViewCellComponent = __decorate$12([ Component({ selector: 'table-cell-view-mode', changeDetection: ChangeDetectionStrategy.OnPush, template: ` <div [ngSwitch]="cell.getColumn().type"> <custom-view-component *ngSwitchCase="'custom'" [cell]="cell"></custom-view-component> <div *ngSwitchCase="'html'" [innerHTML]="cell.getValue()"></div> <div *ngSwitchDefault>{{ cell.getValue() }}</div> </div> `, }) ], ViewCellComponent); var __decorate$13 = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; const CELL_COMPONENTS = [ CellComponent, CustomEditComponent, DefaultEditComponent, EditCellComponent, CheckboxEditorComponent, CompleterEditorComponent, InputEditorComponent, SelectEditorComponent, TextareaEditorComponent, CustomViewComponent, ViewCellComponent, ]; let CellModule = class CellModule { }; CellModule = __decorate$13([ NgModule({ imports: [ CommonModule, FormsModule, Ng2CompleterModule, ], declarations: [ ...CELL_COMPONENTS, ], exports: [ ...CELL_COMPONENTS, ], }) ], CellModule); class DataSource { constructor() { this.onChangedSource = new Subject(); this.onAddedSource = new Subject(); this.onUpdatedSource = new Subject(); this.onRemovedSource = new Subject(); } refresh() { this.emitOnChanged('refresh'); } load(data) { this.emitOnChanged('load'); return Promise.resolve(); } onChanged() { return this.onChangedSource.asObservable(); } onAdded() { return this.onAddedSource.asObservable(); } onUpdated() { return this.onUpdatedSource.asObservable(); } onRemoved() { return this.onRemovedSource.asObservable(); } prepend(element) { this.emitOnAdded(element); this.emitOnChanged('prepend'); return Promise.resolve(); } append(element) { this.emitOnAdded(element); this.emitOnChanged('append'); return Promise.resolve(); } add(element) { this.emitOnAdded(element); this.emitOnChanged('add'); return Promise.resolve(); } remove(element) { this.emitOnRemoved(element); this.emitOnChanged('remove'); return Promise.resolve(); } update(element, values) { this.emitOnUpdated(element); this.emitOnChanged('update'); return Promise.resolve(); } empty() { this.emitOnChanged('empty'); return Promise.resolve(); } setSort(conf, doEmit) { if (doEmit) { this.emitOnChanged('sort'); } } setFilter(conf, andOperator, doEmit) { if (doEmit) { this.emitOnChanged('filter'); } } addFilter(fieldConf, andOperator, doEmit) { if (doEmit) { this.emitOnChanged('filter'); } } setPaging(page, perPage, doEmit) { if (doEmit) { this.emitOnChanged('paging'); } } setPage(page, doEmit) { if (doEmit) { this.emitOnChanged('page'); } } emitOnRemoved(element) { this.onRemovedSource.next(element); } emitOnUpdated(element) { this.onUpdatedSource.next(element); } emitOnAdded(element) { this.onAddedSource.next(element); } emitOnChanged(action) { this.getElements().then((elements) => this.onChangedSource.next({ action: action, elements: elements, paging: this.getPaging(), filter: this.getFilter(), sort: this.getSort(), })); } } var __decorate$14 = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata$13 = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; class FilterDefault { constructor() { this.inputClass = ''; this.filter = new EventEmitter(); this.query = ''; } onFilter(query) { this.source.addFilter({ field: this.column.id, search: query, filter: this.column.getFilterFunction(), }); } } __decorate$14([ Input(), __metadata$13("design:type", Column) ], FilterDefault.prototype, "column", void 0); __decorate$14([ Input(), __metadata$13("design:type", DataSource) ], FilterDefault.prototype, "source", void 0); __decorate$14([ Input(), __metadata$13("design:type", String) ], FilterDefault.prototype, "inputClass", void 0); __decorate$14([ Output(), __metadata$13("design:type", Object) ], FilterDefault.prototype, "filter", void 0)