salix/modules/worker/front/department/index.js

96 lines
2.4 KiB
JavaScript
Raw Normal View History

2019-03-12 14:04:09 +00:00
import ngModule from '../module';
class Controller {
constructor($scope, $http, vnApp, $translate) {
2019-10-08 05:22:38 +00:00
this.$ = $scope;
2019-03-12 14:04:09 +00:00
this.$http = $http;
this.vnApp = vnApp;
this.$translate = $translate;
}
2019-10-08 05:22:38 +00:00
$postLink() {
this.$.treeview.fetch();
}
onFetch(item) {
2019-10-08 10:48:12 +00:00
const params = item ? {parentId: item.id} : null;
2019-10-08 05:22:38 +00:00
return this.$.model.applyFilter({}, params).then(() => {
return this.$.model.data;
});
}
onSort(a, b) {
return a.name.localeCompare(b.name);
}
2019-10-16 06:56:13 +00:00
onDrop(dropped, dragged) {
const params = dropped ? {parentId: dropped.id} : null;
const query = `departments/${dragged.id}/moveChild`;
2019-10-16 06:56:13 +00:00
this.$http.post(query, params).then(() => {
this.$.treeview.move(dragged, dropped);
});
}
2019-10-08 05:22:38 +00:00
onCreate(parent) {
2019-10-04 05:41:24 +00:00
this.newChild = {
parent: parent,
name: ''
};
2019-10-08 05:22:38 +00:00
this.$.createNode.show();
2019-10-04 05:41:24 +00:00
}
2019-03-12 14:04:09 +00:00
onCreateDialogOpen() {
2019-10-04 05:41:24 +00:00
this.newChild.name = '';
2019-03-12 14:04:09 +00:00
}
onCreateResponse(response) {
if (response == 'ACCEPT') {
try {
2019-10-04 05:41:24 +00:00
if (!this.newChild.name)
2019-03-12 14:04:09 +00:00
throw new Error(`Name can't be empty`);
2019-10-04 05:41:24 +00:00
const params = {name: this.newChild.name};
const parent = this.newChild.parent;
2019-10-08 05:22:38 +00:00
if (parent && parent.id)
params.parentId = parent.id;
const query = `departments/createChild`;
2019-10-08 05:22:38 +00:00
this.$http.post(query, params).then(res => {
const item = res.data;
item.parent = parent;
this.$.treeview.create(item);
2019-03-12 14:04:09 +00:00
});
} catch (e) {
this.vnApp.showError(this.$translate.instant(e.message));
return false;
}
}
return true;
}
2019-10-04 05:41:24 +00:00
onRemove(item) {
this.removedChild = item;
2019-10-08 05:22:38 +00:00
this.$.deleteNode.show();
2019-10-04 05:41:24 +00:00
}
2019-03-12 14:04:09 +00:00
onRemoveResponse(response) {
if (response === 'ACCEPT') {
2019-10-04 05:41:24 +00:00
const childId = this.removedChild.id;
const path = `departments/${childId}/removeChild`;
2019-10-04 05:41:24 +00:00
this.$http.post(path).then(() => {
2019-10-08 05:22:38 +00:00
this.$.treeview.remove(this.removedChild);
2019-03-12 14:04:09 +00:00
});
}
}
}
Controller.$inject = ['$scope', '$http', 'vnApp', '$translate'];
ngModule.component('vnWorkerDepartment', {
template: require('./index.html'),
controller: Controller
});