UNPKG

ng2-stable-nxtc

Version:

Angular Smart Table neXtCode Version

123 lines 3.38 kB
import { Row } from './row'; import { Column } from './column'; export 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; } 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)); }); } } //# sourceMappingURL=data-set.js.map