Add a baselayerchange event

This commit is contained in:
John Firebaugh 2012-10-12 12:13:55 -07:00
parent 0e606e268d
commit 5ee7113fe4
4 changed files with 142 additions and 5 deletions

93
spec/happen.js Normal file
View File

@ -0,0 +1,93 @@
// https://github.com/tmcw/happen
!(function(context) {
var h = {};
// Make inheritance bearable: clone one level of properties
function extend(child, parent) {
for (var property in parent) {
if (typeof child[property] == 'undefined') {
child[property] = parent[property];
}
}
return child;
}
h.once = function(x, o) {
var evt;
if (o.type.slice(0, 3) === 'key') {
if (typeof Event === 'function') {
evt = new Event(o.type);
evt.keyCode = o.keyCode || 0;
evt.charCode = o.charCode || 0;
evt.shift = o.shift || false;
evt.meta = o.meta || false;
evt.ctrl = o.ctrl || false;
evt.alt = o.alt || false;
} else {
evt = document.createEvent('KeyboardEvent');
// https://developer.mozilla.org/en/DOM/event.initKeyEvent
// https://developer.mozilla.org/en/DOM/KeyboardEvent
evt[(evt.initKeyEvent) ? 'initKeyEvent'
: 'initKeyboardEvent'](
o.type, // in DOMString typeArg,
true, // in boolean canBubbleArg,
true, // in boolean cancelableArg,
null, // in nsIDOMAbstractView viewArg, Specifies UIEvent.view. This value may be null.
o.ctrl || false, // in boolean ctrlKeyArg,
o.alt || false, // in boolean altKeyArg,
o.shift || false, // in boolean shiftKeyArg,
o.meta || false, // in boolean metaKeyArg,
o.keyCode || 0, // in unsigned long keyCodeArg,
o.charCode || 0 // in unsigned long charCodeArg);
);
}
} else {
evt = document.createEvent('MouseEvents');
// https://developer.mozilla.org/en/DOM/event.initMouseEvent
evt.initMouseEvent(o.type,
true, // canBubble
true, // cancelable
window, // 'AbstractView'
o.clicks || 0, // click count
o.screenX || 0, // screenX
o.screenY || 0, // screenY
o.clientX || 0, // clientX
o.clientY || 0, // clientY
o.ctrl || 0, // ctrl
o.alt || false, // alt
o.shift || false, // shift
o.meta || false, // meta
o.button || false, // mouse button
null // relatedTarget
);
}
x.dispatchEvent(evt);
};
var shortcuts = ['click', 'mousedown', 'mouseup', 'mousemove', 'keydown', 'keyup', 'keypress'],
s, i = 0;
while (s = shortcuts[i++]) {
h[s] = (function(s) {
return function(x, o) {
h.once(x, extend(o || {}, { type: s }));
};
})(s);
}
h.dblclick = function(x, o) {
h.once(x, extend(o || {}, {
type: 'dblclick',
clicks: 2
}));
};
this.happen = h;
if (typeof module !== 'undefined') {
module.exports = this.happen;
}
})(this);

View File

@ -5,6 +5,7 @@
<link rel="stylesheet" type="text/css" href="jasmine/jasmine.css">
<script type="text/javascript" src="jasmine/jasmine.js"></script>
<script type="text/javascript" src="jasmine/jasmine-html.js"></script>
<script type="text/javascript" src="happen.js"></script>
<!-- source files -->
@ -20,6 +21,9 @@
<script type="text/javascript" src="suites/SpecHelper.js"></script>
<script type="text/javascript" src="suites/LeafletSpec.js"></script>
<!-- /control -->
<script type="text/javascript" src="suites/control/Control.LayersSpec.js"></script>
<!-- /core -->
<script type="text/javascript" src="suites/core/UtilSpec.js"></script>
<script type="text/javascript" src="suites/core/ClassSpec.js"></script>
@ -63,4 +67,4 @@
})();
</script>
</body>
</html>
</html>

View File

@ -0,0 +1,32 @@
describe("Control.Layers", function () {
var map;
beforeEach(function () {
map = L.map(document.createElement('div'));
});
describe("baselayerchange event", function () {
it("is fired on input that changes the base layer", function () {
var baseLayers = {"Layer 1": L.tileLayer(), "Layer 2": L.tileLayer()},
layers = L.control.layers(baseLayers).addTo(map),
spy = jasmine.createSpy();
map.on('baselayerchange', spy);
happen.click(layers._baseLayersList.getElementsByTagName("input")[0]);
expect(spy).toHaveBeenCalled();
expect(spy.mostRecentCall.args[0].layer).toBe(baseLayers["Layer 1"]);
});
it("is not fired on input that doesn't change the base layer", function () {
var overlays = {"Marker 1": L.marker([0, 0]), "Marker 2": L.marker([0, 0])},
layers = L.control.layers({}, overlays).addTo(map),
spy = jasmine.createSpy();
map.on('baselayerchange', spy);
happen.click(layers._overlaysList.getElementsByTagName("input")[0]);
expect(spy).not.toHaveBeenCalled();
});
});
});

View File

@ -177,18 +177,26 @@ L.Control.Layers = L.Control.extend({
_onInputClick: function () {
var i, input, obj,
inputs = this._form.getElementsByTagName('input'),
inputsLen = inputs.length;
inputsLen = inputs.length,
baseLayer;
for (i = 0; i < inputsLen; i++) {
input = inputs[i];
obj = this._layers[input.layerId];
if (input.checked) {
this._map.addLayer(obj.layer, !obj.overlay);
} else {
if (input.checked && !this._map.hasLayer(obj.layer)) {
this._map.addLayer(obj.layer);
if (!obj.overlay) {
baseLayer = obj.layer;
}
} else if (!input.checked && this._map.hasLayer(obj.layer)) {
this._map.removeLayer(obj.layer);
}
}
if (baseLayer) {
this._map.fire('baselayerchange', {layer: baseLayer})
}
},
_expand: function () {