salix/client/core/src/components/crud-model/crud-model.js

283 lines
6.6 KiB
JavaScript
Raw Normal View History

import ngModule from '../../module';
import ModelProxy from '../model-proxy/model-proxy';
2018-10-18 07:24:20 +00:00
import {mergeWhere, mergeFilters} from 'vn-loopback/common/filter.js';
export default class CrudModel extends ModelProxy {
2018-10-18 07:24:20 +00:00
constructor($q, $http) {
super();
this.$http = $http;
this.$q = $q;
this.primaryKey = 'id';
this.autoLoad = true;
}
2018-10-18 07:24:20 +00:00
$onInit() {
this.autoRefresh();
}
2018-09-05 11:01:21 +00:00
/**
* Whether the model is loading.
*/
get isLoading() {
return this.canceler != null;
}
2018-10-18 07:24:20 +00:00
set url(url) {
if (this._url === url) return;
this._url = url;
this.clear();
this.autoRefresh();
}
get url() {
return this._url;
}
autoRefresh() {
if (this.autoLoad)
2018-10-18 07:24:20 +00:00
return this.refresh();
return this.$q.resolve();
}
2018-09-05 11:01:21 +00:00
buildFilter() {
let order = this.order;
if (typeof order === 'string')
order = this.order.split(/\s*,\s*/);
let myFilter = {
fields: this.fields,
where: mergeWhere(this.link, this.where),
include: this.include,
2018-09-05 11:01:21 +00:00
order: order,
2018-07-09 16:06:25 +00:00
limit: this.limit
};
let filter = this.filter;
filter = mergeFilters(myFilter, filter);
2018-09-05 11:01:21 +00:00
filter = mergeFilters(this.userFilter, filter);
return filter;
}
2018-09-05 11:01:21 +00:00
/**
* Refresh the model data.
*
* @return {Promise} The request promise
*/
refresh() {
2018-10-18 07:24:20 +00:00
if (!this._url)
return this.$q.resolve();
2018-09-05 11:01:21 +00:00
return this.sendRequest(this.buildFilter());
}
2018-09-05 11:01:21 +00:00
/**
* Applies a new filter to the model.
*
* @param {Object} userFilter The Loopback filter
* @param {Object} userParams Custom parameters
* @return {Promise} The request promise
*/
applyFilter(userFilter, userParams) {
this.userFilter = userFilter;
this.userParams = userParams;
return this.refresh();
}
2018-09-05 11:01:21 +00:00
/**
* Adds a filter to the model.
*
* @param {Object} userFilter The Loopback filter
* @param {Object} userParams Custom parameters
* @return {Promise} The request promise
*/
addFilter(userFilter, userParams) {
this.userFilter = mergeFilters(userFilter, this.userFilter);
Object.assign(this.userParams, userParams);
return this.refresh();
}
2018-09-05 11:01:21 +00:00
/**
* Removes the currently applied user filters.
*
* @return {Promise} The request promise
*/
removeFilter() {
2018-10-18 07:24:20 +00:00
return applyFilter(null, null);
}
2018-09-05 11:01:21 +00:00
/**
* Cancels the current request, if any.
*/
cancelRequest() {
if (this.canceler) {
this.canceler.resolve();
this.canceler = null;
}
}
2018-09-05 11:01:21 +00:00
/**
* When limit is enabled, loads the next set of rows.
2018-10-18 07:24:20 +00:00
*
* @return {Promise} The request promise
2018-09-05 11:01:21 +00:00
*/
loadMore() {
2018-10-18 07:24:20 +00:00
if (!this.moreRows)
return this.$q.resolve();
2018-09-05 11:01:21 +00:00
let filter = Object.assign({}, this.currentFilter);
filter.skip = this.orgData ? this.orgData.length : 0;
2018-10-18 07:24:20 +00:00
return this.sendRequest(filter, true);
}
/**
* Clears the model, removing all it's data.
*/
clear() {
this.orgData = null;
}
2018-09-05 11:01:21 +00:00
/**
* Returns an object with the unsaved changes made to the model.
*
* @return {Object} The current changes
*/
getChanges() {
2018-10-18 07:24:20 +00:00
if (!this.isChanged)
return null;
let create = [];
let update = [];
let remove = [];
let pk = this.primaryKey;
for (let row of this.removed)
remove.push(row.$orgRow[pk]);
for (let row of this._data) {
2018-10-18 07:24:20 +00:00
if (row.$isNew) {
let data = {};
for (let prop in row)
if (prop.charAt(0) !== '$')
data[prop] = row[prop];
create.push(data);
} else if (row.$oldData) {
let data = {};
for (let prop in row.$oldData)
data[prop] = row[prop];
update.push({
data,
where: {[pk]: row.$orgRow[pk]}
});
2018-10-18 07:24:20 +00:00
}
}
let changes = {
create: create,
update: update,
delete: remove
};
return changes;
}
2018-09-05 11:01:21 +00:00
/**
* Saves current changes on the server.
*
* @return {Promise} The save request promise
*/
2018-07-06 14:32:23 +00:00
save() {
let changes = this.getChanges();
if (!changes)
return this.$q.resolve();
2018-10-18 07:24:20 +00:00
let url = this.saveUrl ? this.saveUrl : `${this._url}/crud`;
return this.$http.post(url, changes)
2018-10-18 07:24:20 +00:00
.then(() => this.applyChanges());
}
2018-09-05 11:01:21 +00:00
buildParams() {
let params = {};
if (this.params instanceof Object)
Object.assign(params, this.params);
if (this.userParams instanceof Object)
Object.assign(params, this.userParams);
return params;
}
sendRequest(filter, append) {
this.cancelRequest();
this.canceler = this.$q.defer();
let params = Object.assign(
{filter: filter},
this.buildParams()
);
let options = {
timeout: this.canceler.promise,
params: params
};
2018-10-18 07:24:20 +00:00
return this.$http.get(this._url, options).then(
2018-09-05 11:01:21 +00:00
json => this.onRemoteDone(json, filter, append),
json => this.onRemoteError(json)
);
}
onRemoteDone(json, filter, append) {
let data = json.data;
if (append) {
this.orgData = this.orgData.concat(data);
} else {
this.orgData = data;
this.currentFilter = filter;
}
2018-10-18 07:24:20 +00:00
this.data = this.proxiedData.slice();
2018-09-05 11:01:21 +00:00
this.moreRows = filter.limit && data.length == filter.limit;
this.onRequestEnd();
}
onRemoteError(err) {
this.onRequestEnd();
throw err;
}
onRequestEnd() {
this.canceler = null;
}
2018-10-18 07:24:20 +00:00
undoChanges() {
super.undoChanges();
this.data = this.proxiedData.slice();
}
}
2018-10-18 07:24:20 +00:00
CrudModel.$inject = ['$q', '$http'];
ngModule.component('vnCrudModel', {
controller: CrudModel,
bindings: {
orgData: '<?',
data: '=?',
2018-07-02 09:52:50 +00:00
onDataChange: '&?',
link: '<?',
url: '@?',
saveUrl: '@?',
2018-10-18 07:24:20 +00:00
fields: '<?',
include: '<?',
2018-10-18 07:24:20 +00:00
where: '<?',
order: '@?',
limit: '<?',
filter: '<?',
2018-09-05 11:01:21 +00:00
params: '<?',
userFilter: '<?',
2018-07-09 16:06:25 +00:00
userParams: '<?',
primaryKey: '@?',
autoLoad: '<?'
}
});