3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.util.Format"></div>/**
\r
9 * @class Ext.util.Format
\r
10 * Reusable data formatting functions
\r
13 Ext.util.Format = function(){
\r
14 var trimRe = /^\s+|\s+$/g;
\r
16 <div id="method-Ext.util.Format-ellipsis"></div>/**
\r
17 * Truncate a string and add an ellipsis ('...') to the end if it exceeds the specified length
\r
18 * @param {String} value The string to truncate
\r
19 * @param {Number} length The maximum length to allow before truncating
\r
20 * @param {Boolean} word True to try to find a common work break
\r
21 * @return {String} The converted text
\r
23 ellipsis : function(value, len, word){
\r
24 if(value && value.length > len){
\r
26 var vs = value.substr(0, len - 2);
\r
27 var index = Math.max(vs.lastIndexOf(' '), vs.lastIndexOf('.'), vs.lastIndexOf('!'), vs.lastIndexOf('?'));
\r
28 if(index == -1 || index < (len - 15)){
\r
29 return value.substr(0, len - 3) + "...";
\r
31 return vs.substr(0, index) + "...";
\r
34 return value.substr(0, len - 3) + "...";
\r
40 <div id="method-Ext.util.Format-undef"></div>/**
\r
41 * Checks a reference and converts it to empty string if it is undefined
\r
42 * @param {Mixed} value Reference to check
\r
43 * @return {Mixed} Empty string if converted, otherwise the original value
\r
45 undef : function(value){
\r
46 return value !== undefined ? value : "";
\r
49 <div id="method-Ext.util.Format-defaultValue"></div>/**
\r
50 * Checks a reference and converts it to the default value if it's empty
\r
51 * @param {Mixed} value Reference to check
\r
52 * @param {String} defaultValue The value to insert of it's undefined (defaults to "")
\r
55 defaultValue : function(value, defaultValue){
\r
56 return value !== undefined && value !== '' ? value : defaultValue;
\r
59 <div id="method-Ext.util.Format-htmlEncode"></div>/**
\r
60 * Convert certain characters (&, <, >, and ') to their HTML character equivalents for literal display in web pages.
\r
61 * @param {String} value The string to encode
\r
62 * @return {String} The encoded text
\r
64 htmlEncode : function(value){
\r
65 return !value ? value : String(value).replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """);
\r
68 <div id="method-Ext.util.Format-htmlDecode"></div>/**
\r
69 * Convert certain characters (&, <, >, and ') from their HTML character equivalents.
\r
70 * @param {String} value The string to decode
\r
71 * @return {String} The decoded text
\r
73 htmlDecode : function(value){
\r
74 return !value ? value : String(value).replace(/>/g, ">").replace(/</g, "<").replace(/"/g, '"').replace(/&/g, "&");
\r
77 <div id="method-Ext.util.Format-trim"></div>/**
\r
78 * Trims any whitespace from either side of a string
\r
79 * @param {String} value The text to trim
\r
80 * @return {String} The trimmed text
\r
82 trim : function(value){
\r
83 return String(value).replace(trimRe, "");
\r
86 <div id="method-Ext.util.Format-substr"></div>/**
\r
87 * Returns a substring from within an original string
\r
88 * @param {String} value The original text
\r
89 * @param {Number} start The start index of the substring
\r
90 * @param {Number} length The length of the substring
\r
91 * @return {String} The substring
\r
93 substr : function(value, start, length){
\r
94 return String(value).substr(start, length);
\r
97 <div id="method-Ext.util.Format-lowercase"></div>/**
\r
98 * Converts a string to all lower case letters
\r
99 * @param {String} value The text to convert
\r
100 * @return {String} The converted text
\r
102 lowercase : function(value){
\r
103 return String(value).toLowerCase();
\r
106 <div id="method-Ext.util.Format-uppercase"></div>/**
\r
107 * Converts a string to all upper case letters
\r
108 * @param {String} value The text to convert
\r
109 * @return {String} The converted text
\r
111 uppercase : function(value){
\r
112 return String(value).toUpperCase();
\r
115 <div id="method-Ext.util.Format-capitalize"></div>/**
\r
116 * Converts the first character only of a string to upper case
\r
117 * @param {String} value The text to convert
\r
118 * @return {String} The converted text
\r
120 capitalize : function(value){
\r
121 return !value ? value : value.charAt(0).toUpperCase() + value.substr(1).toLowerCase();
\r
125 call : function(value, fn){
\r
126 if(arguments.length > 2){
\r
127 var args = Array.prototype.slice.call(arguments, 2);
\r
128 args.unshift(value);
\r
129 return eval(fn).apply(window, args);
\r
131 return eval(fn).call(window, value);
\r
135 <div id="method-Ext.util.Format-usMoney"></div>/**
\r
136 * Format a number as US currency
\r
137 * @param {Number/String} value The numeric value to format
\r
138 * @return {String} The formatted currency string
\r
140 usMoney : function(v){
\r
141 v = (Math.round((v-0)*100))/100;
\r
142 v = (v == Math.floor(v)) ? v + ".00" : ((v*10 == Math.floor(v*10)) ? v + "0" : v);
\r
144 var ps = v.split('.');
\r
146 var sub = ps[1] ? '.'+ ps[1] : '.00';
\r
147 var r = /(\d+)(\d{3})/;
\r
148 while (r.test(whole)) {
\r
149 whole = whole.replace(r, '$1' + ',' + '$2');
\r
152 if(v.charAt(0) == '-'){
\r
153 return '-$' + v.substr(1);
\r
158 <div id="method-Ext.util.Format-date"></div>/**
\r
159 * Parse a value into a formatted date using the specified format pattern.
\r
160 * @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)
\r
161 * @param {String} format (optional) Any valid date format string (defaults to 'm/d/Y')
\r
162 * @return {String} The formatted date string
\r
164 date : function(v, format){
\r
168 if(!Ext.isDate(v)){
\r
169 v = new Date(Date.parse(v));
\r
171 return v.dateFormat(format || "m/d/Y");
\r
174 <div id="method-Ext.util.Format-dateRenderer"></div>/**
\r
175 * Returns a date rendering function that can be reused to apply a date format multiple times efficiently
\r
176 * @param {String} format Any valid date format string
\r
177 * @return {Function} The date formatting function
\r
179 dateRenderer : function(format){
\r
180 return function(v){
\r
181 return Ext.util.Format.date(v, format);
\r
186 stripTagsRE : /<\/?[^>]+>/gi,
\r
188 <div id="method-Ext.util.Format-stripTags"></div>/**
\r
189 * Strips all HTML tags
\r
190 * @param {Mixed} value The text from which to strip tags
\r
191 * @return {String} The stripped text
\r
193 stripTags : function(v){
\r
194 return !v ? v : String(v).replace(this.stripTagsRE, "");
\r
197 stripScriptsRe : /(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)/ig,
\r
199 <div id="method-Ext.util.Format-stripScripts"></div>/**
\r
200 * Strips all script tags
\r
201 * @param {Mixed} value The text from which to strip script tags
\r
202 * @return {String} The stripped text
\r
204 stripScripts : function(v){
\r
205 return !v ? v : String(v).replace(this.stripScriptsRe, "");
\r
208 <div id="method-Ext.util.Format-fileSize"></div>/**
\r
209 * Simple format for a file size (xxx bytes, xxx KB, xxx MB)
\r
210 * @param {Number/String} size The numeric value to format
\r
211 * @return {String} The formatted file size
\r
213 fileSize : function(size){
\r
215 return size + " bytes";
\r
216 } else if(size < 1048576) {
\r
217 return (Math.round(((size*10) / 1024))/10) + " KB";
\r
219 return (Math.round(((size*10) / 1048576))/10) + " MB";
\r
223 <div id="method-Ext.util.Format-math"></div>/**
\r
224 * It does simple math for use in a template, for example:<pre><code>
\r
225 * var tpl = new Ext.Template('{value} * 10 = {value:math("* 10")}');
\r
227 * @return {Function} A function that operates on the passed value.
\r
231 return function(v, a){
\r
233 fns[a] = new Function('v', 'return v ' + a + ';');
\r
239 <div id="method-Ext.util.Format-round"></div>/**
\r
240 * Rounds the passed number to the required decimal precision.
\r
241 * @param {Number/String} value The numeric value to round.
\r
242 * @param {Number} precision The number of decimal places to which to round the first parameter's value.
\r
243 * @return {Number} The rounded value.
\r
245 round : function(value, precision) {
\r
246 var result = Number(value);
\r
247 if (typeof precision == 'number') {
\r
248 precision = Math.pow(10, precision);
\r
249 result = Math.round(value * precision) / precision;
\r
254 <div id="method-Ext.util.Format-number"></div>/**
\r
255 * Formats the number according to the format string.
\r
256 * <div style="margin-left:40px">examples (123456.789):
\r
257 * <div style="margin-left:10px">
\r
258 * 0 - (123456) show only digits, no precision<br>
\r
259 * 0.00 - (123456.78) show only digits, 2 precision<br>
\r
260 * 0.0000 - (123456.7890) show only digits, 4 precision<br>
\r
261 * 0,000 - (123,456) show comma and digits, no precision<br>
\r
262 * 0,000.00 - (123,456.78) show comma and digits, 2 precision<br>
\r
263 * 0,0.00 - (123,456.78) shortcut method, show comma and digits, 2 precision<br>
\r
264 * To reverse the grouping (,) and decimal (.) for international numbers, add /i to the end.
\r
265 * For example: 0.000,00/i
\r
267 * @param {Number} v The number to format.
\r
268 * @param {String} format The way you would like to format this text.
\r
269 * @return {String} The formatted number.
\r
271 number: function(v, format) {
\r
275 v = Ext.num(v, NaN);
\r
285 if(format.substr(format.length - 2) == '/i'){
\r
286 format = format.substr(0, format.length - 2);
\r
292 var hasComma = format.indexOf(comma) != -1,
\r
293 psplit = (i18n ? format.replace(/[^\d\,]/g, '') : format.replace(/[^\d\.]/g, '')).split(dec);
\r
295 if(1 < psplit.length){
\r
296 v = v.toFixed(psplit[1].length);
\r
297 }else if(2 < psplit.length){
\r
298 throw ('NumberFormatException: invalid format, formats should have no more than 1 period: ' + format);
\r
303 var fnum = v.toString();
\r
305 psplit = fnum.split('.');
\r
307 var cnum = psplit[0], parr = [], j = cnum.length, m = Math.floor(j / 3), n = cnum.length % 3 || 3;
\r
309 for(var i = 0; i < j; i += n){
\r
313 parr[parr.length] = cnum.substr(i, n);
\r
316 fnum = parr.join(comma);
\r
318 fnum += dec + psplit[1];
\r
322 return (neg ? '-' : '') + format.replace(/[\d,?\.?]+/, fnum);
\r
325 <div id="method-Ext.util.Format-numberRenderer"></div>/**
\r
326 * Returns a number rendering function that can be reused to apply a number format multiple times efficiently
\r
327 * @param {String} format Any valid number format string for {@link #number}
\r
328 * @return {Function} The number formatting function
\r
330 numberRenderer : function(format){
\r
331 return function(v){
\r
332 return Ext.util.Format.number(v, format);
\r
336 <div id="method-Ext.util.Format-plural"></div>/**
\r
337 * Selectively do a plural form of a word based on a numeric value. For example, in a template,
\r
338 * {commentCount:plural("Comment")} would result in "1 Comment" if commentCount was 1 or would be "x Comments"
\r
339 * if the value is 0 or greater than 1.
\r
340 * @param {Number} value The value to compare against
\r
341 * @param {String} singular The singular form of the word
\r
342 * @param {String} plural (optional) The plural form of the word (defaults to the singular with an "s")
\r
344 plural : function(v, s, p){
\r
345 return v +' ' + (v == 1 ? s : (p ? p : s+'s'));
\r
348 <div id="method-Ext.util.Format-nl2br"></div>/**
\r
349 * Converts newline characters to the HTML tag <br/>
\r
350 * @param {String} The string value to format.
\r
351 * @return {String} The string with embedded <br/> tags in place of newlines.
\r
353 nl2br : function(v){
\r
354 return v === undefined || v === null ? '' : v.replace(/\n/g, '<br/>');
\r