5e5fe243986d8b86e84406cf23673d0e5ab958ae.svn-base 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. Ext.util.Format = function() {
  7. var trimRe = /^\s+|\s+$/g;
  8. return {
  9. ellipsis : function(value, len) {
  10. if (value && value.length > len) {
  11. return value.substr(0, len - 3) + "..."
  12. }
  13. return value
  14. },
  15. undef : function(value) {
  16. return value !== undefined ? value : ""
  17. },
  18. defaultValue : function(value, defaultValue) {
  19. return value !== undefined && value !== "" ? value : defaultValue
  20. },
  21. htmlEncode : function(value) {
  22. return !value ? value : String(value).replace(/&/g, "&")
  23. .replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g,
  24. "&quot;")
  25. },
  26. htmlDecode : function(value) {
  27. return !value ? value : String(value).replace(/&amp;/g, "&")
  28. .replace(/&gt;/g, ">").replace(/&lt;/g, "<").replace(
  29. /&quot;/g, "\"")
  30. },
  31. trim : function(value) {
  32. return String(value).replace(trimRe, "")
  33. },
  34. substr : function(value, start, length) {
  35. return String(value).substr(start, length)
  36. },
  37. lowercase : function(value) {
  38. return String(value).toLowerCase()
  39. },
  40. uppercase : function(value) {
  41. return String(value).toUpperCase()
  42. },
  43. capitalize : function(value) {
  44. return !value ? value : value.charAt(0).toUpperCase()
  45. + value.substr(1).toLowerCase()
  46. },
  47. call : function(value, fn) {
  48. if (arguments.length > 2) {
  49. var args = Array.prototype.slice.call(arguments, 2);
  50. args.unshift(value);
  51. return eval(fn).apply(window, args)
  52. } else {
  53. return eval(fn).call(window, value)
  54. }
  55. },
  56. usMoney : function(v) {
  57. v = (Math.round((v - 0) * 100)) / 100;
  58. v = (v == Math.floor(v)) ? v + ".00" : ((v * 10 == Math.floor(v
  59. * 10)) ? v + "0" : v);
  60. v = String(v);
  61. var ps = v.split(".");
  62. var whole = ps[0];
  63. var sub = ps[1] ? "." + ps[1] : ".00";
  64. var r = /(\d+)(\d{3})/;
  65. while (r.test(whole)) {
  66. whole = whole.replace(r, "$1" + "," + "$2")
  67. }
  68. v = whole + sub;
  69. if (v.charAt(0) == "-") {
  70. return "-$" + v.substr(1)
  71. }
  72. return "$" + v
  73. },
  74. date : function(v, format) {
  75. if (!v) {
  76. return ""
  77. }
  78. if (!(v instanceof Date)) {
  79. v = new Date(Date.parse(v))
  80. }
  81. return v.dateFormat(format || "m/d/Y")
  82. },
  83. dateRenderer : function(format) {
  84. return function(v) {
  85. return Ext.util.Format.date(v, format)
  86. }
  87. },
  88. stripTagsRE : /<\/?[^>]+>/gi,
  89. stripTags : function(v) {
  90. return !v ? v : String(v).replace(this.stripTagsRE, "")
  91. },
  92. stripScriptsRe : /(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)/ig,
  93. stripScripts : function(v) {
  94. return !v ? v : String(v).replace(this.stripScriptsRe, "")
  95. },
  96. fileSize : function(size) {
  97. if (size < 1024) {
  98. return size + " bytes"
  99. } else {
  100. if (size < 1048576) {
  101. return (Math.round(((size * 10) / 1024)) / 10) + " KB"
  102. } else {
  103. return (Math.round(((size * 10) / 1048576)) / 10) + " MB"
  104. }
  105. }
  106. },
  107. math : function() {
  108. var fns = {};
  109. return function(v, a) {
  110. if (!fns[a]) {
  111. fns[a] = new Function("v", "return v " + a + ";")
  112. }
  113. return fns[a](v)
  114. }
  115. }()
  116. }
  117. }();