OpenLayers OpenLayers

Ticket #1061: closeCallback.2.patch

File closeCallback.2.patch, 7.0 kB (added by euzuro, 1 year ago)

slightly modified version of eric's latest patch

  • tests/test_Popup.html

    old new  
    2929    } 
    3030     
    3131    function test_02_Popup_constructor (t) { 
    32         t.plan( 5 ); 
     32        t.plan( 8 ); 
    3333 
    3434        var id = "chicken"; 
    3535        var w = 500; 
     
    3939        var lat = 40; 
    4040        var ll = new OpenLayers.LonLat(lon, lat); 
    4141        var content = "foo"; 
     42        var closePopupCallback = function(e) { 
     43            //this should get triggered by the "observer.observer();" call below 
     44            t.ok(true, "closePopupCallback called") 
     45        }; 
    4246 
    4347        popup = new OpenLayers.Popup(id, 
    4448                                     ll, 
    4549                                     sz, 
    46                                      content); 
     50                                     content, 
     51                                     true, 
     52                                     closePopupCallback); 
    4753 
    4854        t.ok( popup instanceof OpenLayers.Popup, "new OpenLayers.Popup returns Popup object" ); 
    4955        t.eq(popup.id, id, "popup.id set correctly"); 
    5056        t.ok(popup.lonlat.equals(ll), "popup.lonlat set correctly"); 
    5157        t.ok(popup.size.equals(sz), "popup.size set correctly"); 
    5258        t.eq(popup.contentHTML, content, "contentHTML porpoerty of set correctly"); 
     59 
     60        // test that a browser event is registered on click on popup closebox 
     61        var closeImgDiv = popup.groupDiv.childNodes[1]; 
     62        var cacheID = closeImgDiv._eventCacheID; 
     63        for (var i = 0; i < OpenLayers.Event.observers[cacheID].length; i++) { 
     64            var observer = OpenLayers.Event.observers[cacheID][i]; 
     65            if (observer.element == closeImgDiv) { 
     66                t.ok(true, "An event was registered for the close box element"); 
     67                t.eq(observer.name, "click", "A click event was registered for the close box element"); 
     68                //call the registered observer to make sure it's the right one 
     69                observer.observer(); 
     70                break; 
     71            } 
     72        } 
    5373    } 
    5474 
    5575    function test_Popup_updatePosition(t) { 
  • lib/OpenLayers/Popup/AnchoredBubble.js

    old new  
    3232     *     a 'size' (<OpenLayers.Size>) and 'offset' (<OpenLayers.Pixel>)  
    3333     *     (Note that this is generally an <OpenLayers.Icon>). 
    3434     * closeBox - {Boolean} 
     35     * closeBoxCallback - {Function} Function to be called on closeBox click. 
    3536     */ 
    36     initialize:function(id, lonlat, size, contentHTML, anchor, closeBox) { 
     37    initialize:function(id, lonlat, size, contentHTML, anchor, closeBox, 
     38                        closeBoxCallback) { 
    3739        OpenLayers.Popup.Anchored.prototype.initialize.apply(this, arguments); 
    3840    }, 
    3941 
  • lib/OpenLayers/Popup/Anchored.js

    old new  
    3838    * anchor - {Object} Object which must expose a 'size' <OpenLayers.Size>  
    3939    *     and 'offset' <OpenLayers.Pixel> (generally an <OpenLayers.Icon>). 
    4040    * closeBox - {Boolean} 
     41    * closeBoxCallback - {Function} Function to be called on closeBox click. 
    4142    */ 
    42     initialize:function(id, lonlat, size, contentHTML, anchor, closeBox) { 
    43         var newArguments = new Array(id, lonlat, size, contentHTML, closeBox); 
     43    initialize:function(id, lonlat, size, contentHTML, anchor, closeBox, 
     44                        closeBoxCallback) { 
     45        var newArguments = new Array(id, lonlat, size, contentHTML, closeBox, 
     46                                     closeBoxCallback); 
    4447        OpenLayers.Popup.prototype.initialize.apply(this, newArguments); 
    4548 
    4649        this.anchor = (anchor != null) ? anchor  
  • lib/OpenLayers/Popup.js

    old new  
    117117    * contentHTML - {String}          The HTML content to display inside the  
    118118    *                                 popup. 
    119119    * closeBox - {Boolean}            Whether to display a close box inside 
    120     *                                 the popup.  
     120    *                                 the popup. 
     121    * closeBoxCallback - {Function}   Function to be called on closeBox click. 
    121122    */ 
    122     initialize:function(id, lonlat, size, contentHTML, closeBox) { 
     123    initialize:function(id, lonlat, size, contentHTML, closeBox, closeBoxCallback) { 
    123124        if (id == null) { 
    124125            id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_"); 
    125126        } 
     
    165166            closeImg.style.top = this.padding + "px"; 
    166167            this.groupDiv.appendChild(closeImg); 
    167168 
    168             var closePopup = function(e) { 
     169            var closePopup = closeBoxCallback || function(e) { 
    169170                this.hide(); 
    170171                OpenLayers.Event.stop(e); 
    171172            } 
  • examples/select-feature-openpopup.html

    old new  
    1212    </style> 
    1313    <script src="../lib/OpenLayers.js"></script> 
    1414    <script type="text/javascript"> 
    15         var map, drawControls, select; 
     15        var map, drawControls, selectControl, selectedFeature; 
     16        function onPopupClose(evt) { 
     17            selectControl.unselect(selectedFeature); 
     18        } 
    1619        function onFeatureSelect(feature) { 
     20            selectedFeature = feature; 
    1721            popup = new OpenLayers.Popup.Anchored("chicken",  
    1822                                     feature.geometry.getBounds().getCenterLonLat(), 
    1923                                     new OpenLayers.Size(250,75), 
    2024                                     "<div style='font-size:.8em'>Feature: " + feature.id +"<br />Area: " + feature.geometry.getArea()+"</div>", 
    21                                      null, true); 
     25                                     null, true, onPopupClose); 
    2226            feature.popup = popup; 
    2327            map.addPopup(popup); 
    2428        } 
     
    3842            map.addControl(new OpenLayers.Control.LayerSwitcher()); 
    3943            map.addControl(new OpenLayers.Control.MousePosition()); 
    4044             
     45            selectControl = new OpenLayers.Control.SelectFeature(polygonLayer, 
     46                {onSelect: onFeatureSelect, onUnselect: onFeatureUnselect}); 
    4147            drawControls = { 
    4248                polygon: new OpenLayers.Control.DrawFeature(polygonLayer, 
    4349                            OpenLayers.Handler.Polygon), 
    44                 select: new OpenLayers.Control.SelectFeature(polygonLayer, 
    45                 {onSelect: onFeatureSelect, 
    46                  onUnselect: onFeatureUnselect 
    47                 }) 
     50                select: selectControl 
    4851            }; 
    4952             
    5053            for(var key in drawControls) {