-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
L.Control.Button for easy button creation
- Loading branch information
johndoe
committed
Dec 18, 2021
1 parent
a154ef3
commit 355d1e1
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |