From b32cf4a386eb35706499a22c60ce223f3b98ba01 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Tue, 8 Oct 2019 07:22:38 +0200 Subject: [PATCH] last refactor --- front/core/components/treeview/childs.html | 26 +-- front/core/components/treeview/content.js | 20 +- front/core/components/treeview/index.html | 8 - front/core/components/treeview/index.js | 173 +++++++++--------- front/core/components/treeview/style.scss | 3 + .../methods/zone-included/toggleIsIncluded.js | 35 ---- modules/agency/back/methods/zone/getLeaves.js | 9 +- .../back/methods/zone/toggleIsIncluded.js | 41 +++++ modules/agency/back/models/zone-included.js | 3 - modules/agency/back/models/zone.js | 1 + modules/agency/front/location/index.html | 16 +- modules/agency/front/location/index.js | 37 +++- modules/worker/front/department/index.html | 6 +- modules/worker/front/department/index.js | 54 +++--- 14 files changed, 225 insertions(+), 207 deletions(-) delete mode 100644 modules/agency/back/methods/zone-included/toggleIsIncluded.js create mode 100644 modules/agency/back/methods/zone/toggleIsIncluded.js delete mode 100644 modules/agency/back/models/zone-included.js diff --git a/front/core/components/treeview/childs.html b/front/core/components/treeview/childs.html index 5573dbbd9..2dd7e77ef 100644 --- a/front/core/components/treeview/childs.html +++ b/front/core/components/treeview/childs.html @@ -14,25 +14,15 @@ - -
- - - + + ng-click="$ctrl.treeview.onRemove(item)" + ng-if="item.parent"> + +
diff --git a/front/core/components/treeview/content.js b/front/core/components/treeview/content.js index 62caf5674..506117d4f 100644 --- a/front/core/components/treeview/content.js +++ b/front/core/components/treeview/content.js @@ -1,16 +1,24 @@ import ngModule from '../../module'; class Controller { - constructor($element) { + constructor($element, $scope, $compile) { this.$element = $element; + this.$scope = $scope; + this.$compile = $compile; } $onInit() { - this.treeview.$transclude(($clone, $scope) => { - this.$contentScope = $scope; - $scope.item = this.item; + if (this.item.parent) { + this.treeview.$transclude(($clone, $scope) => { + this.$contentScope = $scope; + $scope.item = this.item; + this.$element.append($clone); + }); + } else { + let template = `{{$ctrl.treeview.rootLabel}}`; + let $clone = this.$compile(template)(this.$scope); this.$element.append($clone); - }); + } } $onDestroy() { @@ -18,7 +26,7 @@ class Controller { this.$contentScope.$destroy(); } } -Controller.$inject = ['$element']; +Controller.$inject = ['$element', '$scope', '$compile']; ngModule.component('vnTreeviewContent', { controller: Controller, diff --git a/front/core/components/treeview/index.html b/front/core/components/treeview/index.html index 1970ddc43..31e6a88a9 100644 --- a/front/core/components/treeview/index.html +++ b/front/core/components/treeview/index.html @@ -1,11 +1,3 @@ -
- - - - Create new one - -
diff --git a/front/core/components/treeview/index.js b/front/core/components/treeview/index.js index 99d0a864f..d9da39215 100644 --- a/front/core/components/treeview/index.js +++ b/front/core/components/treeview/index.js @@ -7,23 +7,45 @@ import './content'; /** * Treeview - * - * @property {String} position The relative position to the parent */ export default class Treeview extends Component { constructor($element, $scope, $transclude) { super($element, $scope); this.$transclude = $transclude; - this.items = []; + this.readOnly = true; } - $onInit() { - this.refresh(); + get data() { + return this._data; } - refresh() { - this.model.refresh().then(() => { - this.items = this.model.data; + set data(value) { + this._data = value; + + const sons = value.length; + const rootElement = [{ + childs: value, + active: true, + sons: sons + }]; + + this.setParent(rootElement[0], value); + + this.items = rootElement; + } + + fetch() { + return this.fetchFunc().then(res => + this.data = res + ); + } + + setParent(parent, childs) { + childs.forEach(child => { + child.parent = parent; + + if (child.childs) + this.setParent(parent, child.childs); }); } @@ -40,11 +62,11 @@ export default class Treeview extends Component { } unfold(item) { - return this.model.applyFilter({}, {parentFk: item.id}).then(() => { - const newData = this.model.data; + return this.fetchFunc({$item: item}).then(newData => { + this.setParent(item, newData); - if (item.childs) { - let childs = item.childs; + const childs = item.childs; + if (childs) { childs.forEach(child => { let index = newData.findIndex(newChild => { return newChild.id == child.id; @@ -53,84 +75,54 @@ export default class Treeview extends Component { }); } - 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); - }); + if (this.sortFunc) { + item.childs = newData.sort((a, b) => + this.sortFunc({$a: a, $b: b}) + ); + } }).then(() => item.active = true); } + onRemove(item) { + if (this.removeFunc) + this.removeFunc({$item: item}); + } + + remove(item) { + const parent = item.parent; + let childs = parent.childs; + + if (!childs) childs = []; + + let index = childs.indexOf(item); + childs.splice(index, 1); + if (parent) parent.sons--; + } + + onCreate(parent) { + if (this.createFunc) + this.createFunc({$parent: parent}); + } + + create(item) { + const parent = item.parent; + let childs = parent.childs; + if (!childs) childs = []; + + childs.push(item); + + if (this.sortFunc) { + childs = childs.sort((a, b) => + this.sortFunc({$a: a, $b: b}) + ); + } + + if (parent) parent.sons++; + } + onDrop(item, dragged, dropped) { this.emit('drop', {item, dragged, dropped}); } - - onRemove(item, items, parent) { - if (!this.removeFunc) return; - let res = this.removeFunc({$item: item, $parent: parent}); - - function remove() { - let index = items.indexOf(item); - items.splice(index, 1); - if (parent) parent.sons--; - } - - if (res instanceof Object && res.then) - res.then(remove); - else if (res) - remove(); - } - - onCreate(parent, childs) { - if (!this.createFunc) return; - let res = this.createFunc({$parent: parent, $childs: childs}); - - function create() { - if (!childs) childs = []; - childs.push(res); - if (parent) parent.sons++; - } - - 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']; @@ -139,16 +131,15 @@ ngModule.component('vnTreeview', { template: require('./index.html'), controller: Treeview, bindings: { - model: '<', - icons: ' { - Self.remoteMethod('toggleIsIncluded', { - description: 'Toggle include to delivery', - accepts: [{ - arg: 'zoneFk', - type: 'Number', - required: true, - }, - { - arg: 'item', - type: 'Object', - required: true, - }], - returns: { - type: 'object', - root: true - }, - http: { - path: `/toggleIsIncluded`, - verb: 'POST' - } - }); - - Self.toggleIsIncluded = async(zoneFk, item) => { - if (item.isIncluded === null) - return Self.destroyAll({zoneFk, geoFk: item.id}); - else { - return Self.upsert({ - zoneFk: zoneFk, - geoFk: item.id, - isIncluded: item.isIncluded - }); - } - }; -}; diff --git a/modules/agency/back/methods/zone/getLeaves.js b/modules/agency/back/methods/zone/getLeaves.js index 50ee54345..c45136d4f 100644 --- a/modules/agency/back/methods/zone/getLeaves.js +++ b/modules/agency/back/methods/zone/getLeaves.js @@ -6,10 +6,11 @@ module.exports = Self => { { arg: 'id', type: 'Number', + description: 'The zone id', http: {source: 'path'}, required: true }, { - arg: 'parentFk', + arg: 'parentId', type: 'Number', description: 'Get the children of the specified father', }, { @@ -28,10 +29,10 @@ module.exports = Self => { } }); - Self.getLeaves = async(id, parentFk = null, search) => { + Self.getLeaves = async(id, parentId = null, search) => { let [res] = await Self.rawSql( `CALL zone_getLeaves(?, ?, ?)`, - [id, parentFk, search] + [id, parentId, search] ); let map = new Map(); @@ -49,7 +50,7 @@ module.exports = Self => { } } - let leaves = map.get(parentFk); + let leaves = map.get(parentId); setLeaves(leaves); return leaves || []; diff --git a/modules/agency/back/methods/zone/toggleIsIncluded.js b/modules/agency/back/methods/zone/toggleIsIncluded.js new file mode 100644 index 000000000..ae8f7c571 --- /dev/null +++ b/modules/agency/back/methods/zone/toggleIsIncluded.js @@ -0,0 +1,41 @@ +module.exports = Self => { + Self.remoteMethod('toggleIsIncluded', { + description: 'Toggle include to delivery', + accepts: [{ + arg: 'id', + type: 'Number', + description: 'The zone id', + http: {source: 'path'}, + required: true + }, { + arg: 'geoId', + type: 'Number', + required: true + }, { + arg: 'isIncluded', + type: 'Boolean' + }], + returns: { + type: 'object', + root: true + }, + http: { + path: `/:id/toggleIsIncluded`, + verb: 'POST' + } + }); + + Self.toggleIsIncluded = async(id, geoId, isIncluded) => { + const models = Self.app.models; + + if (isIncluded === undefined) + return models.ZoneIncluded.destroyAll({zoneFk: id, geoFk: geoId}); + else { + return models.ZoneIncluded.upsert({ + zoneFk: id, + geoFk: geoId, + isIncluded: isIncluded + }); + } + }; +}; diff --git a/modules/agency/back/models/zone-included.js b/modules/agency/back/models/zone-included.js deleted file mode 100644 index 6cf1192aa..000000000 --- a/modules/agency/back/models/zone-included.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = Self => { - require('../methods/zone-included/toggleIsIncluded')(Self); -}; diff --git a/modules/agency/back/models/zone.js b/modules/agency/back/models/zone.js index a6695bd02..1f8b0a675 100644 --- a/modules/agency/back/models/zone.js +++ b/modules/agency/back/models/zone.js @@ -3,6 +3,7 @@ module.exports = Self => { require('../methods/zone/editPrices')(Self); require('../methods/zone/getLeaves')(Self); require('../methods/zone/getEvents')(Self); + require('../methods/zone/toggleIsIncluded')(Self); Self.validatesPresenceOf('agencyModeFk', { message: `Agency cannot be blank` diff --git a/modules/agency/front/location/index.html b/modules/agency/front/location/index.html index 3d00df006..5d16329dc 100644 --- a/modules/agency/front/location/index.html +++ b/modules/agency/front/location/index.html @@ -1,26 +1,22 @@ + filter="::$ctrl.filter">
- - + diff --git a/modules/agency/front/location/index.js b/modules/agency/front/location/index.js index 3e3decf23..860e73fcf 100644 --- a/modules/agency/front/location/index.js +++ b/modules/agency/front/location/index.js @@ -8,8 +8,29 @@ class Controller { } onSearch(params) { - this.$.model.applyFilter(null, params); - this.$.$applyAsync(() => this.$.treeview.refresh()); + 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) { + 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); } exprBuilder(param, value) { @@ -19,13 +40,11 @@ class Controller { } } - onSelection(item, isIncluded) { - let node = Object.assign({}, item); - node.isIncluded = isIncluded; - node.childs = []; // Data too large - - const path = '/agency/api/ZoneIncludeds/toggleIsIncluded'; - const params = {zoneFk: this.zone.id, item: node}; + onSelection(value, item) { + if (value == null) + value = undefined; + const params = {geoId: item.id, isIncluded: value}; + const path = `/api/zones/${this.zone.id}/toggleIsIncluded`; this.$http.post(path, params); } } diff --git a/modules/worker/front/department/index.html b/modules/worker/front/department/index.html index 16287b349..5da6dafe0 100644 --- a/modules/worker/front/department/index.html +++ b/modules/worker/front/department/index.html @@ -6,9 +6,11 @@
- + create-func="$ctrl.onCreate($parent)" + sort-func="$ctrl.onSort($a, $b)"> {{::item.name}} diff --git a/modules/worker/front/department/index.js b/modules/worker/front/department/index.js index 050bb215f..0023dcca9 100644 --- a/modules/worker/front/department/index.js +++ b/modules/worker/front/department/index.js @@ -2,13 +2,28 @@ import ngModule from '../module'; class Controller { constructor($scope, $http, vnApp, $translate) { - this.$scope = $scope; + this.$ = $scope; this.$http = $http; this.vnApp = vnApp; this.$translate = $translate; } - onDrop(item, dragged, dropped) { + $postLink() { + this.$.treeview.fetch(); + } + + onFetch(item) { + const params = item ? {parentFk: item.id} : null; + return this.$.model.applyFilter({}, params).then(() => { + return this.$.model.data; + }); + } + + onSort(a, b) { + return a.name.localeCompare(b.name); + } + + /* onDrop(item, dragged, dropped) { if (dropped.scope.item) { const droppedItem = dropped.scope.item; const draggedItem = dragged.scope.item; @@ -20,16 +35,15 @@ class Controller { this.$scope.$apply(); } - } + } */ - onCreate(parent, childs) { + onCreate(parent) { this.newChild = { parent: parent, - childs: childs, name: '' }; - this.$scope.createNode.show(); + this.$.createNode.show(); } onCreateDialogOpen() { @@ -44,15 +58,20 @@ class Controller { 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; + if (parent && parent.id) + params.parentId = parent.id; + + if (!parent.active) + this.$.treeview.unfold(parent); const query = `/api/departments/createChild`; - this.$http.post(query, params).then(response => { - if (!childs) childs = []; - childs.push(response.data); + this.$http.post(query, params).then(res => { + const parent = this.newChild.parent; + const item = res.data; + item.parent = parent; + + this.$.treeview.create(item); }); } catch (e) { this.vnApp.showError(this.$translate.instant(e.message)); @@ -64,7 +83,7 @@ class Controller { onRemove(item) { this.removedChild = item; - this.$scope.deleteNode.show(); + this.$.deleteNode.show(); } onRemoveResponse(response) { @@ -72,14 +91,7 @@ class Controller { 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); */ + this.$.treeview.remove(this.removedChild); }); } }