Skip to content

Commit c76511e

Browse files
authored
Issue #16: Add ability to toggle controls on/off via API (#63)
1 parent 06871cd commit c76511e

File tree

3 files changed

+65
-24
lines changed

3 files changed

+65
-24
lines changed

js/leaflet.backdrop.js

+44-22
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,20 @@
9393
popupMinWidth = this.map.popupMinWidth;
9494
}
9595

96+
// Defensive coding: mapControls gets set in leaflet_build_map(), but
97+
// might be missing in edge cases.
98+
let controls = (this.mapControls) ? this.mapControls : {};
99+
let settings = {};
100+
if (controls.ControlFullscreen) {
101+
settings.fullscreenControl = true;
102+
}
96103
// load a settings object with all of our map settings
97-
var settings = {
98-
'fullscreenControl': true,
99-
};
100104
for (let setting in this.map.settings) {
101105
settings[setting] = this.map.settings[setting];
102106
}
103-
settings.zoomControl = false; // replaced by L.Control.Zoomslider
107+
if (controls.ControlZoomslider) {
108+
settings.zoomControl = false; // replaced by L.Control.Zoomslider
109+
}
104110

105111
// Workaround for Safari bug.
106112
// @see https://github.com/backdrop-contrib/leaflet/issues/17
@@ -171,27 +177,39 @@
171177
}
172178

173179
// add scale control //+
174-
lMap.addControl(new L.control.scale({imperial: false}));
180+
if (controls.ControlScale) {
181+
// @todo Evaluate options for dynamic options (imperial).
182+
let scaleControl = new L.control.scale({imperial: false});
183+
lMap.scaleControl = scaleControl;
184+
lMap.addControl(scaleControl);
185+
}
175186

176187
// add Zoomslider control //+
177-
lMap.addControl(new L.Control.Zoomslider());
188+
if (controls.ControlZoomslider) {
189+
let zoomsliderControl = new L.Control.Zoomslider();
190+
lMap.zoomsliderControl = zoomsliderControl;
191+
lMap.addControl(zoomsliderControl);
192+
}
178193

179194
// Small box with lat/lon coordinates of mouse click event on map.
180-
var c = new L.Control.Coordinates({
181-
promptText: Backdrop.t('Press Ctrl+C to copy coordinates'),
182-
precision: 5
183-
});
184-
c.addTo(lMap);
185-
lMap.on('click', function(e) {
186-
c.setCoordinates(e);
187-
// Hide the coordinates box again after 4 seconds.
188-
if (typeof this.hideTimer !== 'undefined') {
189-
clearTimeout(this.hideTimer);
190-
}
191-
this.hideTimer = window.setTimeout(function() {
192-
c._container.classList.add('hidden');
193-
}, 4000);
194-
});
195+
if (controls.ControlCoordinates) {
196+
let c = new L.Control.Coordinates({
197+
promptText: Backdrop.t('Press Ctrl+C to copy coordinates'),
198+
precision: 5
199+
});
200+
lMap.coordinatesControl = c;
201+
c.addTo(lMap);
202+
lMap.on('click', function(e) {
203+
c.setCoordinates(e);
204+
// Hide the coordinates box again after 4 seconds.
205+
if (typeof this.hideTimer !== 'undefined') {
206+
clearTimeout(this.hideTimer);
207+
}
208+
this.hideTimer = window.setTimeout(function() {
209+
c._container.classList.add('hidden');
210+
}, 4000);
211+
});
212+
}
195213

196214
let zoom = this.map.settings.zoom ? this.map.settings.zoom : this.map.settings.zoomDefault;
197215
// Init ViewCenter plugin with some defaults.
@@ -202,7 +220,11 @@
202220
vcLatLng: [0, 0],
203221
vcZoom: zoom
204222
});
205-
lMap.addControl(viewCenter);
223+
// @todo viewCenter is in use further down. Might need restructuring.
224+
if (controls.ControlViewCenter) {
225+
lMap.viewCenterControl = viewCenter;
226+
lMap.addControl(viewCenter);
227+
}
206228

207229
// center the map
208230
if (this.map.center && (this.map.center.force || this.features.length === 0)) {

leaflet.api.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,21 @@ function hook_leaflet_map_info() {
8787
* @param array $settings
8888
* A javascript settings array used for building the leaflet map.
8989
*
90-
* @see leaflet_map_get_info()
91-
* @see hook_leaflet_map_info()
90+
* @see leaflet_build_map()
9291
*/
9392
function hook_leaflet_map_prebuild_alter(array &$settings) {
9493
$settings['mapId'] = 'my-map-id';
9594
$settings['features']['icon'] = 'my-icon-url';
95+
96+
// Remove all controls. Available for all controls besides (dynamic) layer
97+
// switcher or attribution.
98+
$settings['mapControls']['ControlFullscreen'] = FALSE;
99+
$settings['mapControls']['ControlScale'] = FALSE;
100+
$settings['mapControls']['ControlZoomslider'] = FALSE;
101+
$settings['mapControls']['ControlCoordinates'] = FALSE;
102+
$settings['mapControls']['ControlViewCenter'] = FALSE;
103+
104+
// Also turn off default zoom buttons, which would appear, as the Zoomslider
105+
// has been turned off above.
106+
$settings['map']['settings']['zoomControl'] = FALSE;
96107
}

leaflet.module

+8
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ function leaflet_build_map(array $map, array $features = array(), $height = '400
136136
$settings = array(
137137
'mapId' => $map_id,
138138
'map' => $map,
139+
// Also expose our additional (mostly plugin) controls to hook.
140+
'mapControls' => array(
141+
'ControlFullscreen' => TRUE,
142+
'ControlScale' => TRUE,
143+
'ControlZoomslider' => TRUE,
144+
'ControlCoordinates' => TRUE,
145+
'ControlViewCenter' => TRUE,
146+
),
139147
'features' => $features,
140148
);
141149
backdrop_alter('leaflet_map_prebuild', $settings);

0 commit comments

Comments
 (0)