salix/front/core/components/model-proxy/model-proxy.js

370 lines
9.0 KiB
JavaScript
Raw Normal View History

import ngModule from '../../module';
2018-10-22 15:54:04 +00:00
import Component from '../../lib/component';
2018-10-18 07:24:20 +00:00
/**
2018-10-23 10:14:47 +00:00
* A data model. It allows to filter, sort and paginate data.
2018-10-18 07:24:20 +00:00
*
2018-10-23 10:14:47 +00:00
* @property {Boolean} autoLoad Whether to load data automatically when required attributes are setted
2018-10-18 07:24:20 +00:00
* @event dataChange Emitted when data property changes
*/
2018-10-22 15:54:04 +00:00
export class DataModel extends Component {
2018-10-23 10:14:47 +00:00
/**
* @type {Array<Object>} A JavaScript array with the model data.
*/
2018-10-30 14:05:19 +00:00
get data() {
return null;
}
2018-10-23 10:14:47 +00:00
/**
* Refresh the model data.
*
* @return {Promise} The request promise
*/
refresh() {
return Promise.resolve();
}
/**
* Clears the model, removing all it's data.
*/
2018-10-18 07:24:20 +00:00
clear() {}
}
2020-07-24 15:46:06 +00:00
ngModule.vnComponent('vnDataModel', {
2018-10-18 07:24:20 +00:00
controller: DataModel,
bindings: {
data: '=?',
autoLoad: '<?',
order: '@?',
limit: '<?'
}
});
/**
* A data model that monitorizes changes to the data.
2018-10-23 10:14:47 +00:00
* It internally uses the JavaScript Proxy to track changes
* made in model rows.
2018-10-18 07:24:20 +00:00
*
2018-10-30 14:05:19 +00:00
* @property {Boolean} autoSave Whether to save data automatically when model is modified
2018-10-18 07:24:20 +00:00
* @event dataChange Emitted when data property changes
2018-10-23 11:50:31 +00:00
* @event dataUpdate Emitted when data property changes
* @event rowInsert Emitted when new row is inserted
* @event rowRemove Emitted when row is removed
* @event rowChange Emitted when row property is changed
2018-10-30 14:05:19 +00:00
* @event save Emitted when data is saved
2018-10-18 07:24:20 +00:00
*/
export default class ModelProxy extends DataModel {
2018-10-22 06:23:10 +00:00
constructor($element, $scope) {
super($element, $scope);
this.resetChanges();
this.status = 'clear';
}
get orgData() {
return this._orgData;
}
set orgData(value) {
this._orgData = value;
2018-10-18 07:24:20 +00:00
if (value) {
this.proxiedData = new Array(value.length);
2018-10-18 07:24:20 +00:00
for (let i = 0; i < value.length; i++)
this.proxiedData[i] = this.createRow(Object.assign({}, value[i]), i, value[i]);
} else
2018-10-18 07:24:20 +00:00
this.proxiedData = null;
2018-10-18 07:24:20 +00:00
this.data = null;
this.resetChanges();
}
2018-10-23 10:14:47 +00:00
/**
* @type {Array<Object>} A JavaScript array with the model data.
* Rows data can be modified directly but for insertion or removing
* rows use the remove() or insert() model methods, otherwise changes
* are not detected by the model.
*/
get data() {
return this._data;
}
2018-10-18 07:24:20 +00:00
set data(value) {
this._data = value;
if (value == null)
this.status = 'clear';
else if (value.length)
this.status = 'ready';
else
this.status = 'empty';
2018-10-18 07:24:20 +00:00
this.emit('dataChange');
2018-10-23 11:50:31 +00:00
this.emit('dataUpdate');
}
2018-10-23 10:14:47 +00:00
/**
* Removes a row from the model and emits the 'rowRemove' event.
*
* @param {Number} index The row index
*/
remove(index) {
2018-10-18 07:24:20 +00:00
let [item] = this.data.splice(index, 1);
2018-10-18 07:24:20 +00:00
let proxiedIndex = this.proxiedData.indexOf(item);
this.proxiedData.splice(proxiedIndex, 1);
if (!item.$isNew)
this.removed.push(item);
2018-10-18 07:24:20 +00:00
this.isChanged = true;
if (!this.data.length)
this.status = 'empty';
2018-10-18 07:24:20 +00:00
this.emit('rowRemove', index);
2018-10-23 11:50:31 +00:00
this.emit('dataUpdate');
2018-10-30 14:05:19 +00:00
if (this.autoSave)
this.save();
}
2018-10-23 10:14:47 +00:00
/**
* Inserts a new row into the model and emits the 'rowInsert' event.
*
* @param {Object} data The initial data for the new row
* @return {Number} The inserted row index
*/
insert(data) {
data = Object.assign(data || {}, this.link);
2018-10-18 07:24:20 +00:00
let newRow = this.createRow(data, null);
newRow.$isNew = true;
2018-10-18 07:24:20 +00:00
let index = this.proxiedData.push(newRow) - 1;
if (this.data)
this.data.push(newRow);
this.isChanged = true;
this.status = 'ready';
2018-10-18 07:24:20 +00:00
this.emit('rowInsert', index);
2018-10-23 11:50:31 +00:00
this.emit('dataUpdate');
2018-10-18 07:24:20 +00:00
return index;
}
createRow(obj, index, orgRow) {
2018-10-22 15:54:04 +00:00
Object.assign(obj, {
$orgIndex: index,
$orgRow: orgRow,
$oldData: null,
$isNew: false
});
return new Proxy(obj, {
2018-10-18 07:24:20 +00:00
set: (obj, prop, value) => {
let changed = prop.charAt(0) !== '$' && value !== obj[prop];
if (changed && !obj.$isNew) {
2018-10-18 07:24:20 +00:00
if (!obj.$oldData)
obj.$oldData = {};
if (!obj.$oldData[prop])
obj.$oldData[prop] = value;
this.isChanged = true;
2018-10-30 14:05:19 +00:00
}
let ret = Reflect.set(obj, prop, value);
if (changed) {
2018-10-22 15:54:04 +00:00
this.emit('rowChange', {obj, prop, value});
2018-10-23 11:50:31 +00:00
this.emit('dataUpdate');
if (!obj.$isNew && this.autoSave)
2018-10-30 14:05:19 +00:00
this.save();
2018-10-18 07:24:20 +00:00
}
2018-10-30 14:05:19 +00:00
return ret;
2018-10-18 07:24:20 +00:00
}
});
}
resetChanges() {
this.removed = [];
2018-10-18 07:24:20 +00:00
this.isChanged = false;
let data = this.proxiedData;
if (data) {
2018-10-18 07:24:20 +00:00
for (let row of data)
row.$oldData = null;
}
2018-10-18 07:24:20 +00:00
}
2018-10-23 10:14:47 +00:00
/**
* Applies all changes made to the model into the original data source.
*/
2018-10-18 07:24:20 +00:00
applyChanges() {
let data = this.proxiedData;
let orgData = this.orgData;
if (!data) return;
for (let row of data) {
2018-10-18 07:24:20 +00:00
if (row.$isNew) {
let orgRow = {};
for (let prop in row) {
2018-10-18 07:24:20 +00:00
if (prop.charAt(0) !== '$')
orgRow[prop] = row[prop];
}
row.$orgIndex = orgData.push(orgRow) - 1;
row.$orgRow = orgRow;
2018-10-18 07:24:20 +00:00
row.$isNew = false;
} else if (row.$oldData) {
2018-10-18 07:24:20 +00:00
for (let prop in row.$oldData)
row.$orgRow[prop] = row[prop];
}
}
2018-10-18 07:24:20 +00:00
let removed = this.removed;
if (removed) {
removed = removed.sort((a, b) => b.$orgIndex - a.$orgIndex);
for (let row of this.removed)
orgData.splice(row.$orgIndex, 1);
}
2018-10-18 07:24:20 +00:00
this.resetChanges();
}
2018-10-30 14:05:19 +00:00
/**
* Should be implemented by child classes.
*/
save() {
this.emit('save');
}
2018-10-23 10:14:47 +00:00
/**
* Undoes all changes made to the model data.
*/
undoChanges() {
2018-10-18 07:24:20 +00:00
let data = this.proxiedData;
if (!data) return;
for (let i = 0; i < data.length; i++) {
let row = data[i];
if (row.$oldData)
2018-10-18 07:24:20 +00:00
Object.assign(row, row.$oldData);
if (row.$isNew)
data.splice(i--, 1);
}
2018-10-18 07:24:20 +00:00
let removed = this.removed;
2018-10-18 07:24:20 +00:00
if (removed) {
removed = removed.sort((a, b) => a.$orgIndex - b.$orgIndex);
for (let row of this.removed)
data.splice(row.$orgIndex, 0, row);
}
2018-10-18 07:24:20 +00:00
this.resetChanges();
}
}
2020-07-24 15:46:06 +00:00
ngModule.vnComponent('vnModelProxy', {
controller: ModelProxy,
bindings: {
orgData: '<?',
2018-10-18 07:24:20 +00:00
data: '=?'
}
});
2018-10-23 10:14:47 +00:00
/**
* Interface used to filter data coming from different datasources.
*
* @property {Object} filter The base filter
* @property {Object} params The base filter params
*/
export class Filtrable {
/**
* Applies a filter to the model clearing the previous ones.
*
* @param {Object} filter The filter parameters
* @param {*} params Custom user parameters
* @return {Promise} The request promise
*/
applyFilter() {
return Promise.resolve();
}
/**
* Adds a filter to the model mixing it with the currently applied filters.
*
* @param {Object} filter The filter parameters
* @param {*} params Custom user parameters
* @return {Promise} The request promise
*/
addFilter() {
return Promise.resolve();
}
/**
* Removes the currently applied filters.
*
* @return {Promise} The request promise
*/
removeFilter() {
return Promise.resolve();
}
}
/**
* Interface used to sort data coming from different datasources using
* the same specification: columnName [ASC|DESC]
*
* @property {String|Array<String>|Function} order The sort specification.
*/
export class Sortable {}
/**
* Interface used to paginate data coming from different datasources.
*
* @property {Number} limit The page size
*/
export class Paginable {
/**
* @type {Boolean} Whether the model is loading.
*/
2018-10-30 14:05:19 +00:00
get isLoading() {
return false;
}
2018-10-23 10:14:47 +00:00
/**
* @type {ready|loading|clear|empty|error} The current model status.
*/
get status() {
return null;
}
/**
* @type {Boolean} Whether the model is paging.
*/
get isPaging() {
return false;
}
2018-10-23 10:14:47 +00:00
/**
* @type {Boolean} Whether the data is paginated and there are more rows to load.
*/
2018-10-30 14:05:19 +00:00
get moreRows() {
return false;
}
2018-10-23 10:14:47 +00:00
/**
* When limit is enabled, loads the next set of rows.
*
* @return {Promise} The request promise
*/
loadMore() {
return Promise.resolve();
}
}