tvheadend/vendor/ext-3.4.1/docs/source/Format.html
Adam Sutton bafcfff42d webui: restructure webui/extjs source files
I want to keep the 3rd-party packages away from the main source
where possible.
2013-06-03 17:11:01 +01:00

384 lines
16 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The source code</title>
<link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../resources/prettify/prettify.js"></script>
<style type="text/css">
.highlight { display: block; background-color: #ddd; }
</style>
<script type="text/javascript">
function highlight() {
document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
}
</script>
</head>
<body onload="prettyPrint(); highlight();">
<pre class="prettyprint lang-js"><span id='Ext-util-Format'>/**
</span> * @class Ext.util.Format
* Reusable data formatting functions
* @singleton
*/
Ext.util.Format = function() {
var trimRe = /^\s+|\s+$/g,
stripTagsRE = /&lt;\/?[^&gt;]+&gt;/gi,
stripScriptsRe = /(?:&lt;script.*?&gt;)((\n|\r|.)*?)(?:&lt;\/script&gt;)/ig,
nl2brRe = /\r?\n/g;
return {
<span id='Ext-util-Format-method-ellipsis'> /**
</span> * 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 &amp;&amp; value.length &gt; len) {
if (word) {
var vs = value.substr(0, len - 2),
index = Math.max(vs.lastIndexOf(' '), vs.lastIndexOf('.'), vs.lastIndexOf('!'), vs.lastIndexOf('?'));
if (index == -1 || index &lt; (len - 15)) {
return value.substr(0, len - 3) + &quot;...&quot;;
} else {
return vs.substr(0, index) + &quot;...&quot;;
}
} else {
return value.substr(0, len - 3) + &quot;...&quot;;
}
}
return value;
},
<span id='Ext-util-Format-method-undef'> /**
</span> * 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 : &quot;&quot;;
},
<span id='Ext-util-Format-method-defaultValue'> /**
</span> * 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 if it's undefined (defaults to &quot;&quot;)
* @return {String}
*/
defaultValue : function(value, defaultValue) {
if (!defaultValue &amp;&amp; defaultValue !== 0) {
defaultValue = '';
}
return value !== undefined &amp;&amp; value !== '' ? value : defaultValue;
},
<span id='Ext-util-Format-method-htmlEncode'> /**
</span> * Convert certain characters (&amp;, &lt;, &gt;, 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(/&amp;/g, &quot;&amp;amp;&quot;).replace(/&gt;/g, &quot;&amp;gt;&quot;).replace(/&lt;/g, &quot;&amp;lt;&quot;).replace(/&quot;/g, &quot;&amp;quot;&quot;);
},
<span id='Ext-util-Format-method-htmlDecode'> /**
</span> * Convert certain characters (&amp;, &lt;, &gt;, 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(/&amp;gt;/g, &quot;&gt;&quot;).replace(/&amp;lt;/g, &quot;&lt;&quot;).replace(/&amp;quot;/g, '&quot;').replace(/&amp;amp;/g, &quot;&amp;&quot;);
},
<span id='Ext-util-Format-method-trim'> /**
</span> * 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, &quot;&quot;);
},
<span id='Ext-util-Format-method-substr'> /**
</span> * 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);
},
<span id='Ext-util-Format-method-lowercase'> /**
</span> * 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();
},
<span id='Ext-util-Format-method-uppercase'> /**
</span> * 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();
},
<span id='Ext-util-Format-method-capitalize'> /**
</span> * 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 &gt; 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);
}
},
<span id='Ext-util-Format-method-usMoney'> /**
</span> * 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 + &quot;.00&quot; : ((v*10 == Math.floor(v*10)) ? v + &quot;0&quot; : v);
v = String(v);
var ps = v.split('.'),
whole = ps[0],
sub = ps[1] ? '.'+ ps[1] : '.00',
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 &quot;$&quot; + v;
},
<span id='Ext-util-Format-method-date'> /**
</span> * 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 &lt;a href=&quot;http://www.w3schools.com/jsref/jsref_parse.asp&quot;&gt;parse()&lt;/a&gt; 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 &quot;&quot;;
}
if (!Ext.isDate(v)) {
v = new Date(Date.parse(v));
}
return v.dateFormat(format || &quot;m/d/Y&quot;);
},
<span id='Ext-util-Format-method-dateRenderer'> /**
</span> * 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);
};
},
<span id='Ext-util-Format-method-stripTags'> /**
</span> * 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(stripTagsRE, &quot;&quot;);
},
<span id='Ext-util-Format-method-stripScripts'> /**
</span> * 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(stripScriptsRe, &quot;&quot;);
},
<span id='Ext-util-Format-method-fileSize'> /**
</span> * 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 &lt; 1024) {
return size + &quot; bytes&quot;;
} else if (size &lt; 1048576) {
return (Math.round(((size*10) / 1024))/10) + &quot; KB&quot;;
} else {
return (Math.round(((size*10) / 1048576))/10) + &quot; MB&quot;;
}
},
<span id='Ext-util-Format-method-math'> /**
</span> * It does simple math for use in a template, for example:&lt;pre&gt;&lt;code&gt;
* var tpl = new Ext.Template('{value} * 10 = {value:math(&quot;* 10&quot;)}');
* &lt;/code&gt;&lt;/pre&gt;
* @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);
};
}(),
<span id='Ext-util-Format-method-round'> /**
</span> * 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;
},
<span id='Ext-util-Format-method-number'> /**
</span> * Formats the number according to the format string.
* &lt;div style=&quot;margin-left:40px&quot;&gt;examples (123456.789):
* &lt;div style=&quot;margin-left:10px&quot;&gt;
* 0 - (123456) show only digits, no precision&lt;br&gt;
* 0.00 - (123456.78) show only digits, 2 precision&lt;br&gt;
* 0.0000 - (123456.7890) show only digits, 4 precision&lt;br&gt;
* 0,000 - (123,456) show comma and digits, no precision&lt;br&gt;
* 0,000.00 - (123,456.78) show comma and digits, 2 precision&lt;br&gt;
* 0,0.00 - (123,456.78) shortcut method, show comma and digits, 2 precision&lt;br&gt;
* To reverse the grouping (,) and decimal (.) for international numbers, add /i to the end.
* For example: 0.000,00/i
* &lt;/div&gt;&lt;/div&gt;
* @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 &lt; 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 &lt; psplit.length) {
v = v.toFixed(psplit[1].length);
} else if(2 &lt; psplit.length) {
throw ('NumberFormatException: invalid format, formats should have no more than 1 period: ' + format);
} else {
v = v.toFixed(0);
}
var fnum = v.toString();
psplit = fnum.split('.');
if (hasComma) {
var cnum = psplit[0],
parr = [],
j = cnum.length,
m = Math.floor(j / 3),
n = cnum.length % 3 || 3,
i;
for (i = 0; i &lt; 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];
}
} else {
if (psplit[1]) {
fnum = psplit[0] + dec + psplit[1];
}
}
return (neg ? '-' : '') + format.replace(/[\d,?\.?]+/, fnum);
},
<span id='Ext-util-Format-method-numberRenderer'> /**
</span> * 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);
};
},
<span id='Ext-util-Format-method-plural'> /**
</span> * Selectively do a plural form of a word based on a numeric value. For example, in a template,
* {commentCount:plural(&quot;Comment&quot;)} would result in &quot;1 Comment&quot; if commentCount was 1 or would be &quot;x Comments&quot;
* 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 &quot;s&quot;)
*/
plural : function(v, s, p) {
return v +' ' + (v == 1 ? s : (p ? p : s+'s'));
},
<span id='Ext-util-Format-method-nl2br'> /**
</span> * Converts newline characters to the HTML tag &amp;lt;br/&gt;
* @param {String} The string value to format.
* @return {String} The string with embedded &amp;lt;br/&gt; tags in place of newlines.
*/
nl2br : function(v) {
return Ext.isEmpty(v) ? '' : v.replace(nl2brRe, '&lt;br/&gt;');
}
};
}();
</pre>
</body>
</html>