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

84 lines
2.1 KiB
JavaScript
Raw Normal View History

2023-05-15 08:43:06 +00:00
import ngModule from '../../module';
2020-03-18 11:55:22 +00:00
import Section from 'salix/components/section';
2019-03-12 14:04:09 +00:00
2020-03-18 11:55:22 +00:00
class Controller extends Section {
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
}
2020-07-29 08:47:48 +00:00
onCreateResponse() {
try {
if (!this.newChild.name)
throw new Error(`Name can't be empty`);
2019-03-12 14:04:09 +00:00
2020-07-29 08:47:48 +00:00
const params = {name: this.newChild.name};
const parent = this.newChild.parent;
2019-10-04 05:41:24 +00:00
2020-07-29 08:47:48 +00:00
if (parent && parent.id)
params.parentId = parent.id;
2019-10-08 05:22:38 +00:00
2020-07-29 08:47:48 +00:00
const query = `departments/createChild`;
this.$http.post(query, params).then(res => {
const item = res.data;
item.parent = parent;
2019-10-08 05:22:38 +00:00
2020-07-29 08:47:48 +00:00
this.$.treeview.create(item);
});
} catch (e) {
this.vnApp.showError(this.$t(e.message));
return false;
2019-03-12 14:04:09 +00:00
}
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
}
2020-07-29 08:47:48 +00:00
onRemoveResponse() {
const childId = this.removedChild.id;
const path = `departments/${childId}/removeChild`;
this.$http.post(path).then(() => {
this.$.treeview.remove(this.removedChild);
});
2019-03-12 14:04:09 +00:00
}
}
ngModule.vnComponent('vnWorkerDepartment', {
2019-03-12 14:04:09 +00:00
template: require('./index.html'),
controller: Controller
});