For PanZoomBar, this fixes the slider behavior. Now the buttonclick listener argument also includes a buttonXY property, and PanZoomPanel does not need an Events instance for the zoombarDiv any more.
For Panel, this fixes events for panels outside the map. Just setting the element on the Events instance was no longer enough after e70569b2bb. Events::attachToElement is now used, and it needed to be modified to also work if the Events instance had no element previously.
Finally, I renamed the button property of the buttonclick listener argument to buttonElement, to not confuse it with the browser event button property, and added some more tests and documentation.
143 lines
4.7 KiB
JavaScript
143 lines
4.7 KiB
JavaScript
/* Copyright (c) 2006-2012 by OpenLayers Contributors (see authors.txt for
|
|
* full list of contributors). Published under the Clear BSD license.
|
|
* See http://svn.openlayers.org/trunk/openlayers/license.txt for the
|
|
* full text of the license. */
|
|
|
|
/**
|
|
* @requires OpenLayers/Events.js
|
|
*/
|
|
|
|
/**
|
|
* Class: OpenLayers.Events.buttonclick
|
|
* Extension event type for handling buttons on top of a dom element. This
|
|
* event type fires "buttonclick" on its <target> when a button was
|
|
* clicked. Buttons are detected by the "olButton" class.
|
|
*
|
|
* This event type makes sure that button clicks do not interfere with other
|
|
* events that are registered on the same <element>.
|
|
*
|
|
* Event types provided by this extension:
|
|
* - *buttonclick* Triggered when a button is clicked. Listeners receive an
|
|
* object with a *buttonElement* property referencing the dom element of
|
|
* the clicked button, and an *buttonXY* property with the click position
|
|
* relative to the button.
|
|
*/
|
|
OpenLayers.Events.buttonclick = OpenLayers.Class({
|
|
|
|
/**
|
|
* APIProperty: target
|
|
* {<OpenLayers.Events>} The events instance that the buttonclick event will
|
|
* be triggered on.
|
|
*/
|
|
target: null,
|
|
|
|
/**
|
|
* Property: events
|
|
* {Array} Events to observe and conditionally stop from propagating when
|
|
* an element with the olButton class (or its olAlphaImg child) is
|
|
* clicked.
|
|
*/
|
|
events: [
|
|
'mousedown', 'mouseup', 'click', 'dblclick',
|
|
'touchstart', 'touchmove', 'touchend'
|
|
],
|
|
|
|
/**
|
|
* Property: startRegEx
|
|
* {RegExp} Regular expression to test Event.type for events that start
|
|
* a buttonclick sequence.
|
|
*/
|
|
startRegEx: /^mousedown|touchstart$/,
|
|
|
|
/**
|
|
* Property: cancelRegEx
|
|
* {RegExp} Regular expression to test Event.type for events that cancel
|
|
* a buttonclick sequence.
|
|
*/
|
|
cancelRegEx: /^touchmove$/,
|
|
|
|
/**
|
|
* Property: completeRegEx
|
|
* {RegExp} Regular expression to test Event.type for events that complete
|
|
* a buttonclick sequence.
|
|
*/
|
|
completeRegEx: /^mouseup|touchend$/,
|
|
|
|
/**
|
|
* Property: startEvt
|
|
* {Event} The event that started the click sequence
|
|
*/
|
|
|
|
/**
|
|
* Constructor: OpenLayers.Events.buttonclick
|
|
* Construct a buttonclick event type. Applications are not supposed to
|
|
* create instances of this class - they are created on demand by
|
|
* <OpenLayers.Events> instances.
|
|
*
|
|
* Parameters:
|
|
* target - {<OpenLayers.Events>} The events instance that the buttonclick
|
|
* event will be triggered on.
|
|
*/
|
|
initialize: function(target) {
|
|
this.target = target;
|
|
for (var i=this.events.length-1; i>=0; --i) {
|
|
this.target.register(this.events[i], this, this.buttonClick, {
|
|
extension: true
|
|
});
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Method: destroy
|
|
*/
|
|
destroy: function() {
|
|
for (var i=this.events.length-1; i>=0; --i) {
|
|
this.target.unregister(this.events[i], this, this.buttonClick);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Method: buttonClick
|
|
* Check if a button was clicked, and fire the buttonclick event
|
|
*
|
|
* Parameters:
|
|
* evt - {Event}
|
|
*/
|
|
buttonClick: function(evt) {
|
|
var propagate = true,
|
|
element = OpenLayers.Event.element(evt);
|
|
if (element && (OpenLayers.Event.isLeftClick(evt) || !~evt.type.indexOf("mouse"))) {
|
|
if (OpenLayers.Element.hasClass(element, "olAlphaImg")) {
|
|
element = element.parentNode;
|
|
}
|
|
if (OpenLayers.Element.hasClass(element, "olButton")) {
|
|
if (this.startEvt) {
|
|
if (this.completeRegEx.test(evt.type)) {
|
|
var pos = OpenLayers.Util.pagePosition(element);
|
|
this.target.triggerEvent("buttonclick", {
|
|
buttonElement: element,
|
|
buttonXY: {
|
|
x: this.startEvt.clientX - pos[0],
|
|
y: this.startEvt.clientY - pos[1]
|
|
}
|
|
});
|
|
}
|
|
if (this.cancelRegEx.test(evt.type)) {
|
|
delete this.startEvt;
|
|
}
|
|
OpenLayers.Event.stop(evt);
|
|
propagate = false;
|
|
}
|
|
if (this.startRegEx.test(evt.type)) {
|
|
this.startEvt = evt;
|
|
OpenLayers.Event.stop(evt);
|
|
propagate = false;
|
|
}
|
|
} else {
|
|
delete this.startEvt;
|
|
}
|
|
}
|
|
return propagate;
|
|
}
|
|
|
|
}); |