import ngModule from '../module'; class Controller { constructor($scope, $http, vnApp, $translate) { this.$ = $scope; this.$http = $http; this.vnApp = vnApp; this.$translate = $translate; } $postLink() { this.$.treeview.fetch(); } onFetch(item) { const params = item ? {parentId: item.id} : null; return this.$.model.applyFilter({}, params).then(() => { return this.$.model.data; }); } onSort(a, b) { return a.name.localeCompare(b.name); } onDrop(dropped, dragged) { const params = dropped ? {parentId: dropped.id} : null; const query = `departments/${dragged.id}/moveChild`; this.$http.post(query, params).then(() => { this.$.treeview.move(dragged, dropped); }); } onCreate(parent) { this.newChild = { parent: parent, name: '' }; this.$.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; if (parent && parent.id) params.parentId = parent.id; const query = `departments/createChild`; this.$http.post(query, params).then(res => { const item = res.data; item.parent = parent; this.$.treeview.create(item); }); } catch (e) { this.vnApp.showError(this.$translate.instant(e.message)); return false; } } return true; } onRemove(item) { this.removedChild = item; this.$.deleteNode.show(); } onRemoveResponse(response) { if (response === 'accept') { const childId = this.removedChild.id; const path = `departments/${childId}/removeChild`; this.$http.post(path).then(() => { this.$.treeview.remove(this.removedChild); }); } } } Controller.$inject = ['$scope', '$http', 'vnApp', '$translate']; ngModule.component('vnWorkerDepartment', { template: require('./index.html'), controller: Controller });