OpenLayers OpenLayers

Changeset 1205

Show
Ignore:
Timestamp:
08/14/06 17:49:19 (2 years ago)
Author:
euzuro
Message:

split Util.js into two files. BaseTypes.js will contain all the OL Base types, such as Pixel, LonLat, Bounds, etc., as well as all the standard javascript basetypes (string, number, etc) extensions. All else remains in Util.js. In theory I have fixed all dependencies. If this doesn't work, I'll rollback.

Files:

Legend:

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

    r1193 r1205  
    5252        "Rico/Corner.js", 
    5353        "Rico/Color.js", 
     54        "OpenLayers/BaseTypes.js", 
    5455        "OpenLayers/Util.js", 
    5556        "OpenLayers/Ajax.js", 
  • trunk/openlayers/lib/OpenLayers/Feature.js

    r997 r1205  
    77/** 
    88 * @class 
    9  * @require: OpenLayers/Util.js 
     9 * @requires OpenLayers/Util.js 
    1010 */ 
    1111OpenLayers.Feature = Class.create(); 
  • trunk/openlayers/lib/OpenLayers/Layer/Grid.js

    r1199 r1205  
    44 
    55// @require: OpenLayers/Layer/HTTPRequest.js 
    6 // @require: OpenLayers/Util.js 
    76 
    87/** 
  • trunk/openlayers/lib/OpenLayers/Layer/HTTPRequest.js

    r1171 r1205  
    22 * See http://svn.openlayers.org/trunk/openlayers/license.txt for the full 
    33 * text of the license. */ 
     4 
    45// @require: OpenLayers/Layer.js 
    5 // @require: OpenLayers/Util.js 
    66 
    77/** 
  • trunk/openlayers/lib/OpenLayers/Map.js

    r1200 r1205  
    44 
    55// @require: OpenLayers/Util.js 
     6// @require: OpenLayers/Events.js 
    67 
    78/** 
  • trunk/openlayers/lib/OpenLayers/Util.js

    r1201 r1205  
    77*/ 
    88OpenLayers.Util = new Object(); 
    9  
    10 /** 
    11 * @class This class represents a screen coordinate, in x and y coordinates 
    12 */ 
    13 OpenLayers.Pixel = Class.create(); 
    14 OpenLayers.Pixel.prototype = { 
    15      
    16     /** @type float */ 
    17     x: 0.0, 
    18  
    19     /** @type float */ 
    20     y: 0.0, 
    21      
    22     /**  
    23     * @constructor 
    24     * 
    25     * @param {float} x 
    26     * @param {float} y 
    27     */ 
    28     initialize: function(x, y) { 
    29         this.x = x; 
    30         this.y = y; 
    31     }, 
    32      
    33     /** 
    34     * @return string representation of Pixel. ex: "x=200.4,y=242.2" 
    35     * @type str 
    36     */ 
    37     toString:function() { 
    38         return ("x=" + this.x + ",y=" + this.y); 
    39     }, 
    40  
    41     /** 
    42      * @deprecated 
    43      *  
    44      * @type OpenLayers.Pixel 
    45      */ 
    46     copyOf:function() { 
    47         return this.clone(); 
    48     }, 
    49          
    50     /** 
    51      * @type OpenLayers.Pixel 
    52      */ 
    53     clone:function() { 
    54         return new OpenLayers.Pixel(this.x, this.y);  
    55     }, 
    56      
    57     /**  
    58     * @param {OpenLayers.Pixel} px 
    59     *  
    60     * @return whether or not the point passed in as parameter is equal to this 
    61     *          note that if px passed in is null, returns false 
    62     * @type bool 
    63     */ 
    64     equals:function(px) { 
    65         var equals = false; 
    66         if (px != null) { 
    67             equals = ((this.x == px.x && this.y == px.y) || 
    68                       (isNaN(this.x) && isNaN(this.y) && isNaN(px.x) && isNaN(px.y))); 
    69         } 
    70         return equals; 
    71     }, 
    72  
    73     /** 
    74     * @param {int} x 
    75     * @param {int} y 
    76     *  
    77     * @return a new Pixel with this pixel's x&y augmented by the  
    78     *         values passed in. 
    79     * @type OpenLayers.Pixel 
    80     */ 
    81     add:function(x, y) { 
    82         return new OpenLayers.Pixel(this.x + x, this.y + y); 
    83     }, 
    84  
    85     /** 
    86     * @param {OpenLayers.Pixel} px 
    87     *  
    88     * @return a new Pixel with this pixel's x&y augmented by the  
    89     *         x&y values of the pixel passed in. 
    90     * @type OpenLayers.Pixel 
    91     */ 
    92     offset:function(px) { 
    93         return this.add(px.x, px.y);                 
    94     }, 
    95      
    96     /** @final @type str */ 
    97     CLASS_NAME: "OpenLayers.Pixel" 
    98 }; 
    99  
    100  
    101 /** 
    102 * @class This class represents a width and height pair 
    103 */ 
    104 OpenLayers.Size = Class.create(); 
    105 OpenLayers.Size.prototype = { 
    106  
    107     /** @type float */ 
    108     w: 0.0, 
    109      
    110     /** @type float */ 
    111     h: 0.0, 
    112  
    113  
    114     /**  
    115     * @constructor 
    116     *  
    117     * @param {float} w  
    118     * @param {float} h  
    119     */ 
    120     initialize: function(w, h) { 
    121         this.w = w; 
    122         this.h = h; 
    123     }, 
    124  
    125     /**  
    126     * @return String representation of OpenLayers.Size object.  
    127     *         (ex. <i>"w=55,h=66"</i>) 
    128     * @type String 
    129     */ 
    130     toString:function() { 
    131         return ("w=" + this.w + ",h=" + this.h); 
    132     }, 
    133  
    134     /**  
    135      * @deprecated 
    136      *  
    137      * @return New OpenLayers.Size object with the same w and h values 
    138      * @type OpenLayers.Size 
    139      */ 
    140     copyOf:function() { 
    141         return this.clone(); 
    142     }, 
    143  
    144     /**  
    145      * @return New OpenLayers.Size object with the same w and h values 
    146      * @type OpenLayers.Size 
    147      */ 
    148     clone:function() { 
    149         return new OpenLayers.Size(this.w, this.h); 
    150     }, 
    151  
    152     /**  
    153     * @param {OpenLayers.Size} sz 
    154     * @returns Boolean value indicating whether the passed-in OpenLayers.Size  
    155     *          object has the same w and h components as this 
    156     *          note that if sz passed in is null, returns false 
    157     * 
    158     * @type bool 
    159     */ 
    160     equals:function(sz) { 
    161         var equals = false; 
    162         if (sz != null) { 
    163             equals = ((this.w == sz.w && this.h == sz.h) || 
    164                       (isNaN(this.w) && isNaN(this.h) && isNaN(sz.w) && isNaN(sz.h))); 
    165         } 
    166         return equals; 
    167     }, 
    168      
    169     /** @final @type String */ 
    170     CLASS_NAME: "OpenLayers.Size" 
    171 }; 
    172  
    173 /** 
    174 * @class This class represents a longitude and latitude pair 
    175 */ 
    176 OpenLayers.LonLat = Class.create(); 
    177 OpenLayers.LonLat.prototype = { 
    178  
    179     /** @type float */ 
    180     lon: 0.0, 
    181      
    182     /** @type float */ 
    183     lat: 0.0, 
    184  
    185     /** 
    186     * @constructor 
    187     *  
    188     * @param {float} lon 
    189     * @param {float} lat 
    190     */ 
    191     initialize: function(lon, lat) { 
    192         this.lon = lon; 
    193         this.lat = lat; 
    194     }, 
    195      
    196     /**  
    197     * @return String representation of OpenLayers.LonLat object.  
    198     *         (ex. <i>"lon=5,lat=42"</i>) 
    199     * @type String 
    200     */ 
    201     toString:function() { 
    202         return ("lon=" + this.lon + ",lat=" + this.lat); 
    203     }, 
    204  
    205     /**  
    206     * @return Shortened String representation of OpenLayers.LonLat object.  
    207     *         (ex. <i>"5, 42"</i>) 
    208     * @type String 
    209     */ 
    210     toShortString:function() { 
    211         return (this.lon + ", " + this.lat); 
    212     }, 
    213  
    214     /**  
    215      * @deprecated 
    216      *  
    217      * @return New OpenLayers.LonLat object with the same lon and lat values 
    218      * @type OpenLayers.LonLat 
    219      */ 
    220     copyOf:function() { 
    221         return this.clone(); 
    222     }, 
    223  
    224     /**  
    225      * @return New OpenLayers.LonLat object with the same lon and lat values 
    226      * @type OpenLayers.LonLat 
    227      */ 
    228     clone:function() { 
    229         return new OpenLayers.LonLat(this.lon, this.lat); 
    230     }, 
    231  
    232     /**  
    233     * @param {float} lon 
    234     * @param {float} lat 
    235     * 
    236     * @return A new OpenLayers.LonLat object with the lon and lat passed-in 
    237     *         added to this's.  
    238     * @type OpenLayers.LonLat 
    239     */ 
    240     add:function(lon, lat) { 
    241         return new OpenLayers.LonLat(this.lon + lon, this.lat + lat); 
    242     }, 
    243  
    244     /**  
    245     * @param {OpenLayers.LonLat} ll 
    246     * @returns Boolean value indicating whether the passed-in OpenLayers.LonLat 
    247     *          object has the same lon and lat components as this 
    248     *          note that if ll passed in is null, returns false 
    249     * 
    250     * @type bool 
    251     */ 
    252     equals:function(ll) { 
    253         var equals = false; 
    254         if (ll != null) { 
    255             equals = ((this.lon == ll.lon && this.lat == ll.lat) || 
    256                       (isNaN(this.lon) && isNaN(this.lat) && isNaN(ll.lon) && isNaN(ll.lat))); 
    257         } 
    258         return equals; 
    259     }, 
    260      
    261     /** @final @type String */ 
    262     CLASS_NAME: "OpenLayers.LonLat" 
    263 }; 
    264  
    265 /** Alternative constructor that builds a new OpenLayers.LonLat from a  
    266 *    parameter string 
    267 *  
    268 * @constructor 
    269 *  
    270 * @param {String} str Comma-separated Lon,Lat coordinate string.  
    271 *                     (ex. <i>"5,40"</i>) 
    272 * 
    273 * @returns New OpenLayers.LonLat object built from the passed-in String. 
    274 * @type OpenLayers.LonLat 
    275 */ 
    276 OpenLayers.LonLat.fromString = function(str) { 
    277     var pair = str.split(","); 
    278     return new OpenLayers.LonLat(parseFloat(pair[0]),  
    279                                  parseFloat(pair[1])); 
    280 }; 
    281  
    282  
    283  
    284  
    285 /** 
    286 * @class This class represents a bounding box.  
    287 *        Data stored as left, bottom, right, top floats 
    288 */ 
    289 OpenLayers.Bounds = Class.create(); 
    290 OpenLayers.Bounds.prototype = { 
    291  
    292     /** @type float */ 
    293     left: 0.0, 
    294  
    295     /** @type float */ 
    296     bottom: 0.0, 
    297  
    298     /** @type float */ 
    299     right: 0.0, 
    300  
    301     /** @type float */ 
    302     top: 0.0,     
    303  
    304     /** 
    305     * @constructor 
    306     * 
    307     * @param {float} left 
    308     * @param {float} bottom 
    309     * @param {float} right 
    310     * @param {float} top 
    311     * 
    312     */ 
    313     initialize: function(left, bottom, right, top) { 
    314         this.left = left; 
    315         this.bottom = bottom; 
    316         this.right = right; 
    317         this.top = top; 
    318     }, 
    319  
    320     /** 
    321      * @deprecated 
    322      *  
    323      * @returns A fresh copy of the bounds 
    324      * @type OpenLayers.Bounds 
    325      */ 
    326     copyOf:function() { 
    327         return this.clone(); 
    328     }, 
    329  
    330     /** 
    331      * @returns A fresh copy of the bounds 
    332      * @type OpenLayers.Bounds 
    333      */ 
    334     clone:function() { 
    335         return new OpenLayers.Bounds(this.left, this.bottom,  
    336                                      this.right, this.top); 
    337     }, 
    338  
    339     /**  
    340     * @param {OpenLayers.Bounds} bounds 
    341     * @returns Boolean value indicating whether the passed-in OpenLayers.Bounds 
    342     *          object has the same left, right, top, bottom components as this 
    343     *           note that if bounds passed in is null, returns false 
    344     * 
    345     * @type bool 
    346     */ 
    347     equals:function(bounds) { 
    348         var equals = false; 
    349         if (bounds != null) { 
    350             equals = ((this.left == bounds.left) &&  
    351                       (this.right == bounds.right) && 
    352                       (this.top == bounds.top) &&  
    353                       (this.bottom == bounds.bottom)); 
    354         } 
    355         return equals; 
    356     }, 
    357  
    358     /**  
    359     * @return String representation of OpenLayers.Bounds object.  
    360     *         (ex.<i>"left-bottom=(5,42) right-top=(10,45)"</i>) 
    361     * @type String 
    362     */ 
    363     toString:function(){ 
    364         return ( "left-bottom=(" + this.left + "," + this.bottom + ")" 
    365                  + " right-top=(" + this.right + "," + this.top + ")" ); 
    366     }, 
    367  
    368     /**  
    369     * @return Simple String representation of OpenLayers.Bounds object. 
    370     *         (ex. <i>"5,42,10,45"</i>) 
    371     * @type String 
    372     */ 
    373     toBBOX:function() { 
    374         return (this.left + "," + this.bottom + "," 
    375                 + this.right + "," + this.top); 
    376     }, 
    377      
    378     /** 
    379     * @returns The width of the bounds 
    380     * @type float 
    381     */ 
    382     getWidth:function() { 
    383         return (this.right - this.left); 
    384     }, 
    385  
    386     /** 
    387     * @returns The height of the bounds 
    388     * @type float 
    389     */ 
    390     getHeight:function() { 
    391         return (this.top - this.bottom); 
    392     }, 
    393  
    394     /** 
    395     * @returns An OpenLayers.Size which represents the size of the box 
    396     * @type OpenLayers.Size 
    397     */ 
    398     getSize:function() { 
    399         return new OpenLayers.Size(this.getWidth(), this.getHeight()); 
    400     }, 
    401  
    402     /** 
    403     * @returns An OpenLayers.Pixel which represents the center of the bounds 
    404     * @type OpenLayers.Pixel 
    405     */ 
    406     getCenterPixel:function() { 
    407         return new OpenLayers.Pixel( (this.left + this.right) / 2, 
    408                                      (this.bottom + this.top) / 2); 
    409     }, 
    410  
    411     /** 
    412     * @returns An OpenLayers.LonLat which represents the center of the bounds 
    413     * @type OpenLayers.LonLat 
    414     */ 
    415     getCenterLonLat:function() { 
    416         return new OpenLayers.LonLat( (this.left + this.right) / 2, 
    417                                       (this.bottom + this.top) / 2); 
    418     }, 
    419  
    420     /** 
    421     * @param {float} x 
    422     * @param {float} y 
    423     * 
    424     * @returns A new OpenLayers.Bounds whose coordinates are the same as this,  
    425     *          but shifted by the passed-in x and y values 
    426     * @type OpenLayers.Bounds 
    427     */ 
    428     add:function(x, y){ 
    429         return new OpenLayers.Box(this.left + x, this.bottom + y, 
    430                                   this.right + x, this.top + y); 
    431     }, 
    432  
    433     /** 
    434     * @param {float} x 
    435     * @param {float} y 
    436     * @param {Boolean} inclusive Whether or not to include the border.  
    437     *                            Default is true 
    438     * 
    439     * @return Whether or not the passed-in coordinates are within this bounds 
    440     * @type Boolean 
    441     */ 
    442     contains:function(x, y, inclusive) { 
    443      
    444         //set default 
    445         if (inclusive == null) { 
    446             inclusive = true; 
    447         } 
    448          
    449         var contains = false; 
    450         if (inclusive) { 
    451             contains = ((x >= this.left) && (x <= this.right) &&  
    452                         (y >= this.bottom) && (y <= this.top)); 
    453         } else { 
    454             contains = ((x > this.left) && (x < this.right) &&  
    455                         (y > this.bottom) && (y < this.top)); 
    456         }               
    457         return contains; 
    458     }, 
    459   
    460     /** 
    461     * @param {OpenLayers.Bounds} bounds 
    462     * @param {Boolean} partial If true, only part of passed-in  
    463     *                          OpenLayers.Bounds needs be within this bounds.  
    464     *                          If false, the entire passed-in bounds must be 
    465     *                          within. Default is false 
    466     * @param {Boolean} inclusive Whether or not to include the border.  
    467     *                            Default is true 
    468     * 
    469     * @return Whether or not the passed-in OpenLayers.Bounds object is  
    470     *         contained within this bounds.  
    471     * @type Boolean 
    472     */ 
    473     containsBounds:function(bounds, partial, inclusive) { 
    474  
    475         //set defaults 
    476         if (partial == null) { 
    477             partial = false; 
    478         } 
    479         if (inclusive == null) { 
    480             inclusive = true; 
    481         } 
    482  
    483         var inLeft; 
    484         var inTop; 
    485         var inRight; 
    486         var inBottom; 
    487          
    488         if (inclusive) { 
    489             inLeft = (bounds.left >= this.left) && (bounds.left <= this.right); 
    490             inTop = (bounds.top >= this.bottom) && (bounds.top <= this.top); 
    491             inRight= (bounds.right >= this.left) && (bounds.right <= this.right); 
    492             inBottom = (bounds.bottom >= this.bottom) && (bounds.bottom <= this.top); 
    493         } else { 
    494             inLeft = (bounds.left > this.left) && (bounds.left < this.right); 
    495             inTop = (bounds.top > this.bottom) && (bounds.top < this.top); 
    496             inRight= (bounds.right > this.left) && (bounds.right < this.right); 
    497             inBottom = (bounds.bottom > this.bottom) && (bounds.bottom < this.top); 
    498         } 
    499          
    500         return (partial) ? (inTop || inBottom) && (inLeft || inRight ) 
    501                          : (inTop && inLeft && inBottom && inRight); 
    502     }, 
    503  
    504     /**  
    505      * @param {OpenLayers.LonLat} lonlat 
    506      * 
    507      * @returns The quadrant ("br" "tr" "tl" "bl") of the bounds in which  
    508      *           the coordinate lies. 
    509      * @type String 
    510      */ 
    511     determineQuadrant: function(lonlat) { 
    512      
    513         var quadrant = ""; 
    514         var center = this.getCenterLonLat(); 
    515          
    516         quadrant += (lonlat.lat < center.lat) ? "b" : "t"; 
    517         quadrant += (lonlat.lon < center.lon) ? "l" : "r"; 
    518      
    519         return quadrant;  
    520     }, 
    521  
    522     /** @final @type String */ 
    523     CLASS_NAME: "OpenLayers.Bounds" 
    524 }; 
    525  
    526 /** Alternative constructor that builds a new OpenLayers.Bounds from a  
    527 *    parameter string 
    528 *  
    529 * @constructor 
    530 *  
    531 * @param {String} str Comma-separated bounds string. (ex. <i>"5,42,10,45"</i>) 
    532 * 
    533 * @returns New OpenLayers.Bounds object built from the passed-in String. 
    534 * @type OpenLayers.Bounds 
    535 */ 
    536 OpenLayers.Bounds.fromString = function(str) { 
    537     var bounds = str.split(","); 
    538     return OpenLayers.Bounds.fromArray(bounds); 
    539 }; 
    540  
    541 /** Alternative constructor that builds a new OpenLayers.Bounds 
    542 *    from an array 
    543 *  
    544 * @constructor 
    545 *  
    546 * @param {Array} bbox Array of bounds values (ex. <i>[5,42,10,45]</i>) 
    547 * 
    548 * @returns New OpenLayers.Bounds object built from the passed-in Array. 
    549 * @type OpenLayers.Bounds 
    550 */ 
    551 OpenLayers.Bounds.fromArray = function(bbox) { 
    552     return new OpenLayers.Bounds(parseFloat(bbox[0]), 
    553                                  parseFloat(bbox[1]), 
    554                                  parseFloat(bbox[2]), 
    555                                  parseFloat(bbox[3])); 
    556 }; 
    557  
    558 /** Alternative constructor that builds a new OpenLayers.Bounds 
    559 *    from an OpenLayers.Size 
    560 *  
    561 * @constructor 
    562 *  
    563 * @param {OpenLayers.Size} size 
    564 * 
    565 * @returns New OpenLayers.Bounds object built with top and left set to 0 and 
    566 *           bottom right taken from the passed-in OpenLayers.Size. 
    567 * @type OpenLayers.Bounds 
    568 */ 
    569 OpenLayers.Bounds.fromSize = function(size) { 
    570     return new OpenLayers.Bounds(0, 
    571                                  size.h, 
    572                                  size.w, 
    573                                  0); 
    574 }; 
    575 /** 
    576  * @param {String} quadrant  
    577  *  
    578  * @returns The opposing quadrant ("br" "tr" "tl" "bl"). For Example, if  
    579  *           you pass in "bl" it returns "tr", if you pass in "br" it  
    580  *           returns "tl", etc. 
    581  * @type String 
    582  */ 
    583 OpenLayers.Bounds.oppositeQuadrant = function(quadrant) { 
    584     var opp = ""; 
    585      
    586     opp += (quadrant.charAt(0) == 't') ? 'b' : 't'; 
    587     opp += (quadrant.charAt(1) == 'l') ? 'r' : 'l'; 
    588      
    589     return opp; 
    590 }; 
    591  
    592 // Some other helpful things 
    593  
    594 /** 
    595 * @param {String} sStart 
    596 *  
    597 * @returns Whether or not this string starts with the string passed in. 
    598 * @type Boolean 
    599 */ 
    600 String.prototype.startsWith = function(sStart){ 
    601     return (this.substr(0,sStart.length) == sStart); 
    602 }; 
    603  
    604 /** 
    605 * @param {String} str 
    606 *  
    607 * @returns Whether or not this string contains with the string passed in. 
    608 * @type Boolean 
    609 */ 
    610 String.prototype.contains = function(str){ 
    611     return (this.indexOf(str) != -1); 
    612 }; 
    613  
    614 /** 
    615 * @returns A trimmed version of the string - all leading and  
    616 *          trailing spaces removed 
    617 * @type String 
    618 */ 
    619 String.prototype.trim = function() { 
    620      
    621     var b = 0; 
    622     while(this.substr(b,1) == " ") { 
    623         b++; 
    624     } 
    625      
    626     var e = this.length - 1; 
    627     while(this.substr(e,1) == " ") { 
    628         e--; 
    629     } 
    630      
    631     return this.substring(b, e+1); 
    632 }; 
    633  
    634 /** Remove an object from an array. Iterates through the array 
    635 *    to find the item, then removes it. 
    636 * 
    637 * @param {Object} item 
    638 *  
    639 * @returns A reference to the array 
    640 * @type Array 
    641 */ 
    642 Array.prototype.remove = function(item) { 
    643     for(var i=0; i < this.length; i++) { 
    644         if(this[i] == item) { 
    645             this.splice(i,1); 
    646             //break;more than once?? 
    647         } 
    648     } 
    649     return this; 
    650 } 
    651  
    652 /** 
    653  * @deprecated 
    654  *  
    655  * @returns A fresh copy of the array 
    656  * @type Array 
    657  */ 
    658 Array.prototype.copyOf = function() { 
    659     return this.clone(); 
    660 }; 
    661  
    662 /** 
    663 * @returns A fresh copy of the array 
    664 * @type Array 
    665 */ 
    666 Array.prototype.clone = function() { 
    667   var clone = new Array(); 
    668   for (var i = 0; i < this.length; i++) { 
    669       clone[i] = this[i]; 
    670   } 
    671   return clone; 
    672 }; 
    673  
    674 /** 
    675 */ 
    676 Array.prototype.clear = function() { 
    677     this.length = 0; 
    678 }; 
    679  
    680  
    681 /** NOTE: Works only with integer values does *not* work with floats! 
    682  *  
    683  * @param {int} sig 
    684  *  
    685  * @returns The number, rounded to the specified number of significant digits. 
    686  *          If null, 0, or negaive value passed in, returns 0 
    687  * @type int 
    688  */ 
    689 Number.prototype.limitSigDigs = function(sig) { 
    690     var number = (sig > 0) ? this.toString() : 0; 
    691     if (sig < number.length) { 
    692         var exp = number.length - sig; 
    693         number = Math.round( this / Math.pow(10, exp)) * Math.pow(10, exp); 
    694     } 
    695     return parseInt(number); 
    696 } 
    6979 
    69810/**