OpenLayers OpenLayers

Ticket #1061: patch-1061-A2.diff

File patch-1061-A2.diff, 7.2 kB (added by elemoine, 1 year ago)

new patch with an example and tests

  • 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) {}; 
    4243 
     44        OpenLayers.Function.bindAsEventListener = function(func, obj) { return func }; 
    4345        popup = new OpenLayers.Popup(id, 
    4446                                     ll, 
    4547                                     sz, 
    46                                      content); 
     48                                     content, 
     49                                     true, 
     50                                     closePopupCallback); 
    4751 
    4852        t.ok( popup instanceof OpenLayers.Popup, "new OpenLayers.Popup returns Popup object" ); 
    4953        t.eq(popup.id, id, "popup.id set correctly"); 
    5054        t.ok(popup.lonlat.equals(ll), "popup.lonlat set correctly"); 
    5155        t.ok(popup.size.equals(sz), "popup.size set correctly"); 
    5256        t.eq(popup.contentHTML, content, "contentHTML porpoerty of set correctly"); 
     57        // test that a browser event is registered on click on popup closebox 
     58        var map = new OpenLayers.Map('map'); 
     59        map.addPopup(popup); 
     60        var ok = false; 
     61        var closeImgDiv = document.getElementById(popup.id + "_close"); 
     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                t.ok(observer.observer == closePopupCallback, 
     69                    "The proper callback function was registered for the close box element"); 
     70                ok = true; 
     71                break; 
     72            } 
     73        } 
     74        if (!ok) { 
     75            t.fail("No event was registered for the close box element"); 
     76        } 
    5377    } 
    5478 
    5579    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) {