149 lines
No EOL
6.1 KiB
HTML
149 lines
No EOL
6.1 KiB
HTML
<html><head><title>Format.js</title><link rel="stylesheet" type="text/css" href="../resources/style.css" media="screen"/></head><body><h1>Format.js</h1><pre class="highlighted"><code><i>/**
|
|
* @class Ext.util.Format
|
|
* Reusable data formatting functions
|
|
* @singleton
|
|
*/</i>
|
|
Ext.util.Format = <b>function</b>(){
|
|
<b>var</b> trimRe = /^\s+|\s+$/g;
|
|
<b>return</b> {
|
|
<i>/**
|
|
* Truncate a string and add an ellipsis ('...') to the end <b>if</b> it exceeds the specified length
|
|
* @param {String} value The string to truncate
|
|
* @param {Number} length The maximum length to allow before truncating
|
|
* @<b>return</b> {String} The converted text
|
|
*/</i>
|
|
ellipsis : <b>function</b>(value, len){
|
|
<b>if</b>(value && value.length > len){
|
|
<b>return</b> value.substr(0, len-3)+"...";
|
|
}
|
|
<b>return</b> value;
|
|
},
|
|
|
|
<i>/**
|
|
* Checks a reference and converts it to empty string <b>if</b> it is undefined
|
|
* @param {Mixed} value Reference to check
|
|
* @<b>return</b> {Mixed} Empty string <b>if</b> converted, otherwise the original value
|
|
*/</i>
|
|
undef : <b>function</b>(value){
|
|
<b>return</b> typeof value != "undefined" ? value : "";
|
|
},
|
|
|
|
<i>/**
|
|
* Convert certain characters (&, <, >, and ') to their HTML character equivalents <b>for</b> literal display <b>in</b> web pages.
|
|
* @param {String} value The string to encode
|
|
* @<b>return</b> {String} The encoded text
|
|
*/</i>
|
|
htmlEncode : <b>function</b>(value){
|
|
<b>return</b> !value ? value : String(value).replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g, "&quot;");
|
|
},
|
|
|
|
<i>/**
|
|
* Trims any whitespace from either side of a string
|
|
* @param {String} value The text to trim
|
|
* @<b>return</b> {String} The trimmed text
|
|
*/</i>
|
|
trim : <b>function</b>(value){
|
|
<b>return</b> String(value).replace(trimRe, "");
|
|
},
|
|
|
|
<i>/**
|
|
* 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
|
|
* @<b>return</b> {String} The substring
|
|
*/</i>
|
|
substr : <b>function</b>(value, start, length){
|
|
<b>return</b> String(value).substr(start, length);
|
|
},
|
|
|
|
<i>/**
|
|
* Converts a string to all lower <b>case</b> letters
|
|
* @param {String} value The text to convert
|
|
* @<b>return</b> {String} The converted text
|
|
*/</i>
|
|
lowercase : <b>function</b>(value){
|
|
<b>return</b> String(value).toLowerCase();
|
|
},
|
|
|
|
<i>/**
|
|
* Converts a string to all upper <b>case</b> letters
|
|
* @param {String} value The text to convert
|
|
* @<b>return</b> {String} The converted text
|
|
*/</i>
|
|
uppercase : <b>function</b>(value){
|
|
<b>return</b> String(value).toUpperCase();
|
|
},
|
|
|
|
<i>/**
|
|
* Converts the first character only of a string to upper <b>case</b>
|
|
* @param {String} value The text to convert
|
|
* @<b>return</b> {String} The converted text
|
|
*/</i>
|
|
capitalize : <b>function</b>(value){
|
|
<b>return</b> !value ? value : value.charAt(0).toUpperCase() + value.substr(1).toLowerCase();
|
|
},
|
|
|
|
<i>// private</i>
|
|
call : <b>function</b>(value, fn){
|
|
<b>if</b>(arguments.length > 2){
|
|
<b>var</b> args = Array.prototype.slice.call(arguments, 2);
|
|
args.unshift(value);
|
|
<b>return</b> eval(fn).apply(window, args);
|
|
}<b>else</b>{
|
|
<b>return</b> eval(fn).call(window, value);
|
|
}
|
|
},
|
|
|
|
<i>/**
|
|
* Format a number as US currency
|
|
* @param {Number/String} value The numeric value to format
|
|
* @<b>return</b> {String} The formatted currency string
|
|
*/</i>
|
|
usMoney : <b>function</b>(v){
|
|
v = (Math.round((v-0)*100))/100;
|
|
v = (v == Math.floor(v)) ? v + ".00" : ((v*10 == Math.floor(v*10)) ? v + "0" : v);
|
|
<b>return</b> "$" + v ;
|
|
},
|
|
|
|
<i>/**
|
|
* Parse a value into a formatted date using the specified format pattern.
|
|
* @param {Mixed} value The value to format
|
|
* @param {String} format (optional) Any valid date format string (defaults to 'm/d/Y')
|
|
* @<b>return</b> {String} The formatted date string
|
|
*/</i>
|
|
date : <b>function</b>(v, format){
|
|
<b>if</b>(!v){
|
|
<b>return</b> "";
|
|
}
|
|
<b>if</b>(!(v instanceof Date)){
|
|
v = <b>new</b> Date(Date.parse(v));
|
|
}
|
|
<b>return</b> v.dateFormat(format || "m/d/Y");
|
|
},
|
|
|
|
<i>/**
|
|
* Returns a date rendering <b>function</b> that can be reused to apply a date format multiple times efficiently
|
|
* @param {String} format Any valid date format string
|
|
* @<b>return</b> {Function} The date formatting <b>function</b>
|
|
*/</i>
|
|
dateRenderer : <b>function</b>(format){
|
|
<b>return</b> function(v){
|
|
<b>return</b> Ext.util.Format.date(v, format);
|
|
};
|
|
},
|
|
|
|
<i>// private</i>
|
|
stripTagsRE : /<\/?[^>]+>/gi,
|
|
|
|
<i>/**
|
|
* Strips all HTML tags
|
|
* @param {Mixed} value The text from which to strip tags
|
|
* @<b>return</b> {String} The stripped text
|
|
*/</i>
|
|
stripTags : <b>function</b>(v){
|
|
<b>return</b> !v ? v : String(v).replace(<b>this</b>.stripTagsRE, "");
|
|
}
|
|
};
|
|
}();</code></pre><hr><div style="font-size:10px;text-align:center;color:gray;">Ext - Copyright © 2006-2007 Ext JS, LLC<br />All rights reserved.</div>
|
|
</body></html> |