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

106 lines
2.7 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) {
const params = item ? {parentFk: item.id} : null;
return this.$.model.applyFilter({}, params).then(() => {
return this.$.model.data;
});
}
onSort(a, b) {
return a.name.localeCompare(b.name);
}
/* onDrop(item, dragged, dropped) {
if (dropped.scope.item) {
const droppedItem = dropped.scope.item;
const draggedItem = dragged.scope.item;
if (droppedItem.childs)
droppedItem.childs.push(Object.assign({}, draggedItem));
dragged.element.remove();
this.$scope.$apply();
}
2019-10-08 05:22:38 +00:00
} */
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;
if (!parent.active)
this.$.treeview.unfold(parent);
2019-10-04 05:41:24 +00:00
const query = `/api/departments/createChild`;
2019-10-08 05:22:38 +00:00
this.$http.post(query, params).then(res => {
const parent = this.newChild.parent;
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 = `/api/departments/${childId}/removeChild`;
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
});