Hover.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository 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. /**
  65. * Method: mousemove
  66. * Called when the mouse moves on the map.
  67. *
  68. * Parameters:
  69. * evt - {<OpenLayers.Event>}
  70. *
  71. * Returns:
  72. * {Boolean} Continue propagating this event.
  73. */
  74. mousemove: function(evt) {
  75. if(this.passesTolerance(evt.xy)) {
  76. this.clearTimer();
  77. this.callback('move', [evt]);
  78. this.px = evt.xy;
  79. // clone the evt so original properties can be accessed even
  80. // if the browser deletes them during the delay
  81. evt = OpenLayers.Util.extend({}, evt);
  82. this.timerId = window.setTimeout(
  83. OpenLayers.Function.bind(this.delayedCall, this, evt),
  84. this.delay
  85. );
  86. }
  87. return !this.stopMove;
  88. },
  89. /**
  90. * Method: mouseout
  91. * Called when the mouse goes out of the map.
  92. *
  93. * Parameters:
  94. * evt - {<OpenLayers.Event>}
  95. *
  96. * Returns:
  97. * {Boolean} Continue propagating this event.
  98. */
  99. mouseout: function(evt) {
  100. if (OpenLayers.Util.mouseLeft(evt, this.map.viewPortDiv)) {
  101. this.clearTimer();
  102. this.callback('move', [evt]);
  103. }
  104. return true;
  105. },
  106. /**
  107. * Method: passesTolerance
  108. * Determine whether the mouse move is within the optional pixel tolerance.
  109. *
  110. * Parameters:
  111. * px - {<OpenLayers.Pixel>}
  112. *
  113. * Returns:
  114. * {Boolean} The mouse move is within the pixel tolerance.
  115. */
  116. passesTolerance: function(px) {
  117. var passes = true;
  118. if(this.pixelTolerance && this.px) {
  119. var dpx = Math.sqrt(
  120. Math.pow(this.px.x - px.x, 2) +
  121. Math.pow(this.px.y - px.y, 2)
  122. );
  123. if(dpx < this.pixelTolerance) {
  124. passes = false;
  125. }
  126. }
  127. return passes;
  128. },
  129. /**
  130. * Method: clearTimer
  131. * Clear the timer and set <timerId> to null.
  132. */
  133. clearTimer: function() {
  134. if(this.timerId != null) {
  135. window.clearTimeout(this.timerId);
  136. this.timerId = null;
  137. }
  138. },
  139. /**
  140. * Method: delayedCall
  141. * Triggers pause callback.
  142. *
  143. * Parameters:
  144. * evt - {<OpenLayers.Event>}
  145. */
  146. delayedCall: function(evt) {
  147. this.callback('pause', [evt]);
  148. },
  149. /**
  150. * APIMethod: deactivate
  151. * Deactivate the handler.
  152. *
  153. * Returns:
  154. * {Boolean} The handler was successfully deactivated.
  155. */
  156. deactivate: function() {
  157. var deactivated = false;
  158. if(OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) {
  159. this.clearTimer();
  160. deactivated = true;
  161. }
  162. return deactivated;
  163. },
  164. CLASS_NAME: "OpenLayers.Handler.Hover"
  165. });