0ad2ee958e8aed076038e69eebe3ef0cf2b6cebc.svn-base 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // private
  7. // Field objects are not intended to be created directly, but are created
  8. // behind the scenes when defined for Record objects. See Record.js for details.
  9. Ext.data.Field = function(config) {
  10. if (typeof config == "string") {
  11. config = {
  12. name : config
  13. };
  14. }
  15. Ext.apply(this, config);
  16. if (!this.type) {
  17. this.type = "auto";
  18. }
  19. var st = Ext.data.SortTypes;
  20. // named sortTypes are supported, here we look them up
  21. if (typeof this.sortType == "string") {
  22. this.sortType = st[this.sortType];
  23. }
  24. // set default sortType for strings and dates
  25. if (!this.sortType) {
  26. switch (this.type) {
  27. case "string" :
  28. this.sortType = st.asUCString;
  29. break;
  30. case "date" :
  31. this.sortType = st.asDate;
  32. break;
  33. default :
  34. this.sortType = st.none;
  35. }
  36. }
  37. // define once
  38. var stripRe = /[\$,%]/g;
  39. // prebuilt conversion function for this field, instead of
  40. // switching every time we're reading a value
  41. if (!this.convert) {
  42. var cv, dateFormat = this.dateFormat;
  43. switch (this.type) {
  44. case "" :
  45. case "auto" :
  46. case undefined :
  47. cv = function(v) {
  48. return v;
  49. };
  50. break;
  51. case "string" :
  52. cv = function(v) {
  53. return (v === undefined || v === null) ? '' : String(v);
  54. };
  55. break;
  56. case "int" :
  57. cv = function(v) {
  58. return v !== undefined && v !== null && v !== ''
  59. ? parseInt(String(v).replace(stripRe, ""), 10)
  60. : '';
  61. };
  62. break;
  63. case "float" :
  64. cv = function(v) {
  65. return v !== undefined && v !== null && v !== ''
  66. ? parseFloat(String(v).replace(stripRe, ""), 10)
  67. : '';
  68. };
  69. break;
  70. case "bool" :
  71. case "boolean" :
  72. cv = function(v) {
  73. return v === true || v === "true" || v == 1;
  74. };
  75. break;
  76. case "date" :
  77. cv = function(v) {
  78. if (!v) {
  79. return '';
  80. }
  81. if (v instanceof Date) {
  82. return v;
  83. }
  84. if (dateFormat) {
  85. if (dateFormat == "timestamp") {
  86. return new Date(v * 1000);
  87. }
  88. if (dateFormat == "time") {
  89. return new Date(parseInt(v, 10));
  90. }
  91. return Date.parseDate(v, dateFormat);
  92. }
  93. var parsed = Date.parse(v);
  94. return parsed ? new Date(parsed) : null;
  95. };
  96. break;
  97. }
  98. this.convert = cv;
  99. }
  100. };
  101. Ext.data.Field.prototype = {
  102. dateFormat : null,
  103. defaultValue : "",
  104. mapping : null,
  105. sortType : null,
  106. sortDir : "ASC"
  107. };