You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

384 lines
16 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-util-Format'>/**
  19. </span> * @class Ext.util.Format
  20. * Reusable data formatting functions
  21. * @singleton
  22. */
  23. Ext.util.Format = function() {
  24. var trimRe = /^\s+|\s+$/g,
  25. stripTagsRE = /&lt;\/?[^&gt;]+&gt;/gi,
  26. stripScriptsRe = /(?:&lt;script.*?&gt;)((\n|\r|.)*?)(?:&lt;\/script&gt;)/ig,
  27. nl2brRe = /\r?\n/g;
  28. return {
  29. <span id='Ext-util-Format-method-ellipsis'> /**
  30. </span> * Truncate a string and add an ellipsis ('...') to the end if it exceeds the specified length
  31. * @param {String} value The string to truncate
  32. * @param {Number} length The maximum length to allow before truncating
  33. * @param {Boolean} word True to try to find a common work break
  34. * @return {String} The converted text
  35. */
  36. ellipsis : function(value, len, word) {
  37. if (value &amp;&amp; value.length &gt; len) {
  38. if (word) {
  39. var vs = value.substr(0, len - 2),
  40. index = Math.max(vs.lastIndexOf(' '), vs.lastIndexOf('.'), vs.lastIndexOf('!'), vs.lastIndexOf('?'));
  41. if (index == -1 || index &lt; (len - 15)) {
  42. return value.substr(0, len - 3) + &quot;...&quot;;
  43. } else {
  44. return vs.substr(0, index) + &quot;...&quot;;
  45. }
  46. } else {
  47. return value.substr(0, len - 3) + &quot;...&quot;;
  48. }
  49. }
  50. return value;
  51. },
  52. <span id='Ext-util-Format-method-undef'> /**
  53. </span> * Checks a reference and converts it to empty string if it is undefined
  54. * @param {Mixed} value Reference to check
  55. * @return {Mixed} Empty string if converted, otherwise the original value
  56. */
  57. undef : function(value) {
  58. return value !== undefined ? value : &quot;&quot;;
  59. },
  60. <span id='Ext-util-Format-method-defaultValue'> /**
  61. </span> * Checks a reference and converts it to the default value if it's empty
  62. * @param {Mixed} value Reference to check
  63. * @param {String} defaultValue The value to insert if it's undefined (defaults to &quot;&quot;)
  64. * @return {String}
  65. */
  66. defaultValue : function(value, defaultValue) {
  67. if (!defaultValue &amp;&amp; defaultValue !== 0) {
  68. defaultValue = '';
  69. }
  70. return value !== undefined &amp;&amp; value !== '' ? value : defaultValue;
  71. },
  72. <span id='Ext-util-Format-method-htmlEncode'> /**
  73. </span> * Convert certain characters (&amp;, &lt;, &gt;, and ') to their HTML character equivalents for literal display in web pages.
  74. * @param {String} value The string to encode
  75. * @return {String} The encoded text
  76. */
  77. htmlEncode : function(value) {
  78. 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;);
  79. },
  80. <span id='Ext-util-Format-method-htmlDecode'> /**
  81. </span> * Convert certain characters (&amp;, &lt;, &gt;, and ') from their HTML character equivalents.
  82. * @param {String} value The string to decode
  83. * @return {String} The decoded text
  84. */
  85. htmlDecode : function(value) {
  86. 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;);
  87. },
  88. <span id='Ext-util-Format-method-trim'> /**
  89. </span> * Trims any whitespace from either side of a string
  90. * @param {String} value The text to trim
  91. * @return {String} The trimmed text
  92. */
  93. trim : function(value) {
  94. return String(value).replace(trimRe, &quot;&quot;);
  95. },
  96. <span id='Ext-util-Format-method-substr'> /**
  97. </span> * Returns a substring from within an original string
  98. * @param {String} value The original text
  99. * @param {Number} start The start index of the substring
  100. * @param {Number} length The length of the substring
  101. * @return {String} The substring
  102. */
  103. substr : function(value, start, length) {
  104. return String(value).substr(start, length);
  105. },
  106. <span id='Ext-util-Format-method-lowercase'> /**
  107. </span> * Converts a string to all lower case letters
  108. * @param {String} value The text to convert
  109. * @return {String} The converted text
  110. */
  111. lowercase : function(value) {
  112. return String(value).toLowerCase();
  113. },
  114. <span id='Ext-util-Format-method-uppercase'> /**
  115. </span> * Converts a string to all upper case letters
  116. * @param {String} value The text to convert
  117. * @return {String} The converted text
  118. */
  119. uppercase : function(value) {
  120. return String(value).toUpperCase();
  121. },
  122. <span id='Ext-util-Format-method-capitalize'> /**
  123. </span> * Converts the first character only of a string to upper case
  124. * @param {String} value The text to convert
  125. * @return {String} The converted text
  126. */
  127. capitalize : function(value) {
  128. return !value ? value : value.charAt(0).toUpperCase() + value.substr(1).toLowerCase();
  129. },
  130. // private
  131. call : function(value, fn) {
  132. if (arguments.length &gt; 2) {
  133. var args = Array.prototype.slice.call(arguments, 2);
  134. args.unshift(value);
  135. return eval(fn).apply(window, args);
  136. } else {
  137. return eval(fn).call(window, value);
  138. }
  139. },
  140. <span id='Ext-util-Format-method-usMoney'> /**
  141. </span> * Format a number as US currency
  142. * @param {Number/String} value The numeric value to format
  143. * @return {String} The formatted currency string
  144. */
  145. usMoney : function(v) {
  146. v = (Math.round((v-0)*100))/100;
  147. v = (v == Math.floor(v)) ? v + &quot;.00&quot; : ((v*10 == Math.floor(v*10)) ? v + &quot;0&quot; : v);
  148. v = String(v);
  149. var ps = v.split('.'),
  150. whole = ps[0],
  151. sub = ps[1] ? '.'+ ps[1] : '.00',
  152. r = /(\d+)(\d{3})/;
  153. while (r.test(whole)) {
  154. whole = whole.replace(r, '$1' + ',' + '$2');
  155. }
  156. v = whole + sub;
  157. if (v.charAt(0) == '-') {
  158. return '-$' + v.substr(1);
  159. }
  160. return &quot;$&quot; + v;
  161. },
  162. <span id='Ext-util-Format-method-date'> /**
  163. </span> * Parse a value into a formatted date using the specified format pattern.
  164. * @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)
  165. * @param {String} format (optional) Any valid date format string (defaults to 'm/d/Y')
  166. * @return {String} The formatted date string
  167. */
  168. date : function(v, format) {
  169. if (!v) {
  170. return &quot;&quot;;
  171. }
  172. if (!Ext.isDate(v)) {
  173. v = new Date(Date.parse(v));
  174. }
  175. return v.dateFormat(format || &quot;m/d/Y&quot;);
  176. },
  177. <span id='Ext-util-Format-method-dateRenderer'> /**
  178. </span> * Returns a date rendering function that can be reused to apply a date format multiple times efficiently
  179. * @param {String} format Any valid date format string
  180. * @return {Function} The date formatting function
  181. */
  182. dateRenderer : function(format) {
  183. return function(v) {
  184. return Ext.util.Format.date(v, format);
  185. };
  186. },
  187. <span id='Ext-util-Format-method-stripTags'> /**
  188. </span> * Strips all HTML tags
  189. * @param {Mixed} value The text from which to strip tags
  190. * @return {String} The stripped text
  191. */
  192. stripTags : function(v) {
  193. return !v ? v : String(v).replace(stripTagsRE, &quot;&quot;);
  194. },
  195. <span id='Ext-util-Format-method-stripScripts'> /**
  196. </span> * Strips all script tags
  197. * @param {Mixed} value The text from which to strip script tags
  198. * @return {String} The stripped text
  199. */
  200. stripScripts : function(v) {
  201. return !v ? v : String(v).replace(stripScriptsRe, &quot;&quot;);
  202. },
  203. <span id='Ext-util-Format-method-fileSize'> /**
  204. </span> * Simple format for a file size (xxx bytes, xxx KB, xxx MB)
  205. * @param {Number/String} size The numeric value to format
  206. * @return {String} The formatted file size
  207. */
  208. fileSize : function(size) {
  209. if (size &lt; 1024) {
  210. return size + &quot; bytes&quot;;
  211. } else if (size &lt; 1048576) {
  212. return (Math.round(((size*10) / 1024))/10) + &quot; KB&quot;;
  213. } else {
  214. return (Math.round(((size*10) / 1048576))/10) + &quot; MB&quot;;
  215. }
  216. },
  217. <span id='Ext-util-Format-method-math'> /**
  218. </span> * It does simple math for use in a template, for example:&lt;pre&gt;&lt;code&gt;
  219. * var tpl = new Ext.Template('{value} * 10 = {value:math(&quot;* 10&quot;)}');
  220. * &lt;/code&gt;&lt;/pre&gt;
  221. * @return {Function} A function that operates on the passed value.
  222. */
  223. math : function(){
  224. var fns = {};
  225. return function(v, a){
  226. if (!fns[a]) {
  227. fns[a] = new Function('v', 'return v ' + a + ';');
  228. }
  229. return fns[a](v);
  230. };
  231. }(),
  232. <span id='Ext-util-Format-method-round'> /**
  233. </span> * Rounds the passed number to the required decimal precision.
  234. * @param {Number/String} value The numeric value to round.
  235. * @param {Number} precision The number of decimal places to which to round the first parameter's value.
  236. * @return {Number} The rounded value.
  237. */
  238. round : function(value, precision) {
  239. var result = Number(value);
  240. if (typeof precision == 'number') {
  241. precision = Math.pow(10, precision);
  242. result = Math.round(value * precision) / precision;
  243. }
  244. return result;
  245. },
  246. <span id='Ext-util-Format-method-number'> /**
  247. </span> * Formats the number according to the format string.
  248. * &lt;div style=&quot;margin-left:40px&quot;&gt;examples (123456.789):
  249. * &lt;div style=&quot;margin-left:10px&quot;&gt;
  250. * 0 - (123456) show only digits, no precision&lt;br&gt;
  251. * 0.00 - (123456.78) show only digits, 2 precision&lt;br&gt;
  252. * 0.0000 - (123456.7890) show only digits, 4 precision&lt;br&gt;
  253. * 0,000 - (123,456) show comma and digits, no precision&lt;br&gt;
  254. * 0,000.00 - (123,456.78) show comma and digits, 2 precision&lt;br&gt;
  255. * 0,0.00 - (123,456.78) shortcut method, show comma and digits, 2 precision&lt;br&gt;
  256. * To reverse the grouping (,) and decimal (.) for international numbers, add /i to the end.
  257. * For example: 0.000,00/i
  258. * &lt;/div&gt;&lt;/div&gt;
  259. * @param {Number} v The number to format.
  260. * @param {String} format The way you would like to format this text.
  261. * @return {String} The formatted number.
  262. */
  263. number: function(v, format) {
  264. if (!format) {
  265. return v;
  266. }
  267. v = Ext.num(v, NaN);
  268. if (isNaN(v)) {
  269. return '';
  270. }
  271. var comma = ',',
  272. dec = '.',
  273. i18n = false,
  274. neg = v &lt; 0;
  275. v = Math.abs(v);
  276. if (format.substr(format.length - 2) == '/i') {
  277. format = format.substr(0, format.length - 2);
  278. i18n = true;
  279. comma = '.';
  280. dec = ',';
  281. }
  282. var hasComma = format.indexOf(comma) != -1,
  283. psplit = (i18n ? format.replace(/[^\d\,]/g, '') : format.replace(/[^\d\.]/g, '')).split(dec);
  284. if (1 &lt; psplit.length) {
  285. v = v.toFixed(psplit[1].length);
  286. } else if(2 &lt; psplit.length) {
  287. throw ('NumberFormatException: invalid format, formats should have no more than 1 period: ' + format);
  288. } else {
  289. v = v.toFixed(0);
  290. }
  291. var fnum = v.toString();
  292. psplit = fnum.split('.');
  293. if (hasComma) {
  294. var cnum = psplit[0],
  295. parr = [],
  296. j = cnum.length,
  297. m = Math.floor(j / 3),
  298. n = cnum.length % 3 || 3,
  299. i;
  300. for (i = 0; i &lt; j; i += n) {
  301. if (i != 0) {
  302. n = 3;
  303. }
  304. parr[parr.length] = cnum.substr(i, n);
  305. m -= 1;
  306. }
  307. fnum = parr.join(comma);
  308. if (psplit[1]) {
  309. fnum += dec + psplit[1];
  310. }
  311. } else {
  312. if (psplit[1]) {
  313. fnum = psplit[0] + dec + psplit[1];
  314. }
  315. }
  316. return (neg ? '-' : '') + format.replace(/[\d,?\.?]+/, fnum);
  317. },
  318. <span id='Ext-util-Format-method-numberRenderer'> /**
  319. </span> * Returns a number rendering function that can be reused to apply a number format multiple times efficiently
  320. * @param {String} format Any valid number format string for {@link #number}
  321. * @return {Function} The number formatting function
  322. */
  323. numberRenderer : function(format) {
  324. return function(v) {
  325. return Ext.util.Format.number(v, format);
  326. };
  327. },
  328. <span id='Ext-util-Format-method-plural'> /**
  329. </span> * Selectively do a plural form of a word based on a numeric value. For example, in a template,
  330. * {commentCount:plural(&quot;Comment&quot;)} would result in &quot;1 Comment&quot; if commentCount was 1 or would be &quot;x Comments&quot;
  331. * if the value is 0 or greater than 1.
  332. * @param {Number} value The value to compare against
  333. * @param {String} singular The singular form of the word
  334. * @param {String} plural (optional) The plural form of the word (defaults to the singular with an &quot;s&quot;)
  335. */
  336. plural : function(v, s, p) {
  337. return v +' ' + (v == 1 ? s : (p ? p : s+'s'));
  338. },
  339. <span id='Ext-util-Format-method-nl2br'> /**
  340. </span> * Converts newline characters to the HTML tag &amp;lt;br/&gt;
  341. * @param {String} The string value to format.
  342. * @return {String} The string with embedded &amp;lt;br/&gt; tags in place of newlines.
  343. */
  344. nl2br : function(v) {
  345. return Ext.isEmpty(v) ? '' : v.replace(nl2brRe, '&lt;br/&gt;');
  346. }
  347. };
  348. }();
  349. </pre>
  350. </body>
  351. </html>