From c6b2db9a792a13089aaf52971072d00812c2b6f7 Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Sat, 7 Mar 2020 13:52:02 +0100 Subject: [PATCH] Savepoint --- front/core/components/searchbar/searchbar.js | 153 ++++++++++--------- front/core/components/slot/index.js | 6 + front/salix/module.js | 5 +- modules/zone/front/index/index.html | 15 +- modules/zone/front/index/index.js | 68 +++------ modules/zone/front/main/index.html | 12 +- modules/zone/front/main/index.js | 25 ++- 7 files changed, 148 insertions(+), 136 deletions(-) diff --git a/front/core/components/searchbar/searchbar.js b/front/core/components/searchbar/searchbar.js index a82324877..f8099caf1 100644 --- a/front/core/components/searchbar/searchbar.js +++ b/front/core/components/searchbar/searchbar.js @@ -13,8 +13,11 @@ import './style.scss'; * * @property {Object} filter A key-value object with filter parameters * @property {SearchPanel} panel The panel used for advanced searches + * @property {CrudModel} model The model used for searching + * @property {Function} exprBuilder If defined, is used to build each non-null param expresion + * @property {Function} onSearch Function to call when search is submited */ -export default class Controller extends Component { +export default class Searchbar extends Component { constructor($element, $) { super($element, $); this.searchState = '.'; @@ -26,6 +29,9 @@ export default class Controller extends Component { $postLink() { this.onStateChange(); + + if (this.$state.is(this.searchState) && this.filter !== null) + this.doSearch(); } $onDestroy() { @@ -58,8 +64,9 @@ export default class Controller extends Component { onStateChange() { let filter = null; + let isIndex = this.$state.is(this.searchState); - if (this.$state.is(this.searchState)) { + if (isIndex) { if (this.$params.q) { try { filter = JSON.parse(this.$params.q); @@ -72,6 +79,11 @@ export default class Controller extends Component { } this.filter = filter; + + if (isIndex) + this.doSearch(); + else if (this.model) + this.model.clear(); } openPanel(event) { @@ -100,25 +112,21 @@ export default class Controller extends Component { this.$.popover.hide(); filter = compact(filter); filter = filter != null ? filter : {}; - this.doSearch(filter); + this.goSearch(filter); } onSubmit() { - this.doSearch(this.fromBar()); + this.goSearch(this.fromBar()); } removeParam(index) { this.params.splice(index, 1); - this.doSearch(this.fromBar()); + this.goSearch(this.fromBar()); } - doSearch(filter) { + goSearch(filter) { this.filter = filter; - - let opts = this.$state.is(this.searchState) - ? {location: 'replace'} : null; - this.$state.go(this.searchState, - {q: JSON.stringify(filter)}, opts); + this.doSearch(); } fromBar() { @@ -171,61 +179,6 @@ export default class Controller extends Component { this.params.push({chip, key, value}); }); } -} - -ngModule.vnComponent('vnSearchbar', { - controller: Controller, - template: require('./searchbar.html'), - bindings: { - searchState: '@?', - filter: ' this.onStateChange()); - - this.fetchFilter(); - } - - $postLink() { - if (this.filter !== null) - this.doSearch(); - } - - $onDestroy() { - this.deregisterCallback(); - } - - fetchFilter() { - if (this.$state.params.q) { - try { - this.filter = JSON.parse(this.$state.params.q); - } catch (e) { - console.error(e); - } - } else - this.filter = null; - } - - onStateChange() { - this.fetchFilter(); - this.doSearch(); - } doSearch() { let filter = this.filter; @@ -257,25 +210,81 @@ class AutoSearch { this.model.applyFilter( where ? {where} : null, hasParams ? userParams : null - ); + ).then(() => this.onModelFilter()); } else this.model.clear(); } } + onModelFilter() { + let params; + let opts; + let data = this.model.data; + let currentState = this.$state.current.name; + let stateParts = currentState.split('.'); + let baseState = stateParts[0]; + let subState = stateParts[1]; + + if (this.goState && data && data.length == 1) { + switch (subState) { + case 'index': + subState = 'card.summary'; + break; + case 'card': + subState += `.${stateParts[2]}`; + break; + } + + if (this.goStateParams) + params = this.goStateParams({$row: data[0]}); + } else { + if (subState == 'index') + opts = {location: 'replace'}; + subState = `index`; + params = {q: JSON.stringify(this.filter)}; + } + + this.$state.go(`${baseState}.${subState}`, params, opts); + } + exprBuilder(param, value) { return {[param]: value}; } } -AutoSearch.$inject = ['$state', '$transitions']; + +ngModule.vnComponent('vnSearchbar', { + controller: Searchbar, + template: require('./searchbar.html'), + bindings: { + searchState: '@?', + filter: ' - + model="model"> {{::zone.id}} @@ -61,7 +52,7 @@ diff --git a/modules/zone/front/index/index.js b/modules/zone/front/index/index.js index 4ee44ada0..aa69e30b6 100644 --- a/modules/zone/front/index/index.js +++ b/modules/zone/front/index/index.js @@ -1,55 +1,10 @@ import ngModule from '../module'; export default class Controller { - constructor($scope, $http, $state) { - this.$scope = $scope; + constructor($, $http, $state) { + this.$ = $; this.$http = $http; this.$state = $state; - this.filter = { - include: { - relation: 'agencyMode', - scope: {fields: ['name']} - } - }; - } - - exprBuilder(param, value) { - switch (param) { - case 'search': - return /^\d+$/.test(value) - ? {id: value} - : {name: {like: `%${value}%`}}; - case 'name': - return {[param]: {like: `%${value}%`}}; - case 'agencyModeFk': - return {[param]: value}; - } - } - - /** - * Clones a zone and all its properties - * @param {Object} event - Event object - * @param {Object} zone - Selected item - */ - clone(event, zone) { - this.stopEvent(event); - this.selectedZone = zone; - this.$scope.clone.show(); - } - - /** - * Clone response callback - * @param {String} response - Response string (['accept', 'cancel']) - */ - onCloneAccept(response) { - if (!(response == 'accept' && this.selectedZone)) return; - const query = `Zones/${this.selectedZone.id}/clone`; - this.$http.post(query).then(res => { - if (res && res.data) - this.$state.go('zone.card.basicData', {id: res.data.id}); - }); - - this.selectedZone = null; } /** @@ -60,13 +15,28 @@ export default class Controller { preview(event, zone) { this.stopEvent(event); this.selectedZone = zone; - this.$scope.summary.show(); + this.$.summary.show(); } /** - * Prevents normal event propagation + * Clones a zone and all its properties * @param {Object} event - Event object + * @param {Object} zone - Selected item */ + clone(event, zone) { + this.stopEvent(event); + this.selectedZone = zone; + this.$.clone.show(); + } + + onCloneAccept() { + return this.$http.post(`Zones/${this.selectedZone.id}/clone`) + .then(res => { + this.selectedZone = null; + this.$state.go('zone.card.basicData', {id: res.data.id}); + }); + } + stopEvent(event) { event.preventDefault(); event.stopImmediatePropagation(); diff --git a/modules/zone/front/main/index.html b/modules/zone/front/main/index.html index 230bf7f6d..d6e63a1a6 100644 --- a/modules/zone/front/main/index.html +++ b/modules/zone/front/main/index.html @@ -1,8 +1,18 @@ + + + info="Search zone by id or name" + model="model" + expr-builder="$ctrl.exprBuilder(param, value)" + go-state="zone.card.summary" + go-state-params="{id: $row.id}"> diff --git a/modules/zone/front/main/index.js b/modules/zone/front/main/index.js index 3e8890310..3be60c5a1 100644 --- a/modules/zone/front/main/index.js +++ b/modules/zone/front/main/index.js @@ -1,7 +1,30 @@ import ngModule from '../module'; import ModuleMain from 'salix/components/module-main'; -export default class Zone extends ModuleMain {} +export default class Zone extends ModuleMain { + constructor($element, $) { + super($element, $); + this.filter = { + include: { + relation: 'agencyMode', + scope: {fields: ['name']} + } + }; + } + + exprBuilder(param, value) { + switch (param) { + case 'search': + return /^\d+$/.test(value) + ? {id: value} + : {name: {like: `%${value}%`}}; + case 'name': + return {[param]: {like: `%${value}%`}}; + case 'agencyModeFk': + return {[param]: value}; + } + } +} ngModule.vnComponent('vnZone', { controller: Zone,