2 * Ext JS Library 2.2.1
\r
3 * Copyright(c) 2006-2009, Ext JS, LLC.
\r
4 * licensing@extjs.com
\r
6 * http://extjs.com/license
\r
10 * @class Ext.util.Format
\r
11 * Reusable data formatting functions
\r
14 Ext.util.Format = function(){
\r
15 var trimRe = /^\s+|\s+$/g;
\r
18 * Truncate a string and add an ellipsis ('...') to the end if it exceeds the specified length
\r
19 * @param {String} value The string to truncate
\r
20 * @param {Number} length The maximum length to allow before truncating
\r
21 * @return {String} The converted text
\r
23 ellipsis : function(value, len){
\r
24 if(value && value.length > len){
\r
25 return value.substr(0, len-3)+"...";
\r
31 * Checks a reference and converts it to empty string if it is undefined
\r
32 * @param {Mixed} value Reference to check
\r
33 * @return {Mixed} Empty string if converted, otherwise the original value
\r
35 undef : function(value){
\r
36 return value !== undefined ? value : "";
\r
40 * Checks a reference and converts it to the default value if it's empty
\r
41 * @param {Mixed} value Reference to check
\r
42 * @param {String} defaultValue The value to insert of it's undefined (defaults to "")
\r
45 defaultValue : function(value, defaultValue){
\r
46 return value !== undefined && value !== '' ? value : defaultValue;
\r
50 * Convert certain characters (&, <, >, and ') to their HTML character equivalents for literal display in web pages.
\r
51 * @param {String} value The string to encode
\r
52 * @return {String} The encoded text
\r
54 htmlEncode : function(value){
\r
55 return !value ? value : String(value).replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """);
\r
59 * Convert certain characters (&, <, >, and ') from their HTML character equivalents.
\r
60 * @param {String} value The string to decode
\r
61 * @return {String} The decoded text
\r
63 htmlDecode : function(value){
\r
64 return !value ? value : String(value).replace(/>/g, ">").replace(/</g, "<").replace(/"/g, '"').replace(/&/g, "&");
\r
68 * Trims any whitespace from either side of a string
\r
69 * @param {String} value The text to trim
\r
70 * @return {String} The trimmed text
\r
72 trim : function(value){
\r
73 return String(value).replace(trimRe, "");
\r
77 * Returns a substring from within an original string
\r
78 * @param {String} value The original text
\r
79 * @param {Number} start The start index of the substring
\r
80 * @param {Number} length The length of the substring
\r
81 * @return {String} The substring
\r
83 substr : function(value, start, length){
\r
84 return String(value).substr(start, length);
\r
88 * Converts a string to all lower case letters
\r
89 * @param {String} value The text to convert
\r
90 * @return {String} The converted text
\r
92 lowercase : function(value){
\r
93 return String(value).toLowerCase();
\r
97 * Converts a string to all upper case letters
\r
98 * @param {String} value The text to convert
\r
99 * @return {String} The converted text
\r
101 uppercase : function(value){
\r
102 return String(value).toUpperCase();
\r
106 * Converts the first character only of a string to upper case
\r
107 * @param {String} value The text to convert
\r
108 * @return {String} The converted text
\r
110 capitalize : function(value){
\r
111 return !value ? value : value.charAt(0).toUpperCase() + value.substr(1).toLowerCase();
\r
115 call : function(value, fn){
\r
116 if(arguments.length > 2){
\r
117 var args = Array.prototype.slice.call(arguments, 2);
\r
118 args.unshift(value);
\r
119 return eval(fn).apply(window, args);
\r
121 return eval(fn).call(window, value);
\r
126 * Format a number as US currency
\r
127 * @param {Number/String} value The numeric value to format
\r
128 * @return {String} The formatted currency string
\r
130 usMoney : function(v){
\r
131 v = (Math.round((v-0)*100))/100;
\r
132 v = (v == Math.floor(v)) ? v + ".00" : ((v*10 == Math.floor(v*10)) ? v + "0" : v);
\r
134 var ps = v.split('.');
\r
136 var sub = ps[1] ? '.'+ ps[1] : '.00';
\r
137 var r = /(\d+)(\d{3})/;
\r
138 while (r.test(whole)) {
\r
139 whole = whole.replace(r, '$1' + ',' + '$2');
\r
142 if(v.charAt(0) == '-'){
\r
143 return '-$' + v.substr(1);
\r
149 * Parse a value into a formatted date using the specified format pattern.
\r
150 * @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
151 * @param {String} format (optional) Any valid date format string (defaults to 'm/d/Y')
\r
152 * @return {String} The formatted date string
\r
154 date : function(v, format){
\r
158 if(!Ext.isDate(v)){
\r
159 v = new Date(Date.parse(v));
\r
161 return v.dateFormat(format || "m/d/Y");
\r
165 * Returns a date rendering function that can be reused to apply a date format multiple times efficiently
\r
166 * @param {String} format Any valid date format string
\r
167 * @return {Function} The date formatting function
\r
169 dateRenderer : function(format){
\r
170 return function(v){
\r
171 return Ext.util.Format.date(v, format);
\r
176 stripTagsRE : /<\/?[^>]+>/gi,
\r
179 * Strips all HTML tags
\r
180 * @param {Mixed} value The text from which to strip tags
\r
181 * @return {String} The stripped text
\r
183 stripTags : function(v){
\r
184 return !v ? v : String(v).replace(this.stripTagsRE, "");
\r
188 stripScriptsRe : /(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)/ig,
\r
191 * Strips all script tags
\r
192 * @param {Mixed} value The text from which to strip script tags
\r
193 * @return {String} The stripped text
\r
195 stripScripts : function(v){
\r
196 return !v ? v : String(v).replace(this.stripScriptsRe, "");
\r
200 * Simple format for a file size (xxx bytes, xxx KB, xxx MB)
\r
201 * @param {Number/String} size The numeric value to format
\r
202 * @return {String} The formatted file size
\r
204 fileSize : function(size){
\r
206 return size + " bytes";
\r
207 } else if(size < 1048576) {
\r
208 return (Math.round(((size*10) / 1024))/10) + " KB";
\r
210 return (Math.round(((size*10) / 1048576))/10) + " MB";
\r
216 return function(v, a){
\r
218 fns[a] = new Function('v', 'return v ' + a + ';');
\r
225 * Converts newline characters to the HTML tag <br/>
\r
226 * @param {String} The string value to format.
\r
227 * @return {String} The string with embedded <br/> tags in place of newlines.
\r
229 nl2br : function(v){
\r
230 return v === undefined || v === null ? '' : v.replace(/\n/g, '<br/>');
\r