diff --git a/front/core/components/treeview/index.js b/front/core/components/treeview/index.js
index 0e1ace3b0..99d0a864f 100644
--- a/front/core/components/treeview/index.js
+++ b/front/core/components/treeview/index.js
@@ -27,51 +27,44 @@ export default class Treeview extends Component {
});
}
- /**
- * Emits selection event
- * @param {Object} item - Selected item
- * @param {Boolean} value - Changed value
- */
- onSelection(item, value) {
- this.emit('selection', {item, value});
- }
-
- onCreate(parent) {
- this.emit('create', {parent});
- }
-
onToggle(item) {
if (item.active)
- item.childs = undefined;
- else {
- this.model.applyFilter({}, {parentFk: item.id}).then(() => {
- const newData = this.model.data;
+ this.fold(item);
+ else
+ this.unfold(item);
+ }
- if (item.childs) {
- let childs = item.childs;
- childs.forEach(child => {
- let index = newData.findIndex(newChild => {
- return newChild.id == child.id;
- });
- newData[index] = child;
+ fold(item) {
+ item.childs = undefined;
+ item.active = false;
+ }
+
+ unfold(item) {
+ return this.model.applyFilter({}, {parentFk: item.id}).then(() => {
+ const newData = this.model.data;
+
+ if (item.childs) {
+ let childs = item.childs;
+ childs.forEach(child => {
+ let index = newData.findIndex(newChild => {
+ return newChild.id == child.id;
});
+ newData[index] = child;
+ });
+ }
+
+ item.childs = newData.sort((a, b) => {
+ if (b.selected !== a.selected) {
+ if (a.selected == null)
+ return 1;
+ if (b.selected == null)
+ return -1;
+ return b.selected - a.selected;
}
- item.childs = newData.sort((a, b) => {
- if (b.selected !== a.selected) {
- if (a.selected == null)
- return 1;
- if (b.selected == null)
- return -1;
- return b.selected - a.selected;
- }
-
- return a.name.localeCompare(b.name);
- });
+ return a.name.localeCompare(b.name);
});
- }
-
- item.active = !item.active;
+ }).then(() => item.active = true);
}
onDrop(item, dragged, dropped) {
@@ -80,7 +73,7 @@ export default class Treeview extends Component {
onRemove(item, items, parent) {
if (!this.removeFunc) return;
- let res = this.removeFunc({$item: item});
+ let res = this.removeFunc({$item: item, $parent: parent});
function remove() {
let index = items.indexOf(item);
@@ -94,25 +87,50 @@ export default class Treeview extends Component {
remove();
}
- onAdd(parent, childs) {
- if (!this.addFunc) return;
- let res = this.addFunc({$parent: parent});
+ onCreate(parent, childs) {
+ if (!this.createFunc) return;
+ let res = this.createFunc({$parent: parent, $childs: childs});
- function add() {
+ function create() {
if (!childs) childs = [];
childs.push(res);
- if (parent) {
- parent.childs = childs;
- parent.sons++;
- parent.active = true;
- }
+ if (parent) parent.sons++;
}
- if (res instanceof Object && res.then)
- res.then(add);
- else if (res)
- add();
+ if (res instanceof Object && res.then) {
+ if (parent && !parent.active)
+ this.unfold(parent).then(() => res.then(create));
+ else res.then(create);
+ } else if (res) {
+ if (parent && !parent.active)
+ this.unfold(parent).then(() => create());
+ else create();
+ } else if (parent && !parent.active)
+ this.unfold(parent);
}
+
+ /* onCreate(parent, childs) {
+ if (!this.createFunc) return;
+ let res = this.createFunc({$parent: parent});
+
+ function create() {
+ if (!childs) childs = [];
+
+ childs.push(res);
+
+ if (parent) parent.sons++;
+ }
+
+ if (res instanceof Object && res.then) {
+ if (!parent.active)
+ this.unfold(parent).then(() => res.then(create));
+ else res.then(create);
+ } else if (res) {
+ if (!parent.active)
+ this.unfold(parent).then(() => create());
+ else create();
+ }
+ } */
}
Treeview.$inject = ['$element', '$scope', '$transclude'];
@@ -130,7 +148,7 @@ ngModule.component('vnTreeview', {
droppable: '',
aclRole: '@?',
removeFunc: '&?',
- addFunc: '&?'
+ createFunc: '&?'
},
transclude: true
});
diff --git a/loopback/locale/es.json b/loopback/locale/es.json
index b6c70be3a..9daf90d63 100644
--- a/loopback/locale/es.json
+++ b/loopback/locale/es.json
@@ -107,5 +107,6 @@
"Invalid quantity": "Cantidad invalida",
"This postal code is not valid": "This postal code is not valid",
"is invalid": "is invalid",
- "The postcode doesn't exists. Ensure you put the correct format": "El código postal no existe. Asegúrate de ponerlo con el formato correcto"
+ "The postcode doesn't exists. Ensure you put the correct format": "El código postal no existe. Asegúrate de ponerlo con el formato correcto",
+ "The department name can't be repeated": "The department name can't be repeated"
}
\ No newline at end of file
diff --git a/modules/worker/back/methods/department/createChild.js b/modules/worker/back/methods/department/createChild.js
new file mode 100644
index 000000000..57bcbe5be
--- /dev/null
+++ b/modules/worker/back/methods/department/createChild.js
@@ -0,0 +1,40 @@
+const UserError = require('vn-loopback/util/user-error');
+
+module.exports = Self => {
+ Self.remoteMethod('createChild', {
+ description: 'Creates a new child department',
+ accessType: 'WRITE',
+ accepts: [{
+ arg: 'parentId',
+ type: 'Number'
+ },
+ {
+ arg: 'name',
+ type: 'String',
+ required: true,
+ }],
+ returns: {
+ type: 'object',
+ root: true
+ },
+ http: {
+ path: `/createChild`,
+ verb: 'POST'
+ }
+ });
+
+ Self.createChild = async(parentId = null, name) => {
+ const models = Self.app.models;
+ const nameExists = await models.Department.count({name});
+
+ if (nameExists)
+ throw new UserError(`The department name can't be repeated`);
+
+ const newDep = await models.Department.create({
+ parentFk: parentId,
+ name: name
+ });
+
+ return newDep;
+ };
+};
diff --git a/modules/worker/back/methods/department/nodeAdd.js b/modules/worker/back/methods/department/nodeAdd.js
deleted file mode 100644
index 93a6f81c0..000000000
--- a/modules/worker/back/methods/department/nodeAdd.js
+++ /dev/null
@@ -1,43 +0,0 @@
-
-const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
-
-module.exports = Self => {
- Self.remoteMethod('nodeAdd', {
- description: 'Returns the first shipped and landed possible for params',
- accessType: 'WRITE',
- accepts: [{
- arg: 'parentFk',
- type: 'Number',
- required: false,
- },
- {
- arg: 'name',
- type: 'String',
- required: true,
- }],
- returns: {
- type: 'object',
- root: true
- },
- http: {
- path: `/nodeAdd`,
- verb: 'POST'
- }
- });
-
- Self.nodeAdd = async(parentFk = 1, name) => {
- let stmts = [];
- let conn = Self.dataSource.connector;
- let nodeIndex = stmts.push(new ParameterizedSQL(
- `CALL nst.nodeAdd('vn', 'department', ?, ?)`, [parentFk, name])) - 1;
-
- stmts.push(`CALL nst.nodeRecalc('vn', 'department')`);
-
-
- let sql = ParameterizedSQL.join(stmts, ';');
- let result = await conn.executeStmt(sql);
- let [node] = result[nodeIndex];
-
- return node;
- };
-};
diff --git a/modules/worker/back/methods/department/nodeDelete.js b/modules/worker/back/methods/department/nodeDelete.js
deleted file mode 100644
index 92b354ee5..000000000
--- a/modules/worker/back/methods/department/nodeDelete.js
+++ /dev/null
@@ -1,29 +0,0 @@
-
-const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
-
-module.exports = Self => {
- Self.remoteMethod('nodeDelete', {
- description: 'Returns the first shipped and landed possible for params',
- accessType: 'WRITE',
- accepts: [{
- arg: 'parentFk',
- type: 'Number',
- required: false,
- }],
- returns: {
- type: ['object'],
- root: true
- },
- http: {
- path: `/nodeDelete`,
- verb: 'POST'
- }
- });
-
- Self.nodeDelete = async parentFk => {
- let stmt = new ParameterizedSQL(
- `CALL nst.nodeDelete('vn', 'department', ?)`, [parentFk]);
-
- return await Self.rawStmt(stmt);
- };
-};
diff --git a/modules/worker/back/methods/department/removeChild.js b/modules/worker/back/methods/department/removeChild.js
new file mode 100644
index 000000000..2d3f40b36
--- /dev/null
+++ b/modules/worker/back/methods/department/removeChild.js
@@ -0,0 +1,27 @@
+module.exports = Self => {
+ Self.remoteMethod('removeChild', {
+ description: 'Returns the first shipped and landed possible for params',
+ accessType: 'WRITE',
+ accepts: [{
+ arg: 'id',
+ type: 'Number',
+ description: 'The department id',
+ http: {source: 'path'}
+ }],
+ returns: {
+ type: 'Object',
+ root: true
+ },
+ http: {
+ path: `/:id/removeChild`,
+ verb: 'POST'
+ }
+ });
+
+ Self.removeChild = async id => {
+ const models = Self.app.models;
+ const department = await models.Department.findById(id);
+
+ return await department.destroy();
+ };
+};
diff --git a/modules/worker/back/models/department.js b/modules/worker/back/models/department.js
index 085c2bf9b..e6905d273 100644
--- a/modules/worker/back/models/department.js
+++ b/modules/worker/back/models/department.js
@@ -1,15 +1,5 @@
-const UserError = require('vn-loopback/util/user-error');
-
module.exports = Self => {
require('../methods/department/getLeaves')(Self);
- require('../methods/department/nodeAdd')(Self);
- require('../methods/department/nodeDelete')(Self);
-
- Self.rewriteDbError(function(err) {
- if (err.code === 'ER_ROW_IS_REFERENCED_2')
- return new UserError(`You cannot remove this department`);
- if (err.code === 'ER_DUP_ENTRY')
- return new UserError(`The department name can't be repeated`);
- return err;
- });
+ require('../methods/department/createChild')(Self);
+ require('../methods/department/removeChild')(Self);
};
diff --git a/modules/worker/back/models/department.json b/modules/worker/back/models/department.json
index 0b978b56c..bb5d5e943 100644
--- a/modules/worker/back/models/department.json
+++ b/modules/worker/back/models/department.json
@@ -13,6 +13,9 @@
},
"name": {
"type": "String"
+ },
+ "parentFk": {
+ "type": "Number"
}
}
}
diff --git a/modules/worker/front/department/index.html b/modules/worker/front/department/index.html
index 52303b0ad..16287b349 100644
--- a/modules/worker/front/department/index.html
+++ b/modules/worker/front/department/index.html
@@ -6,12 +6,9 @@