ng2-smart-table-custom
Version:
Angular Smart Table with inline-validations support
158 lines • 5.51 kB
JavaScript
import { FormControl } from '@angular/forms';
import { Row } from './row';
import { Column } from './column';
var DataSet = (function () {
function DataSet(data, columnSettings, validator) {
if (data === void 0) { data = []; }
this.columnSettings = columnSettings;
this.validator = validator;
this.data = [];
this.columns = [];
this.rows = [];
this.willSelect = 'first';
this.createColumns(columnSettings);
this.setData(data);
this.createNewRowValidator();
this.createNewRow();
}
DataSet.prototype.addDefaultsToFormGroup = function (formGroup) {
if (this.columnSettings)
for (var id in this.columnSettings)
if (!formGroup.controls[id] && this.columnSettings.hasOwnProperty(id))
formGroup.controls[id] = new FormControl();
return formGroup;
};
DataSet.prototype.createNewRowValidator = function () {
this.newRowValidator = this.addDefaultsToFormGroup(this.validator.getFormGroup());
};
DataSet.prototype.createEditRowValidators = function () {
var _this = this;
this.editRowValidators = new Array();
this.data.forEach(function () {
_this.editRowValidators.push(_this.addDefaultsToFormGroup(_this.validator.getFormGroup()));
});
};
DataSet.prototype.setData = function (data) {
this.data = data;
this.createRows();
this.createEditRowValidators();
};
DataSet.prototype.getColumns = function () {
return this.columns;
};
DataSet.prototype.getRows = function () {
return this.rows;
};
DataSet.prototype.getFirstRow = function () {
return this.rows[0];
};
DataSet.prototype.getLastRow = function () {
return this.rows[this.rows.length - 1];
};
DataSet.prototype.getRowValidator = function (index) {
if (index === -1)
return this.newRowValidator;
else
return this.editRowValidators[index];
};
DataSet.prototype.findRowByData = function (data) {
return this.rows.find(function (row) { return row.getData() === data; });
};
DataSet.prototype.deselectAll = function () {
this.rows.forEach(function (row) {
row.isSelected = false;
});
};
DataSet.prototype.selectRow = function (row) {
var previousIsSelected = row.isSelected;
this.deselectAll();
row.isSelected = !previousIsSelected;
this.selectedRow = row;
return this.selectedRow;
};
DataSet.prototype.multipleSelectRow = function (row) {
row.isSelected = !row.isSelected;
this.selectedRow = row;
return this.selectedRow;
};
DataSet.prototype.selectPreviousRow = function () {
if (this.rows.length > 0) {
var 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;
}
};
DataSet.prototype.selectFirstRow = function () {
if (this.rows.length > 0) {
this.selectRow(this.rows[0]);
return this.selectedRow;
}
};
DataSet.prototype.selectLastRow = function () {
if (this.rows.length > 0) {
this.selectRow(this.rows[this.rows.length - 1]);
return this.selectedRow;
}
};
DataSet.prototype.willSelectFirstRow = function () {
this.willSelect = 'first';
};
DataSet.prototype.willSelectLastRow = function () {
this.willSelect = 'last';
};
DataSet.prototype.select = function () {
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;
};
DataSet.prototype.addInsertedRowValidator = function () {
this.newRowValidator.reset();
this.editRowValidators = [this.addDefaultsToFormGroup(this.validator.getFormGroup())].concat(this.editRowValidators);
};
DataSet.prototype.createNewRow = function () {
this.newRow = new Row(-1, {}, this);
this.newRow.isInEditing = true;
};
/**
* Create columns by mapping from the settings
* @param settings
* @private
*/
DataSet.prototype.createColumns = function (settings) {
for (var 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
*/
DataSet.prototype.createRows = function () {
var _this = this;
this.rows = [];
this.data.forEach(function (el, index) {
_this.rows.push(new Row(index, el, _this));
});
};
return DataSet;
}());
export { DataSet };
//# sourceMappingURL=data-set.js.map