ng2-stable-nxtc
Version:
Angular Smart Table neXtCode Version
224 lines • 7.66 kB
JavaScript
import { Subject } from 'rxjs/Subject';
import { Deferred, getDeepFromObject } from './helpers';
import { DataSet } from './data-set/data-set';
export 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);
});
}
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']);
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();
}
}
//# sourceMappingURL=grid.js.map