代码
1
//
OpenLayers BaseType.js这里包含了一些自定义字符串、数字、数组等操作函数.
2 OpenLayers.String = {
3
4 // 判断字符串str是否以字符串sub为首
5 startsWith: function (str, sub) {
6 return (str.indexOf(sub) == 0 );
7 },
8
9 // 判断str字符串是否包含sub字符串
10 contains: function (str, sub) {
11 return (str.indexOf(sub) != - 1 );
12 },
13
14
15 // 除掉字符中的前后空格
16 trim: function (str) {
17 return str.replace( / ^\s\s* / , '' ).replace( / \s\s*$ / , '' );
18 },
19
20 // 将CSS名称命名规则,变成驼峰命名规则
21 camelize: function (str) {
22 var oStringList = str.split( ' - ' );
23 var camelizedString = oStringList[ 0 ];
24 for ( var i = 1 , len = oStringList.length; i < len; i ++ ) {
25 var s = oStringList[i];
26 camelizedString += s.charAt( 0 ).toUpperCase() + s.substring( 1 );
27 }
28 return camelizedString;
29 },
30
31 /* *
32 * APIFunction: format
33 * Given a string with tokens in the form ${token}, return a string
34 * with tokens replaced with properties from the given context
35 * object. Represent a literal "${" by doubling it, e.g. "${${".
36 * Returns:
37 * {String} A string with tokens replaced from the context object.
38 */
39 format: function (template, context, args) {
40 if ( ! context) {
41 context = window;
42 }
43
44 // Example matching:
45 // str = ${foo.bar}
46 // match = foo.bar
47 var replacer = function (str, match) {
48 var replacement;
49
50 // Loop through all subs. Example: ${a.b.c}
51 // 0 -> replacement = context[a];
52 // 1 -> replacement = context[a][b];
53 // 2 -> replacement = context[a][b][c];
54 var subs = match.split( / \.+ / );
55 for ( var i = 0 ; i < subs.length; i ++ ) {
56 if (i == 0 ) {
57 replacement = context;
58 }
59
60 replacement = replacement[subs[i]];
61 }
62
63 if ( typeof replacement == " function " ) {
64 replacement = args ?
65 replacement.apply( null , args) :
66 replacement();
67 }
68
69 // If replacement is undefined, return the string 'undefined'.
70 // This is a workaround for a bugs in browsers not properly
71 // dealing with non-participating groups in regular expressions:
72 // http://blog.stevenlevithan.com/archives/npcg-javascript
73 if ( typeof replacement == ' undefined ' ) {
74 return ' undefined ' ;
75 } else {
76 return replacement;
77 }
78 };
79
80 return template.replace(OpenLayers.String.tokenRegEx, replacer);
81 },
82
83 /* *
84 * Property: OpenLayers.String.tokenRegEx
85 * Used to find tokens in a string.
86 * Examples: ${a}, ${a.b.c}, ${a-b}, ${5}
87 */
88 tokenRegEx: / \$\{([\w.]+?)\} / g,
89
90 /* *
91 * Property: OpenLayers.String.numberRegEx
92 * Used to test strings as numbers.
93 */
94 numberRegEx: / ^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$ / ,
95
96
97 // 是否为数字
98 isNumeric: function (value) {
99 return OpenLayers.String.numberRegEx.test(value);
100 },
101
102
103 // 如果是一下数字字符串,则转换成数字,
104 numericIf: function (value) {
105 return OpenLayers.String.isNumeric(value) ? parseFloat(value) : value;
106 }
107
108 };
109
110
111
112 /* *
113 * Namespace: OpenLayers.Number
114 * Contains convenience functions for manipulating numbers.
115 */
116 OpenLayers.Number = {
117
118 // 十位数分隔符
119 decimalSeparator: " . " ,
120 // 千位数分隔符
121 thousandsSeparator: " , " ,
122
123 // 指定数字精度
124 limitSigDigs: function (num, sig) {
125 var fig = 0 ;
126 if (sig > 0 ) {
127 fig = parseFloat(num.toPrecision(sig));
128 }
129 return fig;
130 },
131
132 // 格式化数字
133 format: function (num, dec, tsep, dsep) {
134 dec = ( typeof dec != " undefined " ) ? dec : 0 ;
135 tsep = ( typeof tsep != " undefined " ) ? tsep :
136 OpenLayers.Number.thousandsSeparator;
137 dsep = ( typeof dsep != " undefined " ) ? dsep :
138 OpenLayers.Number.decimalSeparator;
139
140 if (dec != null ) {
141 num = parseFloat(num.toFixed(dec));
142 }
143
144 var parts = num.toString().split( " . " );
145 if (parts.length == 1 && dec == null ) {
146 // integer where we do not want to touch the decimals
147 dec = 0 ;
148 }
149
150 var integer = parts[ 0 ];
151 if (tsep) {
152 var thousands = / (-?[0-9]+)([0-9]{3}) / ;
153 while (thousands.test(integer)) {
154 integer = integer.replace(thousands, " $1 " + tsep + " $2 " );
155 }
156 }
157
158 var str;
159 if (dec == 0 ) {
160 str = integer;
161 } else {
162 var rem = parts.length > 1 ? parts[ 1 ] : " 0 " ;
163 if (dec != null ) {
164 rem = rem + new Array(dec - rem.length + 1 ).join( " 0 " );
165 }
166 str = integer + dsep + rem;
167 }
168 return str;
169 }
170 };
171
172
173
174 OpenLayers.Function = {
175
176 // 返回预先绑定在拥有该函数(方法)的对象上的函数实例, 返回的方法将和原来的方法具有相同的参数。
177 bind: function (func, object) {
178 // create a reference to all arguments past the second one
179 var args = Array.prototype.slice.apply(arguments, [ 2 ]);
180 return function () {
181 // Push on any additional arguments from the actual function call.
182 // These will come after those sent to the bind call.
183 var newArgs = args.concat(
184 Array.prototype.slice.apply(arguments, [ 0 ])
185 );
186 return func.apply(object, newArgs);
187 };
188 },
189
190 // 返回预先绑定在拥有该函数(方法)的对象上的函数实例, 返回的方法将把当前的事件对象作为它的参数。
191 bindAsEventListener: function (func, object) {
192 return function (event) {
193 return func.call(object, event || window.event);
194 };
195 },
196
197
198 False : function () {
199 return false ;
200 },
201
202 True : function () {
203 return true ;
204 }
205 };
206
207
208 /* *
209 * Namespace: OpenLayers.Array
210 * Contains convenience functions for array manipulation.
211 */
212 OpenLayers.Array = {
213
214 // 对数组中的每一个元素调用参数中指定的过滤函数,并将对于过滤函数返回值为true的那些数组元素集合为新的数组返回。
215 filter: function (array, callback, caller) {
216 var selected = [];
217 if (Array.prototype.filter) {
218 selected = array.filter(callback, caller);
219 } else {
220 var len = array.length;
221 if ( typeof callback != " function " ) {
222 throw new TypeError();
223 }
224 for ( var i = 0 ; i < len; i ++ ) {
225 if (i in array) {
226 var val = array[i];
227 if (callback.call(caller, val, i, array)) {
228 selected.push(val);
229 }
230 }
231 }
232 }
233 return selected;
234 }
235
236 };
237
2 OpenLayers.String = {
3
4 // 判断字符串str是否以字符串sub为首
5 startsWith: function (str, sub) {
6 return (str.indexOf(sub) == 0 );
7 },
8
9 // 判断str字符串是否包含sub字符串
10 contains: function (str, sub) {
11 return (str.indexOf(sub) != - 1 );
12 },
13
14
15 // 除掉字符中的前后空格
16 trim: function (str) {
17 return str.replace( / ^\s\s* / , '' ).replace( / \s\s*$ / , '' );
18 },
19
20 // 将CSS名称命名规则,变成驼峰命名规则
21 camelize: function (str) {
22 var oStringList = str.split( ' - ' );
23 var camelizedString = oStringList[ 0 ];
24 for ( var i = 1 , len = oStringList.length; i < len; i ++ ) {
25 var s = oStringList[i];
26 camelizedString += s.charAt( 0 ).toUpperCase() + s.substring( 1 );
27 }
28 return camelizedString;
29 },
30
31 /* *
32 * APIFunction: format
33 * Given a string with tokens in the form ${token}, return a string
34 * with tokens replaced with properties from the given context
35 * object. Represent a literal "${" by doubling it, e.g. "${${".
36 * Returns:
37 * {String} A string with tokens replaced from the context object.
38 */
39 format: function (template, context, args) {
40 if ( ! context) {
41 context = window;
42 }
43
44 // Example matching:
45 // str = ${foo.bar}
46 // match = foo.bar
47 var replacer = function (str, match) {
48 var replacement;
49
50 // Loop through all subs. Example: ${a.b.c}
51 // 0 -> replacement = context[a];
52 // 1 -> replacement = context[a][b];
53 // 2 -> replacement = context[a][b][c];
54 var subs = match.split( / \.+ / );
55 for ( var i = 0 ; i < subs.length; i ++ ) {
56 if (i == 0 ) {
57 replacement = context;
58 }
59
60 replacement = replacement[subs[i]];
61 }
62
63 if ( typeof replacement == " function " ) {
64 replacement = args ?
65 replacement.apply( null , args) :
66 replacement();
67 }
68
69 // If replacement is undefined, return the string 'undefined'.
70 // This is a workaround for a bugs in browsers not properly
71 // dealing with non-participating groups in regular expressions:
72 // http://blog.stevenlevithan.com/archives/npcg-javascript
73 if ( typeof replacement == ' undefined ' ) {
74 return ' undefined ' ;
75 } else {
76 return replacement;
77 }
78 };
79
80 return template.replace(OpenLayers.String.tokenRegEx, replacer);
81 },
82
83 /* *
84 * Property: OpenLayers.String.tokenRegEx
85 * Used to find tokens in a string.
86 * Examples: ${a}, ${a.b.c}, ${a-b}, ${5}
87 */
88 tokenRegEx: / \$\{([\w.]+?)\} / g,
89
90 /* *
91 * Property: OpenLayers.String.numberRegEx
92 * Used to test strings as numbers.
93 */
94 numberRegEx: / ^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$ / ,
95
96
97 // 是否为数字
98 isNumeric: function (value) {
99 return OpenLayers.String.numberRegEx.test(value);
100 },
101
102
103 // 如果是一下数字字符串,则转换成数字,
104 numericIf: function (value) {
105 return OpenLayers.String.isNumeric(value) ? parseFloat(value) : value;
106 }
107
108 };
109
110
111
112 /* *
113 * Namespace: OpenLayers.Number
114 * Contains convenience functions for manipulating numbers.
115 */
116 OpenLayers.Number = {
117
118 // 十位数分隔符
119 decimalSeparator: " . " ,
120 // 千位数分隔符
121 thousandsSeparator: " , " ,
122
123 // 指定数字精度
124 limitSigDigs: function (num, sig) {
125 var fig = 0 ;
126 if (sig > 0 ) {
127 fig = parseFloat(num.toPrecision(sig));
128 }
129 return fig;
130 },
131
132 // 格式化数字
133 format: function (num, dec, tsep, dsep) {
134 dec = ( typeof dec != " undefined " ) ? dec : 0 ;
135 tsep = ( typeof tsep != " undefined " ) ? tsep :
136 OpenLayers.Number.thousandsSeparator;
137 dsep = ( typeof dsep != " undefined " ) ? dsep :
138 OpenLayers.Number.decimalSeparator;
139
140 if (dec != null ) {
141 num = parseFloat(num.toFixed(dec));
142 }
143
144 var parts = num.toString().split( " . " );
145 if (parts.length == 1 && dec == null ) {
146 // integer where we do not want to touch the decimals
147 dec = 0 ;
148 }
149
150 var integer = parts[ 0 ];
151 if (tsep) {
152 var thousands = / (-?[0-9]+)([0-9]{3}) / ;
153 while (thousands.test(integer)) {
154 integer = integer.replace(thousands, " $1 " + tsep + " $2 " );
155 }
156 }
157
158 var str;
159 if (dec == 0 ) {
160 str = integer;
161 } else {
162 var rem = parts.length > 1 ? parts[ 1 ] : " 0 " ;
163 if (dec != null ) {
164 rem = rem + new Array(dec - rem.length + 1 ).join( " 0 " );
165 }
166 str = integer + dsep + rem;
167 }
168 return str;
169 }
170 };
171
172
173
174 OpenLayers.Function = {
175
176 // 返回预先绑定在拥有该函数(方法)的对象上的函数实例, 返回的方法将和原来的方法具有相同的参数。
177 bind: function (func, object) {
178 // create a reference to all arguments past the second one
179 var args = Array.prototype.slice.apply(arguments, [ 2 ]);
180 return function () {
181 // Push on any additional arguments from the actual function call.
182 // These will come after those sent to the bind call.
183 var newArgs = args.concat(
184 Array.prototype.slice.apply(arguments, [ 0 ])
185 );
186 return func.apply(object, newArgs);
187 };
188 },
189
190 // 返回预先绑定在拥有该函数(方法)的对象上的函数实例, 返回的方法将把当前的事件对象作为它的参数。
191 bindAsEventListener: function (func, object) {
192 return function (event) {
193 return func.call(object, event || window.event);
194 };
195 },
196
197
198 False : function () {
199 return false ;
200 },
201
202 True : function () {
203 return true ;
204 }
205 };
206
207
208 /* *
209 * Namespace: OpenLayers.Array
210 * Contains convenience functions for array manipulation.
211 */
212 OpenLayers.Array = {
213
214 // 对数组中的每一个元素调用参数中指定的过滤函数,并将对于过滤函数返回值为true的那些数组元素集合为新的数组返回。
215 filter: function (array, callback, caller) {
216 var selected = [];
217 if (Array.prototype.filter) {
218 selected = array.filter(callback, caller);
219 } else {
220 var len = array.length;
221 if ( typeof callback != " function " ) {
222 throw new TypeError();
223 }
224 for ( var i = 0 ; i < len; i ++ ) {
225 if (i in array) {
226 var val = array[i];
227 if (callback.call(caller, val, i, array)) {
228 selected.push(val);
229 }
230 }
231 }
232 }
233 return selected;
234 }
235
236 };
237