Skip to content

Commit

Permalink
Merge pull request #988 from dbauszus-glx/sqlfilter-encoding
Browse files Browse the repository at this point in the history
sqlfilter / json url parameter encoding
  • Loading branch information
RobAndrewHurst authored Nov 2, 2023
2 parents 952a878 + 1b610d6 commit b48454b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 37 deletions.
7 changes: 0 additions & 7 deletions api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,6 @@ module.exports = async (req, res) => {

req.params.template = req.params._template || req.params.template

// Decode string params.
Object.entries(req.params)
.filter(entry => typeof entry[1] === 'string')
.forEach(entry => {
req.params[entry[0]] = decodeURIComponent(entry[1])
})

// Short circuit login view or post request.
if (req.params.login || req.body && req.body.login) return login(req, res)

Expand Down
2 changes: 1 addition & 1 deletion lib/ui/elements/dropdown.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default (params) => {
// Set btn text to reflect selection or show placeholder.
btn.querySelector('[data-id=header-span]')
.textContent = params.selectedTitles.size && Array.from(params.selectedTitles).map(v => decodeURIComponent(v)).join(', ')
.textContent = params.selectedTitles.size && Array.from(params.selectedTitles).map(v => v).join(', ')
|| params.span || params.placeholder
// Execute callback method and pass array of current selection.
Expand Down
35 changes: 20 additions & 15 deletions lib/ui/layers/filters.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@ export default {

let timeout;

function applyFilter(layer, zoom) {
clearTimeout(timeout);
function applyFilter(layer) {

// enable zoomToExtent button.
let btn = layer.view.querySelector('[data-id=zoomToExtent]')
if (btn) btn.disabled = false;
clearTimeout(timeout);

// Debounce layer reload by 500
timeout = setTimeout(() => {
timeout = null;
layer.reload();
layer.mapview.Map.getTargetElement().dispatchEvent(new Event('changeEnd'))
}, 500);
Expand Down Expand Up @@ -186,26 +182,35 @@ async function filter_in(layer, filter) {
multi: true,
placeholder: 'Select Multiple',
entries: filter[filter.type].map(val => ({
title: decodeURIComponent(val),
option: encodeURIComponent(val),
title: val,
option: val,
selected: chkSet.has(val)
})),
callback: async (e, options) => {

Object.assign(layer.filter.current, {
[filter.field]:{
[filter.type]: options
}
})
if (!options.length) {

// Remove empty array filter.
delete layer.filter.current[filter.field]
} else {

// Set filter values array from options.
Object.assign(layer.filter.current, {
[filter.field]:{
[filter.type]: options
}
})
}

layer.reload()
layer.mapview.Map.getTargetElement().dispatchEvent(new Event('changeEnd'))
}
})
}

return filter[filter.type].map(val => mapp.ui.elements.chkbox({
val: encodeURIComponent(val),
label: decodeURIComponent(val),
val: val,
label: val,
checked: chkSet.has(val),
onchange: (checked, val) => {

Expand Down
2 changes: 1 addition & 1 deletion lib/ui/utils/tabulatorUtils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function like(_this) {

// Set filter
_this.layer.filter.current[field] = {
[headerFilterParams.type || 'like']: encodeURIComponent(e.target.value)
[headerFilterParams.type || 'like']: e.target.value
}

} else {
Expand Down
12 changes: 5 additions & 7 deletions lib/utils/paramString.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Create param string for XHR request.
export default params => Object.entries(params)

// Value should be 0 or truthy
.filter(entry => entry[1] === 0 || !!entry[1])

// Value must not be empty functional brackets.
.filter(entry => entry[1] !== '{}')

// Filter out zero length array and objects with empty object values.
Expand All @@ -10,16 +14,10 @@ export default params => Object.entries(params)

.map(entry => {

// if (Array.isArray(entry[1])) {

// return entry[1].map(val => `${entry[0]}=${val}`).join('&')
// }

// Stringify non array objects.
if (typeof entry[1] === 'object' && !Array.isArray(entry[1])) {

entry[1] = JSON.stringify(entry[1])

return `${entry[0]}=${encodeURIComponent(JSON.stringify(entry[1]))}`
}

return encodeURI(`${entry[0]}=${entry[1]}`)
Expand Down
9 changes: 3 additions & 6 deletions mod/utils/sqlFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,17 @@ const filterTypes = {
`(${val
.split(',')
.filter((val) => val.length > 0)
.map((val) => `"${col}" ILIKE \$${addValues(`${val}%`, true)}`)
.map((val) => `"${col}" ILIKE \$${addValues(`${val}%`)}`)
.join(' OR ')})`,

match: (col, val) => `"${col}"::text ILIKE \$${addValues(val)}`
}

let SQLparams

function addValues(val, skip) {
function addValues(val) {

SQLparams.push(Array.isArray(val)
&& val[0].map(v=>decodeURIComponent(v))
|| skip && val
|| decodeURIComponent(val))
SQLparams.push(val)

return SQLparams.length
}
Expand Down

0 comments on commit b48454b

Please sign in to comment.