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

assign null value in merge util. fire changeend event on start. #967

Merged
merged 6 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions lib/layer/format/vector.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,24 @@ export default layer => {

layer.setSource(layer.features)

// Add event listener to reload layer for viewport on changeEnd.
layer.params.viewport && layer.mapview.Map.getTargetElement().addEventListener('changeEnd', ()=>{
// Add event listener to reload layer for viewport or layer.tables on changeEnd.
if (layer.tables || layer.params.viewport) {

if (!layer.display) return;
layer.mapview.Map.getTargetElement().addEventListener('changeEnd', () => {

clearTimeout(layer.timeout)
// Layer is outside of tables zoom range.
if (!layer.tableCurrent()) {
layer.L.setSource(null)
return;
}

layer.timeout = setTimeout(layer.reload, 100)
})
if (!layer.display) return;

clearTimeout(layer.timeout)

layer.timeout = setTimeout(layer.reload, 100)
})
}

// Change method for the cluster feature properties and layer stats.
layer.L.on('change', e => {
Expand Down
10 changes: 8 additions & 2 deletions lib/mapview/_mapview.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,9 @@ export default (mapview) => {
})

// changeEnd
mapview.Map.getTargetElement().addEventListener('changeEnd', () => {
mapview.Map.getTargetElement().addEventListener('changeEnd', changeEnd)

function changeEnd() {

// Update URL params if enabled on mapview
if (!mapview.hooks) return;
Expand All @@ -261,7 +263,7 @@ export default (mapview) => {
lng: Math.round((center[0] + Number.EPSILON) * 100000) / 100000,
z: Math.round((mapview.Map.getView().getZoom() + Number.EPSILON) * 100) / 100
})
})
}

// Timer to debounce changeEnd event.
let changeEndTimer
Expand All @@ -275,5 +277,9 @@ export default (mapview) => {
}, 500)
})

changeEndTimer = setTimeout(() => {
mapview.Map.getTargetElement().dispatchEvent(new CustomEvent('changeEnd'))
}, 500)

return mapview
}
6 changes: 5 additions & 1 deletion lib/ui/layers/view.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,18 @@ export default (layer) => {

// The layer view drawer should be disabled if layer.tables are not available for the current zoom level.
layer.mapview.Map.getTargetElement().addEventListener('changeEnd', ()=>{

if (!layer.tables) return;

if (layer.tableCurrent() === null) {

// Disable layer view out of zoom range
// Collapse drawer and disable layer.view.
layer.view.querySelector('[data-id=layer-drawer]').classList.remove('expanded')
layer.view.classList.add('disabled')

} else {

// Enable the layer.view.
layer.view.classList.remove('disabled')
}

Expand Down
25 changes: 20 additions & 5 deletions lib/utils/merge.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,38 @@
return target;
}

// Shift first source out of sources array.
const source = sources.shift();

// target & source are both objects.
if (isObject(target) && isObject(source)) {

const proto = Object.getPrototypeOf(target)

// Iterate over object keys in source.
for (const key in source) {

if (isObject(source[key])) {
// Key must not be in target object prototype.
if (proto[key]) {
console.warn(`Prototype polution detected for key: ${key}`)
continue;
}

if (source[key] === null) {
target[key] = null;

} else if (isObject(source[key])) {

// Assign empty object on key.
if (!target[key]) Object.assign(target, { [key]: {} });
// Target key must be an object.
target[key] ??= {}

// Call recursive merge for target key object.
mergeDeep(target[key], source[key]);

} else {

// Assign key if not an object itself.
Object.assign(target, { [key]: source[key] });
// source[key] is neither null nor object.
target[key] = source[key]
RobAndrewHurst marked this conversation as resolved.
Dismissed
Show resolved Hide resolved
}
}
}
Expand All @@ -33,11 +46,13 @@

function isObject(item) {

// item is an HTMLElement
if (item instanceof HTMLElement) {
console.warn(item)
return false;
}

// item is an array.
if (Array.isArray(item)) return false;

if (typeof item === 'object') return true;
Expand Down
Loading