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';
|
2018-05-31 09:52:39 +00:00
|
|
|
import UserError from '../../lib/user-error';
|
2017-02-06 17:01:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2018-05-25 15:25:35 +00:00
|
|
|
constructor($element, $scope, $state, $transitions, $http, vnApp, $translate, $attrs, $q) {
|
2017-02-06 17:01:04 +00:00
|
|
|
super($element);
|
2018-05-31 09:52:39 +00:00
|
|
|
this.$ = $scope;
|
2017-02-06 17:01:04 +00:00
|
|
|
this.$state = $state;
|
|
|
|
this.$http = $http;
|
2018-05-31 09:52:39 +00:00
|
|
|
this._ = $translate;
|
2018-01-09 12:49:15 +00:00
|
|
|
this.$attrs = $attrs;
|
2017-06-05 07:01:21 +00:00
|
|
|
this.vnApp = vnApp;
|
2018-05-25 15:25:35 +00:00
|
|
|
this.$q = $q;
|
2017-02-06 17:01:04 +00:00
|
|
|
|
|
|
|
this.state = null;
|
|
|
|
this.deregisterCallback = $transitions.onStart({},
|
2017-03-07 11:37:59 +00:00
|
|
|
transition => this.callback(transition));
|
2017-10-05 07:31:28 +00:00
|
|
|
this.updateOriginalData();
|
2017-02-06 17:01:04 +00:00
|
|
|
}
|
2017-10-04 11:27:39 +00:00
|
|
|
|
2017-02-07 11:58:25 +00:00
|
|
|
$onInit() {
|
2018-05-31 09:52:39 +00:00
|
|
|
if (this.get && this.url)
|
2017-02-07 11:58:25 +00:00
|
|
|
this.fetchData();
|
2018-05-31 09:52:39 +00:00
|
|
|
else if (this.get && !this.url)
|
|
|
|
throw new Error('URL parameter ommitted');
|
2017-02-07 11:58:25 +00:00
|
|
|
}
|
2017-10-04 11:27:39 +00:00
|
|
|
|
2017-02-07 11:58:25 +00:00
|
|
|
$onChanges(changes) {
|
2018-05-31 09:52:39 +00:00
|
|
|
if (this.data)
|
2017-10-05 07:31:28 +00:00
|
|
|
this.updateOriginalData();
|
2017-02-07 11:58:25 +00:00
|
|
|
}
|
2017-10-04 11:27:39 +00:00
|
|
|
|
2017-02-07 11:58:25 +00:00
|
|
|
$onDestroy() {
|
|
|
|
this.deregisterCallback();
|
|
|
|
}
|
2017-10-04 11:27:39 +00:00
|
|
|
|
2018-05-31 09:52:39 +00:00
|
|
|
get dirty() {
|
|
|
|
return this.form && this.form.$dirty || this.dataChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
dataChanged() {
|
|
|
|
let data = this.copyInNewObject(this.data);
|
|
|
|
return !isEqual(data, this.orgData);
|
|
|
|
}
|
|
|
|
|
2017-02-07 11:58:25 +00:00
|
|
|
fetchData() {
|
|
|
|
let id = this.data[this.idField];
|
2018-05-31 09:52:39 +00:00
|
|
|
return this.$http.get(`${this.url}/${id}`).then(
|
2017-09-28 09:29:01 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
);
|
2017-02-07 11:58:25 +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() {
|
2018-05-25 15:25:35 +00:00
|
|
|
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) {
|
2018-05-25 15:25:35 +00:00
|
|
|
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
|
|
|
|
*/
|
2017-02-06 17:01:04 +00:00
|
|
|
submit() {
|
2018-05-31 09:52:39 +00:00
|
|
|
try {
|
|
|
|
this.check();
|
|
|
|
} catch (err) {
|
|
|
|
return this.$q.reject(err);
|
|
|
|
}
|
|
|
|
|
2018-05-25 15:25:35 +00:00
|
|
|
return this.realSubmit().then(res => {
|
2018-05-31 09:52:39 +00:00
|
|
|
this.notifySaved();
|
2018-05-25 15:25:35 +00:00
|
|
|
return res;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-31 09:52:39 +00:00
|
|
|
/**
|
|
|
|
* Submits the data without checking data validity or changes.
|
|
|
|
*
|
|
|
|
* @return {Promise} The http request promise
|
|
|
|
*/
|
2018-05-25 15:25:35 +00:00
|
|
|
realSubmit() {
|
2018-05-31 09:52:39 +00:00
|
|
|
if (this.form)
|
2017-03-07 16:11:56 +00:00
|
|
|
this.form.$setSubmitted();
|
|
|
|
|
2018-05-31 09:52:39 +00:00
|
|
|
if (!this.dataChanged()) {
|
|
|
|
this.updateOriginalData();
|
|
|
|
return this.$q.resolve();
|
2017-02-06 17:01:04 +00:00
|
|
|
}
|
2018-05-25 15:25:35 +00:00
|
|
|
|
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-05-31 09:52:39 +00:00
|
|
|
let id = this.idField ? this.orgData[this.idField] : null;
|
|
|
|
|
|
|
|
// If watcher is associated to mgCrud
|
|
|
|
|
2018-02-01 12:28:45 +00:00
|
|
|
if (this.save && this.save.accept) {
|
2018-05-31 09:52:39 +00:00
|
|
|
if (id)
|
|
|
|
changedData[this.idField] = id;
|
|
|
|
|
2018-05-25 15:25:35 +00:00
|
|
|
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),
|
2018-05-31 09:52:39 +00:00
|
|
|
reject
|
2017-02-21 10:36:43 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-31 09:52:39 +00:00
|
|
|
// When mgCrud is not used
|
2017-02-06 17:01:04 +00:00
|
|
|
|
2017-03-07 11:37:59 +00:00
|
|
|
if (id) {
|
2018-05-25 15:25:35 +00:00
|
|
|
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),
|
2018-05-25 15:25:35 +00:00
|
|
|
reject
|
2017-02-07 11:58:25 +00:00
|
|
|
);
|
|
|
|
});
|
2017-02-06 17:01:04 +00:00
|
|
|
}
|
2017-03-07 11:37:59 +00:00
|
|
|
|
2018-11-06 12:59:16 +00:00
|
|
|
|
2018-05-25 15:25:35 +00:00
|
|
|
return this.$q((resolve, reject) => {
|
2018-02-01 12:28:45 +00:00
|
|
|
this.$http.post(this.url, changedData).then(
|
2017-03-07 11:37:59 +00:00
|
|
|
json => this.writeData(json, resolve),
|
2018-05-25 15:25:35 +00:00
|
|
|
reject
|
2017-03-07 11:37:59 +00:00
|
|
|
);
|
|
|
|
});
|
2017-02-06 17:01:04 +00:00
|
|
|
}
|
2017-10-04 11:27:39 +00:00
|
|
|
|
2018-05-31 09:52:39 +00:00
|
|
|
/**
|
|
|
|
* Checks if data is ready to send.
|
|
|
|
*/
|
|
|
|
check() {
|
|
|
|
if (this.form && this.form.$invalid)
|
|
|
|
throw new UserError(this._.instant('Some fields are invalid'));
|
|
|
|
if (!this.dirty)
|
|
|
|
throw new UserError(this._.instant('No changes to save'));
|
2017-02-07 16:02:17 +00:00
|
|
|
}
|
2017-10-04 11:27:39 +00:00
|
|
|
|
2018-05-31 09:52:39 +00:00
|
|
|
/**
|
|
|
|
* Notifies the user that the data has been saved.
|
|
|
|
*/
|
|
|
|
notifySaved() {
|
2018-06-19 06:12:36 +00:00
|
|
|
this.vnApp.showSuccess(this._.instant('Data saved!'));
|
2017-02-21 10:36:43 +00:00
|
|
|
}
|
2017-10-04 11:27:39 +00:00
|
|
|
|
2018-08-30 06:50:03 +00:00
|
|
|
setPristine() {
|
|
|
|
if (this.form) this.form.$setPristine();
|
|
|
|
}
|
|
|
|
|
|
|
|
setDirty() {
|
|
|
|
if (this.form) this.form.$setDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(transition) {
|
|
|
|
if (!this.state && this.dirty) {
|
|
|
|
this.state = transition.to().name;
|
|
|
|
this.$.confirm.show();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
onConfirmResponse(response) {
|
|
|
|
if (response === 'ACCEPT') {
|
|
|
|
if (this.data)
|
|
|
|
Object.assign(this.data, this.orgData);
|
|
|
|
this.$state.go(this.state);
|
2018-11-06 12:59:16 +00:00
|
|
|
} else
|
2018-08-30 06:50:03 +00:00
|
|
|
this.state = null;
|
|
|
|
}
|
|
|
|
|
2018-05-25 15:25:35 +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-04 11:27:39 +00:00
|
|
|
|
2017-10-05 07:31:28 +00:00
|
|
|
updateOriginalData() {
|
|
|
|
this.orgData = this.copyInNewObject(this.data);
|
2018-05-31 09:52:39 +00:00
|
|
|
this.setPristine();
|
|
|
|
}
|
|
|
|
|
2017-10-05 07:31:28 +00:00
|
|
|
copyInNewObject(data) {
|
|
|
|
let newCopy = {};
|
|
|
|
if (data && typeof data === 'object') {
|
2017-07-13 12:55:07 +00:00
|
|
|
Object.keys(data).forEach(
|
2018-09-24 08:43:54 +00:00
|
|
|
key => {
|
|
|
|
let value = data[key];
|
|
|
|
if (value instanceof Date)
|
|
|
|
newCopy[key] = new Date(value.getTime());
|
|
|
|
else if (!isFullEmpty(value)) {
|
|
|
|
if (typeof value === 'object')
|
|
|
|
newCopy[key] = this.copyInNewObject(value);
|
|
|
|
else
|
|
|
|
newCopy[key] = value;
|
2017-10-05 07:20:40 +00:00
|
|
|
}
|
2017-07-13 12:55:07 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2018-11-06 12:59:16 +00:00
|
|
|
|
2017-10-05 07:31:28 +00:00
|
|
|
return newCopy;
|
2017-07-13 12:55:07 +00:00
|
|
|
}
|
2017-02-06 17:01:04 +00:00
|
|
|
}
|
2018-05-25 15:25:35 +00:00
|
|
|
Watcher.$inject = ['$element', '$scope', '$state', '$transitions', '$http', 'vnApp', '$translate', '$attrs', '$q'];
|
2017-02-06 17:01:04 +00:00
|
|
|
|
2018-02-10 15:18:01 +00:00
|
|
|
ngModule.component('vnWatcher', {
|
2017-05-31 08:28:39 +00:00
|
|
|
template: require('./watcher.html'),
|
2017-02-06 17:01:04 +00:00
|
|
|
bindings: {
|
|
|
|
url: '@?',
|
|
|
|
idField: '@?',
|
2017-02-07 11:58:25 +00:00
|
|
|
data: '<',
|
2017-02-21 10:36:43 +00:00
|
|
|
form: '<',
|
|
|
|
save: '<',
|
2018-05-31 09:52:39 +00:00
|
|
|
get: '<?'
|
2017-02-06 17:01:04 +00:00
|
|
|
},
|
|
|
|
controller: Watcher
|
|
|
|
});
|