]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/leaflet.layers.js
Move base layer UI above overlay layer UI
[rails.git] / app / assets / javascripts / leaflet.layers.js
1 L.OSM.layers = function(options) {
2   var control = L.control(options);
3
4   control.onAdd = function (map) {
5     var layers = options.layers;
6
7     var $container = $('<div>')
8       .attr('class', 'control-layers');
9
10     var link = $('<a>')
11       .attr('class', 'control-button')
12       .attr('href', '#')
13       .attr('title', 'Layers')
14       .html('<span class="icon layers"></span>')
15       .on('click', toggle)
16       .appendTo($container);
17
18     var $ui = $('<div>')
19       .attr('class', 'layers-ui');
20
21     $('<section>')
22        .appendTo($ui)
23        .append(
24           $('<a>')
25             .html('&raquo;')
26             .attr('class', 'close-button')
27             .attr('href', '#')
28             .bind('click', toggle))
29        .append(
30         $('<h2>')
31           .text(I18n.t('javascripts.map.layers.header')));
32
33     var baseSection = $('<section>')
34       .addClass('base-layers')
35       .appendTo($ui);
36
37     $('<p>')
38       .text(I18n.t('javascripts.map.layers.base'))
39       .appendTo(baseSection);
40
41     list = $('<ul>')
42       .appendTo(baseSection);
43
44     layers.forEach(function(layer) {
45       var item = $('<li>')
46         .appendTo(list);
47
48       if (map.hasLayer(layer)) {
49         item.addClass('active');
50       }
51
52       var div = $('<div>')
53         .appendTo(item);
54
55       map.whenReady(function() {
56         var miniMap = L.map(div[0], {attributionControl: false, zoomControl: false})
57           .addLayer(new layer.constructor());
58
59         miniMap.dragging.disable();
60         miniMap.touchZoom.disable();
61         miniMap.doubleClickZoom.disable();
62         miniMap.scrollWheelZoom.disable();
63
64         $ui
65           .on('show', shown)
66           .on('hide', hide);
67
68         function shown() {
69           miniMap.invalidateSize();
70           setView();
71           map.on('moveend', setView);
72         }
73
74         function hide() {
75           map.off('moveend', setView);
76         }
77
78         function setView() {
79           miniMap.setView(map.getCenter(), Math.max(map.getZoom() - 2, 0));
80         }
81       });
82
83       var label = $('<label>')
84         .text(layer.options.name)
85         .appendTo(item);
86
87       item.on('click', function() {
88         layers.forEach(function(other) {
89           if (other === layer) {
90             map.addLayer(other);
91           } else {
92             map.removeLayer(other);
93           }
94         });
95       });
96
97       map.on('layeradd layerremove', function() {
98         item.toggleClass('active', map.hasLayer(layer));
99       });
100     });
101
102     if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
103       var overlaySection = $('<section>')
104         .addClass('overlay-layers')
105         .appendTo($ui);
106
107       $('<p>')
108         .text(I18n.t('javascripts.map.layers.overlays'))
109         .appendTo(overlaySection);
110
111       var list = $('<ul>')
112         .appendTo(overlaySection);
113
114       function addOverlay(layer, name) {
115         var item = $('<li>')
116           .appendTo(list);
117
118         var label = $('<label>')
119           .appendTo(item);
120
121         var input = $('<input>')
122           .attr('type', 'checkbox')
123           .prop('checked', map.hasLayer(layer))
124           .appendTo(label);
125
126         label.append(name);
127
128         input.on('change', function() {
129           if (input.is(':checked')) {
130             map.addLayer(layer);
131           } else {
132             map.removeLayer(layer);
133           }
134         });
135
136         map.on('layeradd layerremove', function() {
137           input.prop('checked', map.hasLayer(layer));
138         });
139       }
140
141       addOverlay(map.noteLayer, I18n.t('javascripts.map.layers.notes'));
142       addOverlay(map.dataLayer, I18n.t('javascripts.map.layers.data'));
143     }
144
145     options.sidebar.addPane($ui);
146
147     function toggle(e) {
148       e.stopPropagation();
149       e.preventDefault();
150       options.sidebar.togglePane($ui);
151     }
152
153     return $container[0];
154   };
155
156   return control;
157 };