94 lines
2.7 KiB
JavaScript
94 lines
2.7 KiB
JavaScript
import ngModule from '../module';
|
|
|
|
class Controller {
|
|
constructor($scope, $http, vnApp, $translate) {
|
|
this.$scope = $scope;
|
|
this.$http = $http;
|
|
this.vnApp = vnApp;
|
|
this.$translate = $translate;
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
onCreate(parent, childs) {
|
|
this.newChild = {
|
|
parent: parent,
|
|
childs: childs,
|
|
name: ''
|
|
};
|
|
|
|
this.$scope.createNode.show();
|
|
}
|
|
|
|
onCreateDialogOpen() {
|
|
this.newChild.name = '';
|
|
}
|
|
|
|
onCreateResponse(response) {
|
|
if (response == 'ACCEPT') {
|
|
try {
|
|
if (!this.newChild.name)
|
|
throw new Error(`Name can't be empty`);
|
|
|
|
const params = {name: this.newChild.name};
|
|
const parent = this.newChild.parent;
|
|
const parentId = parent && parent.id || null;
|
|
let childs = this.newChild.childs;
|
|
|
|
if (parent) params.parentId = parentId;
|
|
|
|
const query = `/api/departments/createChild`;
|
|
this.$http.post(query, params).then(response => {
|
|
if (!childs) childs = [];
|
|
childs.push(response.data);
|
|
});
|
|
} catch (e) {
|
|
this.vnApp.showError(this.$translate.instant(e.message));
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
onRemove(item) {
|
|
this.removedChild = item;
|
|
this.$scope.deleteNode.show();
|
|
}
|
|
|
|
onRemoveResponse(response) {
|
|
if (response === 'ACCEPT') {
|
|
const childId = this.removedChild.id;
|
|
const path = `/api/departments/${childId}/removeChild`;
|
|
this.$http.post(path).then(() => {
|
|
items.splice(index, 1);
|
|
/* let parent = this.selectedNode.parent;
|
|
|
|
if ((parent instanceof Object) && !(parent instanceof Array)) {
|
|
const childs = parent.childs;
|
|
childs.splice(this.selectedNode.index, 1);
|
|
} else if ((parent instanceof Object) && (parent instanceof Array))
|
|
parent.splice(this.selectedNode.index, 1); */
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
Controller.$inject = ['$scope', '$http', 'vnApp', '$translate'];
|
|
|
|
ngModule.component('vnWorkerDepartment', {
|
|
template: require('./index.html'),
|
|
controller: Controller
|
|
});
|