rb-data-table
Version:
Angular Smart Table
249 lines • 9.21 kB
JavaScript
import { Subject } from 'rxjs';
import { Deferred, getDeepFromObject } from './helpers';
import { DataSet } from './data-set/data-set';
var Grid = /** @class */ (function () {
function Grid(source, settings) {
this.createFormShown = false;
this.onSelectRowSource = new Subject();
this.setSettings(settings);
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) {
this.settings = settings;
this.dataSet = new DataSet([], this.getSetting('columns'));
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.setPreSelectCriteria = function (criteria) {
this.preSelectCriteria = criteria;
this.applyPreSelection();
};
Grid.prototype.applyPreSelection = function () {
var _this = this;
if (!this.preSelectCriteria || !this.preSelectCriteria.values || !this.preSelectCriteria.field) {
return;
}
this.getRows().forEach(function (r) {
var select = _this.preSelectCriteria.values.filter(function (v) {
return ((r.getData()[_this.preSelectCriteria.field]) === v);
});
if (select && select.length > 0) {
_this.dataSet.multipeSelectNoToggle(r);
}
});
};
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) {
_this.createFormShown = false;
}
else {
_this.source.prepend(newData).then(function () {
_this.createFormShown = false;
_this.dataSet.createNewRow();
});
}
}).catch(function (err) {
// doing nothing
});
if (this.getSetting('add.confirmCreate')) {
confirmEmitter.emit({
newData: row.getNewData(),
source: this.source,
confirm: deferred,
});
}
else {
deferred.resolve();
}
};
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;
});
}
}).catch(function (err) {
// doing nothing
});
if (this.getSetting('edit.confirmSave')) {
confirmEmitter.emit({
data: row.getData(),
newData: row.getNewData(),
source: this.source,
confirm: deferred,
});
}
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());
}).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']);
this.applyPreSelection();
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