MouseWheel.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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.MouseWheel
  10. * Handler for wheel up/down events.
  11. *
  12. * Inherits from:
  13. * - <OpenLayers.Handler>
  14. */
  15. OpenLayers.Handler.MouseWheel = OpenLayers.Class(OpenLayers.Handler, {
  16. /**
  17. * Property: wheelListener
  18. * {function}
  19. */
  20. wheelListener: null,
  21. /**
  22. * Property: interval
  23. * {Integer} In order to increase server performance, an interval (in
  24. * milliseconds) can be set to reduce the number of up/down events
  25. * called. If set, a new up/down event will not be set until the
  26. * interval has passed.
  27. * Defaults to 0, meaning no interval.
  28. */
  29. interval: 0,
  30. /**
  31. * Property: maxDelta
  32. * {Integer} Maximum delta to collect before breaking from the current
  33. * interval. In cumulative mode, this also limits the maximum delta
  34. * returned from the handler. Default is Number.POSITIVE_INFINITY.
  35. */
  36. maxDelta: Number.POSITIVE_INFINITY,
  37. /**
  38. * Property: delta
  39. * {Integer} When interval is set, delta collects the mousewheel z-deltas
  40. * of the events that occur within the interval.
  41. * See also the cumulative option
  42. */
  43. delta: 0,
  44. /**
  45. * Property: cumulative
  46. * {Boolean} When interval is set: true to collect all the mousewheel
  47. * z-deltas, false to only record the delta direction (positive or
  48. * negative)
  49. */
  50. cumulative: true,
  51. /**
  52. * Constructor: OpenLayers.Handler.MouseWheel
  53. *
  54. * Parameters:
  55. * control - {<OpenLayers.Control>}
  56. * callbacks - {Object} An object containing a single function to be
  57. * called when the drag operation is finished.
  58. * The callback should expect to recieve a single
  59. * argument, the point geometry.
  60. * options - {Object}
  61. */
  62. initialize: function(control, callbacks, options) {
  63. OpenLayers.Handler.prototype.initialize.apply(this, arguments);
  64. this.wheelListener = OpenLayers.Function.bindAsEventListener(
  65. this.onWheelEvent, this
  66. );
  67. },
  68. /**
  69. * Method: destroy
  70. */
  71. destroy: function() {
  72. OpenLayers.Handler.prototype.destroy.apply(this, arguments);
  73. this.wheelListener = null;
  74. },
  75. /**
  76. * Mouse ScrollWheel code thanks to http://adomas.org/javascript-mouse-wheel/
  77. */
  78. /**
  79. * Method: onWheelEvent
  80. * Catch the wheel event and handle it xbrowserly
  81. *
  82. * Parameters:
  83. * e - {Event}
  84. */
  85. onWheelEvent: function(e){
  86. // make sure we have a map and check keyboard modifiers
  87. if (!this.map || !this.checkModifiers(e)) {
  88. return;
  89. }
  90. // Ride up the element's DOM hierarchy to determine if it or any of
  91. // its ancestors was:
  92. // * specifically marked as scrollable (CSS overflow property)
  93. // * one of our layer divs or a div marked as scrollable
  94. // ('olScrollable' CSS class)
  95. // * the map div
  96. //
  97. var overScrollableDiv = false;
  98. var allowScroll = false;
  99. var overMapDiv = false;
  100. var elem = OpenLayers.Event.element(e);
  101. while((elem != null) && !overMapDiv && !overScrollableDiv) {
  102. if (!overScrollableDiv) {
  103. try {
  104. var overflow;
  105. if (elem.currentStyle) {
  106. overflow = elem.currentStyle["overflow"];
  107. } else {
  108. var style =
  109. document.defaultView.getComputedStyle(elem, null);
  110. overflow = style.getPropertyValue("overflow");
  111. }
  112. overScrollableDiv = ( overflow &&
  113. (overflow == "auto") || (overflow == "scroll") );
  114. } catch(err) {
  115. //sometimes when scrolling in a popup, this causes
  116. // obscure browser error
  117. }
  118. }
  119. if (!allowScroll) {
  120. allowScroll = OpenLayers.Element.hasClass(elem, 'olScrollable');
  121. if (!allowScroll) {
  122. for (var i = 0, len = this.map.layers.length; i < len; i++) {
  123. // Are we in the layer div? Note that we have two cases
  124. // here: one is to catch EventPane layers, which have a
  125. // pane above the layer (layer.pane)
  126. var layer = this.map.layers[i];
  127. if (elem == layer.div || elem == layer.pane) {
  128. allowScroll = true;
  129. break;
  130. }
  131. }
  132. }
  133. }
  134. overMapDiv = (elem == this.map.div);
  135. elem = elem.parentNode;
  136. }
  137. // Logic below is the following:
  138. //
  139. // If we are over a scrollable div or not over the map div:
  140. // * do nothing (let the browser handle scrolling)
  141. //
  142. // otherwise
  143. //
  144. // If we are over the layer div or a 'olScrollable' div:
  145. // * zoom/in out
  146. // then
  147. // * kill event (so as not to also scroll the page after zooming)
  148. //
  149. // otherwise
  150. //
  151. // Kill the event (dont scroll the page if we wheel over the
  152. // layerswitcher or the pan/zoom control)
  153. //
  154. if (!overScrollableDiv && overMapDiv) {
  155. if (allowScroll) {
  156. var delta = 0;
  157. if (e.wheelDelta) {
  158. delta = e.wheelDelta;
  159. if (delta % 160 === 0) {
  160. // opera have steps of 160 instead of 120
  161. delta = delta * 0.75;
  162. }
  163. delta = delta / 120;
  164. } else if (e.detail) {
  165. // detail in Firefox on OS X is 1/3 of Windows
  166. // so force delta 1 / -1
  167. delta = - (e.detail / Math.abs(e.detail));
  168. }
  169. this.delta += delta;
  170. window.clearTimeout(this._timeoutId);
  171. if(this.interval && Math.abs(this.delta) < this.maxDelta) {
  172. // store e because window.event might change during delay
  173. var evt = OpenLayers.Util.extend({}, e);
  174. this._timeoutId = window.setTimeout(
  175. OpenLayers.Function.bind(function(){
  176. this.wheelZoom(evt);
  177. }, this),
  178. this.interval
  179. );
  180. } else {
  181. this.wheelZoom(e);
  182. }
  183. }
  184. OpenLayers.Event.stop(e);
  185. }
  186. },
  187. /**
  188. * Method: wheelZoom
  189. * Given the wheel event, we carry out the appropriate zooming in or out,
  190. * based on the 'wheelDelta' or 'detail' property of the event.
  191. *
  192. * Parameters:
  193. * e - {Event}
  194. */
  195. wheelZoom: function(e) {
  196. var delta = this.delta;
  197. this.delta = 0;
  198. if (delta) {
  199. e.xy = this.map.events.getMousePosition(e);
  200. if (delta < 0) {
  201. this.callback("down",
  202. [e, this.cumulative ? Math.max(-this.maxDelta, delta) : -1]);
  203. } else {
  204. this.callback("up",
  205. [e, this.cumulative ? Math.min(this.maxDelta, delta) : 1]);
  206. }
  207. }
  208. },
  209. /**
  210. * Method: activate
  211. */
  212. activate: function (evt) {
  213. if (OpenLayers.Handler.prototype.activate.apply(this, arguments)) {
  214. //register mousewheel events specifically on the window and document
  215. var wheelListener = this.wheelListener;
  216. OpenLayers.Event.observe(window, "DOMMouseScroll", wheelListener);
  217. OpenLayers.Event.observe(window, "mousewheel", wheelListener);
  218. OpenLayers.Event.observe(document, "mousewheel", wheelListener);
  219. return true;
  220. } else {
  221. return false;
  222. }
  223. },
  224. /**
  225. * Method: deactivate
  226. */
  227. deactivate: function (evt) {
  228. if (OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) {
  229. // unregister mousewheel events specifically on the window and document
  230. var wheelListener = this.wheelListener;
  231. OpenLayers.Event.stopObserving(window, "DOMMouseScroll", wheelListener);
  232. OpenLayers.Event.stopObserving(window, "mousewheel", wheelListener);
  233. OpenLayers.Event.stopObserving(document, "mousewheel", wheelListener);
  234. return true;
  235. } else {
  236. return false;
  237. }
  238. },
  239. CLASS_NAME: "OpenLayers.Handler.MouseWheel"
  240. });