Hover.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Copyright (c) 2006-2011 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. * @requires OpenLayers/Handler.js
  7. */
  8. /**
  9. * Class: OpenLayers.Handler.Hover
  10. * The hover handler is to be used to emulate mouseovers on objects
  11. * on the map that aren't DOM elements. For example one can use
  12. * this handler to send WMS/GetFeatureInfo requests as the user
  13. * moves the mouve over the map.
  14. *
  15. * Inherits from:
  16. * - <OpenLayers.Handler>
  17. */
  18. OpenLayers.Handler.Hover = OpenLayers.Class(OpenLayers.Handler, {
  19. /**
  20. * APIProperty: delay
  21. * {Integer} - Number of milliseconds between mousemoves before
  22. * the event is considered a hover. Default is 500.
  23. */
  24. delay: 500,
  25. /**
  26. * APIProperty: pixelTolerance
  27. * {Integer} - Maximum number of pixels between mousemoves for
  28. * an event to be considered a hover. Default is null.
  29. */
  30. pixelTolerance: null,
  31. /**
  32. * APIProperty: stopMove
  33. * {Boolean} - Stop other listeners from being notified on mousemoves.
  34. * Default is false.
  35. */
  36. stopMove: false,
  37. /**
  38. * Property: px
  39. * {<OpenLayers.Pixel>} - The location of the last mousemove, expressed
  40. * in pixels.
  41. */
  42. px: null,
  43. /**
  44. * Property: timerId
  45. * {Number} - The id of the timer.
  46. */
  47. timerId: null,
  48. /**
  49. * Constructor: OpenLayers.Handler.Hover
  50. * Construct a hover handler.
  51. *
  52. * Parameters:
  53. * control - {<OpenLayers.Control>} The control that initialized this
  54. * handler. The control is assumed to have a valid map property; that
  55. * map is used in the handler's own setMap method.
  56. * callbacks - {Object} An object with keys corresponding to callbacks
  57. * that will be called by the handler. The callbacks should
  58. * expect to receive a single argument, the event. Callbacks for
  59. * 'move', the mouse is moving, and 'pause', the mouse is pausing,
  60. * are supported.
  61. * options - {Object} An optional object whose properties will be set on
  62. * the handler.
  63. */
  64. initialize: function(control, callbacks, options) {
  65. OpenLayers.Handler.prototype.initialize.apply(this, arguments);
  66. },
  67. /**
  68. * Method: mousemove
  69. * Called when the mouse moves on the map.
  70. *
  71. * Parameters:
  72. * evt - {<OpenLayers.Event>}
  73. *
  74. * Returns:
  75. * {Boolean} Continue propagating this event.
  76. */
  77. mousemove: function(evt) {
  78. if(this.passesTolerance(evt.xy)) {
  79. this.clearTimer();
  80. this.callback('move', [evt]);
  81. this.px = evt.xy;
  82. // clone the evt so original properties can be accessed even
  83. // if the browser deletes them during the delay
  84. evt = OpenLayers.Util.extend({}, evt);
  85. this.timerId = window.setTimeout(
  86. OpenLayers.Function.bind(this.delayedCall, this, evt),
  87. this.delay
  88. );
  89. }
  90. return !this.stopMove;
  91. },
  92. /**
  93. * Method: mouseout
  94. * Called when the mouse goes out of the map.
  95. *
  96. * Parameters:
  97. * evt - {<OpenLayers.Event>}
  98. *
  99. * Returns:
  100. * {Boolean} Continue propagating this event.
  101. */
  102. mouseout: function(evt) {
  103. if (OpenLayers.Util.mouseLeft(evt, this.map.eventsDiv)) {
  104. this.clearTimer();
  105. this.callback('move', [evt]);
  106. }
  107. return true;
  108. },
  109. /**
  110. * Method: passesTolerance
  111. * Determine whether the mouse move is within the optional pixel tolerance.
  112. *
  113. * Parameters:
  114. * px - {<OpenLayers.Pixel>}
  115. *
  116. * Returns:
  117. * {Boolean} The mouse move is within the pixel tolerance.
  118. */
  119. passesTolerance: function(px) {
  120. var passes = true;
  121. if(this.pixelTolerance && this.px) {
  122. var dpx = Math.sqrt(
  123. Math.pow(this.px.x - px.x, 2) +
  124. Math.pow(this.px.y - px.y, 2)
  125. );
  126. if(dpx < this.pixelTolerance) {
  127. passes = false;
  128. }
  129. }
  130. return passes;
  131. },
  132. /**
  133. * Method: clearTimer
  134. * Clear the timer and set <timerId> to null.
  135. */
  136. clearTimer: function() {
  137. if(this.timerId != null) {
  138. window.clearTimeout(this.timerId);
  139. this.timerId = null;
  140. }
  141. },
  142. /**
  143. * Method: delayedCall
  144. * Triggers pause callback.
  145. *
  146. * Parameters:
  147. * evt - {<OpenLayers.Event>}
  148. */
  149. delayedCall: function(evt) {
  150. this.callback('pause', [evt]);
  151. },
  152. /**
  153. * APIMethod: deactivate
  154. * Deactivate the handler.
  155. *
  156. * Returns:
  157. * {Boolean} The handler was successfully deactivated.
  158. */
  159. deactivate: function() {
  160. var deactivated = false;
  161. if(OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) {
  162. this.clearTimer();
  163. deactivated = true;
  164. }
  165. return deactivated;
  166. },
  167. CLASS_NAME: "OpenLayers.Handler.Hover"
  168. });