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

285 lines
6.8 KiB
JavaScript
Raw Normal View History

import ngModule from '../../module';
import ModelProxy from '../model-proxy/model-proxy';
2018-12-27 11:54:16 +00:00
import {mergeWhere, mergeFilters} from 'vn-loopback/util/filter';
2018-10-23 10:14:47 +00:00
/**
* Model that uses remote loopback model as datasource.
*
* @property {Array<String>} fields the list of fields to fetch
*/
export default class CrudModel extends ModelProxy {
2018-10-22 06:23:10 +00:00
constructor($q, $http, $element, $scope) {
super($element, $scope);
this.$http = $http;
this.$q = $q;
this.primaryKey = 'id';
this.autoLoad = false;
}
2018-10-18 07:24:20 +00:00
$onInit() {
2018-10-22 06:23:10 +00:00
this.initialized = true;
2018-10-18 07:24:20 +00:00
this.autoRefresh();
}
get isLoading() {
return this.canceler != null;
}
2018-10-23 10:14:47 +00:00
/**
* @type {String} The remote model URL
*/
get url() {
return this._url;
}
2018-10-18 07:24:20 +00:00
set url(url) {
if (this._url === url) return;
this._url = url;
this.clear();
2018-10-22 06:23:10 +00:00
if (this.initialized)
this.autoRefresh();
2018-10-18 07:24:20 +00:00
}
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() {
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.
*
2018-10-23 10:14:47 +00:00
* @param {Object} filter The Loopback filter
* @param {Object} params Custom parameters
2018-09-05 11:01:21 +00:00
* @return {Promise} The request promise
*/
2018-10-23 10:14:47 +00:00
applyFilter(filter, params) {
this.userFilter = filter;
this.userParams = params;
2018-09-05 11:01:21 +00:00
return this.refresh();
}
2018-09-05 11:01:21 +00:00
/**
* Adds a filter to the model.
*
2018-10-23 10:14:47 +00:00
* @param {Object} filter The Loopback filter
* @param {Object} params Custom parameters
2018-09-05 11:01:21 +00:00
* @return {Promise} The request promise
*/
2018-10-23 10:14:47 +00:00
addFilter(filter, params) {
this.userFilter = mergeFilters(filter, this.userFilter);
Object.assign(this.userParams, params);
2018-09-05 11:01:21 +00:00
return this.refresh();
}
2018-09-05 11:01:21 +00:00
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;
}
}
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);
}
clear() {
this.orgData = null;
2018-11-29 07:09:40 +00:00
this.moreRows = 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 deletes = [];
let updates = [];
let creates = [];
let pk = this.primaryKey;
for (let row of this.removed)
deletes.push(row.$orgRow[pk]);
2018-11-29 07:09:40 +00:00
for (let row of this._data) {
2018-10-18 07:24:20 +00:00
if (row.$isNew) {
let data = {};
2018-11-29 07:09:40 +00:00
for (let prop in row) {
2018-10-18 07:24:20 +00:00
if (prop.charAt(0) !== '$')
data[prop] = row[prop];
2018-11-29 07:09:40 +00:00
}
creates.push(data);
2018-10-18 07:24:20 +00:00
} else if (row.$oldData) {
let data = {};
for (let prop in row.$oldData)
data[prop] = row[prop];
updates.push({
data,
where: {[pk]: row.$orgRow[pk]}
});
2018-10-18 07:24:20 +00:00
}
2018-11-29 07:09:40 +00:00
}
let changes = {deletes, updates, creates};
2018-11-29 07:09:40 +00:00
for (let prop in changes) {
if (changes[prop].length === 0)
changes[prop] = undefined;
2018-11-29 07:09:40 +00:00
}
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-30 14:08:19 +00:00
.then(() => {
this.applyChanges();
super.save();
});
}
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();
this.isRefreshing = !append;
this.isPaging = append;
2018-09-05 11:01:21 +00:00
let params = Object.assign(
{filter},
2018-09-05 11:01:21 +00:00
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)
).finally(() => {
this.isRefreshing = false;
this.isPaging = false;
});
2018-09-05 11:01:21 +00:00
}
onRemoteDone(json, filter, append) {
let data = json.data;
2018-10-30 14:08:19 +00:00
if (append)
2018-09-05 11:01:21 +00:00
this.orgData = this.orgData.concat(data);
2018-10-30 14:08:19 +00:00
else {
2018-09-05 11:01:21 +00:00
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-22 06:23:10 +00:00
CrudModel.$inject = ['$q', '$http', '$element', '$scope'];
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: '@?',
2018-10-30 14:08:19 +00:00
autoLoad: '<?',
autoSave: '<?'
}
});