salix/client/ticket/src/package/index.js

93 lines
2.5 KiB
JavaScript
Raw Normal View History

import ngModule from '../module';
class Controller {
2018-03-21 10:08:59 +00:00
constructor($http, $scope, $translate, vnApp) {
this.$http = $http;
this.$ = $scope;
2018-03-21 10:08:59 +00:00
this.$translate = $translate;
this.vnApp = vnApp;
this.removedPackages = [];
}
submit() {
let query = `/ticket/api/TicketPackagings/crudTicketPackaging`;
let packagesObj = {
delete: this.removedPackages,
create: [],
update: []
};
this.packages.forEach(item => {
if (typeof item.id === 'undefined')
packagesObj.create.push(item);
2018-03-27 13:06:22 +00:00
if (typeof item.id !== 'undefined' && !this.packageEquals(item, this.oldPackages[item.id]))
2018-03-21 10:08:59 +00:00
packagesObj.update.push(item);
});
2018-03-27 13:06:22 +00:00
if (this.$.form.$invalid)
return this.vnApp.showError(this.$translate.instant('Some fields are invalid'));
2018-03-27 13:06:22 +00:00
if (!this.hasChanges(packagesObj))
return this.vnApp.showError(this.$translate.instant('No changes to save'));
2018-03-27 13:06:22 +00:00
2018-03-21 10:08:59 +00:00
this.$http.post(query, packagesObj).then(res => {
this.$.index.accept();
});
}
removePackage(index) {
if (this.packages[index] && this.packages[index].id)
this.removedPackages.push(this.packages[index].id);
this.packages.splice(index, 1);
}
addPackage() {
let data = {
packagingFk: null,
quantity: null,
created: Date.now(),
ticketFk: this.ticket.id
};
this.packages.push(data);
}
getPackages() {
this.packages = this.$.index.model;
this.setOldPackages();
}
setOldPackages() {
2018-03-27 13:06:22 +00:00
if (this.oldPackages && !this.$.watcher.dataChanged())
return;
2018-03-21 10:08:59 +00:00
this.oldPackages = [];
2018-03-27 13:06:22 +00:00
this.removedPackages = [];
2018-03-21 10:08:59 +00:00
this.packages.forEach(item => {
2018-03-27 13:06:22 +00:00
this.oldPackages[item.id] = Object.assign({}, item);
2018-03-21 10:08:59 +00:00
});
}
2018-03-27 13:06:22 +00:00
packageEquals(newPackage, oldPackage) {
return newPackage.packagingFk === oldPackage.packagingFk && newPackage.quantity == oldPackage.quantity;
}
hasChanges(packagesObj) {
if (packagesObj.create.length || packagesObj.update.length || packagesObj.delete.length)
return true;
return false;
}
}
2018-03-27 13:06:22 +00:00
Controller.$inject = ['$http', '$scope', '$translate', 'vnApp'];
2018-03-27 13:06:22 +00:00
ngModule.component('vnTicketPackageIndex', {
template: require('./index.html'),
controller: Controller,
bindings: {
ticket: '<'
}
});