Skip to content

Commit 095bec0

Browse files
committed
added L.Control.Button
1 parent 6eff9c7 commit 095bec0

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

L.Control.Button.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Author: Jerroyd Moore
2+
3+
L.Control.Button = L.Control.extend({
4+
includes: L.Mixin.Events,
5+
options: {
6+
position: 'topright',
7+
},
8+
initialize: function (label, options) {
9+
L.setOptions(this, options);
10+
var button = null;
11+
12+
if (label instanceof HTMLElement) {
13+
button = label;
14+
try {
15+
button.parentNode.removeChild(button);
16+
} catch (e) { }
17+
} else if (typeof label === "string") {
18+
button = L.DomUtil.create('button', this.options.className)
19+
} else {
20+
throw new Error('L.Control.Button: failed to initialize, label must either be text or a dom element');
21+
}
22+
23+
L.DomUtil.addClass(button, this.options.position);
24+
25+
this._container = button;
26+
27+
return this;
28+
},
29+
isToggled: function () {
30+
return L.DomUtil.hasClass(this._container, this.options.toggleButton);
31+
},
32+
_fireClick: function (e) {
33+
this.fire('click');
34+
35+
if (this.options.toggleButton) {
36+
var btn = this._container;
37+
if (this.isToggled()) {
38+
L.DomUtil.removeClass(this._container, this.options.toggleButton);
39+
} else {
40+
L.DomUtil.addClass(this._container, this.options.toggleButton);
41+
}
42+
}
43+
},
44+
onAdd: function (map) {
45+
if (this._container) {
46+
L.DomEvent.on(this._container, 'click', this._fireClick, this);
47+
var stop = L.DomEvent.stopPropagation;
48+
L.DomEvent.on(this._container, 'mousedown', stop)
49+
.on(this._container, 'touchstart', stop)
50+
.on(this._container, 'dblclick', stop)
51+
.on(this._container, 'mousewheel', stop)
52+
.on(this._container, 'MozMozMousePixelScroll', stop)
53+
this.fire('load');
54+
55+
this._map = map;
56+
}
57+
58+
return this._container;
59+
},
60+
onRemove: function (map) {
61+
if (this._container && this._map) {
62+
L.DomEvent.off(this._container, 'click', this._fireClick, this);
63+
L.DomEvent.off(this._container, 'mousedown', stop)
64+
.off(this._container, 'touchstart', stop)
65+
.off(this._container, 'dblclick', stop)
66+
.off(this._container, 'mousewheel', stop)
67+
.off(this._container, 'MozMozMousePixelScroll', stop)
68+
69+
this.fire('unload');
70+
this._map = null;
71+
}
72+
73+
return this;
74+
}
75+
});
76+
77+
L.control.button = function (label, options) {
78+
return new L.Control.Button(label, options);
79+
};

0 commit comments

Comments
 (0)