Skip to content

Commit 7b60eec

Browse files
committed
wip: Improve highlights management (#1106)
1 parent ccae08c commit 7b60eec

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

map/client/cesium/utils/utils.style.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ function processStyle (style, feature, options, mappings) {
156156

157157
// visibility attribute can be used to hide individual features
158158
// visibility is true by default but can also be a string when it's
159-
// a result of a lodash steing template evaluation
160-
let visibility = _.get(style, 'style.visibility', true)
159+
// a result of a lodash string template evaluation
160+
let visibility = _.get(style, `style.${type}.visibility`, true)
161161
if (typeof visibility === 'string') visibility = visibility === 'true'
162162
// The 'kdk-hidden-features' pane is created when the leaflet map is initialized
163163
// if (!visibility) _.set(style, `style.${type}.pane`, 'kdk-hidden-features')

map/client/composables/highlight.js

+30-21
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ export function useHighlight (name, options = {}) {
5858
}
5959
return id
6060
}
61+
function isHighlightFor (highlightId, layer, feature) {
62+
return feature ? highlightId.includes(`-${getFeatureId(feature, layer)}`) : highlightId.includes(`-${_.kebabCase(layer.name)}`)
63+
}
6164
function hasHighlight (feature, layer) {
6265
return has(getHighlightId(feature, layer))
6366
}
@@ -71,7 +74,6 @@ export function useHighlight (name, options = {}) {
7174
const highlight = {
7275
highlightId,
7376
type: 'Feature',
74-
isDisabled: (layer ? layer.isDisabled : false),
7577
properties: Object.assign({
7678
zOrder: 0,
7779
}, options)
@@ -117,25 +119,37 @@ export function useHighlight (name, options = {}) {
117119
// Add additional information provided by feature, if any, for custom styling
118120
_.merge(highlight, _.omit(feature, ['geometry', 'style']))
119121
set(highlightId, highlight)
120-
requestHighlightsLayerUpdate()
122+
setHighlightEnabled(feature, layer, layer ? !layer.isDisabled : true)
121123
return highlight
122124
}
123125
function unhighlight (feature, layer) {
124126
const highlightId = getHighlightId(feature, layer)
125127
unset(highlightId)
126128
requestHighlightsLayerUpdate()
127129
}
130+
function setHighlightEnabled (feature, layer, enabled = true) {
131+
const highlight = getHighlight(feature, layer)
132+
_.set(highlight, 'style.visibility', enabled)
133+
requestHighlightsLayerUpdate()
134+
}
135+
function setHighlightsEnabled (layer, enabled = true) {
136+
getHighlights(layer).forEach(highlight => setHighlightEnabled(highlight, layer, enabled))
137+
}
128138
function clearHighlights () {
129139
clear()
130140
requestHighlightsLayerUpdate()
131141
}
132-
function getHighlightedFeatures () {
142+
function getHighlights (layer, feature) {
133143
// Iterate over all highlights
134144
let features = []
135145
// For each highlight store
136-
forOwn((store, key) => {
146+
forOwn(store => {
137147
// Retrieve features in highlight store
138-
features = features.concat(_.flatten(_.values(store)))
148+
_.forOwn(store, (value, key) => {
149+
if (!layer || (layer && isHighlightFor(key, layer, feature))) {
150+
features.push(value)
151+
}
152+
})
139153
})
140154
return features
141155
}
@@ -174,7 +188,7 @@ export function useHighlight (name, options = {}) {
174188
}
175189
function updateHighlightsLayer () {
176190
// Get all highlights
177-
let features = getHighlightedFeatures()
191+
let features = getHighlights()
178192
// Filter invisible ones
179193
features = features.filter(feature => !feature.isDisabled)
180194
// Order from back to front
@@ -183,7 +197,7 @@ export function useHighlight (name, options = {}) {
183197
activity.updateLayer(HighlightsLayerName, {
184198
type: 'FeatureCollection',
185199
features
186-
}, { replace: true }) // Always start from fresh data as we debounce the update and might multiple operations might generate a wrong order otherwise
200+
}, { replace: true }) // Always start from fresh data as we debounce the update and multiple operations might generate a wrong order otherwise
187201
}
188202
}
189203
// In order to avoid updating the layer too much often we queue a request update every N ms
@@ -194,24 +208,16 @@ export function useHighlight (name, options = {}) {
194208
if (activity) activity.removeLayer(HighlightsLayerName)
195209
}
196210
function onHighlightedLayerDisabled (layer) {
197-
// Get all highlights
198-
const features = getHighlightedFeatures()
199-
// Tag layer' features as invisible
200-
features.forEach(feature => {
201-
const suffix = `-${_.kebabCase(layer.name)}-${getFeatureId(feature, layer)}`
202-
if (feature.highlightId.endsWith(suffix)) feature.isDisabled = true
211+
// Tag all highlights as invisible
212+
getHighlights(layer).forEach(highlight => {
213+
setHighlightEnabled(highlight, layer, false)
203214
})
204-
requestHighlightsLayerUpdate()
205215
}
206216
function onHighlightedLayerEnabled (layer) {
207-
// Get all highlights
208-
const features = getHighlightedFeatures()
209-
// Tag layer' features as visible
210-
features.forEach(feature => {
211-
const suffix = `-${_.kebabCase(layer.name)}-${getFeatureId(feature, layer)}`
212-
if (feature.highlightId.endsWith(suffix)) feature.isDisabled = false
217+
// Tag all highlights as visible
218+
getHighlights(layer).forEach(highlight => {
219+
setHighlightEnabled(highlight, layer, true)
213220
})
214-
requestHighlightsLayerUpdate()
215221
}
216222

217223
// Cleanup on destroy
@@ -225,8 +231,11 @@ export function useHighlight (name, options = {}) {
225231
highlights: store,
226232
hasHighlight,
227233
getHighlight,
234+
getHighlights,
228235
highlight,
229236
unhighlight,
237+
setHighlightEnabled,
238+
setHighlightsEnabled,
230239
clearHighlights
231240
}
232241
}

map/client/leaflet/utils/utils.style.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ function processStyle (style, feature, options, mappings) {
166166
// visibility attribute can be used to hide individual features
167167
// visibility is true by default but can also be a string when it's
168168
// a result of a lodash steing template evaluation
169-
let visibility = _.get(style, 'style.visibility', true)
169+
let visibility = _.get(style, `style.${type}.visibility`, true)
170170
if (typeof visibility === 'string') visibility = visibility === 'true'
171171
// The 'kdk-hidden-features' pane is created when the leaflet map is initialized
172172
if (!visibility) _.set(style, `style.${type}.pane`, 'kdk-hidden-features')

0 commit comments

Comments
 (0)