Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two new features: #10

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 52 additions & 13 deletions control/controlbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,38 @@ ol.control.Bar.prototype.addControl = function (c, bar)
bar.setTarget(c.element);
$(bar.element).addClass("ol-option-bar");
c.option_bar = bar;
bar.pcontrol = c;
}
if (this.getMap())
{ this.getMap().addControl(c);
if (c.option_bar) this.getMap().addControl(c.option_bar);
}
// If defaultActive control,
if (c.get('defaultActive') == true) {
// if top-level control bar, set control active.
if (!this.pcontrol) {
c.setActive(true);
}
// If nested control, set control active if parent control is active.
else {
if (this.pcontrol.getActive())
c.setActive(true);
}
}
}

/** Find defaultActive control in containing control bar,
* and activate it if present.
*/
ol.control.Bar.prototype.activateDefaultControl = function()
{
var n;
for (n=0; n<this.controls_.length; n++)
if (this.controls_[n].get('defaultActive') == true) break;
// defaultActive control not found in control bar. Nothing to activate.
if (n == this.controls_.length) return;
this.controls_[n].setActive(true);
}

/** Deativate all controls in a bar
* @param {ol.control} except a control
Expand All @@ -105,23 +130,37 @@ ol.control.Bar.prototype.deactivateControls = function (except)
};


/** Activate a control
* @param {ol.event} an object with a target {ol.control} and active {bool}
/** Post-process an activated/deactivated control
* @param {ol.event} an object with a target {ol.control} and active flag {bool}
*/
ol.control.Bar.prototype.onActivateControl_ = function (e)
{ // Deactivate control on option bar
/*
if (!e.target.get("active") && e.target.option_bar)
{ e.target.option_bar.deactivateControls ();
}
*/
if (!e.active || !this.get('toggleOne')) return;
{
// Find control in containing control bar.
var n;
var ctrl = e.target;
for (n=0; n<this.controls_.length; n++)
{ if (this.controls_[n]===ctrl) break;
if (this.controls_[n] === ctrl) break;
// Control not found in control bar. Should not happen. Return!
if (n == this.controls_.length) return;

// If control was activated,
if (e.active) {
// If containing control bar has toggleOne enabled,
if (this.get('toggleOne')) {
// deactivate any other controls contained within this same control bar.
this.deactivateControls (this.controls_[n]);
}
// Check if the containing control bar has a sub-control bar containing a control with defaultActive set.
// If so, activate it.
if (this.controls_[n].option_bar)
this.controls_[n].option_bar.activateDefaultControl();
}
// If control was deactivated,
else {
// Check if the containing control bar has a sub-control bar.
// If so, deactivate all controls within it.
if (this.controls_[n].option_bar)
this.controls_[n].option_bar.deactivateControls();
}
// Not here!
if (n==this.controls_.length) return;
this.deactivateControls (this.controls_[n]);
}

4 changes: 4 additions & 0 deletions control/togglecontrol.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* html {String} html to insert in the control
* interaction {ol.interaction} interaction associated with the control
* active {bool} the control is active
* defaultActive {bool} for top-level control bar, same as 'active' param above;
* for nested control bar, sets the control active if parent control is active.
* onToggle {function} callback when control is clicked (or use change:active event)
*/
ol.control.Toggle = function(options)
Expand Down Expand Up @@ -41,6 +43,8 @@ ol.control.Toggle = function(options)
{ element: element.get(0)
});

this.set('defaultActive', options.defaultActive);

this.setActive (options.active);
}
ol.inherits(ol.control.Toggle, ol.control.Control);
Expand Down