2018-05-31 09:52:39 +00:00
|
|
|
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';
|
2018-05-31 09:52:39 +00:00
|
|
|
|
|
|
|
export default class CrudModel extends ModelProxy {
|
2018-10-18 07:24:20 +00:00
|
|
|
constructor($q, $http) {
|
2018-05-31 09:52:39 +00:00
|
|
|
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.
|
|
|
|
*/
|
2018-06-07 21:47:19 +00:00
|
|
|
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() {
|
2018-05-31 09:52:39 +00:00
|
|
|
if (this.autoLoad)
|
2018-10-18 07:24:20 +00:00
|
|
|
return this.refresh();
|
|
|
|
return this.$q.resolve();
|
2018-05-31 09:52:39 +00:00
|
|
|
}
|
|
|
|
|
2018-09-05 11:01:21 +00:00
|
|
|
buildFilter() {
|
|
|
|
let order = this.order;
|
|
|
|
|
|
|
|
if (typeof order === 'string')
|
|
|
|
order = this.order.split(/\s*,\s*/);
|
2018-05-31 09:52:39 +00:00
|
|
|
|
2018-06-07 21:47:19 +00:00
|
|
|
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
|
2018-06-07 21:47:19 +00:00
|
|
|
};
|
|
|
|
|
2018-05-31 09:52:39 +00:00
|
|
|
let filter = this.filter;
|
2018-06-07 21:47:19 +00:00
|
|
|
filter = mergeFilters(myFilter, filter);
|
2018-09-05 11:01:21 +00:00
|
|
|
filter = mergeFilters(this.userFilter, filter);
|
|
|
|
return filter;
|
2018-06-07 21:47:19 +00:00
|
|
|
}
|
2018-05-31 09:52:39 +00:00
|
|
|
|
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-06-07 21:47:19 +00:00
|
|
|
}
|
|
|
|
|
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-06-07 21:47:19 +00:00
|
|
|
}
|
|
|
|
|
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-06-07 21:47:19 +00:00
|
|
|
}
|
2018-05-31 09:52:39 +00:00
|
|
|
|
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-06-07 21:47:19 +00:00
|
|
|
}
|
|
|
|
|
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-06-07 21:47:19 +00:00
|
|
|
}
|
2018-05-31 09:52:39 +00:00
|
|
|
|
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
|
|
|
*/
|
2018-06-07 21:47:19 +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-05-31 09:52:39 +00:00
|
|
|
}
|
|
|
|
|
2018-09-05 11:01:21 +00:00
|
|
|
/**
|
|
|
|
* Returns an object with the unsaved changes made to the model.
|
|
|
|
*
|
|
|
|
* @return {Object} The current changes
|
|
|
|
*/
|
2018-05-31 09:52:39 +00:00
|
|
|
getChanges() {
|
2018-10-18 07:24:20 +00:00
|
|
|
if (!this.isChanged)
|
|
|
|
return null;
|
|
|
|
|
2018-05-31 09:52:39 +00:00
|
|
|
let create = [];
|
|
|
|
let update = [];
|
|
|
|
let remove = [];
|
|
|
|
|
2018-10-19 17:54:46 +00:00
|
|
|
let pk = this.primaryKey;
|
|
|
|
|
2018-05-31 09:52:39 +00:00
|
|
|
for (let row of this.removed)
|
2018-10-19 17:54:46 +00:00
|
|
|
remove.push(row.$orgRow[pk]);
|
2018-05-31 09:52:39 +00:00
|
|
|
|
|
|
|
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];
|
2018-10-19 17:54:46 +00:00
|
|
|
update.push({
|
|
|
|
data,
|
|
|
|
where: {[pk]: row.$orgRow[pk]}
|
|
|
|
});
|
2018-10-18 07:24:20 +00:00
|
|
|
}
|
2018-05-31 09:52:39 +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() {
|
2018-05-31 09:52:39 +00:00
|
|
|
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`;
|
2018-05-31 09:52:39 +00:00
|
|
|
return this.$http.post(url, changes)
|
2018-10-18 07:24:20 +00:00
|
|
|
.then(() => this.applyChanges());
|
2018-05-31 09:52:39 +00:00
|
|
|
}
|
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-05-31 09:52:39 +00:00
|
|
|
}
|
2018-10-18 07:24:20 +00:00
|
|
|
CrudModel.$inject = ['$q', '$http'];
|
2018-05-31 09:52:39 +00:00
|
|
|
|
|
|
|
ngModule.component('vnCrudModel', {
|
|
|
|
controller: CrudModel,
|
|
|
|
bindings: {
|
|
|
|
orgData: '<?',
|
|
|
|
data: '=?',
|
2018-07-02 09:52:50 +00:00
|
|
|
onDataChange: '&?',
|
2018-05-31 09:52:39 +00:00
|
|
|
link: '<?',
|
|
|
|
url: '@?',
|
|
|
|
saveUrl: '@?',
|
2018-10-18 07:24:20 +00:00
|
|
|
fields: '<?',
|
2018-05-31 09:52:39 +00:00
|
|
|
include: '<?',
|
2018-10-18 07:24:20 +00:00
|
|
|
where: '<?',
|
2018-05-31 09:52:39 +00:00
|
|
|
order: '@?',
|
|
|
|
limit: '<?',
|
|
|
|
filter: '<?',
|
2018-09-05 11:01:21 +00:00
|
|
|
params: '<?',
|
|
|
|
userFilter: '<?',
|
2018-07-09 16:06:25 +00:00
|
|
|
userParams: '<?',
|
2018-05-31 09:52:39 +00:00
|
|
|
primaryKey: '@?',
|
|
|
|
autoLoad: '<?'
|
|
|
|
}
|
|
|
|
});
|