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