1 | /* Copyright (c) 2006-2010 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 | /** |
---|
7 | * @requires OpenLayers/Handler.js |
---|
8 | * @requires OpenLayers/Handler/Drag.js |
---|
9 | */ |
---|
10 | |
---|
11 | /** |
---|
12 | * Class: OpenLayers.Handler.Box |
---|
13 | * Handler for dragging a rectangle across the map. Box is displayed |
---|
14 | * on mouse down, moves on mouse move, and is finished on mouse up. |
---|
15 | * |
---|
16 | * Inherits from: |
---|
17 | * - <OpenLayers.Handler> |
---|
18 | */ |
---|
19 | OpenLayers.Handler.Box = OpenLayers.Class(OpenLayers.Handler, { |
---|
20 | |
---|
21 | /** |
---|
22 | * Property: dragHandler |
---|
23 | * {<OpenLayers.Handler.Drag>} |
---|
24 | */ |
---|
25 | dragHandler: null, |
---|
26 | |
---|
27 | /** |
---|
28 | * APIProperty: boxDivClassName |
---|
29 | * {String} The CSS class to use for drawing the box. Default is |
---|
30 | * olHandlerBoxZoomBox |
---|
31 | */ |
---|
32 | boxDivClassName: 'olHandlerBoxZoomBox', |
---|
33 | |
---|
34 | /** |
---|
35 | * Property: boxCharacteristics |
---|
36 | * {Object} Caches some box characteristics from css. This is used |
---|
37 | * by the getBoxCharacteristics method. |
---|
38 | */ |
---|
39 | boxCharacteristics: null, |
---|
40 | |
---|
41 | /** |
---|
42 | * Constructor: OpenLayers.Handler.Box |
---|
43 | * |
---|
44 | * Parameters: |
---|
45 | * control - {<OpenLayers.Control>} |
---|
46 | * callbacks - {Object} An object containing a single function to be |
---|
47 | * called when the drag operation is finished. |
---|
48 | * The callback should expect to recieve a single |
---|
49 | * argument, the point geometry. |
---|
50 | * options - {Object} |
---|
51 | */ |
---|
52 | initialize: function(control, callbacks, options) { |
---|
53 | OpenLayers.Handler.prototype.initialize.apply(this, arguments); |
---|
54 | var callbacks = { |
---|
55 | "down": this.startBox, |
---|
56 | "move": this.moveBox, |
---|
57 | "out": this.removeBox, |
---|
58 | "up": this.endBox |
---|
59 | }; |
---|
60 | this.dragHandler = new OpenLayers.Handler.Drag( |
---|
61 | this, callbacks, {keyMask: this.keyMask}); |
---|
62 | }, |
---|
63 | |
---|
64 | /** |
---|
65 | * Method: destroy |
---|
66 | */ |
---|
67 | destroy: function() { |
---|
68 | if (this.dragHandler) { |
---|
69 | this.dragHandler.destroy(); |
---|
70 | this.dragHandler = null; |
---|
71 | } |
---|
72 | OpenLayers.Handler.prototype.destroy.apply(this, arguments); |
---|
73 | }, |
---|
74 | |
---|
75 | /** |
---|
76 | * Method: setMap |
---|
77 | */ |
---|
78 | setMap: function (map) { |
---|
79 | OpenLayers.Handler.prototype.setMap.apply(this, arguments); |
---|
80 | if (this.dragHandler) { |
---|
81 | this.dragHandler.setMap(map); |
---|
82 | } |
---|
83 | }, |
---|
84 | |
---|
85 | /** |
---|
86 | * Method: startBox |
---|
87 | * |
---|
88 | * Parameters: |
---|
89 | * evt - {Event} |
---|
90 | */ |
---|
91 | startBox: function (xy) { |
---|
92 | this.zoomBox = OpenLayers.Util.createDiv('zoomBox', |
---|
93 | this.dragHandler.start); |
---|
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 | |
---|
98 | OpenLayers.Element.addClass( |
---|
99 | this.map.viewPortDiv, "olDrawBox" |
---|
100 | ); |
---|
101 | }, |
---|
102 | |
---|
103 | /** |
---|
104 | * Method: moveBox |
---|
105 | */ |
---|
106 | moveBox: function (xy) { |
---|
107 | var startX = this.dragHandler.start.x; |
---|
108 | var startY = this.dragHandler.start.y; |
---|
109 | var deltaX = Math.abs(startX - xy.x); |
---|
110 | var deltaY = Math.abs(startY - xy.y); |
---|
111 | this.zoomBox.style.width = Math.max(1, deltaX) + "px"; |
---|
112 | this.zoomBox.style.height = Math.max(1, deltaY) + "px"; |
---|
113 | this.zoomBox.style.left = xy.x < startX ? xy.x+"px" : startX+"px"; |
---|
114 | this.zoomBox.style.top = xy.y < startY ? xy.y+"px" : startY+"px"; |
---|
115 | |
---|
116 | // depending on the box model, modify width and height to take borders |
---|
117 | // of the box into account |
---|
118 | var box = this.getBoxCharacteristics(); |
---|
119 | if (box.newBoxModel) { |
---|
120 | if (xy.x > startX) { |
---|
121 | this.zoomBox.style.width = |
---|
122 | Math.max(1, deltaX - box.xOffset) + "px"; |
---|
123 | } |
---|
124 | if (xy.y > startY) { |
---|
125 | this.zoomBox.style.height = |
---|
126 | Math.max(1, deltaY - box.yOffset) + "px"; |
---|
127 | } |
---|
128 | } |
---|
129 | }, |
---|
130 | |
---|
131 | /** |
---|
132 | * Method: endBox |
---|
133 | */ |
---|
134 | endBox: function(end) { |
---|
135 | var result; |
---|
136 | if (Math.abs(this.dragHandler.start.x - end.x) > 5 || |
---|
137 | Math.abs(this.dragHandler.start.y - end.y) > 5) { |
---|
138 | var start = this.dragHandler.start; |
---|
139 | var top = Math.min(start.y, end.y); |
---|
140 | var bottom = Math.max(start.y, end.y); |
---|
141 | var left = Math.min(start.x, end.x); |
---|
142 | var right = Math.max(start.x, end.x); |
---|
143 | result = new OpenLayers.Bounds(left, bottom, right, top); |
---|
144 | } else { |
---|
145 | result = this.dragHandler.start.clone(); // i.e. OL.Pixel |
---|
146 | } |
---|
147 | this.removeBox(); |
---|
148 | |
---|
149 | this.callback("done", [result]); |
---|
150 | }, |
---|
151 | |
---|
152 | /** |
---|
153 | * Method: removeBox |
---|
154 | * Remove the zoombox from the screen and nullify our reference to it. |
---|
155 | */ |
---|
156 | removeBox: function() { |
---|
157 | this.map.viewPortDiv.removeChild(this.zoomBox); |
---|
158 | this.zoomBox = null; |
---|
159 | this.boxCharacteristics = null; |
---|
160 | OpenLayers.Element.removeClass( |
---|
161 | this.map.viewPortDiv, "olDrawBox" |
---|
162 | ); |
---|
163 | |
---|
164 | }, |
---|
165 | |
---|
166 | /** |
---|
167 | * Method: activate |
---|
168 | */ |
---|
169 | activate: function () { |
---|
170 | if (OpenLayers.Handler.prototype.activate.apply(this, arguments)) { |
---|
171 | this.dragHandler.activate(); |
---|
172 | return true; |
---|
173 | } else { |
---|
174 | return false; |
---|
175 | } |
---|
176 | }, |
---|
177 | |
---|
178 | /** |
---|
179 | * Method: deactivate |
---|
180 | */ |
---|
181 | deactivate: function () { |
---|
182 | if (OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) { |
---|
183 | this.dragHandler.deactivate(); |
---|
184 | return true; |
---|
185 | } else { |
---|
186 | return false; |
---|
187 | } |
---|
188 | }, |
---|
189 | |
---|
190 | /** |
---|
191 | * Method: getCharacteristics |
---|
192 | * Determines offset and box model for a box. |
---|
193 | * |
---|
194 | * Returns: |
---|
195 | * {Object} a hash with the following properties: |
---|
196 | * - xOffset - Corner offset in x-direction |
---|
197 | * - yOffset - Corner offset in y-direction |
---|
198 | * - newBoxModel - true for all browsers except IE in quirks mode |
---|
199 | */ |
---|
200 | getBoxCharacteristics: function() { |
---|
201 | if (!this.boxCharacteristics) { |
---|
202 | var xOffset = parseInt(OpenLayers.Element.getStyle(this.zoomBox, |
---|
203 | "border-left-width")) + parseInt(OpenLayers.Element.getStyle( |
---|
204 | this.zoomBox, "border-right-width")) + 1; |
---|
205 | var yOffset = parseInt(OpenLayers.Element.getStyle(this.zoomBox, |
---|
206 | "border-top-width")) + parseInt(OpenLayers.Element.getStyle( |
---|
207 | this.zoomBox, "border-bottom-width")) + 1; |
---|
208 | // all browsers use the new box model, except IE in quirks mode |
---|
209 | var newBoxModel = OpenLayers.Util.getBrowserName() == "msie" ? |
---|
210 | document.compatMode != "BackCompat" : true; |
---|
211 | this.boxCharacteristics = { |
---|
212 | xOffset: xOffset, |
---|
213 | yOffset: yOffset, |
---|
214 | newBoxModel: newBoxModel |
---|
215 | }; |
---|
216 | } |
---|
217 | return this.boxCharacteristics; |
---|
218 | }, |
---|
219 | |
---|
220 | CLASS_NAME: "OpenLayers.Handler.Box" |
---|
221 | }); |
---|