MouseDefaults.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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/Control.js
  7. */
  8. /**
  9. * Class: OpenLayers.Control.MouseDefaults
  10. * This class is DEPRECATED in 2.4 and will be removed by 3.0.
  11. * If you need this functionality, use <OpenLayers.Control.Navigation>
  12. * instead!!!
  13. *
  14. * This class is DEPRECATED in 2.4 and will be removed by 3.0.
  15. * If you need this functionality, use Control.Navigation instead!!!
  16. *
  17. * Inherits from:
  18. * - <OpenLayers.Control>
  19. */
  20. OpenLayers.Control.MouseDefaults = OpenLayers.Class(OpenLayers.Control, {
  21. /** WARNING WARNING WARNING!!!
  22. This class is DEPRECATED in 2.4 and will be removed by 3.0.
  23. If you need this functionality, use Control.Navigation instead!!! */
  24. /**
  25. * Property: performedDrag
  26. * {Boolean}
  27. */
  28. performedDrag: false,
  29. /**
  30. * Property: wheelObserver
  31. * {Function}
  32. */
  33. wheelObserver: null,
  34. /**
  35. * Constructor: OpenLayers.Control.MouseDefaults
  36. */
  37. initialize: function() {
  38. OpenLayers.Control.prototype.initialize.apply(this, arguments);
  39. },
  40. /**
  41. * APIMethod: destroy
  42. */
  43. destroy: function() {
  44. if (this.handler) {
  45. this.handler.destroy();
  46. }
  47. this.handler = null;
  48. this.map.events.un({
  49. "click": this.defaultClick,
  50. "dblclick": this.defaultDblClick,
  51. "mousedown": this.defaultMouseDown,
  52. "mouseup": this.defaultMouseUp,
  53. "mousemove": this.defaultMouseMove,
  54. "mouseout": this.defaultMouseOut,
  55. scope: this
  56. });
  57. //unregister mousewheel events specifically on the window and document
  58. OpenLayers.Event.stopObserving(window, "DOMMouseScroll",
  59. this.wheelObserver);
  60. OpenLayers.Event.stopObserving(window, "mousewheel",
  61. this.wheelObserver);
  62. OpenLayers.Event.stopObserving(document, "mousewheel",
  63. this.wheelObserver);
  64. this.wheelObserver = null;
  65. OpenLayers.Control.prototype.destroy.apply(this, arguments);
  66. },
  67. /**
  68. * Method: draw
  69. */
  70. draw: function() {
  71. this.map.events.on({
  72. "click": this.defaultClick,
  73. "dblclick": this.defaultDblClick,
  74. "mousedown": this.defaultMouseDown,
  75. "mouseup": this.defaultMouseUp,
  76. "mousemove": this.defaultMouseMove,
  77. "mouseout": this.defaultMouseOut,
  78. scope: this
  79. });
  80. this.registerWheelEvents();
  81. },
  82. /**
  83. * Method: registerWheelEvents
  84. */
  85. registerWheelEvents: function() {
  86. this.wheelObserver = OpenLayers.Function.bindAsEventListener(
  87. this.onWheelEvent, this
  88. );
  89. //register mousewheel events specifically on the window and document
  90. OpenLayers.Event.observe(window, "DOMMouseScroll", this.wheelObserver);
  91. OpenLayers.Event.observe(window, "mousewheel", this.wheelObserver);
  92. OpenLayers.Event.observe(document, "mousewheel", this.wheelObserver);
  93. },
  94. /**
  95. * Method: defaultClick
  96. *
  97. * Parameters:
  98. * evt - {Event}
  99. *
  100. * Returns:
  101. * {Boolean}
  102. */
  103. defaultClick: function (evt) {
  104. if (!OpenLayers.Event.isLeftClick(evt)) {
  105. return;
  106. }
  107. var notAfterDrag = !this.performedDrag;
  108. this.performedDrag = false;
  109. return notAfterDrag;
  110. },
  111. /**
  112. * Method: defaultDblClick
  113. *
  114. * Parameters:
  115. * evt - {Event}
  116. */
  117. defaultDblClick: function (evt) {
  118. var newCenter = this.map.getLonLatFromViewPortPx( evt.xy );
  119. this.map.setCenter(newCenter, this.map.zoom + 1);
  120. OpenLayers.Event.stop(evt);
  121. return false;
  122. },
  123. /**
  124. * Method: defaultMouseDown
  125. *
  126. * Parameters:
  127. * evt - {Event}
  128. */
  129. defaultMouseDown: function (evt) {
  130. if (!OpenLayers.Event.isLeftClick(evt)) {
  131. return;
  132. }
  133. this.mouseDragStart = evt.xy.clone();
  134. this.performedDrag = false;
  135. if (evt.shiftKey) {
  136. this.map.div.style.cursor = "crosshair";
  137. this.zoomBox = OpenLayers.Util.createDiv('zoomBox',
  138. this.mouseDragStart,
  139. null,
  140. null,
  141. "absolute",
  142. "2px solid red");
  143. this.zoomBox.style.backgroundColor = "white";
  144. this.zoomBox.style.filter = "alpha(opacity=50)"; // IE
  145. this.zoomBox.style.opacity = "0.50";
  146. this.zoomBox.style.fontSize = "1px";
  147. this.zoomBox.style.zIndex = this.map.Z_INDEX_BASE["Popup"] - 1;
  148. this.map.eventsDiv.appendChild(this.zoomBox);
  149. }
  150. document.onselectstart = OpenLayers.Function.False;
  151. OpenLayers.Event.stop(evt);
  152. },
  153. /**
  154. * Method: defaultMouseMove
  155. *
  156. * Parameters:
  157. * evt - {Event}
  158. */
  159. defaultMouseMove: function (evt) {
  160. // record the mouse position, used in onWheelEvent
  161. this.mousePosition = evt.xy.clone();
  162. if (this.mouseDragStart != null) {
  163. if (this.zoomBox) {
  164. var deltaX = Math.abs(this.mouseDragStart.x - evt.xy.x);
  165. var deltaY = Math.abs(this.mouseDragStart.y - evt.xy.y);
  166. this.zoomBox.style.width = Math.max(1, deltaX) + "px";
  167. this.zoomBox.style.height = Math.max(1, deltaY) + "px";
  168. if (evt.xy.x < this.mouseDragStart.x) {
  169. this.zoomBox.style.left = evt.xy.x+"px";
  170. }
  171. if (evt.xy.y < this.mouseDragStart.y) {
  172. this.zoomBox.style.top = evt.xy.y+"px";
  173. }
  174. } else {
  175. var deltaX = this.mouseDragStart.x - evt.xy.x;
  176. var deltaY = this.mouseDragStart.y - evt.xy.y;
  177. var size = this.map.getSize();
  178. var newXY = new OpenLayers.Pixel(size.w / 2 + deltaX,
  179. size.h / 2 + deltaY);
  180. var newCenter = this.map.getLonLatFromViewPortPx( newXY );
  181. this.map.setCenter(newCenter, null, true);
  182. this.mouseDragStart = evt.xy.clone();
  183. this.map.div.style.cursor = "move";
  184. }
  185. this.performedDrag = true;
  186. }
  187. },
  188. /**
  189. * Method: defaultMouseUp
  190. *
  191. * Parameters:
  192. * evt - {<OpenLayers.Event>}
  193. */
  194. defaultMouseUp: function (evt) {
  195. if (!OpenLayers.Event.isLeftClick(evt)) {
  196. return;
  197. }
  198. if (this.zoomBox) {
  199. this.zoomBoxEnd(evt);
  200. } else {
  201. if (this.performedDrag) {
  202. this.map.setCenter(this.map.center);
  203. }
  204. }
  205. document.onselectstart=null;
  206. this.mouseDragStart = null;
  207. this.map.div.style.cursor = "";
  208. },
  209. /**
  210. * Method: defaultMouseOut
  211. *
  212. * Parameters:
  213. * evt - {Event}
  214. */
  215. defaultMouseOut: function (evt) {
  216. if (this.mouseDragStart != null &&
  217. OpenLayers.Util.mouseLeft(evt, this.map.eventsDiv)) {
  218. if (this.zoomBox) {
  219. this.removeZoomBox();
  220. }
  221. this.mouseDragStart = null;
  222. }
  223. },
  224. /**
  225. * Method: defaultWheelUp
  226. * User spun scroll wheel up
  227. *
  228. */
  229. defaultWheelUp: function(evt) {
  230. if (this.map.getZoom() <= this.map.getNumZoomLevels()) {
  231. this.map.setCenter(this.map.getLonLatFromPixel(evt.xy),
  232. this.map.getZoom() + 1);
  233. }
  234. },
  235. /**
  236. * Method: defaultWheelDown
  237. * User spun scroll wheel down
  238. */
  239. defaultWheelDown: function(evt) {
  240. if (this.map.getZoom() > 0) {
  241. this.map.setCenter(this.map.getLonLatFromPixel(evt.xy),
  242. this.map.getZoom() - 1);
  243. }
  244. },
  245. /**
  246. * Method: zoomBoxEnd
  247. * Zoombox function.
  248. */
  249. zoomBoxEnd: function(evt) {
  250. if (this.mouseDragStart != null) {
  251. if (Math.abs(this.mouseDragStart.x - evt.xy.x) > 5 ||
  252. Math.abs(this.mouseDragStart.y - evt.xy.y) > 5) {
  253. var start = this.map.getLonLatFromViewPortPx( this.mouseDragStart );
  254. var end = this.map.getLonLatFromViewPortPx( evt.xy );
  255. var top = Math.max(start.lat, end.lat);
  256. var bottom = Math.min(start.lat, end.lat);
  257. var left = Math.min(start.lon, end.lon);
  258. var right = Math.max(start.lon, end.lon);
  259. var bounds = new OpenLayers.Bounds(left, bottom, right, top);
  260. this.map.zoomToExtent(bounds);
  261. } else {
  262. var end = this.map.getLonLatFromViewPortPx( evt.xy );
  263. this.map.setCenter(new OpenLayers.LonLat(
  264. (end.lon),
  265. (end.lat)
  266. ), this.map.getZoom() + 1);
  267. }
  268. this.removeZoomBox();
  269. }
  270. },
  271. /**
  272. * Method: removeZoomBox
  273. * Remove the zoombox from the screen and nullify our reference to it.
  274. */
  275. removeZoomBox: function() {
  276. this.map.eventsDiv.removeChild(this.zoomBox);
  277. this.zoomBox = null;
  278. },
  279. /**
  280. * Mouse ScrollWheel code thanks to http://adomas.org/javascript-mouse-wheel/
  281. */
  282. /**
  283. * Method: onWheelEvent
  284. * Catch the wheel event and handle it xbrowserly
  285. *
  286. * Parameters:
  287. * e - {Event}
  288. */
  289. onWheelEvent: function(e){
  290. // first determine whether or not the wheeling was inside the map
  291. var inMap = false;
  292. var elem = OpenLayers.Event.element(e);
  293. while(elem != null) {
  294. if (this.map && elem == this.map.div) {
  295. inMap = true;
  296. break;
  297. }
  298. elem = elem.parentNode;
  299. }
  300. if (inMap) {
  301. var delta = 0;
  302. if (!e) {
  303. e = window.event;
  304. }
  305. if (e.wheelDelta) {
  306. delta = e.wheelDelta/120;
  307. if (window.opera && window.opera.version() < 9.2) {
  308. delta = -delta;
  309. }
  310. } else if (e.detail) {
  311. delta = -e.detail / 3;
  312. }
  313. if (delta) {
  314. // add the mouse position to the event because mozilla has a bug
  315. // with clientX and clientY (see https://bugzilla.mozilla.org/show_bug.cgi?id=352179)
  316. // getLonLatFromViewPortPx(e) returns wrong values
  317. e.xy = this.mousePosition;
  318. if (delta < 0) {
  319. this.defaultWheelDown(e);
  320. } else {
  321. this.defaultWheelUp(e);
  322. }
  323. }
  324. //only wheel the map, not the window
  325. OpenLayers.Event.stop(e);
  326. }
  327. },
  328. CLASS_NAME: "OpenLayers.Control.MouseDefaults"
  329. });