@@ -58,6 +58,9 @@ export function useHighlight (name, options = {}) {
58
58
}
59
59
return id
60
60
}
61
+ function isHighlightFor ( highlightId , layer , feature ) {
62
+ return feature ? highlightId . includes ( `-${ getFeatureId ( feature , layer ) } ` ) : highlightId . includes ( `-${ _ . kebabCase ( layer . name ) } ` )
63
+ }
61
64
function hasHighlight ( feature , layer ) {
62
65
return has ( getHighlightId ( feature , layer ) )
63
66
}
@@ -71,7 +74,6 @@ export function useHighlight (name, options = {}) {
71
74
const highlight = {
72
75
highlightId,
73
76
type : 'Feature' ,
74
- isDisabled : ( layer ? layer . isDisabled : false ) ,
75
77
properties : Object . assign ( {
76
78
zOrder : 0 ,
77
79
} , options )
@@ -117,25 +119,37 @@ export function useHighlight (name, options = {}) {
117
119
// Add additional information provided by feature, if any, for custom styling
118
120
_ . merge ( highlight , _ . omit ( feature , [ 'geometry' , 'style' ] ) )
119
121
set ( highlightId , highlight )
120
- requestHighlightsLayerUpdate ( )
122
+ setHighlightEnabled ( feature , layer , layer ? ! layer . isDisabled : true )
121
123
return highlight
122
124
}
123
125
function unhighlight ( feature , layer ) {
124
126
const highlightId = getHighlightId ( feature , layer )
125
127
unset ( highlightId )
126
128
requestHighlightsLayerUpdate ( )
127
129
}
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
+ }
128
138
function clearHighlights ( ) {
129
139
clear ( )
130
140
requestHighlightsLayerUpdate ( )
131
141
}
132
- function getHighlightedFeatures ( ) {
142
+ function getHighlights ( layer , feature ) {
133
143
// Iterate over all highlights
134
144
let features = [ ]
135
145
// For each highlight store
136
- forOwn ( ( store , key ) => {
146
+ forOwn ( store => {
137
147
// 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
+ } )
139
153
} )
140
154
return features
141
155
}
@@ -174,7 +188,7 @@ export function useHighlight (name, options = {}) {
174
188
}
175
189
function updateHighlightsLayer ( ) {
176
190
// Get all highlights
177
- let features = getHighlightedFeatures ( )
191
+ let features = getHighlights ( )
178
192
// Filter invisible ones
179
193
features = features . filter ( feature => ! feature . isDisabled )
180
194
// Order from back to front
@@ -183,7 +197,7 @@ export function useHighlight (name, options = {}) {
183
197
activity . updateLayer ( HighlightsLayerName , {
184
198
type : 'FeatureCollection' ,
185
199
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
187
201
}
188
202
}
189
203
// 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 = {}) {
194
208
if ( activity ) activity . removeLayer ( HighlightsLayerName )
195
209
}
196
210
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 )
203
214
} )
204
- requestHighlightsLayerUpdate ( )
205
215
}
206
216
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 )
213
220
} )
214
- requestHighlightsLayerUpdate ( )
215
221
}
216
222
217
223
// Cleanup on destroy
@@ -225,8 +231,11 @@ export function useHighlight (name, options = {}) {
225
231
highlights : store ,
226
232
hasHighlight,
227
233
getHighlight,
234
+ getHighlights,
228
235
highlight,
229
236
unhighlight,
237
+ setHighlightEnabled,
238
+ setHighlightsEnabled,
230
239
clearHighlights
231
240
}
232
241
}
0 commit comments