initial import
[roojs1] / Roo / data / Field.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11
12  
13 Roo.data.Field = function(config){
14     if(typeof config == "string"){
15         config = {name: config};
16     }
17     Roo.apply(this, config);
18     
19     if(!this.type){
20         this.type = "auto";
21     }
22     
23     var st = Roo.data.SortTypes;
24     // named sortTypes are supported, here we look them up
25     if(typeof this.sortType == "string"){
26         this.sortType = st[this.sortType];
27     }
28     
29     // set default sortType for strings and dates
30     if(!this.sortType){
31         switch(this.type){
32             case "string":
33                 this.sortType = st.asUCString;
34                 break;
35             case "date":
36                 this.sortType = st.asDate;
37                 break;
38             default:
39                 this.sortType = st.none;
40         }
41     }
42
43     // define once
44     var stripRe = /[\$,%]/g;
45
46     // prebuilt conversion function for this field, instead of
47     // switching every time we're reading a value
48     if(!this.convert){
49         var cv, dateFormat = this.dateFormat;
50         switch(this.type){
51             case "":
52             case "auto":
53             case undefined:
54                 cv = function(v){ return v; };
55                 break;
56             case "string":
57                 cv = function(v){ return (v === undefined || v === null) ? '' : String(v); };
58                 break;
59             case "int":
60                 cv = function(v){
61                     return v !== undefined && v !== null && v !== '' ?
62                            parseInt(String(v).replace(stripRe, ""), 10) : '';
63                     };
64                 break;
65             case "float":
66                 cv = function(v){
67                     return v !== undefined && v !== null && v !== '' ?
68                            parseFloat(String(v).replace(stripRe, ""), 10) : ''; 
69                     };
70                 break;
71             case "bool":
72             case "boolean":
73                 cv = function(v){ return v === true || v === "true" || v == 1; };
74                 break;
75             case "date":
76                 cv = function(v){
77                     if(!v){
78                         return '';
79                     }
80                     if(v instanceof Date){
81                         return v;
82                     }
83                     if(dateFormat){
84                         if(dateFormat == "timestamp"){
85                             return new Date(v*1000);
86                         }
87                         return Date.parseDate(v, dateFormat);
88                     }
89                     var parsed = Date.parse(v);
90                     return parsed ? new Date(parsed) : null;
91                 };
92              break;
93             
94         }
95         this.convert = cv;
96     }
97 };
98
99 Roo.data.Field.prototype = {
100     dateFormat: null,
101     defaultValue: "",
102     mapping: null,
103     sortType : null,
104     sortDir : "ASC"
105 };