27 |
jpm |
1 |
/*
|
|
|
2 |
* Ext JS Library 2.0.2
|
|
|
3 |
* Copyright(c) 2006-2008, Ext JS, LLC.
|
|
|
4 |
* licensing@extjs.com
|
|
|
5 |
*
|
|
|
6 |
* http://extjs.com/license
|
|
|
7 |
*/
|
|
|
8 |
|
|
|
9 |
/**
|
|
|
10 |
* @class Ext.util.Format
|
|
|
11 |
* Reusable data formatting functions
|
|
|
12 |
* @singleton
|
|
|
13 |
*/
|
|
|
14 |
Ext.util.Format = function(){
|
|
|
15 |
var trimRe = /^\s+|\s+$/g;
|
|
|
16 |
return {
|
|
|
17 |
/**
|
|
|
18 |
* Truncate a string and add an ellipsis ('...') to the end if it exceeds the specified length
|
|
|
19 |
* @param {String} value The string to truncate
|
|
|
20 |
* @param {Number} length The maximum length to allow before truncating
|
|
|
21 |
* @return {String} The converted text
|
|
|
22 |
*/
|
|
|
23 |
ellipsis : function(value, len){
|
|
|
24 |
if(value && value.length > len){
|
|
|
25 |
return value.substr(0, len-3)+"...";
|
|
|
26 |
}
|
|
|
27 |
return value;
|
|
|
28 |
},
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Checks a reference and converts it to empty string if it is undefined
|
|
|
32 |
* @param {Mixed} value Reference to check
|
|
|
33 |
* @return {Mixed} Empty string if converted, otherwise the original value
|
|
|
34 |
*/
|
|
|
35 |
undef : function(value){
|
|
|
36 |
return value !== undefined ? value : "";
|
|
|
37 |
},
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Checks a reference and converts it to the default value if it's empty
|
|
|
41 |
* @param {Mixed} value Reference to check
|
|
|
42 |
* @param {String} defaultValue The value to insert of it's undefined (defaults to "")
|
|
|
43 |
* @return {String}
|
|
|
44 |
*/
|
|
|
45 |
defaultValue : function(value, defaultValue){
|
|
|
46 |
return value !== undefined && value !== '' ? value : defaultValue;
|
|
|
47 |
},
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Convert certain characters (&, <, >, and ') to their HTML character equivalents for literal display in web pages.
|
|
|
51 |
* @param {String} value The string to encode
|
|
|
52 |
* @return {String} The encoded text
|
|
|
53 |
*/
|
|
|
54 |
htmlEncode : function(value){
|
|
|
55 |
return !value ? value : String(value).replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """);
|
|
|
56 |
},
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Convert certain characters (&, <, >, and ') from their HTML character equivalents.
|
|
|
60 |
* @param {String} value The string to decode
|
|
|
61 |
* @return {String} The decoded text
|
|
|
62 |
*/
|
|
|
63 |
htmlDecode : function(value){
|
|
|
64 |
return !value ? value : String(value).replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, '"');
|
|
|
65 |
},
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Trims any whitespace from either side of a string
|
|
|
69 |
* @param {String} value The text to trim
|
|
|
70 |
* @return {String} The trimmed text
|
|
|
71 |
*/
|
|
|
72 |
trim : function(value){
|
|
|
73 |
return String(value).replace(trimRe, "");
|
|
|
74 |
},
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Returns a substring from within an original string
|
|
|
78 |
* @param {String} value The original text
|
|
|
79 |
* @param {Number} start The start index of the substring
|
|
|
80 |
* @param {Number} length The length of the substring
|
|
|
81 |
* @return {String} The substring
|
|
|
82 |
*/
|
|
|
83 |
substr : function(value, start, length){
|
|
|
84 |
return String(value).substr(start, length);
|
|
|
85 |
},
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Converts a string to all lower case letters
|
|
|
89 |
* @param {String} value The text to convert
|
|
|
90 |
* @return {String} The converted text
|
|
|
91 |
*/
|
|
|
92 |
lowercase : function(value){
|
|
|
93 |
return String(value).toLowerCase();
|
|
|
94 |
},
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* Converts a string to all upper case letters
|
|
|
98 |
* @param {String} value The text to convert
|
|
|
99 |
* @return {String} The converted text
|
|
|
100 |
*/
|
|
|
101 |
uppercase : function(value){
|
|
|
102 |
return String(value).toUpperCase();
|
|
|
103 |
},
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* Converts the first character only of a string to upper case
|
|
|
107 |
* @param {String} value The text to convert
|
|
|
108 |
* @return {String} The converted text
|
|
|
109 |
*/
|
|
|
110 |
capitalize : function(value){
|
|
|
111 |
return !value ? value : value.charAt(0).toUpperCase() + value.substr(1).toLowerCase();
|
|
|
112 |
},
|
|
|
113 |
|
|
|
114 |
// private
|
|
|
115 |
call : function(value, fn){
|
|
|
116 |
if(arguments.length > 2){
|
|
|
117 |
var args = Array.prototype.slice.call(arguments, 2);
|
|
|
118 |
args.unshift(value);
|
|
|
119 |
return eval(fn).apply(window, args);
|
|
|
120 |
}else{
|
|
|
121 |
return eval(fn).call(window, value);
|
|
|
122 |
}
|
|
|
123 |
},
|
|
|
124 |
|
|
|
125 |
/**
|
|
|
126 |
* Format a number as US currency
|
|
|
127 |
* @param {Number/String} value The numeric value to format
|
|
|
128 |
* @return {String} The formatted currency string
|
|
|
129 |
*/
|
|
|
130 |
usMoney : function(v){
|
|
|
131 |
v = (Math.round((v-0)*100))/100;
|
|
|
132 |
v = (v == Math.floor(v)) ? v + ".00" : ((v*10 == Math.floor(v*10)) ? v + "0" : v);
|
|
|
133 |
v = String(v);
|
|
|
134 |
var ps = v.split('.');
|
|
|
135 |
var whole = ps[0];
|
|
|
136 |
var sub = ps[1] ? '.'+ ps[1] : '.00';
|
|
|
137 |
var r = /(\d+)(\d{3})/;
|
|
|
138 |
while (r.test(whole)) {
|
|
|
139 |
whole = whole.replace(r, '$1' + ',' + '$2');
|
|
|
140 |
}
|
|
|
141 |
v = whole + sub;
|
|
|
142 |
if(v.charAt(0) == '-'){
|
|
|
143 |
return '-$' + v.substr(1);
|
|
|
144 |
}
|
|
|
145 |
return "$" + v;
|
|
|
146 |
},
|
|
|
147 |
|
|
|
148 |
/**
|
|
|
149 |
* Parse a value into a formatted date using the specified format pattern.
|
|
|
150 |
* @param {Mixed} value The value to format
|
|
|
151 |
* @param {String} format (optional) Any valid date format string (defaults to 'm/d/Y')
|
|
|
152 |
* @return {String} The formatted date string
|
|
|
153 |
*/
|
|
|
154 |
date : function(v, format){
|
|
|
155 |
if(!v){
|
|
|
156 |
return "";
|
|
|
157 |
}
|
|
|
158 |
if(!Ext.isDate(v)){
|
|
|
159 |
v = new Date(Date.parse(v));
|
|
|
160 |
}
|
|
|
161 |
return v.dateFormat(format || "m/d/Y");
|
|
|
162 |
},
|
|
|
163 |
|
|
|
164 |
/**
|
|
|
165 |
* Returns a date rendering function that can be reused to apply a date format multiple times efficiently
|
|
|
166 |
* @param {String} format Any valid date format string
|
|
|
167 |
* @return {Function} The date formatting function
|
|
|
168 |
*/
|
|
|
169 |
dateRenderer : function(format){
|
|
|
170 |
return function(v){
|
|
|
171 |
return Ext.util.Format.date(v, format);
|
|
|
172 |
};
|
|
|
173 |
},
|
|
|
174 |
|
|
|
175 |
// private
|
|
|
176 |
stripTagsRE : /<\/?[^>]+>/gi,
|
|
|
177 |
|
|
|
178 |
/**
|
|
|
179 |
* Strips all HTML tags
|
|
|
180 |
* @param {Mixed} value The text from which to strip tags
|
|
|
181 |
* @return {String} The stripped text
|
|
|
182 |
*/
|
|
|
183 |
stripTags : function(v){
|
|
|
184 |
return !v ? v : String(v).replace(this.stripTagsRE, "");
|
|
|
185 |
},
|
|
|
186 |
|
|
|
187 |
stripScriptsRe : /(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)/ig,
|
|
|
188 |
|
|
|
189 |
/**
|
|
|
190 |
* Strips all script tags
|
|
|
191 |
* @param {Mixed} value The text from which to strip script tags
|
|
|
192 |
* @return {String} The stripped text
|
|
|
193 |
*/
|
|
|
194 |
stripScripts : function(v){
|
|
|
195 |
return !v ? v : String(v).replace(this.stripScriptsRe, "");
|
|
|
196 |
},
|
|
|
197 |
|
|
|
198 |
/**
|
|
|
199 |
* Simple format for a file size (xxx bytes, xxx KB, xxx MB)
|
|
|
200 |
* @param {Number/String} size The numeric value to format
|
|
|
201 |
* @return {String} The formatted file size
|
|
|
202 |
*/
|
|
|
203 |
fileSize : function(size){
|
|
|
204 |
if(size < 1024) {
|
|
|
205 |
return size + " bytes";
|
|
|
206 |
} else if(size < 1048576) {
|
|
|
207 |
return (Math.round(((size*10) / 1024))/10) + " KB";
|
|
|
208 |
} else {
|
|
|
209 |
return (Math.round(((size*10) / 1048576))/10) + " MB";
|
|
|
210 |
}
|
|
|
211 |
},
|
|
|
212 |
|
|
|
213 |
math : function(){
|
|
|
214 |
var fns = {};
|
|
|
215 |
return function(v, a){
|
|
|
216 |
if(!fns[a]){
|
|
|
217 |
fns[a] = new Function('v', 'return v ' + a + ';');
|
|
|
218 |
}
|
|
|
219 |
return fns[a](v);
|
|
|
220 |
}
|
|
|
221 |
}()
|
|
|
222 |
};
|
|
|
223 |
}();
|