`
soswane
  • 浏览: 21565 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

openlayers(四)BaseTypes.js

阅读更多
//首先是对String的扩展   
OpenLayers.String = {   
    //判断str是不是以sub开头   
    startsWith: function(str, sub) {   
        return (str.indexOf(sub) == 0);   
    },   
    //判断str是不是包含sub   
    contains: function(str, sub) {   
        return (str.indexOf(sub) != -1);   
    },   
       
    //str去空白符   
    trim: function(str) {   
        return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');   
    },   
       
    //把用横线连接的单词转换成驼峰形式,例如:my-name -> myName   
    camelize: function(str) {   
        var oStringList = str.split('-');   
        var camelizedString = oStringList[0];   
        for (var i=1, len=oStringList.length; i<len; i++) {   
            var s = oStringList[i];   
            camelizedString += s.charAt(0).toUpperCase() + s.substring(1);   
        }   
        return camelizedString;   
    },   
       
    //对字符串进行format,这个函数在prototype框架中也有。   
    format: function(template, context, args) {   
        if(!context) {   
            context = window;   
        }   
        // Example matching:    
        // str   = ${foo.bar}   
        // match = foo.bar   
        var replacer = function(str, match) {   
            var replacement;   
            // Loop through all subs. Example: ${a.b.c}   
            // 0 -> replacement = context[a];   
            // 1 -> replacement = context[a][b];   
            // 2 -> replacement = context[a][b][c];   
            var subs = match.split(/\.+/);   
            for (var i=0; i< subs.length; i++) {   
                if (i == 0) {   
                    replacement = context;   
                }   
                replacement = replacement[subs[i]];   
            }   
            if(typeof replacement == "function") {   
                replacement = args ?   
                    replacement.apply(null, args) :   
                    replacement();   
            }   
            // If replacement is undefined, return the string 'undefined'.   
            // This is a workaround for a bugs in browsers not properly    
            // dealing with non-participating groups in regular expressions:   
            // http://blog.stevenlevithan.com/archives/npcg-javascript   
            if (typeof replacement == 'undefined') {   
                return 'undefined';   
            } else {   
                return replacement;    
            }   
        };   
        return template.replace(OpenLayers.String.tokenRegEx, replacer);   
    },   
    /**  
     * Property: OpenLayers.String.tokenRegEx  
     * Used to find tokens in a string.  
     * Examples: ${a}, ${a.b.c}, ${a-b}, ${5}  
     */  
    tokenRegEx:  /\$\{([\w.]+?)\}/g,   
       
    /**  
     * Property: OpenLayers.String.numberRegEx  
     * Used to test strings as numbers.  
     */  
    numberRegEx: /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/,   
       
    /**  
     * APIFunction: OpenLayers.String.isNumeric  
     * Determine whether a string contains only a numeric value.  
     *  
     * Examples:  
     * (code)  
     * OpenLayers.String.isNumeric("6.02e23") // true  
     * OpenLayers.String.isNumeric("12 dozen") // false  
     * OpenLayers.String.isNumeric("4") // true  
     * OpenLayers.String.isNumeric(" 4 ") // false  
     * (end)  
     *  
     * Returns:  
     * {Boolean} String contains only a number.  
     */  
    isNumeric: function(value) {   
        return OpenLayers.String.numberRegEx.test(value);   
    },   
       
    /**  
     * APIFunction: numericIf  
     * Converts a string that appears to be a numeric value into a number.  
     *   
     * Returns  
     * {Number|String} a Number if the passed value is a number, a String  
     *     otherwise.   
     */  
    numericIf: function(value) {   
        return OpenLayers.String.isNumeric(value) ? parseFloat(value) : value;   
    }   
};   
//下面这段代码过时了,意思就是把startsWith绑到String.prototype上去。3.0中估计会删掉,下面还有很多这样的代码,都一样   
if (!String.prototype.startsWith) {   
    /**  
     * APIMethod: String.startsWith  
     * *Deprecated*. Whether or not a string starts with another string.   
     *   
     * Parameters:  
     * sStart - {Sring} The string we're testing for.  
     *    
     * Returns:  
     * {Boolean} Whether or not this string starts with the string passed in.  
     */  
    String.prototype.startsWith = function(sStart) {   
        OpenLayers.Console.warn(OpenLayers.i18n("methodDeprecated",   
                                {'newMethod':'OpenLayers.String.startsWith'}));   
        return OpenLayers.String.startsWith(this, sStart);   
    };   
}   
if (!String.prototype.contains) {   
    /**  
     * APIMethod: String.contains  
     * *Deprecated*. Whether or not a string contains another string.  
     *   
     * Parameters:  
     * str - {String} The string that we're testing for.  
     *   
     * Returns:  
     * {Boolean} Whether or not this string contains with the string passed in.  
     */  
    String.prototype.contains = function(str) {   
        OpenLayers.Console.warn(OpenLayers.i18n("methodDeprecated",   
                                  {'newMethod':'OpenLayers.String.contains'}));   
        return OpenLayers.String.contains(this, str);   
    };   
}   
if (!String.prototype.trim) {   
    /**  
     * APIMethod: String.trim  
     * *Deprecated*. Removes leading and trailing whitespace characters from a string.  
     *   
     * Returns:  
     * {String} A trimmed version of the string - all leading and   
     *          trailing spaces removed  
     */  
    String.prototype.trim = function() {   
        OpenLayers.Console.warn(OpenLayers.i18n("methodDeprecated",   
                                      {'newMethod':'OpenLayers.String.trim'}));   
        return OpenLayers.String.trim(this);   
    };   
}   
if (!String.prototype.camelize) {   
    /**  
     * APIMethod: String.camelize  
     * *Deprecated*. Camel-case a hyphenated string.   
     *     Ex. "chicken-head" becomes "chickenHead", and  
     *     "-chicken-head" becomes "ChickenHead".  
     *   
     * Returns:  
     * {String} The string, camelized  
     */  
    String.prototype.camelize = function() {   
        OpenLayers.Console.warn(OpenLayers.i18n("methodDeprecated",   
                                  {'newMethod':'OpenLayers.String.camelize'}));   
        return OpenLayers.String.camelize(this);   
    };   
}   
//对Number对象的一个扩展   
OpenLayers.Number = {   
    /**  
     * Property: decimalSeparator  
     * Decimal separator to use when formatting numbers.  
     */  
    decimalSeparator: ".",   
       
    /**  
     * Property: thousandsSeparator  
     * Thousands separator to use when formatting numbers.  
     */  
    thousandsSeparator: ",",   
       
    /**  
     * APIFunction: limitSigDigs  
     * Limit the number of significant digits on a float.  
     *   
     * Parameters:  
     * num - {Float}  
     * sig - {Integer}  
     *   
     * Returns:  
     * {Float} The number, rounded to the specified number of significant  
     *     digits.  
     */  
    limitSigDigs: function(num, sig) {   
        var fig = 0;   
        if (sig > 0) {   
            fig = parseFloat(num.toPrecision(sig));   
        }   
        return fig;   
    },   
       
    /**  
     * APIFunction: format  
     * Formats a number for output.  
     *   
     * Parameters:  
     * num  - {Float}  
     * dec  - {Integer} Number of decimal places to round to.  
     *        Defaults to 0. Set to null to leave decimal places unchanged.  
     * tsep - {String} Thousands separator.  
     *        Default is ",".  
     * dsep - {String} Decimal separator.  
     *        Default is ".".  
     *  
     * Returns:  
     * {String} A string representing the formatted number.  
     */  
    format: function(num, dec, tsep, dsep) {   
        dec = (typeof dec != "undefined") ? dec : 0;    
        tsep = (typeof tsep != "undefined") ? tsep :   
            OpenLayers.Number.thousandsSeparator;    
        dsep = (typeof dsep != "undefined") ? dsep :   
            OpenLayers.Number.decimalSeparator;   
        if (dec != null) {   
            num = parseFloat(num.toFixed(dec));   
        }   
        var parts = num.toString().split(".");   
        if (parts.length == 1 && dec == null) {   
            // integer where we do not want to touch the decimals   
            dec = 0;   
        }   
           
        var integer = parts[0];   
        if (tsep) {   
            var thousands = /(-?[0-9]+)([0-9]{3})/;    
            while(thousands.test(integer)) {    
                integer = integer.replace(thousands, "$1" + tsep + "$2");    
            }   
        }   
           
        var str;   
        if (dec == 0) {   
            str = integer;   
        } else {   
            var rem = parts.length > 1 ? parts[1] : "0";   
            if (dec != null) {   
                rem = rem + new Array(dec - rem.length + 1).join("0");   
            }   
            str = integer + dsep + rem;   
        }   
        return str;   
    }   
};   
if (!Number.prototype.limitSigDigs) {   
    /**  
     * APIMethod: Number.limitSigDigs  
     * *Deprecated*. Limit the number of significant digits on an integer. Does *not*  
     *     work with floats!  
     *   
     * Parameters:  
     * sig - {Integer}  
     *   
     * Returns:  
     * {Integer} The number, rounded to the specified number of significant digits.  
     *           If null, 0, or negative value passed in, returns 0  
     */  
    Number.prototype.limitSigDigs = function(sig) {   
        OpenLayers.Console.warn(OpenLayers.i18n("methodDeprecated",   
                              {'newMethod':'OpenLayers.Number.limitSigDigs'}));   
        return OpenLayers.Number.limitSigDigs(this, sig);   
    };   
}   
//对Function对象的一个扩展   
OpenLayers.Function = {   
    //这个函数很有用,可以指定函数中的this指针。在prototype框架中有这个函数   
    bind: function(func, object) {   
        // create a reference to all arguments past the second one   
        var args = Array.prototype.slice.apply(arguments, [2]);   
        return function() {   
            // Push on any additional arguments from the actual function call.   
            // These will come after those sent to the bind call.   
            var newArgs = args.concat(   
                Array.prototype.slice.apply(arguments, [0])   
            );   
            return func.apply(object, newArgs);   
        };   
    },   
       
    //扩展了一下bind,传参数时给了event对象   
    bindAsEventListener: function(func, object) {   
        return function(event) {   
            return func.call(object, event || window.event);   
        };   
    }   
};   
if (!Function.prototype.bind) {   
    /**  
     * APIMethod: Function.bind  
     * *Deprecated*. Bind a function to an object.   
     * Method to easily create closures with 'this' altered.  
     *   
     * Parameters:  
     * object - {Object} the this parameter  
     *   
     * Returns:  
     * {Function} A closure with 'this' altered to the first  
     *            argument.  
     */  
    Function.prototype.bind = function() {   
        OpenLayers.Console.warn(OpenLayers.i18n("methodDeprecated",   
                                {'newMethod':'OpenLayers.Function.bind'}));   
        // new function takes the same arguments with this function up front   
        Array.prototype.unshift.apply(arguments, [this]);   
        return OpenLayers.Function.bind.apply(null, arguments);   
    };   
}   
if (!Function.prototype.bindAsEventListener) {   
    /**  
     * APIMethod: Function.bindAsEventListener  
     * *Deprecated*. Bind a function to an object, and configure it to receive the  
     *     event object as first parameter when called.   
     *   
     * Parameters:  
     * object - {Object} A reference to this.  
     *   
     * Returns:  
     * {Function}  
     */  
    Function.prototype.bindAsEventListener = function(object) {   
        OpenLayers.Console.warn(OpenLayers.i18n("methodDeprecated",   
                        {'newMethod':'OpenLayers.Function.bindAsEventListener'}));   
        return OpenLayers.Function.bindAsEventListener(this, object);   
    };   
}   
//对Array对象进行了扩展   
OpenLayers.Array = {   
    /**  
     * APIMethod: filter  
     * Filter an array.  Provides the functionality of the  
     *     Array.prototype.filter extension to the ECMA-262 standard.  Where  
     *     available, Array.prototype.filter will be used.  
     *  
     * Based on well known example from http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:filter  
     *  
     * Parameters:  
     * array - {Array} The array to be filtered.  This array is not mutated.  
     *     Elements added to this array by the callback will not be visited.  
     * callback - {Function} A function that is called for each element in  
     *     the array.  If this function returns true, the element will be  
     *     included in the return.  The function will be called with three  
     *     arguments: the element in the array, the index of that element, and  
     *     the array itself.  If the optional caller parameter is specified  
     *     the callback will be called with this set to caller.  
     * caller - {Object} Optional object to be set as this when the callback  
     *     is called.  
     *  
     * Returns:  
     * {Array} An array of elements from the passed in array for which the  
     *     callback returns true.  
     */  
    filter: function(array, callback, caller) {   
        var selected = [];   
        if (Array.prototype.filter) {   
            selected = array.filter(callback, caller);   
        } else {   
            var len = array.length;   
            if (typeof callback != "function") {   
                throw new TypeError();   
            }   
            for(var i=0; i<len; i++) {   
                if (i in array) {   
                    var val = array[i];   
                    if (callback.call(caller, val, i, array)) {   
                        selected.push(val);   
                    }   
                }   
            }           
        }   
        return selected;   
    }   
       
};  





出处:http://blog.csdn.net/baozhifei/archive/2009/08/15/4450536.aspx
分享到:
评论

相关推荐

    openlayers : ol.js、ol.css 下载

    `ol.js`是OpenLayers的核心JavaScript库,包含了所有的地图渲染、图层管理、交互操作等功能;而`ol.css`则是配套的样式表文件,提供了默认的用户界面样式和地图元素的布局。 1. **OpenLayers 概述** OpenLayers ...

    openlayers热力图用heatmap.js

    OpenLayers 是一个强大的开源JavaScript库,用于在网页上创建交互式的地图应用。它支持多种地图服务,包括WMS、WMTS等,并且可以与其他JavaScript库集成,如D3.js和Leaflet。在这个场景中,我们将关注如何使用...

    OpenLayers.mobile.debug.js

    OpenLayers.mobile.debug.js

    openlayers : ol.css ol.js

    `ol.js` 文件则是OpenLayers的核心JavaScript库,包含了所有地图操作、图层管理、事件处理等功能的实现。通过这个文件,开发者可以创建地图实例、加载不同类型的图层、添加交互功能、处理用户事件等。它提供了一套...

    OpenLayers_6.1.0.zip

    OpenLayers 是一个专为WebGIS 客户端开发的JavaScript 类库包,用于实现对相关地图的操作展示,目前最新版本为Openlayers6,本文的版本为Openlayers6.11版本;

    openlayers河流动态轨迹.zip

    OpenLayers 是一个开源JavaScript库,专门用于在网页上创建交互式的地图应用。它支持多种地图数据源,包括WMS、WMTS、TMS等服务,以及GeoJSON、KML、GML等地理数据格式。"openlayers河流动态轨迹.zip"这个文件很可能...

    openlayers js库,最新版

    OpenLayers 是一个强大的开源JavaScript库,专为构建交互式的、基于Web的地图应用而设计。它提供了丰富的地图操作功能,如图层管理、地理坐标转换、瓦片图层、矢量图层、WMS和WFS服务支持等。最新版OpenLayers库是...

    WebGIS之OpenLayers全面解析源码.rar

    OpenLayers 是一个强大的开源JavaScript库,用于在Web上创建交互式的地图应用,是WebGIS(Web地理信息系统)领域中的重要工具。本资源“WebGIS之OpenLayers全面解析源码.rar”提供了OpenLayers的详细解析及随书源码...

    openlayers开发用到的js库.zip

    此外,JS(JavaScript)是网页开发中的主要脚本语言,OpenLayers完全用JavaScript编写,所以理解JavaScript基础和DOM操作对于使用OpenLayers至关重要。例如,你可以通过JavaScript动态添加图层、更改地图视图、响应...

    OpenLayers.debug.js

    OpenLayers.debug.js

    openlayers5T弹窗.zip

    OpenLayers 是一个强大的开源JavaScript库,用于在网页上创建交互式的地图应用。它支持多种地图服务,包括WMS、WMTS等,并且可以轻松地与各种数据源集成。在这个"openlayers5T弹窗.zip"压缩包中,重点探讨的是...

    openLayers_geoserver常见问题.

    十四、改良 OpenLayers 的 Popup OpenLayers 提供了多种方式来改良 Popup,例如使用 `OpenLayers.Popup.Anchored` 或 `OpenLayers.Popup.Fixed`。以下代码示例使用 `OpenLayers.Popup.Anchored` 改良 Popup: ```...

    openlayers中文文档1.zip

    OpenLayers 是一个强大的开源JavaScript库,用于在网页上创建交互式的地图应用。它支持多种地图服务,包括WMS、WMTS、TMS等,并且兼容各种浏览器,包括桌面端和移动设备。这个"openlayers中文文档1.zip"压缩包包含的...

    OpenLayers-2.5 examples .rar

    OpenLayers 是一个强大的开源JavaScript库,专为在Web上创建交互式地图应用而设计。它支持多种地图数据源,包括WMS、WFS、TMS等,并与多种地理信息系统(GIS)服务器如Geoserver、MapServer等无缝集成。这个...

    openLayers2热力图.zip

    openlayers2还没将heatmap类加入,到openlayers3采用。但是考虑到浏览器的兼容性兼容到更低本版的IE,所以这里采用openlayers2版本开发...想要进行热力图开发就必须先对其扩展 (heatmap-openlayers.js)。下载直接可用

    OpenLayers JS文件下载

    OpenLayers 源文件,OpenLayers 源文件,OpenLayers 源文件,OpenLayers 源文件,OpenLayers 源文件,OpenLayers 源文件,OpenLayers 源文件,OpenLayers 源文件、

    openlayers资源v6.5.0.zip

    OpenLayers 是一个开源JavaScript库,专门用于在网页上创建交互式的地图应用。它支持多种地图服务,包括WMS、WMTS、TMS等,并且能够处理多种地图数据格式,如GeoJSON、KML和GML。OpenLayers v6.5.0是该库的一个版本...

    scottcoder-openlayers-v6.5.0.zip

    OpenLayers是一个强大的JavaScript库,专为构建交互式地图应用程序而设计。它允许开发者在网页上轻松地展示地图,包括来自多个来源的数据,如WMS、WMTS、TMS等服务。在最新的版本6.5.0中,OpenLayers继续提供了丰富...

Global site tag (gtag.js) - Google Analytics