Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update queryParams.mjs #962

Merged
merged 13 commits into from
Oct 13, 2023
49 changes: 22 additions & 27 deletions lib/utils/queryParams.mjs
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
export default _this => {

// Assign empty object if not defined.
_this.queryparams = _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

// Assign table name from layer.
_this.queryparams.table &&= _this.location?.layer?.tableCurrent()
// Assign table name from layer
const tableCurrent = _this.location?.layer?.tableCurrent() || _this.layer?.tableCurrent()

// Assign fieldValues from the location to queryparams.
if (Array.isArray(_this.queryparams.fieldValues) && _this.location) {
// If table is set to true, use the current table name from the layer, otherwise use the table name from the queryparams.
const table = _this.queryparams.table === true ? tableCurrent : _this.queryparams.table;

// Iterate through the fieldValues array.
_this.queryparams.fieldValues.forEach(field => {

// Find entry in location infoj matching the field.
let entry = _this.location.infoj.find(entry => entry.field === field)

// Assign entry value as field value in queryparams.
_this.queryparams[field] = entry.value
})
}
_this.queryparams.table &&= table;

// Get bounds from mapview.
const bounds = _this.viewport && _this.layer.mapview.getBounds();
@@ -32,36 +26,37 @@ export default _this => {
`EPSG:${_this.layer.mapview.srid}`,
`EPSG:4326`)

return {

return Object.assign({}, _this.queryparams, {
// 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: _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,
locale: _this.queryparams.layer ? _this.layer.mapview.locale.key : undefined,

// Locale param is only required for layer lookups.
layer: _this.queryparams.layer && _this.layer.key,
// Layer param is only required for layer lookups.
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,
id: _this.queryparams.id ? _this.location?.id : undefined,

// 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.
fieldValues: undefined

})
}
}