[OpenLayers-Users] Auto toggling other controls off when using multiple external control panels

Josh Rosenthal maric423 at gmail.com
Tue Apr 14 08:53:15 EDT 2009


As always, its amazing what a fresh start allows one to see.
As an alternate (and, I suspect better solution), one can also accomplish
the goal of having activating a control on a panel deactivate controls on
other panels by passing in a custom activateControl when generating all
panels that are to be linked in this multi panel, as shown below.

The event solution (or some variation of it) is probably more version
stable, in the sense that a future change to panel.activateControl would
require adjustment in the custom method solution.  On the other hand, the
custom method works within the structure already set up to accomplish the
deactivation of other controls, and therefore seems preferable.  Am I
missing anything?  Any reason to prefer one over the other?

Thanks,

Josh

custom activateControl as follows:

      var multiPanelActivateControl = function (control) {
            if (!this.active) { return false; }
            if (control.type == OpenLayers.Control.TYPE_BUTTON) {
                control.trigger();
                this.redraw();
                return;
            }
            if (control.type == OpenLayers.Control.TYPE_TOGGLE) {
                if (control.active) {
                    control.deactivate();
                } else {
                    control.activate();
                }
                this.redraw();
                return;
            }

            var panelList =
map.getControlsByClass("OpenLayers.Control.Panel");
            for (var j=0, pLen=panelList.length; j<pLen; j++) {
                var currPanel = panelList[j];
                for (var i=0, len=currPanel.controls.length; i<len; i++) {
                    if (currPanel.controls[i] != control) {
                        if (currPanel.controls[i].type !=
OpenLayers.Control.TYPE_TOGGLE) {
                            currPanel.controls[i].deactivate();
                        }
                    }
                }
            }
            control.activate();
    };

declare panels like
panel = new OpenLayers.Control.Panel({div:
document.getElementById("mapToolBox2"),
activateControl:multiPanelActivateControl});

On Mon, Apr 13, 2009 at 7:17 PM, Josh Rosenthal <maric423 at gmail.com> wrote:

> Hi All,
> I'm running into a bit of a snag and am hoping someone can offer some
> advice.  I'm trying to build a map with multiple control panels, ie: Basic
> Navigation, Querying (draw a shape, generate a query based off the shape)
> etc.  For the most part its going quite well, but I'm running into a problem
> where I activate a control in one panel, click on a button in another panel,
> and the original controls remain active and both try to run... in other
> words, I'll turn on an Info control, and then switch to DrawPoint, and it'll
> be trying to do both.  Controls deactivate other controls within their own
> panel when clicked, but don't seem to know to deactivate the other panel's
> controls.
>
> Is there a straightforward way to do this?  Thus far, the best idea I've
> come up with (shown below) is setting a listener for each control when it
> activates, such that it runs through everything in the other panel.controls,
> and deactivates them all.  This strikes me as painfully awkward, and gets
> extremely annoying to scale if I choose to add a few more panels.  Is there
> any way to make the panels aware of each other's existence such that they
> deactivate the controls appropriately (or can anyone suggest a better of
> implementing this)?
>
> Thanks!
>
> Josh
>
>
> given two external panels, panel and selPan2, run the following at the end
> of initialization.
>
> for (eachControl in panel.controls) {
>
>  panel.controls[eachControl].events.register("activate",selPan2.controls,function
> () {
>         for (otherControls in this) {
>             this[otherControls].deactivate();
>         }
>     });
> }
>
> for (eachControl in selPan2.controls) {
>
>  selPan2.controls[eachControl].events.register("activate",panel.controls,function
> () {
>         for (otherControls in this) {
>             this[otherControls].deactivate();
>         }
>     });
> }
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20090414/40cc71bc/attachment.html


More information about the Users mailing list