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

81 lines
2.1 KiB
JavaScript
Raw Normal View History

import ngModule from '../module';
class Controller {
constructor($http, $scope, $stateParams, $translate, vnApp) {
this.$http = $http;
this.$scope = $scope;
this.$stateParams = $stateParams;
2018-03-21 10:08:59 +00:00
this.$translate = $translate;
this.vnApp = vnApp;
this.removedPackages = [];
}
submit() {
let query = `/ticket/api/TicketPackagings/crud`;
2018-03-21 10:08:59 +00:00
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);
});
this.$scope.watcher.check();
2018-03-27 13:06:22 +00:00
2018-03-21 10:08:59 +00:00
this.$http.post(query, packagesObj).then(res => {
this.$scope.watcher.notifySaved();
this.$scope.model.refresh();
2018-03-21 10:08:59 +00:00
});
}
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.$scope.model.data;
2018-03-21 10:08:59 +00:00
this.setOldPackages();
}
setOldPackages() {
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;
}
}
Controller.$inject = ['$http', '$scope', '$stateParams', '$translate', 'vnApp'];
2018-03-27 13:06:22 +00:00
ngModule.component('vnTicketPackageIndex', {
template: require('./index.html'),
controller: Controller,
bindings: {
ticket: '<'
}
});