Skip to content

Commit

Permalink
Merge pull request #1577 from simon-leech/layer_dependents
Browse files Browse the repository at this point in the history
Infoj Entry - Layer Dependents
  • Loading branch information
dbauszus-glx authored Oct 21, 2024
2 parents aaf3f01 + d0f948b commit 86404ee
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 21 deletions.
68 changes: 52 additions & 16 deletions lib/location/decorate.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
### /location/decorate
@requires /utils/xhr
@module /location/decorate
*/

Expand Down Expand Up @@ -95,8 +97,23 @@ 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 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() {

Expand All @@ -106,21 +123,23 @@ 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)
.filter(entry => typeof entry.newValue !== 'undefined')
.forEach(entry => {

entry.newValue = {
'jsonb': {
[entry.jsonb_field]: {
[entry.jsonb_key]: entry.newValue
.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
}
}
}
}
})
})

// Create newValue on json_field entries.
this.infoj
.filter(entry => entry.json_field)
.filter(entry => entry.json_key)
Expand All @@ -136,11 +155,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({
Expand All @@ -163,17 +183,32 @@ async function update() {
return;
}

// Update entry.values with newValues.
// Return dependents from updated entries.
// 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;
// Remove newValue
delete entry.newValue;

// Iterate through the dependents_layers array
if (Array.isArray(entry.dependents_layers)) {

entry.dependents_layers.forEach(layer => {

// 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
})
// Flatten dependents array and filter out undefined values.
.flat()
.filter(dependents => dependents !== undefined)

Expand All @@ -183,6 +218,7 @@ async function update() {
// Reload layer.
this.layer.reload()

// Execute update callbacks.
this.updateCallbacks?.forEach(fn => typeof fn === 'function' && fn(this))
}

Expand Down Expand Up @@ -236,7 +272,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;

Expand Down
5 changes: 0 additions & 5 deletions tests/assets/layers/scratch/infoj.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@
{
"title": "char_field",
"field": "char_field"
},
{
"title": "minutes",
"field": "integer_field",
"inline": true
}
]
}
19 changes: 19 additions & 0 deletions tests/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand Down

0 comments on commit 86404ee

Please sign in to comment.