[OpenLayers-Dev] Re: How to add control to LayerSwitcher?

Xavier Mamano (jorix) xavier.mamano at gmail.com
Sat May 19 05:52:23 EDT 2012


Hi TcKev,

Management of events in the control window has changed completely and there
AFAIK no way to make custom controls like this.

Try using this experimental code:

Add this metod on "LayerSwitcher.js":

    keepEvents:function(div) {
        this.keepEventsDiv = new OpenLayers.Events(this, div, null, true);

        var triggerButton = function (evt) { 
            var element = OpenLayers.Event.element(evt),
                buttonclick = this.map.events.extensions.buttonclick;        
            if (element) {
                element = buttonclick.getPressedButton(element);
                if (element && OpenLayers.Element.hasClass(element,
"olButton")) {
                    buttonclick.buttonClick(evt);
                }
            }
        };
        var listeners = {
            "mousedown": function (evt) {
                this.mousedown = true;
                triggerButton(evt);
                OpenLayers.Event.stop(evt, true);
            },
            "mousemove": function (evt) {
                if (this.mousedown) {
                    OpenLayers.Event.stop(evt, true);
                }
            },
            "mouseup": function (evt) {
                if (this.mousedown) {
                    this.mousedown = false;
                    triggerButton(evt);
                    OpenLayers.Event.stop(evt, true);
                }
            },
            "click": function (evt) {
                var element = OpenLayers.Event.element(evt);
                triggerButton(evt);
                OpenLayers.Event.stop(evt, true);
            },
            "mouseout": function (evt) {
                this.mousedown = false;
            },
            "dblclick": function (evt) {
                var element = OpenLayers.Event.element(evt);
                triggerButton(evt);
                OpenLayers.Event.stop(evt, true);
            },
            "touchstart": function (evt) {
                OpenLayers.Event.stop(evt, true);
            },
            scope: this
        }
        this.keepEventsDiv.on(listeners);
    },

and add:

this.keepEvents(this.layersDiv);

on "draw" method before "return this.div;"

NOTE: Need to work on unregister "listeners" and destruction of
"keepEventsDiv", but for now there is enough to test.

Xavier


TcKev wrote
> 
> The best I can tell in a reasonable amount of time, OpenLayers does not
> allow me to do what I want.
> 
> I can replace my select tag with an input/checkbox tag and mimic the
> OpenLayers.Event.observe handling done in the LayerSwitcher.  That is easy
> to do because taking over the control of the checkbox is easy,
> inputElem.checked  =  !inputElem.checked.
> However the taking over the operations of the select tag would be
> considerably more difficult because a dropdown control is alot more
> complicated than a checkbox.  For that, I could really benefit from being
> able to direct OpenLayers to allow the default browser behavior for that
> control.
> 
> My solution is to scale back what I want to do and use a simple checkbox
> instead of a dropdown.
> 

--
View this message in context: http://osgeo-org.1560.n6.nabble.com/How-to-add-control-to-LayerSwitcher-tp4975445p4975707.html
Sent from the OpenLayers Dev mailing list archive at Nabble.com.


More information about the Dev mailing list