-
Notifications
You must be signed in to change notification settings - Fork 0
/
legend.py
106 lines (88 loc) · 3.21 KB
/
legend.py
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
from branca.element import MacroElement
from jinja2 import Template
from folium.elements import JSCSSMixin
from folium.utilities import parse_options
class Legend(JSCSSMixin, MacroElement):
"""
https://github.com/mikeskaug/Leaflet.Legend?tab=readme-ov-file
"""
_template = Template(
"""
{% macro script(this, kwargs) %}
L.Control.Legend.prototype.updateItems = function(items) {
var container = this._container;
container.innerHTML = ''; // Clear existing content
var className = 'leaflet-legend';
var list = L.DomUtil.create('div', 'leaflet-legend-list', container);
items.forEach(function(item) {
if (item.active == true){
var div = L.DomUtil.create('div', className + '-item', list);
var colorbox = L.DomUtil.create('div', className + '-color', div);
colorbox.innerHTML = ' ';
colorbox.style.backgroundColor = item.color;
L.DomUtil.create('div', className + '-text', div).innerHTML = item.label;
}
});
//this.expand();
};
var legenda = L.control.legend({
items: [
{color: 'red', label: 'cervene'},
{label: 'bez farby'},
{color: 'blue', label: 'modre'}
],
collapsed: false, //nastavenia v mape
position: 'bottomright',
buttonHtml: '<i class="fa fa-info" style="color: #000"></i>'
});
legenda.addTo({{this._parent.get_name()}});
// $('.leaflet-control-attribution').hide();
{{this._parent.get_name()}}.attributionControl.setPrefix(false);
var co_do_legendy = [];
{% for x in this.legend %}
co_do_legendy.push({color: '{{ x[1] }}', label: '{{ x[0] }}',active: true});
{% endfor %}
legenda.updateItems(co_do_legendy);
{{this._parent.get_name()}}.on('overlayadd',function(e){
co_do_legendy.forEach(element => {
if (element.label == e.name) {
element.active = true;
legenda.updateItems(co_do_legendy);
}
});
});
{{this._parent.get_name()}}.on('overlayremove',function(e){
co_do_legendy.forEach(element => {
if (element.label == e.name) {
element.active = false;
legenda.updateItems(co_do_legendy);
}
});
});
{{this._parent.get_name()}}.legenda = legenda;
{% endmacro %}
"""
)
default_js = [
(
"Legend.js",
"https://cdn.jsdelivr.net/gh/zostera/leaflet-legend@master/leaflet-legend.js",
)
]
default_css = [
(
"Legend.css",
"https://cdn.jsdelivr.net/gh/zostera/leaflet-legend@master/leaflet-legend.css",
)
]
def __init__(
self,
legend=None,
**kwargs
):
super().__init__()
self.legend = legend or []
self._name = "Legend"
self.options = parse_options(
**kwargs
)