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/Control.js |
---|
8 | */ |
---|
9 | |
---|
10 | /** |
---|
11 | * Class: OpenLayers.Control.Panel |
---|
12 | * The Panel control is a container for other controls. With it toolbars |
---|
13 | * may be composed. |
---|
14 | * |
---|
15 | * Inherits from: |
---|
16 | * - <OpenLayers.Control> |
---|
17 | */ |
---|
18 | OpenLayers.Control.Panel = OpenLayers.Class(OpenLayers.Control, { |
---|
19 | /** |
---|
20 | * Property: controls |
---|
21 | * {Array(<OpenLayers.Control>)} |
---|
22 | */ |
---|
23 | controls: null, |
---|
24 | |
---|
25 | /** |
---|
26 | * APIProperty: autoActivate |
---|
27 | * {Boolean} Activate the control when it is added to a map. Default is |
---|
28 | * true. |
---|
29 | */ |
---|
30 | autoActivate: true, |
---|
31 | |
---|
32 | /** |
---|
33 | * APIProperty: defaultControl |
---|
34 | * {<OpenLayers.Control>} The control which is activated when the control is |
---|
35 | * activated (turned on), which also happens at instantiation. |
---|
36 | * If <saveState> is true, <defaultControl> will be nullified after the |
---|
37 | * first activation of the panel. |
---|
38 | */ |
---|
39 | defaultControl: null, |
---|
40 | |
---|
41 | /** |
---|
42 | * APIProperty: saveState |
---|
43 | * {Boolean} If set to true, the active state of this panel's controls will |
---|
44 | * be stored on panel deactivation, and restored on reactivation. Default |
---|
45 | * is false. |
---|
46 | */ |
---|
47 | saveState: false, |
---|
48 | |
---|
49 | /** |
---|
50 | * Property: activeState |
---|
51 | * {Object} stores the active state of this panel's controls. |
---|
52 | */ |
---|
53 | activeState: null, |
---|
54 | |
---|
55 | /** |
---|
56 | * Constructor: OpenLayers.Control.Panel |
---|
57 | * Create a new control panel. |
---|
58 | * |
---|
59 | * Each control in the panel is represented by an icon. When clicking |
---|
60 | * on an icon, the <activateControl> method is called. |
---|
61 | * |
---|
62 | * Specific properties for controls on a panel: |
---|
63 | * type - {Number} One of <OpenLayers.Control.TYPE_TOOL>, |
---|
64 | * <OpenLayers.Control.TYPE_TOGGLE>, <OpenLayers.Control.TYPE_BUTTON>. |
---|
65 | * If not provided, <OpenLayers.Control.TYPE_TOOL> is assumed. |
---|
66 | * title - {string} Text displayed when mouse is over the icon that |
---|
67 | * represents the control. |
---|
68 | * |
---|
69 | * The <OpenLayers.Control.type> of a control determines the behavior when |
---|
70 | * clicking its icon: |
---|
71 | * <OpenLayers.Control.TYPE_TOOL> - The control is activated and other |
---|
72 | * controls of this type in the same panel are deactivated. This is |
---|
73 | * the default type. |
---|
74 | * <OpenLayers.Control.TYPE_TOGGLE> - The active state of the control is |
---|
75 | * toggled. |
---|
76 | * <OpenLayers.Control.TYPE_BUTTON> - The |
---|
77 | * <OpenLayers.Control.Button.trigger> method of the control is called, |
---|
78 | * but its active state is not changed. |
---|
79 | * |
---|
80 | * If a control is <OpenLayers.Control.active>, it will be drawn with the |
---|
81 | * olControl[Name]ItemActive class, otherwise with the |
---|
82 | * olControl[Name]ItemInactive class. |
---|
83 | * |
---|
84 | * Parameters: |
---|
85 | * options - {Object} An optional object whose properties will be used |
---|
86 | * to extend the control. |
---|
87 | */ |
---|
88 | initialize: function(options) { |
---|
89 | OpenLayers.Control.prototype.initialize.apply(this, [options]); |
---|
90 | this.controls = []; |
---|
91 | this.activeState = {}; |
---|
92 | }, |
---|
93 | |
---|
94 | /** |
---|
95 | * APIMethod: destroy |
---|
96 | */ |
---|
97 | destroy: function() { |
---|
98 | OpenLayers.Control.prototype.destroy.apply(this, arguments); |
---|
99 | for(var i = this.controls.length - 1 ; i >= 0; i--) { |
---|
100 | if(this.controls[i].events) { |
---|
101 | this.controls[i].events.un({ |
---|
102 | "activate": this.redraw, |
---|
103 | "deactivate": this.redraw, |
---|
104 | scope: this |
---|
105 | }); |
---|
106 | } |
---|
107 | OpenLayers.Event.stopObservingElement(this.controls[i].panel_div); |
---|
108 | this.controls[i].panel_div = null; |
---|
109 | } |
---|
110 | this.activeState = null; |
---|
111 | }, |
---|
112 | |
---|
113 | /** |
---|
114 | * APIMethod: activate |
---|
115 | */ |
---|
116 | activate: function() { |
---|
117 | if (OpenLayers.Control.prototype.activate.apply(this, arguments)) { |
---|
118 | var control; |
---|
119 | for (var i=0, len=this.controls.length; i<len; i++) { |
---|
120 | control = this.controls[i]; |
---|
121 | if (control === this.defaultControl || |
---|
122 | (this.saveState && this.activeState[control.id])) { |
---|
123 | control.activate(); |
---|
124 | } |
---|
125 | } |
---|
126 | if (this.saveState === true) { |
---|
127 | this.defaultControl = null; |
---|
128 | } |
---|
129 | this.redraw(); |
---|
130 | return true; |
---|
131 | } else { |
---|
132 | return false; |
---|
133 | } |
---|
134 | }, |
---|
135 | |
---|
136 | /** |
---|
137 | * APIMethod: deactivate |
---|
138 | */ |
---|
139 | deactivate: function() { |
---|
140 | if (OpenLayers.Control.prototype.deactivate.apply(this, arguments)) { |
---|
141 | var control; |
---|
142 | for (var i=0, len=this.controls.length; i<len; i++) { |
---|
143 | control = this.controls[i]; |
---|
144 | this.activeState[control.id] = control.deactivate(); |
---|
145 | } |
---|
146 | return true; |
---|
147 | } else { |
---|
148 | return false; |
---|
149 | } |
---|
150 | }, |
---|
151 | |
---|
152 | /** |
---|
153 | * Method: draw |
---|
154 | * |
---|
155 | * Returns: |
---|
156 | * {DOMElement} |
---|
157 | */ |
---|
158 | draw: function() { |
---|
159 | OpenLayers.Control.prototype.draw.apply(this, arguments); |
---|
160 | this.addControlsToMap(this.controls); |
---|
161 | return this.div; |
---|
162 | }, |
---|
163 | |
---|
164 | /** |
---|
165 | * Method: redraw |
---|
166 | */ |
---|
167 | redraw: function() { |
---|
168 | if (this.div.children.length>0) { |
---|
169 | for (var l=this.div.children.length, i=l-1 ; i>=0 ; i--) { |
---|
170 | this.div.removeChild(this.div.children[i]); |
---|
171 | } |
---|
172 | } |
---|
173 | this.div.innerHTML = ""; |
---|
174 | if (this.active) { |
---|
175 | for (var i=0, len=this.controls.length; i<len; i++) { |
---|
176 | var element = this.controls[i].panel_div; |
---|
177 | if (this.controls[i].active) { |
---|
178 | element.className = this.controls[i].displayClass + "ItemActive"; |
---|
179 | } else { |
---|
180 | element.className = this.controls[i].displayClass + "ItemInactive"; |
---|
181 | } |
---|
182 | this.div.appendChild(element); |
---|
183 | } |
---|
184 | } |
---|
185 | }, |
---|
186 | |
---|
187 | /** |
---|
188 | * APIMethod: activateControl |
---|
189 | * This method is called when the user click on the icon representing a |
---|
190 | * control in the panel. |
---|
191 | * |
---|
192 | * Parameters: |
---|
193 | * control - {<OpenLayers.Control>} |
---|
194 | */ |
---|
195 | activateControl: function (control) { |
---|
196 | if (!this.active) { return false; } |
---|
197 | if (control.type == OpenLayers.Control.TYPE_BUTTON) { |
---|
198 | control.trigger(); |
---|
199 | this.redraw(); |
---|
200 | return; |
---|
201 | } |
---|
202 | if (control.type == OpenLayers.Control.TYPE_TOGGLE) { |
---|
203 | if (control.active) { |
---|
204 | control.deactivate(); |
---|
205 | } else { |
---|
206 | control.activate(); |
---|
207 | } |
---|
208 | this.redraw(); |
---|
209 | return; |
---|
210 | } |
---|
211 | var c; |
---|
212 | for (var i=0, len=this.controls.length; i<len; i++) { |
---|
213 | c = this.controls[i]; |
---|
214 | if (c != control && |
---|
215 | (c.type === OpenLayers.Control.TYPE_TOOL || c.type == null)) { |
---|
216 | c.deactivate(); |
---|
217 | } |
---|
218 | } |
---|
219 | control.activate(); |
---|
220 | }, |
---|
221 | |
---|
222 | /** |
---|
223 | * APIMethod: addControls |
---|
224 | * To build a toolbar, you add a set of controls to it. addControls |
---|
225 | * lets you add a single control or a list of controls to the |
---|
226 | * Control Panel. |
---|
227 | * |
---|
228 | * Parameters: |
---|
229 | * controls - {<OpenLayers.Control>} Controls to add in the panel. |
---|
230 | */ |
---|
231 | addControls: function(controls) { |
---|
232 | if (!(controls instanceof Array)) { |
---|
233 | controls = [controls]; |
---|
234 | } |
---|
235 | this.controls = this.controls.concat(controls); |
---|
236 | |
---|
237 | // Give each control a panel_div which will be used later. |
---|
238 | // Access to this div is via the panel_div attribute of the |
---|
239 | // control added to the panel. |
---|
240 | // Also, stop mousedowns and clicks, but don't stop mouseup, |
---|
241 | // since they need to pass through. |
---|
242 | for (var i=0, len=controls.length; i<len; i++) { |
---|
243 | var element = document.createElement("div"); |
---|
244 | controls[i].panel_div = element; |
---|
245 | if (controls[i].title != "") { |
---|
246 | controls[i].panel_div.title = controls[i].title; |
---|
247 | } |
---|
248 | OpenLayers.Event.observe(controls[i].panel_div, "click", |
---|
249 | OpenLayers.Function.bind(this.onClick, this, controls[i])); |
---|
250 | OpenLayers.Event.observe(controls[i].panel_div, "dblclick", |
---|
251 | OpenLayers.Function.bind(this.onDoubleClick, this, controls[i])); |
---|
252 | OpenLayers.Event.observe(controls[i].panel_div, "mousedown", |
---|
253 | OpenLayers.Function.bindAsEventListener(OpenLayers.Event.stop)); |
---|
254 | } |
---|
255 | |
---|
256 | if (this.map) { // map.addControl() has already been called on the panel |
---|
257 | this.addControlsToMap(controls); |
---|
258 | this.redraw(); |
---|
259 | } |
---|
260 | }, |
---|
261 | |
---|
262 | /** |
---|
263 | * Method: addControlsToMap |
---|
264 | * Only for internal use in draw() and addControls() methods. |
---|
265 | * |
---|
266 | * Parameters: |
---|
267 | * controls - {Array(<OpenLayers.Control>)} Controls to add into map. |
---|
268 | */ |
---|
269 | addControlsToMap: function (controls) { |
---|
270 | var control; |
---|
271 | for (var i=0, len=controls.length; i<len; i++) { |
---|
272 | control = controls[i]; |
---|
273 | if (control.autoActivate === true) { |
---|
274 | control.autoActivate = false; |
---|
275 | this.map.addControl(control); |
---|
276 | control.autoActivate = true; |
---|
277 | } else { |
---|
278 | this.map.addControl(control); |
---|
279 | control.deactivate(); |
---|
280 | } |
---|
281 | control.events.on({ |
---|
282 | "activate": this.redraw, |
---|
283 | "deactivate": this.redraw, |
---|
284 | scope: this |
---|
285 | }); |
---|
286 | } |
---|
287 | }, |
---|
288 | |
---|
289 | /** |
---|
290 | * Method: onClick |
---|
291 | */ |
---|
292 | onClick: function (ctrl, evt) { |
---|
293 | OpenLayers.Event.stop(evt ? evt : window.event); |
---|
294 | this.activateControl(ctrl); |
---|
295 | }, |
---|
296 | |
---|
297 | /** |
---|
298 | * Method: onDoubleClick |
---|
299 | */ |
---|
300 | onDoubleClick: function(ctrl, evt) { |
---|
301 | OpenLayers.Event.stop(evt ? evt : window.event); |
---|
302 | }, |
---|
303 | |
---|
304 | /** |
---|
305 | * APIMethod: getControlsBy |
---|
306 | * Get a list of controls with properties matching the given criteria. |
---|
307 | * |
---|
308 | * Parameter: |
---|
309 | * property - {String} A control property to be matched. |
---|
310 | * match - {String | Object} A string to match. Can also be a regular |
---|
311 | * expression literal or object. In addition, it can be any object |
---|
312 | * with a method named test. For reqular expressions or other, if |
---|
313 | * match.test(control[property]) evaluates to true, the control will be |
---|
314 | * included in the array returned. If no controls are found, an empty |
---|
315 | * array is returned. |
---|
316 | * |
---|
317 | * Returns: |
---|
318 | * {Array(<OpenLayers.Control>)} A list of controls matching the given criteria. |
---|
319 | * An empty array is returned if no matches are found. |
---|
320 | */ |
---|
321 | getControlsBy: function(property, match) { |
---|
322 | var test = (typeof match.test == "function"); |
---|
323 | var found = OpenLayers.Array.filter(this.controls, function(item) { |
---|
324 | return item[property] == match || (test && match.test(item[property])); |
---|
325 | }); |
---|
326 | return found; |
---|
327 | }, |
---|
328 | |
---|
329 | /** |
---|
330 | * APIMethod: getControlsByName |
---|
331 | * Get a list of contorls with names matching the given name. |
---|
332 | * |
---|
333 | * Parameter: |
---|
334 | * match - {String | Object} A control name. The name can also be a regular |
---|
335 | * expression literal or object. In addition, it can be any object |
---|
336 | * with a method named test. For reqular expressions or other, if |
---|
337 | * name.test(control.name) evaluates to true, the control will be included |
---|
338 | * in the list of controls returned. If no controls are found, an empty |
---|
339 | * array is returned. |
---|
340 | * |
---|
341 | * Returns: |
---|
342 | * {Array(<OpenLayers.Control>)} A list of controls matching the given name. |
---|
343 | * An empty array is returned if no matches are found. |
---|
344 | */ |
---|
345 | getControlsByName: function(match) { |
---|
346 | return this.getControlsBy("name", match); |
---|
347 | }, |
---|
348 | |
---|
349 | /** |
---|
350 | * APIMethod: getControlsByClass |
---|
351 | * Get a list of controls of a given type (CLASS_NAME). |
---|
352 | * |
---|
353 | * Parameter: |
---|
354 | * match - {String | Object} A control class name. The type can also be a |
---|
355 | * regular expression literal or object. In addition, it can be any |
---|
356 | * object with a method named test. For reqular expressions or other, |
---|
357 | * if type.test(control.CLASS_NAME) evaluates to true, the control will |
---|
358 | * be included in the list of controls returned. If no controls are |
---|
359 | * found, an empty array is returned. |
---|
360 | * |
---|
361 | * Returns: |
---|
362 | * {Array(<OpenLayers.Control>)} A list of controls matching the given type. |
---|
363 | * An empty array is returned if no matches are found. |
---|
364 | */ |
---|
365 | getControlsByClass: function(match) { |
---|
366 | return this.getControlsBy("CLASS_NAME", match); |
---|
367 | }, |
---|
368 | |
---|
369 | CLASS_NAME: "OpenLayers.Control.Panel" |
---|
370 | }); |
---|
371 | |
---|