Skip to content

Commit

Permalink
L.Control.Button for easy button creation
Browse files Browse the repository at this point in the history
  • Loading branch information
johndoe committed Dec 18, 2021
1 parent a154ef3 commit 355d1e1
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions core/code/LeafletButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
var Button = L.Control.extend({
includes: L.Mixin.Events,

options: {
position: 'topleft',
listenTo: 'click',
label: '',
title: '',
className: '',
},

initialize: function (options) {
L.setOptions(this, options);
this.toggled = false;
if (this.options.style) {
var style = document.createElement('style');
document.head.appendChild(style);
style.type = 'text/css';
style.appendChild(document.createTextNode(this.options.style));
}
},

onAdd: function (map) {
var options = this.options;
var button = this.button = document.createElement('a');
button.innerHTML = options.label;
button.title = options.title;
L.DomEvent.disableClickPropagation(button);
L.DomEvent.on(button, 'click', L.DomEvent.preventDefault);

var self = this;
if (options.toggle) {
L.DomEvent.on(button, 'click', function (ev) {
this.classes.toggle('active');
self.toggled = !self.toggled;
var data = { originalEvent: ev };
self.fire(self.toggled ? 'toggle' : 'untoggle', data);
});
}
L.DomEvent.on(button, options.listenTo, function (ev) {
var data = { originalEvent: ev };
this.fire(ev.type, data);
}, this);

button.setAttribute('role', 'button');
button.setAttribute('aria-label', options.title);

var container = document.createElement('div');
container.className = 'leaflet-bar ' + (options.className || '');
container.appendChild(button);

this.fire('add');
return container;
},

onRemove: function (map) {
this.fire('remove');
}
});

L.Control.Button = Button;

0 comments on commit 355d1e1

Please sign in to comment.