From e0ba7f3e846c5566df9c6542e6eb47d1365614fe Mon Sep 17 00:00:00 2001 From: volterra79 Date: Thu, 8 Feb 2024 16:03:15 +0100 Subject: [PATCH 01/17] add cache --- src/components/ChangeMapMenu.vue | 37 +++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/src/components/ChangeMapMenu.vue b/src/components/ChangeMapMenu.vue index 5439ea8a3..0e4520dae 100644 --- a/src/components/ChangeMapMenu.vue +++ b/src/components/ChangeMapMenu.vue @@ -171,13 +171,17 @@ export default { this.loading = true; this.parent = item; try { - this.items = await XHR.get({ - url: encodeURI(`/${ApplicationService.getApplicationUser().i18n}${API_BASE_URLS.ABOUT.group}${item.id}/`) - }); + if (undefined === this._cache.macrogroups[item.id]) { + this._cache.macrogroups[item.id] = await XHR.get({ + url: encodeURI(`/${ApplicationService.getApplicationUser().i18n}${API_BASE_URLS.ABOUT.group}${item.id}/`) + }); + } + this.items = this._cache.macrogroups[item.id]; this.current = 'groups'; } catch(err) { this.items = []; } + this.steps.push(this.parent); this.loading = false; }, @@ -191,10 +195,13 @@ export default { this.current = 'projects'; } else { try { - this.items = await XHR.get({ - url: encodeURI(`/${ApplicationService.getApplicationUser().i18n}${API_BASE_URLS.ABOUT.projects.replace('__G3W_GROUP_ID__', item.id)}`) - }); - this.items.forEach(item => this.setItemImageSrc({ item, type: 'project' })); + if (undefined === this._cache.groups[item.id]) { + this._cache.groups[item.id] = await XHR.get({ + url: encodeURI(`/${ApplicationService.getApplicationUser().i18n}${API_BASE_URLS.ABOUT.projects.replace('__G3W_GROUP_ID__', item.id)}`) + }); + this._cache.groups[item.id].forEach(item => this.setItemImageSrc({ item, type: 'project' })); + } + this.items = this._cache.groups[item.id]; this.current = 'projects'; } catch(err) { this.items = []; @@ -272,7 +279,18 @@ export default { }, - created() { + async created() { + /** + * @since 3.10.0 + * Store configuration of macrogroups and groups + * avoid wasting time-server request + * @type {{macrogroups: {}, groups: {}}} + * @private + */ + this._cache = { + macrogroups : {}, + groups : {} + } // at start time set item projects this.items = ProjectsRegistry.getListableProjects(); @@ -307,6 +325,9 @@ export default { } }, + beforeDestroy() { + this._cache = null; + } }; From 962f82f61dc00c3e454ca8aa3d4625dbb47b7528 Mon Sep 17 00:00:00 2001 From: volterra79 Date: Mon, 12 Feb 2024 17:07:26 +0100 Subject: [PATCH 02/17] Temp dev --- src/components/ChangeMapMenu.vue | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/components/ChangeMapMenu.vue b/src/components/ChangeMapMenu.vue index 0e4520dae..12cc4ada9 100644 --- a/src/components/ChangeMapMenu.vue +++ b/src/components/ChangeMapMenu.vue @@ -141,13 +141,18 @@ export default { else if (item.header_logo_img) item.header_logo_img = g3w_logo; }, - back() { + async back() { if (this.steps.length > 1) { const item = this.steps[0]; this.steps = []; this.showGroups(item); } else { - this.showRoot(); + if (this.init && undefined === this.parent.macro_id) { + await this.showGroups(this.macrogroups.find(mg => 1 === mg.id)); + this.init = false; + } else { + this.showRoot(); + } } }, @@ -280,6 +285,7 @@ export default { }, async created() { + this.init = true; /** * @since 3.10.0 * Store configuration of macrogroups and groups From e5b89e72fa6b9337a14af6e03fccd729beadf24f Mon Sep 17 00:00:00 2001 From: volterra79 Date: Mon, 19 Feb 2024 15:15:04 +0100 Subject: [PATCH 03/17] Check if current project (initiial) belong to a macrogroup/s checking initCongif.group.macrogroup_id attribute --- src/components/ChangeMapMenu.vue | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/components/ChangeMapMenu.vue b/src/components/ChangeMapMenu.vue index 12cc4ada9..1bcf26697 100644 --- a/src/components/ChangeMapMenu.vue +++ b/src/components/ChangeMapMenu.vue @@ -147,8 +147,13 @@ export default { this.steps = []; this.showGroups(item); } else { - if (this.init && undefined === this.parent.macro_id) { - await this.showGroups(this.macrogroups.find(mg => 1 === mg.id)); + if ( + this.init + && Array.isArray(this.parent.macrogroup_id) + && this.parent.macrogroup_id.length > 0 + ) { + //get first + await this.showGroups(this.macrogroups.find(mg => this.parent.macrogroup_id[0] === mg.id)); this.init = false; } else { this.showRoot(); From 798d11b16ab413ccd4fca2f9d0d505d3d82a3e70 Mon Sep 17 00:00:00 2001 From: Raruto Date: Tue, 20 Feb 2024 08:22:12 +0100 Subject: [PATCH 04/17] avoid nested conditions --- src/components/ChangeMapMenu.vue | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/components/ChangeMapMenu.vue b/src/components/ChangeMapMenu.vue index 1bcf26697..07bbdb479 100644 --- a/src/components/ChangeMapMenu.vue +++ b/src/components/ChangeMapMenu.vue @@ -142,22 +142,19 @@ export default { }, async back() { - if (this.steps.length > 1) { + const has_steps = this.steps.length > 1; + const macro = this.parent.macrogroup_id; + if (has_steps) { const item = this.steps[0]; this.steps = []; this.showGroups(item); + } + if (!has_steps && this.init && Array.isArray(macro) && macro.length > 0) { + // get first + await this.showGroups(this.macrogroups.find(mg => macro[0] === mg.id)); + this.init = false; } else { - if ( - this.init - && Array.isArray(this.parent.macrogroup_id) - && this.parent.macrogroup_id.length > 0 - ) { - //get first - await this.showGroups(this.macrogroups.find(mg => this.parent.macrogroup_id[0] === mg.id)); - this.init = false; - } else { - this.showRoot(); - } + this.showRoot(); } }, From 48ef8bfe26096cf0c6205a8725a82e317260e644 Mon Sep 17 00:00:00 2001 From: Raruto Date: Tue, 20 Feb 2024 09:39:47 +0100 Subject: [PATCH 05/17] replace `this._cache` with internal functions `get_macro(id)` + `get_group(id)` --- src/components/ChangeMapMenu.vue | 57 +++++++++++++------------------- 1 file changed, 23 insertions(+), 34 deletions(-) diff --git a/src/components/ChangeMapMenu.vue b/src/components/ChangeMapMenu.vue index 07bbdb479..62ed92b74 100644 --- a/src/components/ChangeMapMenu.vue +++ b/src/components/ChangeMapMenu.vue @@ -73,6 +73,20 @@ import { API_BASE_URLS, LOGO_GIS3W } from "app/constant"; const Projections = require('g3w-ol/projection/projections'); const { XHR } = require('utils'); +/** Cached HTTP GET request */ +async function get_macro(id) { + get_macro[id] = get_macro[id] || await XHR.get({ url: encodeURI(`/${ApplicationService.getApplicationUser().i18n}${API_BASE_URLS.ABOUT.group}${id}/`) }); + return get_macro[id]; +} + +/** Cached HTTP GET request */ +async function get_group(id) { + const g = get_group[id] || await XHR.get({ url: encodeURI(`/${ApplicationService.getApplicationUser().i18n}${API_BASE_URLS.ABOUT.projects.replace('__G3W_GROUP_ID__', id)}`) }); + if (!get_group[id]) g.forEach(cb); + else get_group[id] = g; + return g; +} + export default { /** @since 3.8.6 */ @@ -142,20 +156,20 @@ export default { }, async back() { - const has_steps = this.steps.length > 1; - const macro = this.parent.macrogroup_id; - if (has_steps) { + if (this.steps.length > 1) { const item = this.steps[0]; this.steps = []; this.showGroups(item); + return; } - if (!has_steps && this.init && Array.isArray(macro) && macro.length > 0) { - // get first + const macro = this.parent && this.parent.macrogroup_id; + // go back to first MacroGroup when Group belongs to multiple MacroGroups + if (this.init && Array.isArray(macro) && macro.length > 0 ) { await this.showGroups(this.macrogroups.find(mg => macro[0] === mg.id)); this.init = false; - } else { - this.showRoot(); + return; } + this.showRoot(); }, showRoot() { @@ -178,12 +192,7 @@ export default { this.loading = true; this.parent = item; try { - if (undefined === this._cache.macrogroups[item.id]) { - this._cache.macrogroups[item.id] = await XHR.get({ - url: encodeURI(`/${ApplicationService.getApplicationUser().i18n}${API_BASE_URLS.ABOUT.group}${item.id}/`) - }); - } - this.items = this._cache.macrogroups[item.id]; + this.items = await get_macro(item.id); this.current = 'groups'; } catch(err) { this.items = []; @@ -202,13 +211,7 @@ export default { this.current = 'projects'; } else { try { - if (undefined === this._cache.groups[item.id]) { - this._cache.groups[item.id] = await XHR.get({ - url: encodeURI(`/${ApplicationService.getApplicationUser().i18n}${API_BASE_URLS.ABOUT.projects.replace('__G3W_GROUP_ID__', item.id)}`) - }); - this._cache.groups[item.id].forEach(item => this.setItemImageSrc({ item, type: 'project' })); - } - this.items = this._cache.groups[item.id]; + this.items = get_group(item.id, item => this.setItemImageSrc({ item, type: 'project' })); this.current = 'projects'; } catch(err) { this.items = []; @@ -288,17 +291,6 @@ export default { async created() { this.init = true; - /** - * @since 3.10.0 - * Store configuration of macrogroups and groups - * avoid wasting time-server request - * @type {{macrogroups: {}, groups: {}}} - * @private - */ - this._cache = { - macrogroups : {}, - groups : {} - } // at start time set item projects this.items = ProjectsRegistry.getListableProjects(); @@ -333,9 +325,6 @@ export default { } }, - beforeDestroy() { - this._cache = null; - } }; From c4d73ffc452befe2a3d72fcc777f4019706c60bc Mon Sep 17 00:00:00 2001 From: Raruto Date: Tue, 20 Feb 2024 09:58:54 +0100 Subject: [PATCH 06/17] missing `cb` --- src/components/ChangeMapMenu.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/ChangeMapMenu.vue b/src/components/ChangeMapMenu.vue index 62ed92b74..aeab7b1b2 100644 --- a/src/components/ChangeMapMenu.vue +++ b/src/components/ChangeMapMenu.vue @@ -80,7 +80,7 @@ async function get_macro(id) { } /** Cached HTTP GET request */ -async function get_group(id) { +async function get_group(id, cb) { const g = get_group[id] || await XHR.get({ url: encodeURI(`/${ApplicationService.getApplicationUser().i18n}${API_BASE_URLS.ABOUT.projects.replace('__G3W_GROUP_ID__', id)}`) }); if (!get_group[id]) g.forEach(cb); else get_group[id] = g; From 41172ffd5c097a6c2df3617fc14c06b664f3f8cf Mon Sep 17 00:00:00 2001 From: volterra79 Date: Tue, 20 Feb 2024 10:18:07 +0100 Subject: [PATCH 07/17] Show macrogroups list if a project initial group belong to more than one macro groups --- src/components/ChangeMapMenu.vue | 77 ++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 24 deletions(-) diff --git a/src/components/ChangeMapMenu.vue b/src/components/ChangeMapMenu.vue index 62ed92b74..1b5f4975b 100644 --- a/src/components/ChangeMapMenu.vue +++ b/src/components/ChangeMapMenu.vue @@ -6,7 +6,13 @@