-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLegend.js
More file actions
416 lines (367 loc) · 10.7 KB
/
Legend.js
File metadata and controls
416 lines (367 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/**
* @module ol/control/Legend
*/
import Control from 'ol/control/Control.js';
import EventType from 'ol/events/EventType.js';
import {CLASS_COLLAPSED, CLASS_CONTROL, CLASS_UNSELECTABLE} from 'ol/css.js';
import {LineString, Point, Polygon} from 'ol/geom';
import {equals} from 'ol/array.js';
import {inView} from 'ol/layer/Layer.js';
import {removeChildren, replaceNode} from 'ol/dom.js';
import {toContext} from 'ol/render';
/**
* @typedef {object} Options
* @property {string} [className='ol-legend'] CSS class name.
* @property {HTMLElement|string} [target] Specify a target if you
* want the control to be rendered outside of the map's
* viewport.
* @property {boolean} [collapsible] Specify if legends can
* be collapsed. If not specified, sources control this behavior with their
* `legendsCollapsible` setting.
* @property {boolean} [collapsed=true] Specify if legends should
* be collapsed at startup.
* @property {string} [tipLabel='Legends'] Text label to use for the button tip.
* @property {string} [label='L'] Text label to use for the
* collapsed legends button.
* Instead of text, also an element (e.g. a `span` element) can be used.
* @property {string|HTMLElement} [collapseLabel='»'] Text label to use
* for the expanded legends button.
* Instead of text, also an element (e.g. a `span` element) can be used.
* @property {function(import("../MapEvent.js").default)} [render] Function called when
* the control should be re-rendered. This is called in a `requestAnimationFrame`
* callback.
*/
/**
* @classdesc
* Control to show all the legends associated with the layer sources
* By default it will show in the bottom left portion of the map, but this can
* be changed by using a css selector for `.ol-legend`.
*
*/
class Legend extends Control {
/**
* Build the legend
*
* @param {object} opt_options - Legend options.
* @example new Legend()
*/
constructor(opt_options) {
const options = opt_options ? opt_options : {};
super({
element: document.createElement('div'),
render: options.render || render,
target: options.target,
});
/**
* @private
* @type {HTMLElement}
*/
this.divElement_ = document.createElement('div');
/**
* @private
* @type {boolean}
*/
this.collapsed_ =
options.collapsed !== undefined ? options.collapsed : true;
/**
* @private
* @type {boolean}
*/
this.overrideCollapsible_ = options.collapsible !== undefined;
/**
* @private
* @type {boolean}
*/
this.collapsible_ =
options.collapsible !== undefined ? options.collapsible : true;
if (!this.collapsible_) {
this.collapsed_ = false;
}
const className =
options.className !== undefined ? options.className : 'ol-legend';
const tipLabel =
options.tipLabel !== undefined ? options.tipLabel : 'Legends';
const collapseLabel =
options.collapseLabel !== undefined ? options.collapseLabel : '\u00AB';
if (typeof collapseLabel === 'string') {
/**
* @private
* @type {HTMLElement}
*/
this.collapseLabel_ = document.createElement('span');
this.collapseLabel_.textContent = collapseLabel;
} else {
this.collapseLabel_ = collapseLabel;
}
const label = options.label !== undefined ? options.label : 'L';
if (typeof label === 'string') {
/**
* @private
* @type {HTMLElement}
*/
this.label_ = document.createElement('span');
this.label_.textContent = label;
} else {
this.label_ = label;
}
const activeLabel =
this.collapsible_ && !this.collapsed_ ? this.collapseLabel_ : this.label_;
const button = document.createElement('button');
button.setAttribute('type', 'button');
button.title = tipLabel;
button.appendChild(activeLabel);
button.addEventListener(
EventType.CLICK,
this.handleClick_.bind(this),
false
);
const cssClasses =
className +
' ' +
CLASS_UNSELECTABLE +
' ' +
CLASS_CONTROL +
(this.collapsed_ && this.collapsible_ ? ' ' + CLASS_COLLAPSED : '') +
(this.collapsible_ ? '' : ' ol-uncollapsible');
const element = this.element;
element.className = cssClasses;
element.appendChild(this.divElement_);
element.appendChild(button);
/**
* A list of currently rendered resolutions.
*
* @type {Array<string>}
* @private
*/
this.renderedLegends_ = [];
/**
* @private
* @type {boolean}
*/
this.renderedVisible_ = true;
}
/**
* Collect a list of visible legends and set the collapsible state.
*
* @param {object} frameState - Frame state.
* @returns {Array} legends - Array of legends.
* @private
* @example
*/
collectSourceLegends_(frameState) {
/**
* A list of visible legends.
*
* @type {Array<string>}
*/
const visibleLegends = [];
const layerStatesArray = frameState.layerStatesArray;
for (let i = 0, ii = layerStatesArray.length; i < ii; ++i) {
const layerState = layerStatesArray[i];
if (!inView(layerState, frameState.viewState)) {
continue;
}
const source = /** @type {import("../layer/Layer.js").default} */ (layerState.layer).getSource();
if (!source) {
continue;
}
let legends;
if (typeof source.getLegends === 'function') {
legends = source.getLegends();
}
if (!legends) {
continue;
}
const divElement = document.createElement('div');
const ulElement = document.createElement('ul');
if (source.get('title')) {
const labelElement = document.createElement('div');
labelElement.textContent = source.get('title');
labelElement.className = 'ol-legend-label';
divElement.appendChild(labelElement);
}
if (Array.isArray(legends)) {
for (let j = 0, jj = legends.length; j < jj; ++j) {
const legendItem = this.createLegendItem_(
legends[j].label,
legends[j].style,
legends[j].geometry
);
ulElement.appendChild(legendItem);
}
} else if (legends) {
const legendItem = this.createLegendItem_(
legends.label,
legends.style,
legends.geometry
);
ulElement.appendChild(legendItem);
}
divElement.appendChild(ulElement);
visibleLegends.push(divElement);
}
return visibleLegends;
}
/**
* Create a legend item.
*
* @param {string} label - Label of the legend.
* @param {import("ol/Style.js").default} style - The style or array of styles.
* @param {string} geometry - Point, LineString or Polygon
* @returns {HTMLElement} legendItem - Legends item.
* @private
* @example
*/
createLegendItem_(label, style, geometry) {
const legendItem = document.createElement('li');
const legendItemCanvas = document.createElement('canvas');
const legendItemLabel = document.createElement('span');
legendItemLabel.textContent = label;
let olGeometry;
const vectorContext = toContext(legendItemCanvas.getContext('2d'), {
size: [30, 30],
});
vectorContext.setStyle(style);
if (geometry == 'Polygon') {
olGeometry = new Polygon([
[
[2, 6],
[15, 6],
[28, 2],
[26, 24],
[10, 20],
[4, 26],
[2, 2],
],
]);
} else if (geometry == 'LineString') {
olGeometry = new LineString([
[0, 15],
[6, 10],
[12, 20],
[18, 18],
[24, 15],
[30, 15],
]);
} else {
olGeometry = new Point([15, 15]);
}
vectorContext.drawGeometry(olGeometry);
legendItem.appendChild(legendItemCanvas);
legendItem.appendChild(legendItemLabel);
return legendItem;
}
/**
* @private
* @param {?import("ol/PluggableMap.js").FrameState} frameState Frame state.
* @example
*/
updateElement_(frameState) {
if (!frameState) {
if (this.renderedVisible_) {
this.element.style.display = 'none';
this.renderedVisible_ = false;
}
return;
}
const legends = this.collectSourceLegends_(frameState);
const visible = legends.length > 0;
if (this.renderedVisible_ != visible) {
this.element.style.display = visible ? '' : 'none';
this.renderedVisible_ = visible;
}
if (equals(legends, this.renderedLegends_)) {
return;
}
removeChildren(this.divElement_);
// append the legends
for (let i = 0, ii = legends.length; i < ii; ++i) {
const element = legends[i];
this.divElement_.appendChild(element);
}
this.renderedLegends_ = legends;
}
/**
* @param {MouseEvent} event The event to handle
* @private
* @example
*/
handleClick_(event) {
event.preventDefault();
this.handleToggle_();
}
/**
* @private
* @example
*/
handleToggle_() {
this.element.classList.toggle(CLASS_COLLAPSED);
if (this.collapsed_) {
replaceNode(this.collapseLabel_, this.label_);
} else {
replaceNode(this.label_, this.collapseLabel_);
}
this.collapsed_ = !this.collapsed_;
}
/**
* Return `true` if the legend is collapsible, `false` otherwise.
*
* @returns {boolean} True if the widget is collapsible.
* @example
*/
getCollapsible() {
return this.collapsible_;
}
/**
* Set whether the legend should be collapsible.
*
* @param {boolean} collapsible - True if the widget is collapsible.
* @example
*/
setCollapsible(collapsible) {
if (this.collapsible_ === collapsible) {
return;
}
this.collapsible_ = collapsible;
this.element.classList.toggle('ol-uncollapsible');
if (!collapsible && this.collapsed_) {
this.handleToggle_();
}
}
/**
* Collapse or expand the legend according to the passed parameter. Will
* not do anything if the legend isn't collapsible or if the current
* collapsed state is already the one requested.
*
* @param {boolean} collapsed - True if the widget is collapsed.
* @example
*/
setCollapsed(collapsed) {
if (!this.collapsible_ || this.collapsed_ === collapsed) {
return;
}
this.handleToggle_();
}
/**
* Return `true` when the legend is currently collapsed or `false`
* otherwise.
*
* @returns {boolean} True if the widget is collapsed.
* @example
*/
getCollapsed() {
return this.collapsed_;
}
}
/**
* Update the legend element.
*
* @class
* @param {import("ol/MapEvent.js").default} mapEvent Map event.
* @this {Legend} Legend
* @example
*/
export function render(mapEvent) {
this.updateElement_(mapEvent.frameState);
}
export default Legend;