npmap.js
Version:
A JavaScript web mapping library, built as a Leaflet plugin, for the National Park Service.
53 lines (42 loc) • 1.19 kB
JavaScript
/* global L */
;
var HomeControl = L.Control.extend({
options: {
position: 'topleft'
},
initialize: function(options) {
L.Util.extend(this.options, options);
return this;
},
onAdd: function() {
var container = L.DomUtil.create('div', 'leaflet-control-home leaflet-bar leaflet-control'),
button = L.DomUtil.create('button', 'leaflet-bar-single', container);
button.title = 'Pan/zoom to initial extent';
L.DomEvent.disableClickPropagation(button);
L.DomEvent
.on(button, 'click', L.DomEvent.preventDefault)
.on(button, 'click', this.toHome, this);
return container;
},
toHome: function() {
var map = this._map,
options = map.options;
map.setView(options.center, options.zoom);
map.closePopup();
}
});
L.Map.mergeOptions({
homeControl: true
});
L.Map.addInitHook(function() {
if (this.options.homeControl) {
var options = {};
if (typeof this.options.homeControl === 'object') {
options = this.options.homeControl;
}
this.homeControl = L.npmap.control.home(options).addTo(this);
}
});
module.exports = function(options) {
return new HomeControl(options);
};