]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/leaflet.key.js
Only one pane visible at a time
[rails.git] / app / assets / javascripts / leaflet.key.js
1 L.OSM.key = function (options) {
2   var control = L.control(options);
3
4   control.onAdd = function (map) {
5     var $container = $('<div>')
6       .attr('class', 'control-key');
7
8     $('<a>')
9       .attr('class', 'control-button')
10       .attr('href', '#')
11       .attr('title', I18n.t('javascripts.key.tooltip'))
12       .html('<span class="icon key"></span>')
13       .on('click', toggle)
14       .appendTo($container);
15
16     var $ui = $('<div>')
17       .attr('class', 'layers-ui');
18
19     $('<h2>')
20       .text(I18n.t('javascripts.key.title'))
21       .appendTo($ui);
22
23     var $section = $('<section>')
24       .appendTo($ui);
25
26     options.sidebar.addPane($ui);
27
28     $ui
29       .on('show', shown)
30       .on('hide', hidden);
31
32     function shown() {
33       map.on('zoomend baselayerchange', update);
34       $section.load('/key', update);
35     }
36
37     function hidden() {
38       map.off('zoomend baselayerchange', update);
39     }
40
41     function toggle(e) {
42       e.stopPropagation();
43       e.preventDefault();
44       options.sidebar.togglePane($ui);
45     }
46
47     function update() {
48       var layer = getMapBaseLayerId(map),
49         zoom = map.getZoom();
50
51       $('.mapkey-table-entry').each(function () {
52         var data = $(this).data();
53         if (layer == data.layer && zoom >= data.zoomMin && zoom <= data.zoomMax) {
54           $(this).show();
55         } else {
56           $(this).hide();
57         }
58       });
59     }
60
61     return $container[0];
62   };
63
64   return control;
65 };