1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
6 * @requires OpenLayers/Control.js
10 * Class: OpenLayers.Control.Panel
11 * The Panel control is a container for other controls. With it toolbars
15 * - <OpenLayers.Control>
17 OpenLayers.Control.Panel = OpenLayers.Class(OpenLayers.Control, {
20 * {Array(<OpenLayers.Control>)}
25 * APIProperty: defaultControl
26 * {<OpenLayers.Control>} The control which is activated when the control is
27 * activated (turned on), which also happens at instantiation.
32 * Constructor: OpenLayers.Control.Panel
33 * Create a new control panel.
36 * options - {Object} An optional object whose properties will be used
37 * to extend the control.
39 initialize: function(options) {
40 OpenLayers.Control.prototype.initialize.apply(this, [options]);
48 OpenLayers.Control.prototype.destroy.apply(this, arguments);
49 for(var i = this.controls.length - 1 ; i >= 0; i--) {
50 if(this.controls[i].events) {
51 this.controls[i].events.un({
52 "activate": this.redraw,
53 "deactivate": this.redraw,
57 OpenLayers.Event.stopObservingElement(this.controls[i].panel_div);
58 this.controls[i].panel_div = null;
65 activate: function() {
66 if (OpenLayers.Control.prototype.activate.apply(this, arguments)) {
67 for(var i=0, len=this.controls.length; i<len; i++) {
68 if (this.controls[i] == this.defaultControl) {
69 this.controls[i].activate();
80 * APIMethod: deactivate
82 deactivate: function() {
83 if (OpenLayers.Control.prototype.deactivate.apply(this, arguments)) {
84 for(var i=0, len=this.controls.length; i<len; i++) {
85 this.controls[i].deactivate();
100 OpenLayers.Control.prototype.draw.apply(this, arguments);
101 for (var i=0, len=this.controls.length; i<len; i++) {
102 this.map.addControl(this.controls[i]);
103 this.controls[i].deactivate();
104 this.controls[i].events.on({
105 "activate": this.redraw,
106 "deactivate": this.redraw,
118 this.div.innerHTML = "";
120 for (var i=0, len=this.controls.length; i<len; i++) {
121 var element = this.controls[i].panel_div;
122 if (this.controls[i].active) {
123 element.className = this.controls[i].displayClass + "ItemActive";
125 element.className = this.controls[i].displayClass + "ItemInactive";
127 this.div.appendChild(element);
133 * APIMethod: activateControl
136 * control - {<OpenLayers.Control>}
138 activateControl: function (control) {
139 if (!this.active) { return false; }
140 if (control.type == OpenLayers.Control.TYPE_BUTTON) {
145 if (control.type == OpenLayers.Control.TYPE_TOGGLE) {
146 if (control.active) {
147 control.deactivate();
154 for (var i=0, len=this.controls.length; i<len; i++) {
155 if (this.controls[i] != control) {
156 if (this.controls[i].type != OpenLayers.Control.TYPE_TOGGLE) {
157 this.controls[i].deactivate();
165 * APIMethod: addControls
166 * To build a toolbar, you add a set of controls to it. addControls
167 * lets you add a single control or a list of controls to the
171 * controls - {<OpenLayers.Control>}
173 addControls: function(controls) {
174 if (!(controls instanceof Array)) {
175 controls = [controls];
177 this.controls = this.controls.concat(controls);
179 // Give each control a panel_div which will be used later.
180 // Access to this div is via the panel_div attribute of the
181 // control added to the panel.
182 // Also, stop mousedowns and clicks, but don't stop mouseup,
183 // since they need to pass through.
184 for (var i=0, len=controls.length; i<len; i++) {
185 var element = document.createElement("div");
186 var textNode = document.createTextNode(" ");
187 controls[i].panel_div = element;
188 if (controls[i].title != "") {
189 controls[i].panel_div.title = controls[i].title;
191 OpenLayers.Event.observe(controls[i].panel_div, "click",
192 OpenLayers.Function.bind(this.onClick, this, controls[i]));
193 OpenLayers.Event.observe(controls[i].panel_div, "mousedown",
194 OpenLayers.Function.bindAsEventListener(OpenLayers.Event.stop));
197 if (this.map) { // map.addControl() has already been called on the panel
198 for (var i=0, len=controls.length; i<len; i++) {
199 this.map.addControl(controls[i]);
200 controls[i].deactivate();
201 controls[i].events.on({
202 "activate": this.redraw,
203 "deactivate": this.redraw,
214 onClick: function (ctrl, evt) {
215 OpenLayers.Event.stop(evt ? evt : window.event);
216 this.activateControl(ctrl);
220 * APIMethod: getControlsBy
221 * Get a list of controls with properties matching the given criteria.
224 * property - {String} A control property to be matched.
225 * match - {String | Object} A string to match. Can also be a regular
226 * expression literal or object. In addition, it can be any object
227 * with a method named test. For reqular expressions or other, if
228 * match.test(control[property]) evaluates to true, the control will be
229 * included in the array returned. If no controls are found, an empty
233 * {Array(<OpenLayers.Control>)} A list of controls matching the given criteria.
234 * An empty array is returned if no matches are found.
236 getControlsBy: function(property, match) {
237 var test = (typeof match.test == "function");
238 var found = OpenLayers.Array.filter(this.controls, function(item) {
239 return item[property] == match || (test && match.test(item[property]));
245 * APIMethod: getControlsByName
246 * Get a list of contorls with names matching the given name.
249 * match - {String | Object} A control name. The name can also be a regular
250 * expression literal or object. In addition, it can be any object
251 * with a method named test. For reqular expressions or other, if
252 * name.test(control.name) evaluates to true, the control will be included
253 * in the list of controls returned. If no controls are found, an empty
257 * {Array(<OpenLayers.Control>)} A list of controls matching the given name.
258 * An empty array is returned if no matches are found.
260 getControlsByName: function(match) {
261 return this.getControlsBy("name", match);
265 * APIMethod: getControlsByClass
266 * Get a list of controls of a given type (CLASS_NAME).
269 * match - {String | Object} A control class name. The type can also be a
270 * regular expression literal or object. In addition, it can be any
271 * object with a method named test. For reqular expressions or other,
272 * if type.test(control.CLASS_NAME) evaluates to true, the control will
273 * be included in the list of controls returned. If no controls are
274 * found, an empty array is returned.
277 * {Array(<OpenLayers.Control>)} A list of controls matching the given type.
278 * An empty array is returned if no matches are found.
280 getControlsByClass: function(match) {
281 return this.getControlsBy("CLASS_NAME", match);
284 CLASS_NAME: "OpenLayers.Control.Panel"