OpenLayers OpenLayers

Changeset 7886

Show
Ignore:
Timestamp:
08/28/08 16:04:17 (3 months ago)
Author:
euzuro
Message:

Bubble out the code that handles the re-sizing of the popup into an APIMethod: updateSize(). Other change is that now instead of using the 'contentHTML' string property to autosize, we will now use the actual contentDiv's 'innerHTML' property. This is possible because in the setContentHTML() function, we switch around the order and set the conentDiv.innerHTML *before* calling updateSize(). Seemingly minor, this change actually does wonders towards distancing us from the horrendous idea that the 'contentHTML' property was in the first place. Now, if users go in and fudge with the contentDiv DOMElement directly, they can still benefit from the auto-sizing. In fact, 'contentHTML' can be totally ignored altogether. joy. All tests pass, including an eyeballing of the acceptance test examples/popupMatrix.html in ff2 and ie7. Mil gracias to cr5 for the speedy review (Closes #1708)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/lib/OpenLayers/Popup.js

    r7684 r7886  
    5656    /**  
    5757     * Property: contentHTML  
    58      * {String} The HTML that this popup displays
    59      */ 
    60     contentHTML: ""
     58     * {String} An HTML string for this popup to display
     59     */ 
     60    contentHTML: null
    6161     
    6262    /**  
     
    185185    *                                 be shown. 
    186186    * size - {<OpenLayers.Size>}      The size of the popup. 
    187     * contentHTML - {String}          The HTML content to display inside the  
     187    * contentHTML - {String}          An HTML string to display inside the   
    188188    *                                 popup. 
    189189    * closeBox - {Boolean}            Whether to display a close box inside 
     
    449449 
    450450    /** 
     451     * APIMethod: updateSize 
     452     * Auto size the popup so that it precisely fits its contents (as  
     453     *     determined by this.contentDiv.innerHTML). Popup size will, of 
     454     *     course, be limited by the available space on the current map 
     455     */ 
     456    updateSize: function() { 
     457         
     458        // determine actual render dimensions of the contents by putting its 
     459        // contents into a fake contentDiv (for the CSS) and then measuring it 
     460        var preparedHTML = "<div class='" + this.contentDisplayClass+ "'>" +  
     461            this.contentDiv.innerHTML +  
     462            "<div>"; 
     463        var realSize = OpenLayers.Util.getRenderedDimensions( 
     464            preparedHTML, null, { displayClass: this.displayClass } 
     465        ); 
     466 
     467        // is the "real" size of the div is safe to display in our map? 
     468        var safeSize = this.getSafeContentSize(realSize); 
     469 
     470        var newSize = null; 
     471        if (safeSize.equals(realSize)) { 
     472            //real size of content is small enough to fit on the map,  
     473            // so we use real size. 
     474            newSize = realSize; 
     475 
     476        } else { 
     477 
     478            //make a new OL.Size object with the clipped dimensions  
     479            // set or null if not clipped. 
     480            var fixedSize = new OpenLayers.Size(); 
     481            fixedSize.w = (safeSize.w < realSize.w) ? safeSize.w : null; 
     482            fixedSize.h = (safeSize.h < realSize.h) ? safeSize.h : null; 
     483         
     484            if (fixedSize.w && fixedSize.h) { 
     485                //content is too big in both directions, so we will use  
     486                // max popup size (safeSize), knowing well that it will  
     487                // overflow both ways.                 
     488                newSize = safeSize; 
     489            } else { 
     490                //content is clipped in only one direction, so we need to  
     491                // run getRenderedDimensions() again with a fixed dimension 
     492                var clippedSize = OpenLayers.Util.getRenderedDimensions( 
     493                    preparedHTML, fixedSize,  
     494                    { displayClass: this.contentDisplayClass } 
     495                ); 
     496                 
     497                //if the clipped size is still the same as the safeSize,  
     498                // that means that our content must be fixed in the  
     499                // offending direction. If overflow is 'auto', this means  
     500                // we are going to have a scrollbar for sure, so we must  
     501                // adjust for that. 
     502                // 
     503                var currentOverflow = OpenLayers.Element.getStyle( 
     504                    this.contentDiv, "overflow" 
     505                ); 
     506                if ( (currentOverflow != "hidden") &&  
     507                     (clippedSize.equals(safeSize)) ) { 
     508                    var scrollBar = OpenLayers.Util.getScrollbarWidth(); 
     509                    if (fixedSize.w) { 
     510                        clippedSize.h += scrollBar; 
     511                    } else { 
     512                        clippedSize.w += scrollBar; 
     513                    } 
     514                } 
     515                 
     516                newSize = this.getSafeContentSize(clippedSize); 
     517            } 
     518        }                         
     519        this.setSize(newSize);      
     520    },     
     521 
     522    /** 
    451523     * Method: setBackgroundColor 
    452524     * Sets the background color of the popup. 
     
    512584    setContentHTML:function(contentHTML) { 
    513585 
    514         var preparedHTML; 
    515586        if (contentHTML != null) { 
    516587            this.contentHTML = contentHTML; 
    517588        } 
    518589        
    519         if (this.autoSize) { 
    520             //fake the contentDiv for the CSS context 
    521             preparedHTML = "<div class='" + this.contentDisplayClass+ "'>" + this.contentHTML + "<div>"; 
    522   
    523             // determine actual render dimensions of the contents 
    524             var realSize =  
    525                 OpenLayers.Util.getRenderedDimensions(preparedHTML, null,  
    526                     { displayClass: this.displayClass }); 
    527  
    528             // is the "real" size of the div is safe to display in our map? 
    529             var safeSize = this.getSafeContentSize(realSize); 
    530  
    531             var newSize = null; 
    532               
    533             if (safeSize.equals(realSize)) { 
    534                 //real size of content is small enough to fit on the map,  
    535                 // so we use real size. 
    536                 newSize = realSize; 
    537  
    538             } else { 
    539  
    540                 //make a new OL.Size object with the clipped dimensions  
    541                 // set or null if not clipped. 
    542                 var fixedSize = new OpenLayers.Size(); 
    543                 fixedSize.w = (safeSize.w < realSize.w) ? safeSize.w : null; 
    544                 fixedSize.h = (safeSize.h < realSize.h) ? safeSize.h : null; 
    545              
    546                 if (fixedSize.w && fixedSize.h) { 
    547                     //content is too big in both directions, so we will use  
    548                     // max popup size (safeSize), knowing well that it will  
    549                     // overflow both ways.                 
    550                     newSize = safeSize; 
    551                 } else { 
    552                     //content is clipped in only one direction, so we need to  
    553                     // run getRenderedDimensions() again with a fixed dimension 
    554                     var clippedSize = OpenLayers.Util.getRenderedDimensions( 
    555                         preparedHTML, fixedSize,  
    556                         { displayClass: this.contentDisplayClass } 
    557                     ); 
    558                      
    559                     //if the clipped size is still the same as the safeSize,  
    560                     // that means that our content must be fixed in the  
    561                     // offending direction. If overflow is 'auto', this means  
    562                     // we are going to have a scrollbar for sure, so we must  
    563                     // adjust for that. 
    564                     // 
    565                     var currentOverflow = OpenLayers.Element.getStyle( 
    566                         this.contentDiv, "overflow" 
    567                     ); 
    568                     if ( (currentOverflow != "hidden") &&  
    569                          (clippedSize.equals(safeSize)) ) { 
    570                         var scrollBar = OpenLayers.Util.getScrollbarWidth(); 
    571                         if (fixedSize.w) { 
    572                             clippedSize.h += scrollBar; 
    573                         } else { 
    574                             clippedSize.w += scrollBar; 
    575                         } 
    576                     } 
    577                      
    578                     newSize = this.getSafeContentSize(clippedSize); 
    579                 } 
    580             }                         
    581             this.setSize(newSize);      
    582         }         
    583  
    584         if (this.contentDiv != null) { 
     590        if ((this.contentDiv != null) &&  
     591            (this.contentHTML != null) && 
     592            (this.contentHTML != this.contentDiv.innerHTML)) { 
    585593            this.contentDiv.innerHTML = this.contentHTML; 
    586594        }     
     595 
     596        if (this.autoSize) { 
     597            this.updateSize(); 
     598        } 
     599 
    587600    }, 
    588601     
  • trunk/openlayers/tests/Popup.html

    r7656 r7886  
    1818        var firstID = popup.id; 
    1919        t.ok(popup.size.equals(size), "good default popup.size"); 
    20         t.eq(popup.contentHTML, "", "good default popup.contentHTML"); 
     20        t.eq(popup.contentHTML, null, "good default popup.contentHTML"); 
    2121        t.eq(popup.backgroundColor, OpenLayers.Popup.COLOR, "good default popup.backgroundColor"); 
    2222        t.eq(popup.opacity, OpenLayers.Popup.OPACITY, "good default popup.opacity"); 
  • trunk/openlayers/tests/Popup/Anchored.html

    r6724 r7886  
    1414        t.ok(popup.id.startsWith("OpenLayers.Popup.Anchored"), "valid default popupid"); 
    1515        var firstID = popup.id; 
    16         t.eq(popup.contentHTML, "", "good default popup.contentHTML"); 
     16        t.eq(popup.contentHTML, null, "good default popup.contentHTML"); 
    1717 
    1818