From f498f0723173dab576b60b6f17fc8fc37cec1b6f Mon Sep 17 00:00:00 2001 From: Simon Leech <56163132+simon-leech@users.noreply.github.com> Date: Thu, 17 Oct 2024 13:54:33 +0100 Subject: [PATCH 1/6] Update decorate.mjs --- lib/location/decorate.mjs | 54 ++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/lib/location/decorate.mjs b/lib/location/decorate.mjs index cabbdc739..46a27679e 100644 --- a/lib/location/decorate.mjs +++ b/lib/location/decorate.mjs @@ -107,19 +107,19 @@ async function update() { } this.infoj - .filter(entry => entry.jsonb_key) - .filter(entry => entry.jsonb_field) - .filter(entry => typeof entry.newValue !== 'undefined') - .forEach(entry => { + .filter(entry => entry.jsonb_key) + .filter(entry => entry.jsonb_field) + .filter(entry => typeof entry.newValue !== 'undefined') + .forEach(entry => { - entry.newValue = { - 'jsonb': { - [entry.jsonb_field]: { - [entry.jsonb_key]: entry.newValue + entry.newValue = { + 'jsonb': { + [entry.jsonb_field]: { + [entry.jsonb_key]: entry.newValue + } } } - } - }) + }) this.infoj .filter(entry => entry.json_field) @@ -163,22 +163,28 @@ async function update() { return; } - // Update entry.values with newValues. - // Return dependents from updated entries. - const dependents = this.infoj - .filter(entry => typeof entry.newValue !== 'undefined') - .map(entry => { +// Update entry.values with newValues. +// Return dependents and layer_dependents from updated entries. +const result = this.infoj + .filter(entry => typeof entry.newValue !== 'undefined') + .map(entry => { + entry.value = entry.newValue; - entry.value = entry.newValue; - delete entry.newValue; + return { + 'dependents': entry.dependents, + 'layer_dependents': entry.layer_dependents + }; + }) + .filter(entry => entry.dependents || entry.layer_dependents); - return entry.dependents - }) - .flat() - .filter(dependents => dependents !== undefined) +const dependents = result.map(entry => entry.dependents).flat().filter(Boolean); +const layer_dependents = result.map(entry => entry.layer_dependents).flat().filter(Boolean); + +// Sync dependent fields +if (dependents.length) await this.syncFields([...new Set(dependents)]); - // sync dependent fields - if (dependents.length) await this.syncFields([...new Set(dependents)]) +// Reload dependent layers +if (layer_dependents.length) layer_dependents.forEach(layer => this.layer.mapview.layers[layer].reload()); // Reload layer. this.layer.reload() @@ -236,7 +242,7 @@ function flyTo(maxZoom) { async function trash() { - const confirm = await mapp.ui.elements.confirm({text: mapp.dictionary.confirm_delete}) + const confirm = await mapp.ui.elements.confirm({ text: mapp.dictionary.confirm_delete }) if (!confirm) return; From 42c8059294e0b86ce040e1c24a38f1c846daf84c Mon Sep 17 00:00:00 2001 From: dbauszus-glx Date: Thu, 17 Oct 2024 15:59:26 +0100 Subject: [PATCH 2/6] update dependents[layer] --- lib/location/decorate.mjs | 40 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/lib/location/decorate.mjs b/lib/location/decorate.mjs index 46a27679e..4253fda2c 100644 --- a/lib/location/decorate.mjs +++ b/lib/location/decorate.mjs @@ -163,33 +163,29 @@ async function update() { return; } -// Update entry.values with newValues. -// Return dependents and layer_dependents from updated entries. -const result = this.infoj - .filter(entry => typeof entry.newValue !== 'undefined') - .map(entry => { - entry.value = entry.newValue; - - return { - 'dependents': entry.dependents, - 'layer_dependents': entry.layer_dependents - }; - }) - .filter(entry => entry.dependents || entry.layer_dependents); -const dependents = result.map(entry => entry.dependents).flat().filter(Boolean); -const layer_dependents = result.map(entry => entry.layer_dependents).flat().filter(Boolean); + // Find newValue entries for update. + const dependents = this.infoj + .filter(entry => typeof entry.newValue !== 'undefined') + .map(entry => { + + // Update entry.values with newValues. + entry.value = entry.newValue; -// Sync dependent fields -if (dependents.length) await this.syncFields([...new Set(dependents)]); + if (Array.isArray(entry.dependents_layer)) { -// Reload dependent layers -if (layer_dependents.length) layer_dependents.forEach(layer => this.layer.mapview.layers[layer].reload()); + entry.dependents_layer.forEach(layer => { + + // Iterate through the dependents_layer array and reload matching layers in mapview. + entry.location.layer.mapview.layers[layer].reload() + }) + } - // Reload layer. - this.layer.reload() + return entry.dependents + }).flat() - this.updateCallbacks?.forEach(fn => typeof fn === 'function' && fn(this)) + // Sync dependent fields + if (dependents.length) await this.syncFields([...new Set(dependents)]); } async function syncFields(fields) { From 0fd306d04dc10698d818c314596986a69deab454 Mon Sep 17 00:00:00 2001 From: dbauszus-glx Date: Thu, 17 Oct 2024 16:16:15 +0100 Subject: [PATCH 3/6] update docs --- lib/location/decorate.mjs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/location/decorate.mjs b/lib/location/decorate.mjs index 4253fda2c..63318d70c 100644 --- a/lib/location/decorate.mjs +++ b/lib/location/decorate.mjs @@ -1,6 +1,8 @@ /** ### /location/decorate +@requires /utils/xhr + @module /location/decorate */ @@ -95,8 +97,20 @@ function remove() { } /** -Update a location from host @function update + +@description +The update method is bound to the location (this) in the decorator method. + +The update method will abort if some of the location.infoj entries is invalid. + +JSON newValues are created for any json or jsonb type entries in the location.infoj array. + +An xhr POST request with all newValues is passed to location_update query template. + +infoj entry values are set to the newValues after a successful update. + +Dependent fields for updated entries are filtered out and passed to the syncFields method. The dependents_layer for updated fields are reloaded in the process. */ async function update() { @@ -106,6 +120,7 @@ async function update() { return new Error('Unable to update.'); } + // Create newValue for jsonb entries. this.infoj .filter(entry => entry.jsonb_key) .filter(entry => entry.jsonb_field) @@ -121,6 +136,7 @@ async function update() { } }) + // Create newValue on json_field entries. this.infoj .filter(entry => entry.json_field) .filter(entry => entry.json_key) @@ -136,11 +152,12 @@ async function update() { delete entry.newValue }) - // Map new values into newValues object. + // Map newValues from infoj entry objects. const newValues = Object.fromEntries(this.infoj .filter(entry => typeof entry.newValue !== 'undefined') .map(entry => [entry.field, entry.newValue])) + // Shortcircuit if no update is required. if (!Object.keys(newValues).length) return; const location_update = await mapp.utils.xhr({ @@ -163,7 +180,6 @@ async function update() { return; } - // Find newValue entries for update. const dependents = this.infoj .filter(entry => typeof entry.newValue !== 'undefined') From 4065735610ff5cc01fbb08e7d469d69feb4b5ef4 Mon Sep 17 00:00:00 2001 From: Simon Leech <56163132+simon-leech@users.noreply.github.com> Date: Fri, 18 Oct 2024 09:56:10 +0100 Subject: [PATCH 4/6] Update decorate.mjs --- lib/location/decorate.mjs | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/location/decorate.mjs b/lib/location/decorate.mjs index 63318d70c..94a26bb12 100644 --- a/lib/location/decorate.mjs +++ b/lib/location/decorate.mjs @@ -110,7 +110,7 @@ An xhr POST request with all newValues is passed to location_update query templa infoj entry values are set to the newValues after a successful update. -Dependent fields for updated entries are filtered out and passed to the syncFields method. The dependents_layer for updated fields are reloaded in the process. +Dependent fields for updated entries are filtered out and passed to the syncFields method. The dependents_layers for updated fields are reloaded in the process. */ async function update() { @@ -187,21 +187,36 @@ async function update() { // Update entry.values with newValues. entry.value = entry.newValue; + // Remove newValue + delete entry.newValue; - if (Array.isArray(entry.dependents_layer)) { + // Iterate through the dependents_layers array + if (Array.isArray(entry.dependents_layers)) { - entry.dependents_layer.forEach(layer => { + entry.dependents_layers.forEach(layer => { - // Iterate through the dependents_layer array and reload matching layers in mapview. - entry.location.layer.mapview.layers[layer].reload() + // Find the layer in mapview. + const mapview_layer = entry.location.layer.mapview.layers[layer] + + // If the layer exists, reload it. + if (mapview_layer) mapview_layer.reload(); }) } return entry.dependents - }).flat() + }) + // Flatten dependents array and filter out undefined values. + .flat() + .filter(dependents => dependents !== undefined) + + // sync dependent fields + if (dependents.length) await this.syncFields([...new Set(dependents)]) + + // Reload layer. + this.layer.reload() - // Sync dependent fields - if (dependents.length) await this.syncFields([...new Set(dependents)]); + // Execute update callbacks. + this.updateCallbacks?.forEach(fn => typeof fn === 'function' && fn(this)) } async function syncFields(fields) { From be27144a6d898e123690a06b3c2575dfc6598de4 Mon Sep 17 00:00:00 2001 From: Robert Hurst Date: Mon, 21 Oct 2024 10:05:39 +0200 Subject: [PATCH 5/6] Update tests assets --- tests/assets/layers/scratch/infoj.json | 5 ----- tests/workspace.json | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/tests/assets/layers/scratch/infoj.json b/tests/assets/layers/scratch/infoj.json index dad0c56d0..736bf0e8d 100644 --- a/tests/assets/layers/scratch/infoj.json +++ b/tests/assets/layers/scratch/infoj.json @@ -22,11 +22,6 @@ { "title": "char_field", "field": "char_field" - }, - { - "title": "minutes", - "field": "integer_field", - "inline": true } ] } \ No newline at end of file diff --git a/tests/workspace.json b/tests/workspace.json index 3afa03fe8..1a0a5039e 100644 --- a/tests/workspace.json +++ b/tests/workspace.json @@ -156,6 +156,25 @@ "polygon": true, "point": true }, + "infoj": [ + { + "label": "check", + "field": "bool_field", + "edit": true, + "inline": true, + "type": "boolean", + "dependents": [ + "integer_field" + ] + }, + { + "title": "minutes", + "field": "integer_field", + "value": null, + "edit": "true", + "inline": true + } + ], "infoj_skip": [ "textarea" ] From d0e5853b7fcbe4ba7da3c0ca396726c5ad3d1ef3 Mon Sep 17 00:00:00 2001 From: Simon Leech <56163132+simon-leech@users.noreply.github.com> Date: Mon, 21 Oct 2024 15:01:03 +0100 Subject: [PATCH 6/6] Bit more info on the docs --- lib/location/decorate.mjs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/location/decorate.mjs b/lib/location/decorate.mjs index 94a26bb12..15a7c2451 100644 --- a/lib/location/decorate.mjs +++ b/lib/location/decorate.mjs @@ -110,7 +110,10 @@ An xhr POST request with all newValues is passed to location_update query templa infoj entry values are set to the newValues after a successful update. -Dependent fields for updated entries are filtered out and passed to the syncFields method. The dependents_layers for updated fields are reloaded in the process. +Dependent fields for updated entries is an array of fields that is passed to the syncFields method, to retrieve the updated values from the location. +These dependents fields are reloaded with the updated values from the location. + +Dependent layers is an array of layer keys that will be reloaded after the update. */ async function update() {