salix/client/core/src/components/watcher/watcher.js

234 lines
6.4 KiB
JavaScript
Raw Normal View History

2018-02-10 15:18:01 +00:00
import ngModule from '../../module';
import Component from '../../lib/component';
import getModifiedData from '../../lib/modified';
import copyObject from '../../lib/copy';
import isEqual from '../../lib/equals';
import isFullEmpty from '../../lib/full-empty';
/**
* Component that checks for changes on a specific model property and
* asks the user to save or discard it when the state changes.
* Also it can save the data to the server when the @url and @idField
* properties are provided.
*/
export default class Watcher extends Component {
constructor($element, $scope, $state, $transitions, $http, vnApp, $translate, $attrs, $q) {
super($element);
this.$scope = $scope;
this.$state = $state;
this.$http = $http;
2017-02-07 16:38:30 +00:00
this.$translate = $translate;
2018-01-09 12:49:15 +00:00
this.$attrs = $attrs;
this.vnApp = vnApp;
this.$q = $q;
this.state = null;
this.deregisterCallback = $transitions.onStart({},
transition => this.callback(transition));
2017-10-05 07:31:28 +00:00
this.updateOriginalData();
}
$onInit() {
2017-09-28 09:29:01 +00:00
if (this.get && this.url) {
this.fetchData();
2017-09-28 09:29:01 +00:00
} else if (this.get && !this.url) {
throw new Error('Error: Parameter url ommitted');
}
}
$onChanges(changes) {
if (this.data) {
2017-10-05 07:31:28 +00:00
this.updateOriginalData();
}
}
$onDestroy() {
this.deregisterCallback();
}
fetchData() {
let id = this.data[this.idField];
2018-02-08 12:13:42 +00:00
// return new Promise((resolve, reject) => {
2017-09-28 09:29:01 +00:00
this.$http.get(`${this.url}/${id}`).then(
json => {
2017-10-05 07:20:40 +00:00
this.data = copyObject(json.data);
2017-10-05 07:31:28 +00:00
this.updateOriginalData();
2017-09-28 09:29:01 +00:00
}
// json => reject(json)
);
2018-02-08 12:13:42 +00:00
// });
}
2018-02-08 12:13:42 +00:00
2017-03-07 16:11:56 +00:00
/**
* Submits the data and goes back in the history.
2018-02-08 12:13:42 +00:00
*
* @return {Promise} The request promise
2017-03-07 16:11:56 +00:00
*/
submitBack() {
return this.submit().then(res => {
this.window.history.back();
return res;
});
2017-03-07 16:11:56 +00:00
}
2018-02-08 12:13:42 +00:00
2017-06-03 11:01:47 +00:00
/**
* Submits the data and goes another state.
*
* @param {String} state The state name
2018-02-08 12:13:42 +00:00
* @param {Object} params The request params
* @return {Promise} The request promise
2017-06-03 11:01:47 +00:00
*/
2017-12-04 07:17:29 +00:00
submitGo(state, params) {
return this.submit().then(res => {
this.$state.go(state, params || {});
return res;
});
2017-06-03 11:01:47 +00:00
}
2018-02-08 12:13:42 +00:00
2017-03-07 16:11:56 +00:00
/**
* Submits the data to the server.
*
* @return {Promise} The http request promise
*/
submit() {
return this.realSubmit().then(res => {
this.vnApp.showMessage(this.$translate.instant('Data saved!'));
return res;
});
}
errorHandler(err) {
this.vnApp.showError(err.message);
return err;
}
realSubmit() {
2017-03-07 16:11:56 +00:00
if (this.form) {
this.form.$setSubmitted();
if (!this.form.$valid)
return this.invalidForm();
}
if (!this.dataChanged())
return this.noChanges();
2018-02-21 10:09:27 +00:00
let isPost = (this.$attrs.save && this.$attrs.save.toLowerCase() === 'post');
let changedData = isPost
2018-05-24 14:09:32 +00:00
? this.data
2018-02-08 11:57:57 +00:00
: getModifiedData(this.data, this.orgData);
2017-02-21 10:36:43 +00:00
2018-02-01 12:28:45 +00:00
if (this.save && this.save.accept) {
this.save.model = changedData;
return this.$q((resolve, reject) => {
2017-02-21 10:36:43 +00:00
this.save.accept().then(
json => this.writeData({data: json}, resolve),
json => reject(json)
);
});
}
// XXX: Alternative when mgCrud is not used
2018-02-01 12:28:45 +00:00
let id = this.idField ? this.orgData[this.idField] : null;
if (id) {
return this.$q((resolve, reject) => {
2017-06-03 11:01:47 +00:00
this.$http.patch(`${this.url}/${id}`, changedData).then(
2017-02-07 16:02:17 +00:00
json => this.writeData(json, resolve),
reject
);
});
}
return this.$q((resolve, reject) => {
2018-02-01 12:28:45 +00:00
this.$http.post(this.url, changedData).then(
json => this.writeData(json, resolve),
reject
);
});
}
noChanges() {
let message = this.$translate.instant('No changes to save');
let p = this.$q.reject(new Error(message));
console.log(p);
return p;
2017-02-07 16:02:17 +00:00
}
invalidForm() {
let message = this.$translate.instant('Some fields are invalid');
return this.$q.reject(new Error(message));
2017-02-21 10:36:43 +00:00
}
writeData(json, resolve) {
Object.assign(this.data, json.data);
this.updateOriginalData();
resolve(json);
2017-02-21 10:36:43 +00:00
}
2017-10-05 07:31:28 +00:00
updateOriginalData() {
this.orgData = this.copyInNewObject(this.data);
2018-02-06 08:33:37 +00:00
if (this.form && this.form.$dirty)
this.form.$setPristine();
}
2017-10-05 07:31:28 +00:00
copyInNewObject(data) {
let newCopy = {};
if (data && typeof data === 'object') {
Object.keys(data).forEach(
val => {
2017-12-21 13:09:16 +00:00
if (!isFullEmpty(data[val])) {
2017-10-05 07:20:40 +00:00
if (typeof data[val] === 'object') {
2017-10-05 07:31:28 +00:00
newCopy[val] = this.copyInNewObject(data[val]);
2017-10-05 07:20:40 +00:00
} else {
2017-10-05 07:31:28 +00:00
newCopy[val] = data[val];
2017-10-05 07:20:40 +00:00
}
}
}
);
}
2017-10-05 07:31:28 +00:00
return newCopy;
}
callback(transition) {
2017-09-21 10:24:14 +00:00
let dataChanged = this.dataChanged();
if (!this.state && dataChanged) {
this.state = transition.to().name;
2017-02-07 16:38:30 +00:00
this.$scope.confirm.show();
return false;
}
return true;
}
dataChanged() {
2017-10-05 07:31:28 +00:00
let newData = this.copyInNewObject(this.data);
if (this.form && this.form.$dirty) return !isEqual(newData, this.orgData);
return !isEqual(newData, this.orgData);
}
onConfirmResponse(response) {
if (response === 'ACCEPT') {
2018-02-27 07:09:51 +00:00
if (this.data)
Object.assign(this.data, this.orgData);
this.$state.go(this.state);
} else {
this.state = null;
}
}
}
Watcher.$inject = ['$element', '$scope', '$state', '$transitions', '$http', 'vnApp', '$translate', '$attrs', '$q'];
2018-02-10 15:18:01 +00:00
ngModule.component('vnWatcher', {
2017-05-31 08:28:39 +00:00
template: require('./watcher.html'),
bindings: {
url: '@?',
idField: '@?',
data: '<',
2017-02-21 10:36:43 +00:00
form: '<',
save: '<',
get: '=?'
},
controller: Watcher
});