-
Notifications
You must be signed in to change notification settings - Fork 1
/
L.Control.Base64Legend.js
executable file
·89 lines (73 loc) · 3.18 KB
/
L.Control.Base64Legend.js
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
L.Control.Base64Legend = L.Control.extend({
_map: null,
includes: L.Evented ? L.Evented.prototype : L.Mixin.Events,
options: {
position: 'topright',
legends: [], // array of legend entries - see README for format
collapseSimple: false, // if true, legend entries that are from a simple renderer will use compact presentation
detectStretched: false // if true, will test to see if legend entries look stretched. These are usually in sets of 3 with the middle element having no label.
},
onAdd: function (map) {
this._map = map;
var container = L.DomUtil.create('div', 'leaflet-control leaflet-bar leaflet-legend');
this._container = container;
// Disable events on container
L.DomEvent.disableClickPropagation(container);
if (!L.Browser.touch) {
L.DomEvent.disableScrollPropagation(container);
}
this.render();
return this._container;
},
// render this.options.entries
render: function () {
L.DomUtil.empty(this._container);
var legends = this.options.legends;
if (!legends) return;
legends.forEach(function(legend) {
if (!legend.elements) return;
var elements = legend.elements;
var className = 'legend-block';
if (this.options.detectStretched) {
if (elements.length === 3 && elements[0].label !== '' && elements[1].label === '' && elements[2].label !== ''){
legend.type = 'stretched';
}
}
if (legend.type === 'stretched') {
className += ' legend-stretched';
}
var block = L.DomUtil.create('div', className, this._container);
if (this.options.collapseSimple && elements.length == 1 && !elements[0].label){
this._addElement(elements[0].imageData, legend.name, block);
return;
}
if (legend.name) {
var header = L.DomUtil.create('h4', null, block);
L.DomUtil.create('div', 'caret', header);
L.DomUtil.create('span', null, header).innerHTML = legend.name;
L.DomEvent.on(header, 'click', function() {
if (L.DomUtil.hasClass(header, 'closed')) {
L.DomUtil.removeClass(header, 'closed');
}
else {
L.DomUtil.addClass(header, 'closed');
}
}, this);
}
var elementContainer = L.DomUtil.create('div', 'legend-elements', block);
elements.forEach(function (element) {
this._addElement(element.imageData, element.label, elementContainer);
}, this);
}, this);
},
_addElement: function(imageData, label, container) {
var row = L.DomUtil.create('div', 'legend-row', container);
L.DomUtil.create('img', null, row).src = imageData;
if (!!label) {
L.DomUtil.create('label', null, row).innerHTML = label;
}
}
});
L.control.base64legend = function (options) {
return new L.Control.Base64Legend(options);
};