New saveState option to restore the active state of controls in a panel after re-activating the panel. r=crschmidt,jorix (closes #2753)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@10679 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
ahocevar
2010-08-23 12:09:42 +00:00
parent a6319b2974
commit ce0ba0b570
2 changed files with 85 additions and 6 deletions

View File

@@ -32,9 +32,25 @@ OpenLayers.Control.Panel = OpenLayers.Class(OpenLayers.Control, {
* 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,
/**
* Property: activeState
* {Object} stores the active state of this panel's controls.
*/
activeState: null,
/**
* Constructor: OpenLayers.Control.Panel
* Create a new control panel.
@@ -71,6 +87,7 @@ OpenLayers.Control.Panel = OpenLayers.Class(OpenLayers.Control, {
initialize: function(options) {
OpenLayers.Control.prototype.initialize.apply(this, [options]);
this.controls = [];
this.activeState = {};
},
/**
@@ -88,7 +105,8 @@ OpenLayers.Control.Panel = OpenLayers.Class(OpenLayers.Control, {
}
OpenLayers.Event.stopObservingElement(this.controls[i].panel_div);
this.controls[i].panel_div = null;
}
}
this.activeState = null;
},
/**
@@ -96,11 +114,17 @@ OpenLayers.Control.Panel = OpenLayers.Class(OpenLayers.Control, {
*/
activate: function() {
if (OpenLayers.Control.prototype.activate.apply(this, arguments)) {
for(var i=0, len=this.controls.length; i<len; i++) {
if (this.controls[i] == this.defaultControl) {
this.controls[i].activate();
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 {
@@ -113,8 +137,10 @@ OpenLayers.Control.Panel = OpenLayers.Class(OpenLayers.Control, {
*/
deactivate: function() {
if (OpenLayers.Control.prototype.deactivate.apply(this, arguments)) {
for(var i=0, len=this.controls.length; i<len; i++) {
this.controls[i].deactivate();
var control;
for (var i=0, len=this.controls.length; i<len; i++) {
control = this.controls[i];
this.activeState[control.id] = control.deactivate();
}
return true;
} else {

View File

@@ -157,6 +157,59 @@
}
function test_Control_Panel_saveState (t) {
t.plan(11);
var map = new OpenLayers.Map('map');
var defaultControl = new OpenLayers.Control();
var panel = new OpenLayers.Control.Panel({
defaultControl: defaultControl
});
panel.addControls([new OpenLayers.Control(), defaultControl]);
map.addControl(panel);
t.eq(defaultControl.active, true,
"After panel activation default control is active.");
t.ok(panel.defaultControl,
"defaultControl not nullified after initial panel activation");
// activate the 1st control
panel.activateControl(panel.controls[0]);
panel.deactivate();
t.ok(!panel.controls[0].active && !panel.controls[1].active,
"No controls are active after panel deactivation.");
panel.activate();
t.eq(panel.controls[0].active, false,
"After panel reactivation first control is inactive.");
t.eq(panel.controls[1].active, true,
"After panel reactivation default control is active again.");
panel.destroy();
defaultControl = new OpenLayers.Control();
panel = new OpenLayers.Control.Panel({
saveState: true,
defaultControl: defaultControl
});
panel.addControls([new OpenLayers.Control(), defaultControl]);
map.addControl(panel);
t.eq(defaultControl.active, true,
"After panel activation default control is active.");
t.eq(panel.defaultControl, null,
"defaultControl nullified after initial panel activation");
// activate the 1st control, which will deactivate the 2nd
panel.activateControl(panel.controls[0]);
t.eq(panel.controls[1].active, false,
"2nd control deactivated with activation of 1st");
panel.deactivate();
t.ok(!panel.controls[0].active && !panel.controls[1].active,
"No controls are active after panel deactivation.");
panel.activate();
t.eq(panel.controls[0].active, true,
"After panel reactivation first control is active.");
t.eq(panel.controls[1].active, false,
"After panel reactivation second control is inactive.");
panel.destroy();
map.destroy();
}
</script>
</head>