UNPKG

ng2-smart-table-custom

Version:

Angular Smart Table with inline-validations support

248 lines 9.69 kB
import { Subject } from 'rxjs/Subject'; import { Deferred, getDeepFromObject } from './helpers'; import { DataSet } from './data-set/data-set'; var Grid = (function () { function Grid(source, settings, validator) { this.createFormShown = false; this.onSelectRowSource = new Subject(); this.setSettings(settings, validator); this.setSource(source); } Grid.prototype.showActionColumn = function (position) { return this.isCurrentActionsPosition(position) && this.isActionsVisible(); }; Grid.prototype.isCurrentActionsPosition = function (position) { return position == this.getSetting('actions.position'); }; Grid.prototype.isActionsVisible = function () { return this.getSetting('actions.add') || this.getSetting('actions.edit') || this.getSetting('actions.delete') || this.getSetting('actions.custom').length; }; Grid.prototype.isMultiSelectVisible = function () { return this.getSetting('selectMode') === 'multi'; }; Grid.prototype.getNewRow = function () { return this.dataSet.newRow; }; Grid.prototype.setSettings = function (settings, validator) { this.settings = settings; this.createFormShown = this.getSetting('add.createFormShownAlways') || this.getSetting('add.createFormShownInitial'); this.dataSet = new DataSet([], this.getSetting('columns'), validator); if (this.source) { this.source.refresh(); } }; Grid.prototype.getDataSet = function () { return this.dataSet; }; Grid.prototype.setSource = function (source) { var _this = this; this.source = this.prepareSource(source); this.source.onChanged().subscribe(function (changes) { return _this.processDataChange(changes); }); this.source.onUpdated().subscribe(function (data) { var changedRow = _this.dataSet.findRowByData(data); changedRow.setData(data); }); }; Grid.prototype.getSetting = function (name, defaultValue) { return getDeepFromObject(this.settings, name, defaultValue); }; Grid.prototype.getColumns = function () { return this.dataSet.getColumns(); }; Grid.prototype.getRows = function () { return this.dataSet.getRows(); }; Grid.prototype.selectRow = function (row) { this.dataSet.selectRow(row); }; Grid.prototype.multipleSelectRow = function (row) { this.dataSet.multipleSelectRow(row); }; Grid.prototype.onSelectRow = function () { return this.onSelectRowSource.asObservable(); }; Grid.prototype.edit = function (row) { row.isInEditing = true; }; Grid.prototype.create = function (row, confirmEmitter) { var _this = this; var deferred = new Deferred(); deferred.promise.then(function (newData) { newData = newData ? newData : row.getNewData(); if (deferred.resolve.skipAdd) { if (!_this.getSetting('add.createFormShownAlways')) _this.createFormShown = false; } else { _this.insert(newData); } }).catch(function (err) { // doing nothing }); if (this.getSetting('add.confirmCreate')) { confirmEmitter.emit({ newData: row.getNewData(), source: this.source, confirm: deferred, validator: this.dataSet.newRowValidator, }); } else { if (this.dataSet.newRowValidator.invalid) deferred.reject(); else deferred.resolve(); } }; Grid.prototype.insert = function (newData) { var _this = this; this.source[this.getSetting('add.insertMethod')](newData).then(function () { if (!_this.getSetting('add.createFormShownAlways')) _this.createFormShown = false; _this.dataSet.addInsertedRowValidator(); _this.dataSet.createNewRow(); }); }; Grid.prototype.save = function (row, confirmEmitter) { var _this = this; var deferred = new Deferred(); deferred.promise.then(function (newData) { newData = newData ? newData : row.getNewData(); if (deferred.resolve.skipEdit) { row.isInEditing = false; } else { _this.source.update(row.getData(), newData).then(function () { row.isInEditing = false; _this.dataSet.newRowValidator.reset(); }); } }).catch(function (err) { // doing nothing }); if (this.getSetting('edit.confirmSave')) { confirmEmitter.emit({ data: row.getData(), newData: row.getNewData(), source: this.source, confirm: deferred, validator: this.dataSet.getRowValidator(row.index), }); } else { if (this.dataSet.getRowValidator(row.index).invalid) deferred.reject(); else deferred.resolve(); } }; Grid.prototype.delete = function (row, confirmEmitter) { var _this = this; var deferred = new Deferred(); deferred.promise.then(function () { _this.source.remove(row.getData()); _this.dataSet.editRowValidators = _this.dataSet.editRowValidators.splice(row.index, 1); }).catch(function (err) { // doing nothing }); if (this.getSetting('delete.confirmDelete')) { confirmEmitter.emit({ data: row.getData(), source: this.source, confirm: deferred, }); } else { deferred.resolve(); } }; Grid.prototype.processDataChange = function (changes) { if (this.shouldProcessChange(changes)) { this.dataSet.setData(changes['elements']); if (this.getSetting('selectMode') !== 'multi') { var row = this.determineRowToSelect(changes); if (row) { this.onSelectRowSource.next(row); } } } }; Grid.prototype.shouldProcessChange = function (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 Grid.prototype.determineRowToSelect = function (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; }; Grid.prototype.prepareSource = function (source) { var 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; }; Grid.prototype.getInitialSort = function () { var sortConf = {}; this.getColumns().forEach(function (column) { if (column.isSortable && column.defaultSortDirection) { sortConf['field'] = column.id; sortConf['direction'] = column.defaultSortDirection; sortConf['compare'] = column.getCompareFunction(); } }); return sortConf; }; Grid.prototype.getSelectedRows = function () { return this.dataSet.getRows() .filter(function (r) { return r.isSelected; }); }; Grid.prototype.selectAllRows = function (status) { this.dataSet.getRows() .forEach(function (r) { return r.isSelected = status; }); }; Grid.prototype.getFirstRow = function () { return this.dataSet.getFirstRow(); }; Grid.prototype.getLastRow = function () { return this.dataSet.getLastRow(); }; return Grid; }()); export { Grid }; //# sourceMappingURL=grid.js.map