import ngModule from '../../module'; import Section from 'salix/components/section'; class Controller extends Section { $postLink() { this.$.treeview.fetch(); if (this.$params.q) { const search = JSON.parse(this.$params.q); this.onSearch(search); } } onSearch(params) { this.$.model.applyFilter({}, params).then(() => { const data = this.$.model.data; this.$.treeview.data = data; }); } 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() { 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.$t(e.message)); return false; } return true; } onRemove(item) { this.removedChild = item; this.$.deleteNode.show(); } onRemoveResponse() { const childId = this.removedChild.id; const path = `departments/${childId}/removeChild`; this.$http.post(path).then(() => { this.$.treeview.remove(this.removedChild); }); } } ngModule.vnComponent('vnWorkerDepartmentIndex', { template: require('./index.html'), controller: Controller });