2018-05-31 09:52:39 +00:00
|
|
|
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);
|
2018-05-31 09:52:39 +00:00
|
|
|
this.resetChanges();
|
2019-10-03 17:34:24 +00:00
|
|
|
this.status = 'clear';
|
2018-05-31 09:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-06-07 21:47:19 +00:00
|
|
|
|
2018-10-18 07:24:20 +00:00
|
|
|
for (let i = 0; i < value.length; i++)
|
2018-10-19 17:54:46 +00:00
|
|
|
this.proxiedData[i] = this.createRow(Object.assign({}, value[i]), i, value[i]);
|
2018-06-07 21:47:19 +00:00
|
|
|
} else
|
2018-10-18 07:24:20 +00:00
|
|
|
this.proxiedData = null;
|
2018-05-31 09:52:39 +00:00
|
|
|
|
2018-10-18 07:24:20 +00:00
|
|
|
this.data = null;
|
2018-05-31 09:52:39 +00:00
|
|
|
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.
|
|
|
|
*/
|
2018-05-31 09:52:39 +00:00
|
|
|
get data() {
|
|
|
|
return this._data;
|
|
|
|
}
|
|
|
|
|
2018-10-18 07:24:20 +00:00
|
|
|
set data(value) {
|
|
|
|
this._data = value;
|
2019-10-03 17:34:24 +00:00
|
|
|
|
|
|
|
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-05-31 09:52:39 +00:00
|
|
|
}
|
|
|
|
|
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
|
2020-03-09 08:00:03 +00:00
|
|
|
* @return {Promise} The save request promise
|
2018-10-23 10:14:47 +00:00
|
|
|
*/
|
2018-05-31 09:52:39 +00:00
|
|
|
remove(index) {
|
2020-03-09 08:00:03 +00:00
|
|
|
let [row] = this.data.splice(index, 1);
|
2018-05-31 09:52:39 +00:00
|
|
|
|
2020-03-09 08:00:03 +00:00
|
|
|
let proxiedIndex = this.proxiedData.indexOf(row);
|
2018-10-18 07:24:20 +00:00
|
|
|
this.proxiedData.splice(proxiedIndex, 1);
|
2018-05-31 09:52:39 +00:00
|
|
|
|
2020-03-09 08:00:03 +00:00
|
|
|
if (!row.$isNew)
|
|
|
|
this.removed.push(row);
|
2018-10-18 07:24:20 +00:00
|
|
|
|
|
|
|
this.isChanged = true;
|
2019-10-03 17:34:24 +00:00
|
|
|
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');
|
2019-10-03 17:34:24 +00:00
|
|
|
|
2018-10-30 14:05:19 +00:00
|
|
|
if (this.autoSave)
|
2020-03-09 08:00:03 +00:00
|
|
|
return this.save();
|
|
|
|
else
|
|
|
|
return this.$q.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a row from the model and emits the 'rowRemove' event.
|
|
|
|
*
|
|
|
|
* @param {Object} row The row object
|
|
|
|
* @return {Promise} The save request promise
|
|
|
|
*/
|
|
|
|
removeRow(row) {
|
|
|
|
return this.remove(this.data.indexOf(row));
|
2018-05-31 09:52:39 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2018-05-31 09:52:39 +00:00
|
|
|
insert(data) {
|
|
|
|
data = Object.assign(data || {}, this.link);
|
2018-10-18 07:24:20 +00:00
|
|
|
let newRow = this.createRow(data, null);
|
2018-05-31 09:52:39 +00:00
|
|
|
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;
|
2019-10-03 17:34:24 +00:00
|
|
|
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');
|
2019-10-03 17:34:24 +00:00
|
|
|
|
2018-10-18 07:24:20 +00:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2018-10-19 17:54:46 +00:00
|
|
|
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) => {
|
2019-01-29 13:38:31 +00:00
|
|
|
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');
|
2019-01-29 13:38:31 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
});
|
2018-05-31 09:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resetChanges() {
|
|
|
|
this.removed = [];
|
2018-10-18 07:24:20 +00:00
|
|
|
this.isChanged = false;
|
|
|
|
|
|
|
|
let data = this.proxiedData;
|
2019-01-29 13:38:31 +00:00
|
|
|
if (data) {
|
2018-10-18 07:24:20 +00:00
|
|
|
for (let row of data)
|
|
|
|
row.$oldData = null;
|
2019-01-29 13:38:31 +00:00
|
|
|
}
|
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;
|
|
|
|
|
2019-01-29 13:38:31 +00:00
|
|
|
for (let row of data) {
|
2018-10-18 07:24:20 +00:00
|
|
|
if (row.$isNew) {
|
2018-10-19 17:54:46 +00:00
|
|
|
let orgRow = {};
|
2019-01-29 13:38:31 +00:00
|
|
|
for (let prop in row) {
|
2018-10-18 07:24:20 +00:00
|
|
|
if (prop.charAt(0) !== '$')
|
2018-10-19 17:54:46 +00:00
|
|
|
orgRow[prop] = row[prop];
|
2019-01-29 13:38:31 +00:00
|
|
|
}
|
2018-10-19 17:54:46 +00:00
|
|
|
row.$orgIndex = orgData.push(orgRow) - 1;
|
|
|
|
row.$orgRow = orgRow;
|
2018-10-18 07:24:20 +00:00
|
|
|
row.$isNew = false;
|
2019-01-29 13:38:31 +00:00
|
|
|
} else if (row.$oldData) {
|
2018-10-18 07:24:20 +00:00
|
|
|
for (let prop in row.$oldData)
|
2018-10-19 17:54:46 +00:00
|
|
|
row.$orgRow[prop] = row[prop];
|
2019-01-29 13:38:31 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-31 09:52:39 +00:00
|
|
|
|
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-05-31 09:52:39 +00:00
|
|
|
}
|
2018-10-18 07:24:20 +00:00
|
|
|
|
|
|
|
this.resetChanges();
|
2018-05-31 09:52:39 +00:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2018-05-31 09:52:39 +00:00
|
|
|
undoChanges() {
|
2018-10-18 07:24:20 +00:00
|
|
|
let data = this.proxiedData;
|
|
|
|
if (!data) return;
|
2018-05-31 09:52:39 +00:00
|
|
|
|
|
|
|
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);
|
2018-05-31 09:52:39 +00:00
|
|
|
if (row.$isNew)
|
|
|
|
data.splice(i--, 1);
|
|
|
|
}
|
|
|
|
|
2018-10-18 07:24:20 +00:00
|
|
|
let removed = this.removed;
|
2018-05-31 09:52:39 +00:00
|
|
|
|
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-06-07 21:47:19 +00:00
|
|
|
|
2018-10-18 07:24:20 +00:00
|
|
|
this.resetChanges();
|
2018-06-07 21:47:19 +00:00
|
|
|
}
|
2018-05-31 09:52:39 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 15:46:06 +00:00
|
|
|
ngModule.vnComponent('vnModelProxy', {
|
2018-05-31 09:52:39 +00:00
|
|
|
controller: ModelProxy,
|
|
|
|
bindings: {
|
|
|
|
orgData: '<?',
|
2018-10-18 07:24:20 +00:00
|
|
|
data: '=?'
|
2018-05-31 09:52:39 +00:00
|
|
|
}
|
|
|
|
});
|
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
|
|
|
|
2019-05-10 12:42:46 +00:00
|
|
|
/**
|
2019-10-03 17:34:24 +00:00
|
|
|
* @type {ready|loading|clear|empty|error} The current model status.
|
2019-05-10 12:42:46 +00:00
|
|
|
*/
|
2019-10-03 17:34:24 +00:00
|
|
|
get status() {
|
|
|
|
return null;
|
2019-05-10 12:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*
|
2022-12-20 08:48:41 +00:00
|
|
|
* @param {Boolean} append - Whether should append new data
|
2018-10-23 10:14:47 +00:00
|
|
|
* @return {Promise} The request promise
|
|
|
|
*/
|
2022-12-20 08:48:41 +00:00
|
|
|
loadMore(append) {
|
|
|
|
return Promise.resolve(append);
|
2018-10-23 10:14:47 +00:00
|
|
|
}
|
|
|
|
}
|