[76] | 1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
| 2 | * full list of contributors). Published under the Clear BSD license. |
---|
| 3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
| 4 | * full text of the license. */ |
---|
| 5 | |
---|
| 6 | /** |
---|
| 7 | * @requires OpenLayers/Handler.js |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | /** |
---|
| 11 | * Class: OpenLayers.Handler.Click |
---|
| 12 | * A handler for mouse clicks. The intention of this handler is to give |
---|
| 13 | * controls more flexibility with handling clicks. Browsers trigger |
---|
| 14 | * click events twice for a double-click. In addition, the mousedown, |
---|
| 15 | * mousemove, mouseup sequence fires a click event. With this handler, |
---|
| 16 | * controls can decide whether to ignore clicks associated with a double |
---|
| 17 | * click. By setting a <pixelTolerance>, controls can also ignore clicks |
---|
| 18 | * that include a drag. Create a new instance with the |
---|
| 19 | * <OpenLayers.Handler.Click> constructor. |
---|
| 20 | * |
---|
| 21 | * Inherits from: |
---|
| 22 | * - <OpenLayers.Handler> |
---|
| 23 | */ |
---|
| 24 | OpenLayers.Handler.Click = OpenLayers.Class(OpenLayers.Handler, { |
---|
| 25 | |
---|
| 26 | /** |
---|
| 27 | * APIProperty: delay |
---|
| 28 | * {Number} Number of milliseconds between clicks before the event is |
---|
| 29 | * considered a double-click. |
---|
| 30 | */ |
---|
| 31 | delay: 300, |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * APIProperty: single |
---|
| 35 | * {Boolean} Handle single clicks. Default is true. If false, clicks |
---|
| 36 | * will not be reported. If true, single-clicks will be reported. |
---|
| 37 | */ |
---|
| 38 | single: true, |
---|
| 39 | |
---|
| 40 | /** |
---|
| 41 | * APIProperty: double |
---|
| 42 | * {Boolean} Handle double-clicks. Default is false. |
---|
| 43 | */ |
---|
| 44 | 'double': false, |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | * APIProperty: pixelTolerance |
---|
| 48 | * {Number} Maximum number of pixels between mouseup and mousedown for an |
---|
| 49 | * event to be considered a click. Default is 0. If set to an |
---|
| 50 | * integer value, clicks with a drag greater than the value will be |
---|
| 51 | * ignored. This property can only be set when the handler is |
---|
| 52 | * constructed. |
---|
| 53 | */ |
---|
| 54 | pixelTolerance: 0, |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * APIProperty: stopSingle |
---|
| 58 | * {Boolean} Stop other listeners from being notified of clicks. Default |
---|
| 59 | * is false. If true, any click listeners registered before this one |
---|
| 60 | * will not be notified of *any* click event (associated with double |
---|
| 61 | * or single clicks). |
---|
| 62 | */ |
---|
| 63 | stopSingle: false, |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | * APIProperty: stopDouble |
---|
| 67 | * {Boolean} Stop other listeners from being notified of double-clicks. |
---|
| 68 | * Default is false. If true, any click listeners registered before |
---|
| 69 | * this one will not be notified of *any* double-click events. |
---|
| 70 | * |
---|
| 71 | * The one caveat with stopDouble is that given a map with two click |
---|
| 72 | * handlers, one with stopDouble true and the other with stopSingle |
---|
| 73 | * true, the stopSingle handler should be activated last to get |
---|
| 74 | * uniform cross-browser performance. Since IE triggers one click |
---|
| 75 | * with a dblclick and FF triggers two, if a stopSingle handler is |
---|
| 76 | * activated first, all it gets in IE is a single click when the |
---|
| 77 | * second handler stops propagation on the dblclick. |
---|
| 78 | */ |
---|
| 79 | stopDouble: false, |
---|
| 80 | |
---|
| 81 | /** |
---|
| 82 | * Property: timerId |
---|
| 83 | * {Number} The id of the timeout waiting to clear the <delayedCall>. |
---|
| 84 | */ |
---|
| 85 | timerId: null, |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * Property: down |
---|
| 89 | * {<OpenLayers.Pixel>} The pixel location of the last mousedown. |
---|
| 90 | */ |
---|
| 91 | down: null, |
---|
| 92 | |
---|
| 93 | /** |
---|
| 94 | * Property: rightclickTimerId |
---|
| 95 | * {Number} The id of the right mouse timeout waiting to clear the |
---|
| 96 | * <delayedEvent>. |
---|
| 97 | */ |
---|
| 98 | rightclickTimerId: null, |
---|
| 99 | |
---|
| 100 | /** |
---|
| 101 | * Constructor: OpenLayers.Handler.Click |
---|
| 102 | * Create a new click handler. |
---|
| 103 | * |
---|
| 104 | * Parameters: |
---|
| 105 | * control - {<OpenLayers.Control>} The control that is making use of |
---|
| 106 | * this handler. If a handler is being used without a control, the |
---|
| 107 | * handler's setMap method must be overridden to deal properly with |
---|
| 108 | * the map. |
---|
| 109 | * callbacks - {Object} An object with keys corresponding to callbacks |
---|
| 110 | * that will be called by the handler. The callbacks should |
---|
| 111 | * expect to recieve a single argument, the click event. |
---|
| 112 | * Callbacks for 'click' and 'dblclick' are supported. |
---|
| 113 | * options - {Object} Optional object whose properties will be set on the |
---|
| 114 | * handler. |
---|
| 115 | */ |
---|
| 116 | initialize: function(control, callbacks, options) { |
---|
| 117 | OpenLayers.Handler.prototype.initialize.apply(this, arguments); |
---|
| 118 | // optionally register for mouseup and mousedown |
---|
| 119 | if(this.pixelTolerance != null) { |
---|
| 120 | this.mousedown = function(evt) { |
---|
| 121 | this.down = evt.xy; |
---|
| 122 | return true; |
---|
| 123 | }; |
---|
| 124 | } |
---|
| 125 | }, |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | * Method: mousedown |
---|
| 129 | * Handle mousedown. Only registered as a listener if pixelTolerance is |
---|
| 130 | * a non-zero value at construction. |
---|
| 131 | * |
---|
| 132 | * Returns: |
---|
| 133 | * {Boolean} Continue propagating this event. |
---|
| 134 | */ |
---|
| 135 | mousedown: null, |
---|
| 136 | |
---|
| 137 | /** |
---|
| 138 | * Method: mouseup |
---|
| 139 | * Handle mouseup. Installed to support collection of right mouse events. |
---|
| 140 | * |
---|
| 141 | * Returns: |
---|
| 142 | * {Boolean} Continue propagating this event. |
---|
| 143 | */ |
---|
| 144 | mouseup: function (evt) { |
---|
| 145 | var propagate = true; |
---|
| 146 | |
---|
| 147 | // Collect right mouse clicks from the mouseup |
---|
| 148 | // IE - ignores the second right click in mousedown so using |
---|
| 149 | // mouseup instead |
---|
| 150 | if (this.checkModifiers(evt) && |
---|
| 151 | this.control.handleRightClicks && |
---|
| 152 | OpenLayers.Event.isRightClick(evt)) { |
---|
| 153 | propagate = this.rightclick(evt); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | return propagate; |
---|
| 157 | }, |
---|
| 158 | |
---|
| 159 | /** |
---|
| 160 | * Method: rightclick |
---|
| 161 | * Handle rightclick. For a dblrightclick, we get two clicks so we need |
---|
| 162 | * to always register for dblrightclick to properly handle single |
---|
| 163 | * clicks. |
---|
| 164 | * |
---|
| 165 | * Returns: |
---|
| 166 | * {Boolean} Continue propagating this event. |
---|
| 167 | */ |
---|
| 168 | rightclick: function(evt) { |
---|
| 169 | if(this.passesTolerance(evt)) { |
---|
| 170 | if(this.rightclickTimerId != null) { |
---|
| 171 | //Second click received before timeout this must be |
---|
| 172 | // a double click |
---|
| 173 | this.clearTimer(); |
---|
| 174 | this.callback('dblrightclick', [evt]); |
---|
| 175 | return !this.stopDouble; |
---|
| 176 | } else { |
---|
| 177 | //Set the rightclickTimerId, send evt only if double is |
---|
| 178 | // true else trigger single |
---|
| 179 | var clickEvent = this['double'] ? |
---|
| 180 | OpenLayers.Util.extend({}, evt) : |
---|
| 181 | this.callback('rightclick', [evt]); |
---|
| 182 | |
---|
| 183 | var delayedRightCall = OpenLayers.Function.bind( |
---|
| 184 | this.delayedRightCall, |
---|
| 185 | this, |
---|
| 186 | clickEvent |
---|
| 187 | ); |
---|
| 188 | this.rightclickTimerId = window.setTimeout( |
---|
| 189 | delayedRightCall, this.delay |
---|
| 190 | ); |
---|
| 191 | } |
---|
| 192 | } |
---|
| 193 | return !this.stopSingle; |
---|
| 194 | }, |
---|
| 195 | |
---|
| 196 | /** |
---|
| 197 | * Method: delayedRightCall |
---|
| 198 | * Sets <rightclickTimerId> to null. And optionally triggers the |
---|
| 199 | * rightclick callback if evt is set. |
---|
| 200 | */ |
---|
| 201 | delayedRightCall: function(evt) { |
---|
| 202 | this.rightclickTimerId = null; |
---|
| 203 | if (evt) { |
---|
| 204 | this.callback('rightclick', [evt]); |
---|
| 205 | } |
---|
| 206 | return !this.stopSingle; |
---|
| 207 | }, |
---|
| 208 | |
---|
| 209 | /** |
---|
| 210 | * Method: dblclick |
---|
| 211 | * Handle dblclick. For a dblclick, we get two clicks in some browsers |
---|
| 212 | * (FF) and one in others (IE). So we need to always register for |
---|
| 213 | * dblclick to properly handle single clicks. |
---|
| 214 | * |
---|
| 215 | * Returns: |
---|
| 216 | * {Boolean} Continue propagating this event. |
---|
| 217 | */ |
---|
| 218 | dblclick: function(evt) { |
---|
| 219 | if(this.passesTolerance(evt)) { |
---|
| 220 | if(this["double"]) { |
---|
| 221 | this.callback('dblclick', [evt]); |
---|
| 222 | } |
---|
| 223 | this.clearTimer(); |
---|
| 224 | } |
---|
| 225 | return !this.stopDouble; |
---|
| 226 | }, |
---|
| 227 | |
---|
| 228 | /** |
---|
| 229 | * Method: click |
---|
| 230 | * Handle click. |
---|
| 231 | * |
---|
| 232 | * Returns: |
---|
| 233 | * {Boolean} Continue propagating this event. |
---|
| 234 | */ |
---|
| 235 | click: function(evt) { |
---|
| 236 | if(this.passesTolerance(evt)) { |
---|
| 237 | if(this.timerId != null) { |
---|
| 238 | // already received a click |
---|
| 239 | this.clearTimer(); |
---|
| 240 | } else { |
---|
| 241 | // set the timer, send evt only if single is true |
---|
| 242 | //use a clone of the event object because it will no longer |
---|
| 243 | //be a valid event object in IE in the timer callback |
---|
| 244 | var clickEvent = this.single ? |
---|
| 245 | OpenLayers.Util.extend({}, evt) : null; |
---|
| 246 | this.timerId = window.setTimeout( |
---|
| 247 | OpenLayers.Function.bind(this.delayedCall, this, clickEvent), |
---|
| 248 | this.delay |
---|
| 249 | ); |
---|
| 250 | } |
---|
| 251 | } |
---|
| 252 | return !this.stopSingle; |
---|
| 253 | }, |
---|
| 254 | |
---|
| 255 | /** |
---|
| 256 | * Method: passesTolerance |
---|
| 257 | * Determine whether the event is within the optional pixel tolerance. Note |
---|
| 258 | * that the pixel tolerance check only works if mousedown events get to |
---|
| 259 | * the listeners registered here. If they are stopped by other elements, |
---|
| 260 | * the <pixelTolerance> will have no effect here (this method will always |
---|
| 261 | * return true). |
---|
| 262 | * |
---|
| 263 | * Returns: |
---|
| 264 | * {Boolean} The click is within the pixel tolerance (if specified). |
---|
| 265 | */ |
---|
| 266 | passesTolerance: function(evt) { |
---|
| 267 | var passes = true; |
---|
| 268 | if(this.pixelTolerance != null && this.down) { |
---|
| 269 | var dpx = Math.sqrt( |
---|
| 270 | Math.pow(this.down.x - evt.xy.x, 2) + |
---|
| 271 | Math.pow(this.down.y - evt.xy.y, 2) |
---|
| 272 | ); |
---|
| 273 | if(dpx > this.pixelTolerance) { |
---|
| 274 | passes = false; |
---|
| 275 | } |
---|
| 276 | } |
---|
| 277 | return passes; |
---|
| 278 | }, |
---|
| 279 | |
---|
| 280 | /** |
---|
| 281 | * Method: clearTimer |
---|
| 282 | * Clear the timer and set <timerId> to null. |
---|
| 283 | */ |
---|
| 284 | clearTimer: function() { |
---|
| 285 | if(this.timerId != null) { |
---|
| 286 | window.clearTimeout(this.timerId); |
---|
| 287 | this.timerId = null; |
---|
| 288 | } |
---|
| 289 | if(this.rightclickTimerId != null) { |
---|
| 290 | window.clearTimeout(this.rightclickTimerId); |
---|
| 291 | this.rightclickTimerId = null; |
---|
| 292 | } |
---|
| 293 | }, |
---|
| 294 | |
---|
| 295 | /** |
---|
| 296 | * Method: delayedCall |
---|
| 297 | * Sets <timerId> to null. And optionally triggers the click callback if |
---|
| 298 | * evt is set. |
---|
| 299 | */ |
---|
| 300 | delayedCall: function(evt) { |
---|
| 301 | this.timerId = null; |
---|
| 302 | if(evt) { |
---|
| 303 | this.callback('click', [evt]); |
---|
| 304 | } |
---|
| 305 | }, |
---|
| 306 | |
---|
| 307 | /** |
---|
| 308 | * APIMethod: deactivate |
---|
| 309 | * Deactivate the handler. |
---|
| 310 | * |
---|
| 311 | * Returns: |
---|
| 312 | * {Boolean} The handler was successfully deactivated. |
---|
| 313 | */ |
---|
| 314 | deactivate: function() { |
---|
| 315 | var deactivated = false; |
---|
| 316 | if(OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) { |
---|
| 317 | this.clearTimer(); |
---|
| 318 | this.down = null; |
---|
| 319 | deactivated = true; |
---|
| 320 | } |
---|
| 321 | return deactivated; |
---|
| 322 | }, |
---|
| 323 | |
---|
| 324 | CLASS_NAME: "OpenLayers.Handler.Click" |
---|
| 325 | }); |
---|