]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/leaflet.key.js
c5124e39d6cf139e4e5b1115e2c691fed532303d
[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', 'key-ui');
18
19     $('<div>')
20       .attr('class', 'sidebar_heading')
21       .appendTo($ui)
22       .append(
23         $('<a>')
24           .text(I18n.t('javascripts.close'))
25           .attr('class', 'sidebar_close')
26           .attr('href', '#')
27           .bind('click', toggle))
28       .append(
29         $('<h4>')
30           .text(I18n.t('javascripts.key.title')));
31
32     var $section = $('<div>')
33       .attr('class', 'section')
34       .appendTo($ui);
35
36     options.sidebar.addPane($ui);
37
38     $ui
39       .on('show', shown)
40       .on('hide', hidden);
41
42     function shown() {
43       map.on('zoomend baselayerchange', update);
44       $section.load('/key', update);
45     }
46
47     function hidden() {
48       map.off('zoomend baselayerchange', update);
49     }
50
51     function toggle(e) {
52       e.stopPropagation();
53       e.preventDefault();
54       options.sidebar.togglePane($ui);
55     }
56
57     function update() {
58       var layer = map.getMapBaseLayerId(),
59         zoom = map.getZoom();
60
61       $('.mapkey-table-entry').each(function () {
62         var data = $(this).data();
63         if (layer == data.layer && zoom >= data.zoomMin && zoom <= data.zoomMax) {
64           $(this).show();
65         } else {
66           $(this).hide();
67         }
68       });
69     }
70
71     return $container[0];
72   };
73
74   return control;
75 };