From 1980dd0615ef1df7584cf33446b3a60f3952158c Mon Sep 17 00:00:00 2001 From: Simon Leech Date: Thu, 12 Oct 2023 15:33:19 +0100 Subject: [PATCH 01/13] Update queryParams.mjs --- lib/utils/queryParams.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils/queryParams.mjs b/lib/utils/queryParams.mjs index a4b4c06e1..cf0f156f5 100644 --- a/lib/utils/queryParams.mjs +++ b/lib/utils/queryParams.mjs @@ -7,7 +7,7 @@ export default _this => { _this.queryparams.layer = _this.queryparams.layer || _this.viewport // Assign table name from layer. - _this.queryparams.table &&= _this.location?.layer?.tableCurrent() + _this.queryparams.table &&= _this.location?.layer?.tableCurrent() || _this.layer?.tableCurrent() // Assign fieldValues from the location to queryparams. if (Array.isArray(_this.queryparams.fieldValues) && _this.location) { From 1495967c32cd37a4832a36802bf2186cdd44728e Mon Sep 17 00:00:00 2001 From: Simon Leech Date: Thu, 12 Oct 2023 16:07:59 +0100 Subject: [PATCH 02/13] Check on table===true instead of presence of table flag --- lib/utils/queryParams.mjs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/utils/queryParams.mjs b/lib/utils/queryParams.mjs index cf0f156f5..4dc9ab0a3 100644 --- a/lib/utils/queryParams.mjs +++ b/lib/utils/queryParams.mjs @@ -6,8 +6,9 @@ export default _this => { // The layer queryparam must be true to support viewport params. _this.queryparams.layer = _this.queryparams.layer || _this.viewport - // Assign table name from layer. - _this.queryparams.table &&= _this.location?.layer?.tableCurrent() || _this.layer?.tableCurrent() + // Assign table name from layer, if table is set to true + // Table can be set to a string, which is valid + if (_this.queryparams.table === true) _this.queryparams.table = _this.location?.layer?.tableCurrent() || _this.layer?.tableCurrent() // Assign fieldValues from the location to queryparams. if (Array.isArray(_this.queryparams.fieldValues) && _this.location) { From 795e483fc63d64a10489f2ede60035b313da2468 Mon Sep 17 00:00:00 2001 From: Simon Leech Date: Thu, 12 Oct 2023 16:56:28 +0100 Subject: [PATCH 03/13] Reduce cognitive complexity --- lib/utils/queryParams.mjs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/utils/queryParams.mjs b/lib/utils/queryParams.mjs index 4dc9ab0a3..514cbf939 100644 --- a/lib/utils/queryParams.mjs +++ b/lib/utils/queryParams.mjs @@ -8,8 +8,9 @@ export default _this => { // Assign table name from layer, if table is set to true // Table can be set to a string, which is valid - if (_this.queryparams.table === true) _this.queryparams.table = _this.location?.layer?.tableCurrent() || _this.layer?.tableCurrent() - + let tableCurrent = _this.location?.layer?.tableCurrent() || _this.layer?.tableCurrent() + _this.queryparams.table &&= _this.queryparams.table === true ? tableCurrent : _this.queryparams.table + // Assign fieldValues from the location to queryparams. if (Array.isArray(_this.queryparams.fieldValues) && _this.location) { From 5fca64e5f8aaf61cc2bb1dc8aad0d2a3ea603c74 Mon Sep 17 00:00:00 2001 From: Simon Leech Date: Thu, 12 Oct 2023 17:01:10 +0100 Subject: [PATCH 04/13] Update queryParams.mjs --- lib/utils/queryParams.mjs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/utils/queryParams.mjs b/lib/utils/queryParams.mjs index 514cbf939..47bd8e7ee 100644 --- a/lib/utils/queryParams.mjs +++ b/lib/utils/queryParams.mjs @@ -6,10 +6,13 @@ export default _this => { // The layer queryparam must be true to support viewport params. _this.queryparams.layer = _this.queryparams.layer || _this.viewport - // Assign table name from layer, if table is set to true - // Table can be set to a string, which is valid + // Assign table name from layer let tableCurrent = _this.location?.layer?.tableCurrent() || _this.layer?.tableCurrent() - _this.queryparams.table &&= _this.queryparams.table === true ? tableCurrent : _this.queryparams.table + + // If table is set to true, use the current table name from the layer, otherwise use the table name from the queryparams. + let table = _this.queryparams.table === true ? tableCurrent : _this.queryparams.table; + + _this.queryparams.table &&= table; // Assign fieldValues from the location to queryparams. if (Array.isArray(_this.queryparams.fieldValues) && _this.location) { From de7f4a0bcbcfe145ad81015a4c04d03799a0edc3 Mon Sep 17 00:00:00 2001 From: Simon Leech Date: Thu, 12 Oct 2023 17:03:14 +0100 Subject: [PATCH 05/13] Update queryParams.mjs --- lib/utils/queryParams.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/utils/queryParams.mjs b/lib/utils/queryParams.mjs index 47bd8e7ee..7f2b13567 100644 --- a/lib/utils/queryParams.mjs +++ b/lib/utils/queryParams.mjs @@ -1,7 +1,7 @@ export default _this => { // Assign empty object if not defined. - _this.queryparams = _this.queryparams || {} + _this.queryparams ??= {} // The layer queryparam must be true to support viewport params. _this.queryparams.layer = _this.queryparams.layer || _this.viewport @@ -11,7 +11,7 @@ export default _this => { // If table is set to true, use the current table name from the layer, otherwise use the table name from the queryparams. let table = _this.queryparams.table === true ? tableCurrent : _this.queryparams.table; - + _this.queryparams.table &&= table; // Assign fieldValues from the location to queryparams. From de7103479d146814674271a9386c36c518487c04 Mon Sep 17 00:00:00 2001 From: Simon Leech Date: Fri, 13 Oct 2023 08:02:19 +0100 Subject: [PATCH 06/13] Return if queryparams not an object; tidy comments --- lib/utils/queryParams.mjs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/utils/queryParams.mjs b/lib/utils/queryParams.mjs index 7f2b13567..deaa28197 100644 --- a/lib/utils/queryParams.mjs +++ b/lib/utils/queryParams.mjs @@ -1,7 +1,10 @@ export default _this => { - // Assign empty object if not defined. - _this.queryparams ??= {} + // If queryparams is not an object, return. + if (typeof _this.queryparams !== 'object') { + console.warn('queryparams must be an object') + return; + }; // The layer queryparam must be true to support viewport params. _this.queryparams.layer = _this.queryparams.layer || _this.viewport @@ -13,7 +16,7 @@ export default _this => { let table = _this.queryparams.table === true ? tableCurrent : _this.queryparams.table; _this.queryparams.table &&= table; - + // Assign fieldValues from the location to queryparams. if (Array.isArray(_this.queryparams.fieldValues) && _this.location) { @@ -49,20 +52,20 @@ export default _this => { // Locale param is only required for layer lookups. locale: _this.queryparams.layer && _this.layer.mapview.locale.key, - // Locale param is only required for layer lookups. + // Layer param is only required for layer lookups. layer: _this.queryparams.layer && _this.layer.key, // ID will be taken if a location object is provided with the params. id: _this.queryparams?.id && _this.location?.id, - // lat lng must be explicit or the the center flag param must be set. + // lat lng must be explicit or the center flag param must be set. lat: center && center[1], lng: center && center[0], - // z will generated if the z flag is set in the params. + // z will be generated if the z flag is set in the params. z: _this.queryparams?.z && _this.layer.mapview.Map.getView().getZoom(), - // Viewport will onlcy be generated if the viewport flag is set on the params. + // Viewport will only be generated if the viewport flag is set on the params. viewport: bounds && [bounds.west, bounds.south, bounds.east, bounds.north, _this.layer.mapview.srid], // The fieldValues array entries should not be part of the url params. From 7ffe4d170a2969332cece413740f42366f29e5aa Mon Sep 17 00:00:00 2001 From: Simon Leech Date: Fri, 13 Oct 2023 08:07:22 +0100 Subject: [PATCH 07/13] Update queryParams.mjs --- lib/utils/queryParams.mjs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/utils/queryParams.mjs b/lib/utils/queryParams.mjs index deaa28197..e3debc5b1 100644 --- a/lib/utils/queryParams.mjs +++ b/lib/utils/queryParams.mjs @@ -19,16 +19,19 @@ export default _this => { // Assign fieldValues from the location to queryparams. if (Array.isArray(_this.queryparams.fieldValues) && _this.location) { + assignFieldValues(_this.queryparams.fieldValues, _this.location.infoj, _this.queryparams); + } + // Function to assign field values from infoj to queryparams. + function assignFieldValues(fieldValues, infoj, queryparams) { // Iterate through the fieldValues array. - _this.queryparams.fieldValues.forEach(field => { - + fieldValues.forEach(field => { // Find entry in location infoj matching the field. - let entry = _this.location.infoj.find(entry => entry.field === field) - + const entry = infoj.find(entry => entry.field === field); // Assign entry value as field value in queryparams. - _this.queryparams[field] = entry.value - }) + queryparams[field] = entry.value; + + }); } // Get bounds from mapview. From 966c95c47768e95aaf9468606433b1f9be8b7ea8 Mon Sep 17 00:00:00 2001 From: Simon Leech Date: Fri, 13 Oct 2023 08:09:38 +0100 Subject: [PATCH 08/13] Update queryParams.mjs --- lib/utils/queryParams.mjs | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/utils/queryParams.mjs b/lib/utils/queryParams.mjs index e3debc5b1..4965efd48 100644 --- a/lib/utils/queryParams.mjs +++ b/lib/utils/queryParams.mjs @@ -17,21 +17,19 @@ export default _this => { _this.queryparams.table &&= table; - // Assign fieldValues from the location to queryparams. - if (Array.isArray(_this.queryparams.fieldValues) && _this.location) { - assignFieldValues(_this.queryparams.fieldValues, _this.location.infoj, _this.queryparams); - } - - // Function to assign field values from infoj to queryparams. - function assignFieldValues(fieldValues, infoj, queryparams) { - // Iterate through the fieldValues array. - fieldValues.forEach(field => { - // Find entry in location infoj matching the field. - const entry = infoj.find(entry => entry.field === field); - // Assign entry value as field value in queryparams. - queryparams[field] = entry.value; - - }); + // Only if a location is provided, can we use the location infoj to assign field values to the queryparams. + if (_this.location) { + // Assign fieldValues from the location to queryparams. + if (Array.isArray(_this.queryparams.fieldValues)) { + // Iterate through the fieldValues array. + fieldValues.forEach(field => { + // Find entry in location infoj matching the field. + const entry = infoj.find(entry => entry.field === field); + // Assign entry value as field value in queryparams. + queryparams[field] = entry.value; + + }) + } } // Get bounds from mapview. From 1337af2e0998d6b45f9b32419bffe5adb64889b8 Mon Sep 17 00:00:00 2001 From: Simon Leech Date: Fri, 13 Oct 2023 08:19:55 +0100 Subject: [PATCH 09/13] Refactoring to avoid complexity --- lib/utils/queryParams.mjs | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/utils/queryParams.mjs b/lib/utils/queryParams.mjs index 4965efd48..c9e09b9e9 100644 --- a/lib/utils/queryParams.mjs +++ b/lib/utils/queryParams.mjs @@ -42,22 +42,42 @@ export default _this => { `EPSG:4326`) - return Object.assign({}, _this.queryparams, { + // Set the queryparams + + // If layer is true, the locale and layer params will be taken from the layer object. + if (this.queryparams.layer) { + locale = this.layer.mapview.locale.key, + layer = this.layer.key + } + + // If filter is true, the filter param will be taken from the layer object. + if (this.queryparams.filter) { + filter = this.layer.filter?.current + } + + // If id is true, the id param will be taken from the location object. + if (this.queryparams.id) { + id = this.location?.id + } + + return { + // Spread the queryparams object. + ..._this.queryparams, // Queries will fail if the template can not be accessed in workspace. template: encodeURIComponent(_this.query), // Layer filter can only be applied if the layer is provided as reference to a layer object in the layers list. - filter: _this.queryparams.filter && _this.layer.filter?.current, + filter: filter, // Locale param is only required for layer lookups. - locale: _this.queryparams.layer && _this.layer.mapview.locale.key, + locale: locale, // Layer param is only required for layer lookups. - layer: _this.queryparams.layer && _this.layer.key, + layer: layer, // ID will be taken if a location object is provided with the params. - id: _this.queryparams?.id && _this.location?.id, + id: id, // lat lng must be explicit or the center flag param must be set. lat: center && center[1], @@ -72,5 +92,5 @@ export default _this => { // The fieldValues array entries should not be part of the url params. fieldValues: undefined - }) + } } \ No newline at end of file From 420360e6fba27b4663b9183e2182770e1753fc3c Mon Sep 17 00:00:00 2001 From: Simon Leech Date: Fri, 13 Oct 2023 08:23:18 +0100 Subject: [PATCH 10/13] Update queryParams.mjs --- lib/utils/queryParams.mjs | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/lib/utils/queryParams.mjs b/lib/utils/queryParams.mjs index c9e09b9e9..b74cb9767 100644 --- a/lib/utils/queryParams.mjs +++ b/lib/utils/queryParams.mjs @@ -41,25 +41,6 @@ export default _this => { `EPSG:${_this.layer.mapview.srid}`, `EPSG:4326`) - - // Set the queryparams - - // If layer is true, the locale and layer params will be taken from the layer object. - if (this.queryparams.layer) { - locale = this.layer.mapview.locale.key, - layer = this.layer.key - } - - // If filter is true, the filter param will be taken from the layer object. - if (this.queryparams.filter) { - filter = this.layer.filter?.current - } - - // If id is true, the id param will be taken from the location object. - if (this.queryparams.id) { - id = this.location?.id - } - return { // Spread the queryparams object. ..._this.queryparams, @@ -68,16 +49,16 @@ export default _this => { template: encodeURIComponent(_this.query), // Layer filter can only be applied if the layer is provided as reference to a layer object in the layers list. - filter: filter, + filter: this.queryparams.filter ? this.layer.filter?.current : undefined, // Locale param is only required for layer lookups. - locale: locale, + locale: this.queryparams.layer ? this.layer.mapview.locale.key : undefined, // Layer param is only required for layer lookups. - layer: layer, + layer: this.queryparams.layer ? this.layer.key : undefined, // ID will be taken if a location object is provided with the params. - id: id, + id: this.queryparams.id ? this.location?.id : undefined, // lat lng must be explicit or the center flag param must be set. lat: center && center[1], From 8d8a40d05d09992b603ac59b4070099d9b3eb9e7 Mon Sep 17 00:00:00 2001 From: Simon Leech Date: Fri, 13 Oct 2023 08:26:06 +0100 Subject: [PATCH 11/13] Update queryParams.mjs --- lib/utils/queryParams.mjs | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/lib/utils/queryParams.mjs b/lib/utils/queryParams.mjs index b74cb9767..9f8f6b4ca 100644 --- a/lib/utils/queryParams.mjs +++ b/lib/utils/queryParams.mjs @@ -17,21 +17,6 @@ export default _this => { _this.queryparams.table &&= table; - // Only if a location is provided, can we use the location infoj to assign field values to the queryparams. - if (_this.location) { - // Assign fieldValues from the location to queryparams. - if (Array.isArray(_this.queryparams.fieldValues)) { - // Iterate through the fieldValues array. - fieldValues.forEach(field => { - // Find entry in location infoj matching the field. - const entry = infoj.find(entry => entry.field === field); - // Assign entry value as field value in queryparams. - queryparams[field] = entry.value; - - }) - } - } - // Get bounds from mapview. const bounds = _this.viewport && _this.layer.mapview.getBounds(); From db5a8d45a7ad0544e4a175f2c0b31d0e128946bb Mon Sep 17 00:00:00 2001 From: Simon Leech Date: Fri, 13 Oct 2023 08:31:43 +0100 Subject: [PATCH 12/13] Fix incorrect reference to this instead of _this --- lib/utils/queryParams.mjs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/utils/queryParams.mjs b/lib/utils/queryParams.mjs index 9f8f6b4ca..2608621ef 100644 --- a/lib/utils/queryParams.mjs +++ b/lib/utils/queryParams.mjs @@ -34,16 +34,16 @@ export default _this => { template: encodeURIComponent(_this.query), // Layer filter can only be applied if the layer is provided as reference to a layer object in the layers list. - filter: this.queryparams.filter ? this.layer.filter?.current : undefined, + filter: _this.queryparams.filter ? _this.layer.filter?.current : undefined, // Locale param is only required for layer lookups. - locale: this.queryparams.layer ? this.layer.mapview.locale.key : undefined, + locale: _this.queryparams.layer ? _this.layer.mapview.locale.key : undefined, // Layer param is only required for layer lookups. - layer: this.queryparams.layer ? this.layer.key : undefined, + layer: _this.queryparams.layer ? _this.layer.key : undefined, // ID will be taken if a location object is provided with the params. - id: this.queryparams.id ? this.location?.id : undefined, + id: _this.queryparams.id ? _this.location?.id : undefined, // lat lng must be explicit or the center flag param must be set. lat: center && center[1], From 502a44335d10c7042b390616524d7fc828bdff01 Mon Sep 17 00:00:00 2001 From: dbauszus-glx Date: Fri, 13 Oct 2023 09:46:05 +0100 Subject: [PATCH 13/13] use const instead of let --- lib/utils/queryParams.mjs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/utils/queryParams.mjs b/lib/utils/queryParams.mjs index 2608621ef..3196fd176 100644 --- a/lib/utils/queryParams.mjs +++ b/lib/utils/queryParams.mjs @@ -10,10 +10,10 @@ export default _this => { _this.queryparams.layer = _this.queryparams.layer || _this.viewport // Assign table name from layer - let tableCurrent = _this.location?.layer?.tableCurrent() || _this.layer?.tableCurrent() + const tableCurrent = _this.location?.layer?.tableCurrent() || _this.layer?.tableCurrent() // If table is set to true, use the current table name from the layer, otherwise use the table name from the queryparams. - let table = _this.queryparams.table === true ? tableCurrent : _this.queryparams.table; + const table = _this.queryparams.table === true ? tableCurrent : _this.queryparams.table; _this.queryparams.table &&= table; @@ -27,6 +27,7 @@ export default _this => { `EPSG:4326`) return { + // Spread the queryparams object. ..._this.queryparams, @@ -57,6 +58,5 @@ export default _this => { // The fieldValues array entries should not be part of the url params. fieldValues: undefined - } } \ No newline at end of file