Box.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. * @requires OpenLayers/Handler/Drag.js
  8. */
  9. /**
  10. * Class: OpenLayers.Handler.Box
  11. * Handler for dragging a rectangle across the map. Box is displayed
  12. * on mouse down, moves on mouse move, and is finished on mouse up.
  13. *
  14. * Inherits from:
  15. * - <OpenLayers.Handler>
  16. */
  17. OpenLayers.Handler.Box = OpenLayers.Class(OpenLayers.Handler, {
  18. /**
  19. * Property: dragHandler
  20. * {<OpenLayers.Handler.Drag>}
  21. */
  22. dragHandler: null,
  23. /**
  24. * APIProperty: boxDivClassName
  25. * {String} The CSS class to use for drawing the box. Default is
  26. * olHandlerBoxZoomBox
  27. */
  28. boxDivClassName: 'olHandlerBoxZoomBox',
  29. /**
  30. * Property: boxOffsets
  31. * {Object} Caches box offsets from css. This is used by the getBoxOffsets
  32. * method.
  33. */
  34. boxOffsets: null,
  35. /**
  36. * Constructor: OpenLayers.Handler.Box
  37. *
  38. * Parameters:
  39. * control - {<OpenLayers.Control>}
  40. * callbacks - {Object} An object with a properties whose values are
  41. * functions. Various callbacks described below.
  42. * options - {Object}
  43. *
  44. * Named callbacks:
  45. * start - Called when the box drag operation starts.
  46. * done - Called when the box drag operation is finished.
  47. * The callback should expect to receive a single argument, the box
  48. * bounds or a pixel. If the box dragging didn't span more than a 5
  49. * pixel distance, a pixel will be returned instead of a bounds object.
  50. */
  51. initialize: function(control, callbacks, options) {
  52. OpenLayers.Handler.prototype.initialize.apply(this, arguments);
  53. this.dragHandler = new OpenLayers.Handler.Drag(
  54. this,
  55. {
  56. down: this.startBox,
  57. move: this.moveBox,
  58. out: this.removeBox,
  59. up: this.endBox
  60. },
  61. {keyMask: this.keyMask}
  62. );
  63. },
  64. /**
  65. * Method: destroy
  66. */
  67. destroy: function() {
  68. OpenLayers.Handler.prototype.destroy.apply(this, arguments);
  69. if (this.dragHandler) {
  70. this.dragHandler.destroy();
  71. this.dragHandler = null;
  72. }
  73. },
  74. /**
  75. * Method: setMap
  76. */
  77. setMap: function (map) {
  78. OpenLayers.Handler.prototype.setMap.apply(this, arguments);
  79. if (this.dragHandler) {
  80. this.dragHandler.setMap(map);
  81. }
  82. },
  83. /**
  84. * Method: startBox
  85. *
  86. * Parameters:
  87. * xy - {<OpenLayers.Pixel>}
  88. */
  89. startBox: function (xy) {
  90. this.callback("start", []);
  91. this.zoomBox = OpenLayers.Util.createDiv('zoomBox', {
  92. x: -9999, y: -9999
  93. });
  94. this.zoomBox.className = this.boxDivClassName;
  95. this.zoomBox.style.zIndex = this.map.Z_INDEX_BASE["Popup"] - 1;
  96. this.map.viewPortDiv.appendChild(this.zoomBox);
  97. OpenLayers.Element.addClass(
  98. this.map.viewPortDiv, "olDrawBox"
  99. );
  100. },
  101. /**
  102. * Method: moveBox
  103. */
  104. moveBox: function (xy) {
  105. var startX = this.dragHandler.start.x;
  106. var startY = this.dragHandler.start.y;
  107. var deltaX = Math.abs(startX - xy.x);
  108. var deltaY = Math.abs(startY - xy.y);
  109. var offset = this.getBoxOffsets();
  110. this.zoomBox.style.width = (deltaX + offset.width + 1) + "px";
  111. this.zoomBox.style.height = (deltaY + offset.height + 1) + "px";
  112. this.zoomBox.style.left = (xy.x < startX ?
  113. startX - deltaX - offset.left : startX - offset.left) + "px";
  114. this.zoomBox.style.top = (xy.y < startY ?
  115. startY - deltaY - offset.top : startY - offset.top) + "px";
  116. },
  117. /**
  118. * Method: endBox
  119. */
  120. endBox: function(end) {
  121. var result;
  122. if (Math.abs(this.dragHandler.start.x - end.x) > 5 ||
  123. Math.abs(this.dragHandler.start.y - end.y) > 5) {
  124. var start = this.dragHandler.start;
  125. var top = Math.min(start.y, end.y);
  126. var bottom = Math.max(start.y, end.y);
  127. var left = Math.min(start.x, end.x);
  128. var right = Math.max(start.x, end.x);
  129. result = new OpenLayers.Bounds(left, bottom, right, top);
  130. } else {
  131. result = this.dragHandler.start.clone(); // i.e. OL.Pixel
  132. }
  133. this.removeBox();
  134. this.callback("done", [result]);
  135. },
  136. /**
  137. * Method: removeBox
  138. * Remove the zoombox from the screen and nullify our reference to it.
  139. */
  140. removeBox: function() {
  141. this.map.viewPortDiv.removeChild(this.zoomBox);
  142. this.zoomBox = null;
  143. this.boxOffsets = null;
  144. OpenLayers.Element.removeClass(
  145. this.map.viewPortDiv, "olDrawBox"
  146. );
  147. },
  148. /**
  149. * Method: activate
  150. */
  151. activate: function () {
  152. if (OpenLayers.Handler.prototype.activate.apply(this, arguments)) {
  153. this.dragHandler.activate();
  154. return true;
  155. } else {
  156. return false;
  157. }
  158. },
  159. /**
  160. * Method: deactivate
  161. */
  162. deactivate: function () {
  163. if (OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) {
  164. if (this.dragHandler.deactivate()) {
  165. if (this.zoomBox) {
  166. this.removeBox();
  167. }
  168. }
  169. return true;
  170. } else {
  171. return false;
  172. }
  173. },
  174. /**
  175. * Method: getBoxOffsets
  176. * Determines border offsets for a box, according to the box model.
  177. *
  178. * Returns:
  179. * {Object} an object with the following offsets:
  180. * - left
  181. * - right
  182. * - top
  183. * - bottom
  184. * - width
  185. * - height
  186. */
  187. getBoxOffsets: function() {
  188. if (!this.boxOffsets) {
  189. // Determine the box model. If the testDiv's clientWidth is 3, then
  190. // the borders are outside and we are dealing with the w3c box
  191. // model. Otherwise, the browser uses the traditional box model and
  192. // the borders are inside the box bounds, leaving us with a
  193. // clientWidth of 1.
  194. var testDiv = document.createElement("div");
  195. //testDiv.style.visibility = "hidden";
  196. testDiv.style.position = "absolute";
  197. testDiv.style.border = "1px solid black";
  198. testDiv.style.width = "3px";
  199. document.body.appendChild(testDiv);
  200. var w3cBoxModel = testDiv.clientWidth == 3;
  201. document.body.removeChild(testDiv);
  202. var left = parseInt(OpenLayers.Element.getStyle(this.zoomBox,
  203. "border-left-width"));
  204. var right = parseInt(OpenLayers.Element.getStyle(
  205. this.zoomBox, "border-right-width"));
  206. var top = parseInt(OpenLayers.Element.getStyle(this.zoomBox,
  207. "border-top-width"));
  208. var bottom = parseInt(OpenLayers.Element.getStyle(
  209. this.zoomBox, "border-bottom-width"));
  210. this.boxOffsets = {
  211. left: left,
  212. right: right,
  213. top: top,
  214. bottom: bottom,
  215. width: w3cBoxModel === false ? left + right : 0,
  216. height: w3cBoxModel === false ? top + bottom : 0
  217. };
  218. }
  219. return this.boxOffsets;
  220. },
  221. CLASS_NAME: "OpenLayers.Handler.Box"
  222. });