1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
|
/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
* full list of contributors). Published under the 2-clause BSD license.
* See license.txt in the OpenLayers distribution or repository for the
* full text of the license. */
/**
* @requires OpenLayers/Control.js
* @requires OpenLayers/Events/buttonclick.js
*/
/**
* Class: OpenLayers.Control.Panel
* The Panel control is a container for other controls. With it toolbars
* may be composed.
*
* Inherits from:
* - <OpenLayers.Control>
*/
OpenLayers.Control.Panel = OpenLayers.Class(OpenLayers.Control, {
/**
* Property: controls
* {Array(<OpenLayers.Control>)}
*/
controls: null,
/**
* APIProperty: autoActivate
* {Boolean} Activate the control when it is added to a map. Default is
* true.
*/
autoActivate: true,
/**
* APIProperty: defaultControl
* {<OpenLayers.Control>} The control which is activated when the control is
* activated (turned on), which also happens at instantiation.
* If <saveState> is true, <defaultControl> will be nullified after the
* first activation of the panel.
*/
defaultControl: null,
/**
* APIProperty: saveState
* {Boolean} If set to true, the active state of this panel's controls will
* be stored on panel deactivation, and restored on reactivation. Default
* is false.
*/
saveState: false,
/**
* APIProperty: allowDepress
* {Boolean} If is true the <OpenLayers.Control.TYPE_TOOL> controls can
* be deactivated by clicking the icon that represents them. Default
* is false.
*/
allowDepress: false,
/**
* Property: activeState
* {Object} stores the active state of this panel's controls.
*/
activeState: null,
/**
* Constructor: OpenLayers.Control.Panel
* Create a new control panel.
*
* Each control in the panel is represented by an icon. When clicking
* on an icon, the <activateControl> method is called.
*
* Specific properties for controls on a panel:
* type - {Number} One of <OpenLayers.Control.TYPE_TOOL>,
* <OpenLayers.Control.TYPE_TOGGLE>, <OpenLayers.Control.TYPE_BUTTON>.
* If not provided, <OpenLayers.Control.TYPE_TOOL> is assumed.
* title - {string} Text displayed when mouse is over the icon that
* represents the control.
*
* The <OpenLayers.Control.type> of a control determines the behavior when
* clicking its icon:
* <OpenLayers.Control.TYPE_TOOL> - The control is activated and other
* controls of this type in the same panel are deactivated. This is
* the default type.
* <OpenLayers.Control.TYPE_TOGGLE> - The active state of the control is
* toggled.
* <OpenLayers.Control.TYPE_BUTTON> - The
* <OpenLayers.Control.Button.trigger> method of the control is called,
* but its active state is not changed.
*
* If a control is <OpenLayers.Control.active>, it will be drawn with the
* olControl[Name]ItemActive class, otherwise with the
* olControl[Name]ItemInactive class.
*
* Parameters:
* options - {Object} An optional object whose properties will be used
* to extend the control.
*/
initialize: function(options) {
OpenLayers.Control.prototype.initialize.apply(this, [options]);
this.controls = [];
this.activeState = {};
},
/**
* APIMethod: destroy
*/
destroy: function() {
if (this.map) {
this.map.events.unregister("buttonclick", this, this.onButtonClick);
}
OpenLayers.Control.prototype.destroy.apply(this, arguments);
for (var ctl, i = this.controls.length - 1; i >= 0; i--) {
ctl = this.controls[i];
if (ctl.events) {
ctl.events.un({
activate: this.iconOn,
deactivate: this.iconOff
});
}
ctl.panel_div = null;
}
this.activeState = null;
},
/**
* APIMethod: activate
*/
activate: function() {
if (OpenLayers.Control.prototype.activate.apply(this, arguments)) {
var control;
for (var i=0, len=this.controls.length; i<len; i++) {
control = this.controls[i];
if (control === this.defaultControl ||
(this.saveState && this.activeState[control.id])) {
control.activate();
}
}
if (this.saveState === true) {
this.defaultControl = null;
}
this.redraw();
return true;
} else {
return false;
}
},
/**
* APIMethod: deactivate
*/
deactivate: function() {
if (OpenLayers.Control.prototype.deactivate.apply(this, arguments)) {
var control;
for (var i=0, len=this.controls.length; i<len; i++) {
control = this.controls[i];
this.activeState[control.id] = control.deactivate();
}
this.redraw();
return true;
} else {
return false;
}
},
/**
* Method: draw
*
* Returns:
* {DOMElement}
*/
draw: function() {
OpenLayers.Control.prototype.draw.apply(this, arguments);
if (this.outsideViewport) {
this.events.attachToElement(this.div);
this.events.register("buttonclick", this, this.onButtonClick);
} else {
this.map.events.register("buttonclick", this, this.onButtonClick);
}
this.addControlsToMap(this.controls);
return this.div;
},
/**
* Method: redraw
*/
redraw: function() {
for (var l=this.div.childNodes.length, i=l-1; i>=0; i--) {
this.div.removeChild(this.div.childNodes[i]);
}
this.div.innerHTML = "";
if (this.active) {
for (var i=0, len=this.controls.length; i<len; i++) {
this.div.appendChild(this.controls[i].panel_div);
}
}
},
/**
* APIMethod: activateControl
* This method is called when the user click on the icon representing a
* control in the panel.
*
* Parameters:
* control - {<OpenLayers.Control>}
*/
activateControl: function (control) {
if (!this.active) { return false; }
if (control.type == OpenLayers.Control.TYPE_BUTTON) {
control.trigger();
return;
}
if (control.type == OpenLayers.Control.TYPE_TOGGLE) {
if (control.active) {
control.deactivate();
} else {
control.activate();
}
return;
}
if (this.allowDepress && control.active) {
control.deactivate();
} else {
var c;
for (var i=0, len=this.controls.length; i<len; i++) {
c = this.controls[i];
if (c != control &&
(c.type === OpenLayers.Control.TYPE_TOOL || c.type == null)) {
c.deactivate();
}
}
control.activate();
}
},
/**
* APIMethod: addControls
* To build a toolbar, you add a set of controls to it. addControls
* lets you add a single control or a list of controls to the
* Control Panel.
*
* Parameters:
* controls - {<OpenLayers.Control>} Controls to add in the panel.
*/
addControls: function(controls) {
if (!(OpenLayers.Util.isArray(controls))) {
controls = [controls];
}
this.controls = this.controls.concat(controls);
for (var i=0, len=controls.length; i<len; i++) {
var control = controls[i],
element = this.createControlMarkup(control);
OpenLayers.Element.addClass(element,
control.displayClass + "ItemInactive");
OpenLayers.Element.addClass(element, "olButton");
if (control.title != "" && !element.title) {
element.title = control.title;
}
control.panel_div = element;
}
if (this.map) { // map.addControl() has already been called on the panel
this.addControlsToMap(controls);
this.redraw();
}
},
/**
* APIMethod: createControlMarkup
* This function just creates a div for the control. If specific HTML
* markup is needed this function can be overridden in specific classes,
* or at panel instantiation time:
*
* Example:
* (code)
* var panel = new OpenLayers.Control.Panel({
* defaultControl: control,
* // ovverride createControlMarkup to create actual buttons
* // including texts wrapped into span elements.
* createControlMarkup: function(control) {
* var button = document.createElement('button'),
* span = document.createElement('span');
* if (control.text) {
* span.innerHTML = control.text;
* }
* return button;
* }
* });
* (end)
*
* Parameters:
* control - {<OpenLayers.Control>} The control to create the HTML
* markup for.
*
* Returns:
* {DOMElement} The markup.
*/
createControlMarkup: function(control) {
return document.createElement("div");
},
/**
* Method: addControlsToMap
* Only for internal use in draw() and addControls() methods.
*
* Parameters:
* controls - {Array(<OpenLayers.Control>)} Controls to add into map.
*/
addControlsToMap: function (controls) {
var control;
for (var i=0, len=controls.length; i<len; i++) {
control = controls[i];
if (control.autoActivate === true) {
control.autoActivate = false;
this.map.addControl(control);
control.autoActivate = true;
} else {
this.map.addControl(control);
control.deactivate();
}
control.events.on({
activate: this.iconOn,
deactivate: this.iconOff
});
}
},
/**
* Method: iconOn
* Internal use, for use only with "controls[i].events.on/un".
*/
iconOn: function() {
var d = this.panel_div; // "this" refers to a control on panel!
var re = new RegExp("\\b(" + this.displayClass + "Item)Inactive\\b");
d.className = d.className.replace(re, "$1Active");
},
/**
* Method: iconOff
* Internal use, for use only with "controls[i].events.on/un".
*/
iconOff: function() {
var d = this.panel_div; // "this" refers to a control on panel!
var re = new RegExp("\\b(" + this.displayClass + "Item)Active\\b");
d.className = d.className.replace(re, "$1Inactive");
},
/**
* Method: onButtonClick
*
* Parameters:
* evt - {Event}
*/
onButtonClick: function (evt) {
var controls = this.controls,
button = evt.buttonElement;
for (var i=controls.length-1; i>=0; --i) {
if (controls[i].panel_div === button) {
this.activateControl(controls[i]);
break;
}
}
},
/**
* APIMethod: getControlsBy
* Get a list of controls with properties matching the given criteria.
*
* Parameters:
* property - {String} A control property to be matched.
* match - {String | Object} A string to match. Can also be a regular
* expression literal or object. In addition, it can be any object
* with a method named test. For reqular expressions or other, if
* match.test(control[property]) evaluates to true, the control will be
* included in the array returned. If no controls are found, an empty
* array is returned.
*
* Returns:
* {Array(<OpenLayers.Control>)} A list of controls matching the given criteria.
* An empty array is returned if no matches are found.
*/
getControlsBy: function(property, match) {
var test = (typeof match.test == "function");
var found = OpenLayers.Array.filter(this.controls, function(item) {
return item[property] == match || (test && match.test(item[property]));
});
return found;
},
/**
* APIMethod: getControlsByName
* Get a list of contorls with names matching the given name.
*
* Parameters:
* match - {String | Object} A control name. The name can also be a regular
* expression literal or object. In addition, it can be any object
* with a method named test. For reqular expressions or other, if
* name.test(control.name) evaluates to true, the control will be included
* in the list of controls returned. If no controls are found, an empty
* array is returned.
*
* Returns:
* {Array(<OpenLayers.Control>)} A list of controls matching the given name.
* An empty array is returned if no matches are found.
*/
getControlsByName: function(match) {
return this.getControlsBy("name", match);
},
/**
* APIMethod: getControlsByClass
* Get a list of controls of a given type (CLASS_NAME).
*
* Parameters:
* match - {String | Object} A control class name. The type can also be a
* regular expression literal or object. In addition, it can be any
* object with a method named test. For reqular expressions or other,
* if type.test(control.CLASS_NAME) evaluates to true, the control will
* be included in the list of controls returned. If no controls are
* found, an empty array is returned.
*
* Returns:
* {Array(<OpenLayers.Control>)} A list of controls matching the given type.
* An empty array is returned if no matches are found.
*/
getControlsByClass: function(match) {
return this.getControlsBy("CLASS_NAME", match);
},
CLASS_NAME: "OpenLayers.Control.Panel"
});
|