]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/reports/oils_rpt_tforms.js
refactoring widgets, round one
[Evergreen.git] / Open-ILS / web / reports / oils_rpt_tforms.js
1
2 var OILS_RPT_TRANSFORMS = {
3         Bare : {
4                 label : 'Raw Data'
5         },
6
7         first : {
8                 label : 'First Value'
9         },
10
11         last : {
12                 label : 'Last Value'
13         },
14
15         count : {
16                 aggregate : true,
17                 label :  'Count'
18         },
19
20         count_distinct : {
21                 aggregate : true,
22                 label : 'Count Distinct'
23         },
24
25         min : {
26                 aggregate : true,
27                 label : 'Min'
28         },
29
30         max : {
31                 aggregate : true,
32                 label : 'Max'
33         },
34
35         /* string transforms ------------------------- */
36
37         substring : {
38                 datatype : OILS_RPT_DTYPE_STRING,
39                 label : 'Substring'
40         },
41
42         lower : {
43                 datatype : OILS_RPT_DTYPE_STRING,
44                 label : 'Lower case'
45         },
46
47         upper : {
48                 datatype : OILS_RPT_DTYPE_STRING,
49                 label : 'Upper case'
50         },
51
52         /* timestamp transforms ----------------------- */
53         dow : {
54                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
55                 label : 'Day of Week',
56                 regex : /^[0-6]$/
57         },
58         dom : {
59                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
60                 label : 'Day of Month',
61                 regex : /^[0-9]{1,2}$/
62         },
63
64         doy : {
65                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
66                 label : 'Day of Year',
67                 regex : /^[0-9]{1,3}$/
68         },
69
70         woy : {
71                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
72                 label : 'Week of Year',
73                 regex : /^[0-9]{1,2}$/
74         },
75
76         moy : {
77                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
78                 label : 'Month of Year',
79                 regex : /^[0-9]{1,2}$/
80         },
81
82         qoy : {
83                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
84                 label : 'Quarter of Year',
85                 regex : /^[1234]$/
86         }, 
87
88         date : {
89                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
90                 label : 'Date',
91                 regex : /^\d{4}-\d{2}-\d{2}$/,
92                 hint  : 'YYYY-MM-DD',
93                 cal_format : '%Y-%m-%d',
94                 input_size : 10
95         },
96
97         month_trunc : {
98                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
99                 label : 'Year + Month',
100                 regex : /^\d{4}-\d{2}$/,
101                 hint  : 'YYYY-MM',
102                 cal_format : '%Y-%m',
103                 input_size : 7
104         },
105
106         year_trunc : {
107                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
108                 label : 'Year',
109                 regex : /^\d{4}$/,
110                 hint  : 'YYYY',
111                 cal_format : '%Y',
112                 input_size : 4
113         },
114
115         day_name : {
116                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
117                 label : 'Day Name'
118         }, 
119
120         month_name : {
121                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
122                 label : 'Month Name'
123         },
124         age : {
125                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
126                 label : 'Age'
127         },
128
129         relative_year : {
130                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
131                 label : 'Relative year'
132         },
133
134         relative_month : {
135                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
136                 label : 'Relative month'
137         },
138
139         relative_week : {
140                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
141                 label : 'Relative week'
142         },
143
144         relative_date : {
145                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
146                 label : 'Relative date'
147         },
148
149         /* int  / float transforms ----------------------------------- */
150         sum : {
151                 datatype : [ OILS_RPT_DTYPE_INT, OILS_RPT_DTYPE_FLOAT ],
152                 label : 'Sum',
153                 aggregate : true
154         }, 
155
156         average : {
157                 datatype : [ OILS_RPT_DTYPE_INT, OILS_RPT_DTYPE_FLOAT ],
158                 label : 'Average',
159                 aggregate : true
160         },
161
162         round : {
163                 datatype : [ OILS_RPT_DTYPE_INT, OILS_RPT_DTYPE_FLOAT ],
164                 label : 'Round',
165         },
166
167         'int' : {
168                 datatype : OILS_RPT_DTYPE_FLOAT,
169                 label : 'Drop trailing decimals'
170         }
171 }
172
173
174 function oilsRptGetTforms(args) {
175         var dtype = args.datatype;
176         var agg = args.aggregate;
177         var tforms = OILS_RPT_TRANSFORMS;
178         var nonagg = args.non_aggregate;
179
180         var keys = oilsRptObjectKeys(OILS_RPT_TRANSFORMS);
181         var tforms = [];
182
183         _debug('getting tform '+dtype+' : ' + agg + ' : ' + nonagg);
184
185         for( var i = 0; i < keys.length; i++ ) {
186                 var key = keys[i];
187                 var obj = OILS_RPT_TRANSFORMS[key];
188                 if( dtype && !oilsRptTformIsDtype(key,dtype) ) continue;
189                 if( agg && !nonagg && !obj.aggregate ) continue;
190                 if( !agg && nonagg && obj.aggregate ) continue;
191                 tforms.push(key);
192         }
193
194         return tforms;
195 }
196
197
198 function oilsRptTformIsDtype(tform, dtype) {
199         var obj = OILS_RPT_TRANSFORMS[tform];
200         if( typeof obj.datatype == 'string' )
201                 return (obj.datatype == dtype);
202         return !obj.datatype || grep(obj.datatype, function(d) { return (d == dtype) });
203 }
204
205
206
207
208 /* builds a new transform picker */
209 function oilsRptTformPicker(args) {
210         this.node = args.node;
211         this.selector = elem('select');
212         this.tforms = oilsRptGetTforms(args);
213         for( var i = 0; i < this.tforms.length; i++ ) 
214                 this.addOpt(this.tforms[i]);
215         appendClear(this.node, this.selector);
216 }
217
218 oilsRptTformPicker.prototype.addOpt = function(key) {
219         var tform = OILS_RPT_TRANSFORMS[key];           
220         var obj = this;
221         insertSelectorVal(this.selector, -1, tform.label, key);
222 }
223
224 oilsRptTformPicker.prototype.getSelected = function(key) {
225         return getSelectorVal(this.selector);
226 }
227
228
229