2017-02-06 17:01:04 +00:00
|
|
|
import {module} from '../module';
|
2017-02-07 13:34:26 +00:00
|
|
|
import Component from '../lib/component';
|
|
|
|
import getModifiedData from '../lib/modified';
|
|
|
|
import copyObject from '../lib/copy';
|
|
|
|
import isEqual from '../lib/equals';
|
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 {
|
2017-02-07 11:58:25 +00:00
|
|
|
constructor($element, $scope, $state, $transitions, $http, vnAppLogger) {
|
2017-02-06 17:01:04 +00:00
|
|
|
super($element);
|
2017-02-07 11:58:25 +00:00
|
|
|
this.$scope = $scope;
|
2017-02-06 17:01:04 +00:00
|
|
|
this.$state = $state;
|
|
|
|
this.$http = $http;
|
|
|
|
this.vnAppLogger = vnAppLogger;
|
|
|
|
|
|
|
|
this.state = null;
|
|
|
|
this.deregisterCallback = $transitions.onStart({},
|
|
|
|
(transition) => this.callback(transition));
|
|
|
|
this.copyData();
|
|
|
|
}
|
2017-02-07 11:58:25 +00:00
|
|
|
$onInit() {
|
|
|
|
if(this.get) {
|
|
|
|
this.fetchData();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$onChanges(changes) {
|
|
|
|
if(this.data) {
|
|
|
|
this.copyData();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$onDestroy() {
|
|
|
|
this.deregisterCallback();
|
|
|
|
}
|
|
|
|
fetchData() {
|
|
|
|
let id = this.data[this.idField];
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.$http.get(`${this.url}/${id}`).then(
|
2017-02-07 16:02:17 +00:00
|
|
|
json => this.writeData(json, resolve),
|
2017-02-07 11:58:25 +00:00
|
|
|
json => reject(json)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2017-02-06 17:01:04 +00:00
|
|
|
submit() {
|
|
|
|
if (!this.dataChanged()) {
|
|
|
|
this.vnAppLogger.showMessage('No changes detected');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-07 11:58:25 +00:00
|
|
|
let id = this.orgData[this.idField];
|
2017-02-06 17:01:04 +00:00
|
|
|
let changedData = getModifiedData(this.data, this.orgData);
|
|
|
|
|
2017-02-07 11:58:25 +00:00
|
|
|
if(id) {
|
2017-02-07 16:02:17 +00:00
|
|
|
console.log(changedData);
|
2017-02-07 11:58:25 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.$http.put(`${this.url}/${id}`, changedData).then(
|
2017-02-07 16:02:17 +00:00
|
|
|
json => this.writeData(json, resolve),
|
2017-02-07 11:58:25 +00:00
|
|
|
json => reject(json)
|
|
|
|
);
|
|
|
|
});
|
2017-02-06 17:01:04 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-02-07 11:58:25 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.$http.post(this.url, this.data).then(
|
2017-02-07 16:02:17 +00:00
|
|
|
json => this.writeData(json, resolve),
|
2017-02-07 11:58:25 +00:00
|
|
|
json => reject(json)
|
|
|
|
);
|
|
|
|
});
|
2017-02-06 17:01:04 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-07 16:02:17 +00:00
|
|
|
writeData(json, resolve) {
|
|
|
|
copyObject(json.data, this.data);
|
|
|
|
this.copyData();
|
|
|
|
resolve(json);
|
|
|
|
}
|
2017-02-06 17:01:04 +00:00
|
|
|
copyData() {
|
2017-02-07 16:02:17 +00:00
|
|
|
this.orgData = copyObject(this.data);
|
2017-02-06 17:01:04 +00:00
|
|
|
}
|
|
|
|
callback(transition) {
|
|
|
|
if (!this.state && this.dataChanged()) {
|
|
|
|
this.state = transition.to().name;
|
|
|
|
let dialog = this.element.querySelector('vn-confirm');
|
|
|
|
dialog.$ctrl.show();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
dataChanged() {
|
2017-02-07 11:58:25 +00:00
|
|
|
return !isEqual(this.data, this.orgData);
|
2017-02-06 17:01:04 +00:00
|
|
|
}
|
|
|
|
onConfirmResponse(response) {
|
|
|
|
if(response == 'ACCEPT') {
|
|
|
|
copyObject(this.orgData, this.data);
|
|
|
|
this.$state.go(this.state);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.state = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-07 11:58:25 +00:00
|
|
|
Watcher.$inject = ['$element', '$scope', '$state', '$transitions', '$http', 'vnAppLogger'];
|
2017-02-06 17:01:04 +00:00
|
|
|
|
|
|
|
module.component('vnWatcher', {
|
|
|
|
template: require('./index.html'),
|
|
|
|
bindings: {
|
|
|
|
url: '@?',
|
|
|
|
idField: '@?',
|
2017-02-07 11:58:25 +00:00
|
|
|
data: '<',
|
|
|
|
get: '=?'
|
2017-02-06 17:01:04 +00:00
|
|
|
},
|
|
|
|
controller: Watcher
|
|
|
|
});
|