Navigation.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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/Control/ZoomBox.js
  7. * @requires OpenLayers/Control/DragPan.js
  8. * @requires OpenLayers/Handler/MouseWheel.js
  9. * @requires OpenLayers/Handler/Click.js
  10. */
  11. /**
  12. * Class: OpenLayers.Control.Navigation
  13. * The navigation control handles map browsing with mouse events (dragging,
  14. * double-clicking, and scrolling the wheel). Create a new navigation
  15. * control with the <OpenLayers.Control.Navigation> control.
  16. *
  17. * Note that this control is added to the map by default (if no controls
  18. * array is sent in the options object to the <OpenLayers.Map>
  19. * constructor).
  20. *
  21. * Inherits:
  22. * - <OpenLayers.Control>
  23. */
  24. OpenLayers.Control.Navigation = OpenLayers.Class(OpenLayers.Control, {
  25. /**
  26. * Property: dragPan
  27. * {<OpenLayers.Control.DragPan>}
  28. */
  29. dragPan: null,
  30. /**
  31. * APIProperty: dragPanOptions
  32. * {Object} Options passed to the DragPan control.
  33. */
  34. dragPanOptions: null,
  35. /**
  36. * Property: pinchZoom
  37. * {<OpenLayers.Control.PinchZoom>}
  38. */
  39. pinchZoom: null,
  40. /**
  41. * APIProperty: pinchZoomOptions
  42. * {Object} Options passed to the PinchZoom control.
  43. */
  44. pinchZoomOptions: null,
  45. /**
  46. * APIProperty: documentDrag
  47. * {Boolean} Allow panning of the map by dragging outside map viewport.
  48. * Default is false.
  49. */
  50. documentDrag: false,
  51. /**
  52. * Property: zoomBox
  53. * {<OpenLayers.Control.ZoomBox>}
  54. */
  55. zoomBox: null,
  56. /**
  57. * APIProperty: zoomBoxEnabled
  58. * {Boolean} Whether the user can draw a box to zoom
  59. */
  60. zoomBoxEnabled: true,
  61. /**
  62. * APIProperty: zoomWheelEnabled
  63. * {Boolean} Whether the mousewheel should zoom the map
  64. */
  65. zoomWheelEnabled: true,
  66. /**
  67. * Property: mouseWheelOptions
  68. * {Object} Options passed to the MouseWheel control (only useful if
  69. * <zoomWheelEnabled> is set to true). Default is no options for maps
  70. * with fractionalZoom set to true, otherwise
  71. * {cumulative: false, interval: 50, maxDelta: 6}
  72. */
  73. mouseWheelOptions: null,
  74. /**
  75. * APIProperty: handleRightClicks
  76. * {Boolean} Whether or not to handle right clicks. Default is false.
  77. */
  78. handleRightClicks: false,
  79. /**
  80. * APIProperty: zoomBoxKeyMask
  81. * {Integer} <OpenLayers.Handler> key code of the key, which has to be
  82. * pressed, while drawing the zoom box with the mouse on the screen.
  83. * You should probably set handleRightClicks to true if you use this
  84. * with MOD_CTRL, to disable the context menu for machines which use
  85. * CTRL-Click as a right click.
  86. * Default: <OpenLayers.Handler.MOD_SHIFT>
  87. */
  88. zoomBoxKeyMask: OpenLayers.Handler.MOD_SHIFT,
  89. /**
  90. * APIProperty: autoActivate
  91. * {Boolean} Activate the control when it is added to a map. Default is
  92. * true.
  93. */
  94. autoActivate: true,
  95. /**
  96. * Constructor: OpenLayers.Control.Navigation
  97. * Create a new navigation control
  98. *
  99. * Parameters:
  100. * options - {Object} An optional object whose properties will be set on
  101. * the control
  102. */
  103. initialize: function(options) {
  104. this.handlers = {};
  105. OpenLayers.Control.prototype.initialize.apply(this, arguments);
  106. },
  107. /**
  108. * Method: destroy
  109. * The destroy method is used to perform any clean up before the control
  110. * is dereferenced. Typically this is where event listeners are removed
  111. * to prevent memory leaks.
  112. */
  113. destroy: function() {
  114. this.deactivate();
  115. if (this.dragPan) {
  116. this.dragPan.destroy();
  117. }
  118. this.dragPan = null;
  119. if (this.zoomBox) {
  120. this.zoomBox.destroy();
  121. }
  122. this.zoomBox = null;
  123. if (this.pinchZoom) {
  124. this.pinchZoom.destroy();
  125. }
  126. this.pinchZoom = null;
  127. OpenLayers.Control.prototype.destroy.apply(this,arguments);
  128. },
  129. /**
  130. * Method: activate
  131. */
  132. activate: function() {
  133. this.dragPan.activate();
  134. if (this.zoomWheelEnabled) {
  135. this.handlers.wheel.activate();
  136. }
  137. this.handlers.click.activate();
  138. if (this.zoomBoxEnabled) {
  139. this.zoomBox.activate();
  140. }
  141. if (this.pinchZoom) {
  142. this.pinchZoom.activate();
  143. }
  144. return OpenLayers.Control.prototype.activate.apply(this,arguments);
  145. },
  146. /**
  147. * Method: deactivate
  148. */
  149. deactivate: function() {
  150. if (this.pinchZoom) {
  151. this.pinchZoom.deactivate();
  152. }
  153. this.zoomBox.deactivate();
  154. this.dragPan.deactivate();
  155. this.handlers.click.deactivate();
  156. this.handlers.wheel.deactivate();
  157. return OpenLayers.Control.prototype.deactivate.apply(this,arguments);
  158. },
  159. /**
  160. * Method: draw
  161. */
  162. draw: function() {
  163. // disable right mouse context menu for support of right click events
  164. if (this.handleRightClicks) {
  165. this.map.viewPortDiv.oncontextmenu = OpenLayers.Function.False;
  166. }
  167. var clickCallbacks = {
  168. 'click': this.defaultClick,
  169. 'dblclick': this.defaultDblClick,
  170. 'dblrightclick': this.defaultDblRightClick
  171. };
  172. var clickOptions = {
  173. 'double': true,
  174. 'stopDouble': true
  175. };
  176. this.handlers.click = new OpenLayers.Handler.Click(
  177. this, clickCallbacks, clickOptions
  178. );
  179. this.dragPan = new OpenLayers.Control.DragPan(
  180. OpenLayers.Util.extend({
  181. map: this.map,
  182. documentDrag: this.documentDrag
  183. }, this.dragPanOptions)
  184. );
  185. this.zoomBox = new OpenLayers.Control.ZoomBox(
  186. {map: this.map, keyMask: this.zoomBoxKeyMask});
  187. this.dragPan.draw();
  188. this.zoomBox.draw();
  189. var wheelOptions = this.map.fractionalZoom ? {} : {
  190. cumulative: false,
  191. interval: 50,
  192. maxDelta: 6
  193. };
  194. this.handlers.wheel = new OpenLayers.Handler.MouseWheel(
  195. this, {up : this.wheelUp, down: this.wheelDown},
  196. OpenLayers.Util.extend(wheelOptions, this.mouseWheelOptions)
  197. );
  198. if (OpenLayers.Control.PinchZoom) {
  199. this.pinchZoom = new OpenLayers.Control.PinchZoom(
  200. OpenLayers.Util.extend(
  201. {map: this.map}, this.pinchZoomOptions));
  202. }
  203. },
  204. /**
  205. * Method: defaultClick
  206. *
  207. * Parameters:
  208. * evt - {Event}
  209. */
  210. defaultClick: function (evt) {
  211. if (evt.lastTouches && evt.lastTouches.length == 2) {
  212. this.map.zoomOut();
  213. }
  214. },
  215. /**
  216. * Method: defaultDblClick
  217. *
  218. * Parameters:
  219. * evt - {Event}
  220. */
  221. defaultDblClick: function (evt) {
  222. this.map.zoomTo(this.map.zoom + 1, evt.xy);
  223. },
  224. /**
  225. * Method: defaultDblRightClick
  226. *
  227. * Parameters:
  228. * evt - {Event}
  229. */
  230. defaultDblRightClick: function (evt) {
  231. this.map.zoomTo(this.map.zoom - 1, evt.xy);
  232. },
  233. /**
  234. * Method: wheelChange
  235. *
  236. * Parameters:
  237. * evt - {Event}
  238. * deltaZ - {Integer}
  239. */
  240. wheelChange: function(evt, deltaZ) {
  241. if (!this.map.fractionalZoom) {
  242. deltaZ = Math.round(deltaZ);
  243. }
  244. var currentZoom = this.map.getZoom(),
  245. newZoom = currentZoom + deltaZ;
  246. newZoom = Math.max(newZoom, 0);
  247. newZoom = Math.min(newZoom, this.map.getNumZoomLevels());
  248. if (newZoom === currentZoom) {
  249. return;
  250. }
  251. this.map.zoomTo(newZoom, evt.xy);
  252. },
  253. /**
  254. * Method: wheelUp
  255. * User spun scroll wheel up
  256. *
  257. * Parameters:
  258. * evt - {Event}
  259. * delta - {Integer}
  260. */
  261. wheelUp: function(evt, delta) {
  262. this.wheelChange(evt, delta || 1);
  263. },
  264. /**
  265. * Method: wheelDown
  266. * User spun scroll wheel down
  267. *
  268. * Parameters:
  269. * evt - {Event}
  270. * delta - {Integer}
  271. */
  272. wheelDown: function(evt, delta) {
  273. this.wheelChange(evt, delta || -1);
  274. },
  275. /**
  276. * Method: disableZoomBox
  277. */
  278. disableZoomBox : function() {
  279. this.zoomBoxEnabled = false;
  280. this.zoomBox.deactivate();
  281. },
  282. /**
  283. * Method: enableZoomBox
  284. */
  285. enableZoomBox : function() {
  286. this.zoomBoxEnabled = true;
  287. if (this.active) {
  288. this.zoomBox.activate();
  289. }
  290. },
  291. /**
  292. * Method: disableZoomWheel
  293. */
  294. disableZoomWheel : function() {
  295. this.zoomWheelEnabled = false;
  296. this.handlers.wheel.deactivate();
  297. },
  298. /**
  299. * Method: enableZoomWheel
  300. */
  301. enableZoomWheel : function() {
  302. this.zoomWheelEnabled = true;
  303. if (this.active) {
  304. this.handlers.wheel.activate();
  305. }
  306. },
  307. CLASS_NAME: "OpenLayers.Control.Navigation"
  308. });