Format_wev8.js
12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*!
* Ext JS Library 3.0.0
* Copyright(c) 2006-2009 Ext JS, LLC
* licensing@extjs.com
* http://www.extjs.com/license
*/
/**
* @class Ext.util.Format
* Reusable data formatting functions
* @singleton
*/
Ext.util.Format = function(){
var trimRe = /^\s+|\s+$/g;
return {
/**
* Truncate a string and add an ellipsis ('...') to the end if it exceeds the specified length
* @param {String} value The string to truncate
* @param {Number} length The maximum length to allow before truncating
* @param {Boolean} word True to try to find a common work break
* @return {String} The converted text
*/
ellipsis : function(value, len, word){
if(value && value.length > len){
if(word){
var vs = value.substr(0, len - 2);
var index = Math.max(vs.lastIndexOf(' '), vs.lastIndexOf('.'), vs.lastIndexOf('!'), vs.lastIndexOf('?'));
if(index == -1 || index < (len - 15)){
return value.substr(0, len - 3) + "...";
}else{
return vs.substr(0, index) + "...";
}
} else{
return value.substr(0, len - 3) + "...";
}
}
return value;
},
/**
* Checks a reference and converts it to empty string if it is undefined
* @param {Mixed} value Reference to check
* @return {Mixed} Empty string if converted, otherwise the original value
*/
undef : function(value){
return value !== undefined ? value : "";
},
/**
* Checks a reference and converts it to the default value if it's empty
* @param {Mixed} value Reference to check
* @param {String} defaultValue The value to insert of it's undefined (defaults to "")
* @return {String}
*/
defaultValue : function(value, defaultValue){
return value !== undefined && value !== '' ? value : defaultValue;
},
/**
* Convert certain characters (&, <, >, and ') to their HTML character equivalents for literal display in web pages.
* @param {String} value The string to encode
* @return {String} The encoded text
*/
htmlEncode : function(value){
return !value ? value : String(value).replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """);
},
/**
* Convert certain characters (&, <, >, and ') from their HTML character equivalents.
* @param {String} value The string to decode
* @return {String} The decoded text
*/
htmlDecode : function(value){
return !value ? value : String(value).replace(/>/g, ">").replace(/</g, "<").replace(/"/g, '"').replace(/&/g, "&");
},
/**
* Trims any whitespace from either side of a string
* @param {String} value The text to trim
* @return {String} The trimmed text
*/
trim : function(value){
return String(value).replace(trimRe, "");
},
/**
* Returns a substring from within an original string
* @param {String} value The original text
* @param {Number} start The start index of the substring
* @param {Number} length The length of the substring
* @return {String} The substring
*/
substr : function(value, start, length){
return String(value).substr(start, length);
},
/**
* Converts a string to all lower case letters
* @param {String} value The text to convert
* @return {String} The converted text
*/
lowercase : function(value){
return String(value).toLowerCase();
},
/**
* Converts a string to all upper case letters
* @param {String} value The text to convert
* @return {String} The converted text
*/
uppercase : function(value){
return String(value).toUpperCase();
},
/**
* Converts the first character only of a string to upper case
* @param {String} value The text to convert
* @return {String} The converted text
*/
capitalize : function(value){
return !value ? value : value.charAt(0).toUpperCase() + value.substr(1).toLowerCase();
},
// private
call : function(value, fn){
if(arguments.length > 2){
var args = Array.prototype.slice.call(arguments, 2);
args.unshift(value);
return eval(fn).apply(window, args);
}else{
return eval(fn).call(window, value);
}
},
/**
* Format a number as US currency
* @param {Number/String} value The numeric value to format
* @return {String} The formatted currency string
*/
usMoney : function(v){
v = (Math.round((v-0)*100))/100;
v = (v == Math.floor(v)) ? v + ".00" : ((v*10 == Math.floor(v*10)) ? v + "0" : v);
v = String(v);
var ps = v.split('.');
var whole = ps[0];
var sub = ps[1] ? '.'+ ps[1] : '.00';
var r = /(\d+)(\d{3})/;
while (r.test(whole)) {
whole = whole.replace(r, '$1' + ',' + '$2');
}
v = whole + sub;
if(v.charAt(0) == '-'){
return '-$' + v.substr(1);
}
return "$" + v;
},
/**
* Parse a value into a formatted date using the specified format pattern.
* @param {String/Date} value The value to format (Strings must conform to the format expected by the javascript Date object's <a href="http://www.w3schools.com/jsref/jsref_parse.asp">parse()</a> method)
* @param {String} format (optional) Any valid date format string (defaults to 'm/d/Y')
* @return {String} The formatted date string
*/
date : function(v, format){
if(!v){
return "";
}
if(!Ext.isDate(v)){
v = new Date(Date.parse(v));
}
return v.dateFormat(format || "m/d/Y");
},
/**
* Returns a date rendering function that can be reused to apply a date format multiple times efficiently
* @param {String} format Any valid date format string
* @return {Function} The date formatting function
*/
dateRenderer : function(format){
return function(v){
return Ext.util.Format.date(v, format);
};
},
// private
stripTagsRE : /<\/?[^>]+>/gi,
/**
* Strips all HTML tags
* @param {Mixed} value The text from which to strip tags
* @return {String} The stripped text
*/
stripTags : function(v){
return !v ? v : String(v).replace(this.stripTagsRE, "");
},
stripScriptsRe : /(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)/ig,
/**
* Strips all script tags
* @param {Mixed} value The text from which to strip script tags
* @return {String} The stripped text
*/
stripScripts : function(v){
return !v ? v : String(v).replace(this.stripScriptsRe, "");
},
/**
* Simple format for a file size (xxx bytes, xxx KB, xxx MB)
* @param {Number/String} size The numeric value to format
* @return {String} The formatted file size
*/
fileSize : function(size){
if(size < 1024) {
return size + " bytes";
} else if(size < 1048576) {
return (Math.round(((size*10) / 1024))/10) + " KB";
} else {
return (Math.round(((size*10) / 1048576))/10) + " MB";
}
},
/**
* It does simple math for use in a template, for example:<pre><code>
* var tpl = new Ext.Template('{value} * 10 = {value:math("* 10")}');
* </code></pre>
* @return {Function} A function that operates on the passed value.
*/
math : function(){
var fns = {};
return function(v, a){
if(!fns[a]){
fns[a] = new Function('v', 'return v ' + a + ';');
}
return fns[a](v);
}
}(),
/**
* Rounds the passed number to the required decimal precision.
* @param {Number/String} value The numeric value to round.
* @param {Number} precision The number of decimal places to which to round the first parameter's value.
* @return {Number} The rounded value.
*/
round : function(value, precision) {
var result = Number(value);
if (typeof precision == 'number') {
precision = Math.pow(10, precision);
result = Math.round(value * precision) / precision;
}
return result;
},
/**
* Formats the number according to the format string.
* <div style="margin-left:40px">examples (123456.789):
* <div style="margin-left:10px">
* 0 - (123456) show only digits, no precision<br>
* 0.00 - (123456.78) show only digits, 2 precision<br>
* 0.0000 - (123456.7890) show only digits, 4 precision<br>
* 0,000 - (123,456) show comma and digits, no precision<br>
* 0,000.00 - (123,456.78) show comma and digits, 2 precision<br>
* 0,0.00 - (123,456.78) shortcut method, show comma and digits, 2 precision<br>
* To reverse the grouping (,) and decimal (.) for international numbers, add /i to the end.
* For example: 0.000,00/i
* </div></div>
* @param {Number} v The number to format.
* @param {String} format The way you would like to format this text.
* @return {String} The formatted number.
*/
number: function(v, format) {
if(!format){
return v;
}
v = Ext.num(v, NaN);
if (isNaN(v)){
return '';
}
var comma = ',',
dec = '.',
i18n = false,
neg = v < 0;
v = Math.abs(v);
if(format.substr(format.length - 2) == '/i'){
format = format.substr(0, format.length - 2);
i18n = true;
comma = '.';
dec = ',';
}
var hasComma = format.indexOf(comma) != -1,
psplit = (i18n ? format.replace(/[^\d\,]/g, '') : format.replace(/[^\d\.]/g, '')).split(dec);
if(1 < psplit.length){
v = v.toFixed(psplit[1].length);
}else if(2 < psplit.length){
throw ('NumberFormatException: invalid format, formats should have no more than 1 period: ' + format);
}else{
v = v.toFixed(0);
}
var fnum = v.toString();
if(hasComma){
psplit = fnum.split('.');
var cnum = psplit[0], parr = [], j = cnum.length, m = Math.floor(j / 3), n = cnum.length % 3 || 3;
for(var i = 0; i < j; i += n){
if(i != 0){
n = 3;
}
parr[parr.length] = cnum.substr(i, n);
m -= 1;
}
fnum = parr.join(comma);
if(psplit[1]){
fnum += dec + psplit[1];
}
}
return (neg ? '-' : '') + format.replace(/[\d,?\.?]+/, fnum);
},
/**
* Returns a number rendering function that can be reused to apply a number format multiple times efficiently
* @param {String} format Any valid number format string for {@link #number}
* @return {Function} The number formatting function
*/
numberRenderer : function(format){
return function(v){
return Ext.util.Format.number(v, format);
};
},
/**
* Selectively do a plural form of a word based on a numeric value. For example, in a template,
* {commentCount:plural("Comment")} would result in "1 Comment" if commentCount was 1 or would be "x Comments"
* if the value is 0 or greater than 1.
* @param {Number} value The value to compare against
* @param {String} singular The singular form of the word
* @param {String} plural (optional) The plural form of the word (defaults to the singular with an "s")
*/
plural : function(v, s, p){
return v +' ' + (v == 1 ? s : (p ? p : s+'s'));
},
/**
* Converts newline characters to the HTML tag <br/>
* @param {String} The string value to format.
* @return {String} The string with embedded <br/> tags in place of newlines.
*/
nl2br : function(v){
return v === undefined || v === null ? '' : v.replace(/\n/g, '<br/>');
}
}
}();