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