]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/reports/oils_rpt_builder.js
more work on transforms
[Evergreen.git] / Open-ILS / web / reports / oils_rpt_builder.js
1 /** initializes reports, some basid display settings, 
2   * grabs and builds the IDL tree
3   */
4 function oilsInitReportBuilder() {
5         oilsInitReports();
6         oilsReportBuilderReset();
7         oilsDrawRptTree(
8                 function() { 
9                         hideMe(DOM.oils_rpt_tree_loading); 
10                         unHideMe(DOM.oils_rpt_table); 
11                 }
12         );
13 }
14
15 function oilsReportBuilderReset() {
16         var n = (oilsRpt) ? oilsRpt.name : "";
17         oilsRpt = new oilsReport();
18         oilsRpt.name = n;
19         oilsRptDisplaySelector  = DOM.oils_rpt_display_selector;
20         oilsRptFilterSelector   = DOM.oils_rpt_filter_selector;
21         removeChildren(oilsRptDisplaySelector);
22         removeChildren(oilsRptFilterSelector);
23         oilsRptDebug();
24         oilsRptResetParams();
25 }
26
27 /* returns just the column name */
28 function oilsRptPathCol(path) {
29         var parts = path.split(/-/);
30         return parts.pop();
31 }
32
33 /* returns the IDL class of the selected column */
34 function oilsRptPathClass(path) {
35         var parts = path.split(/-/);
36         parts.pop();
37         return parts.pop();
38 }
39
40 /* returns everything prior to the column name */
41 function oilsRptPathRel(path) {
42         var parts = path.split(/-/);
43         parts.pop();
44         return parts.join('-');
45 }
46
47 /* creates a label "path" based on the column path */
48 function oilsRptMakeLabel(path) {
49         var parts = path.split(/-/);
50         var str = '';
51         for( var i = 0; i < parts.length; i++ ) {
52                 if(i%2 == 0) {
53                         if( i == 0 )
54                                 str += oilsIDL[parts[i]].label;
55                 } else {
56                         var column = parts[i];
57                         var data = oilsIDL[parts[i-1]];
58                         var f = grep(data.fields, 
59                                 function(j){return (j.name == column); })[0];
60                         str += ":"+f.label;
61                 }
62         }
63         return str;
64 }
65
66
67 /* adds an item to the display window */
68 function oilsAddRptDisplayItem(path, name, tform, params) {
69         if( ! oilsAddSelectorItem(oilsRptDisplaySelector, path, name) ) 
70                 return;
71
72         /* add the selected columns to the report output */
73         name = (name) ? name : oilsRptPathCol(path);
74         var param = oilsRptNextParam();
75
76         /* add this item to the select blob */
77         var sel = {
78                 relation:oilsRptPathRel(path), 
79                 alias:param
80         };
81
82         if( tform ) {
83                 sel.column = {};
84                 if( params ) {
85                         params.unshift(oilsRptPathCol(path));
86                         sel.column[tform] = params;
87                 } else {
88                         sel.column[tform] = oilsRptPathCol(path); 
89                 }
90         } else { sel.column = oilsRptPathCol(path); }
91
92         oilsRpt.def.select.push(sel);
93
94         mergeObjects( oilsRpt.def.from, oilsRptBuildFromClause(path));
95         oilsRpt.params[param] = name;
96         oilsRptDebug();
97 }
98
99 /* takes a column path and builds a from-clause object for the path */
100 function oilsRptBuildFromClause(path) {
101         var parts = path.split(/-/);
102         var obj = {};
103         var tobj = obj;
104         var newpath = "";
105
106         for( var i = 0; i < parts.length; i += 2 ) {
107                 var cls = parts[i];
108                 var col = parts[i+1];
109                 var node = oilsIDL[parts[i]];
110                 var field = oilsRptFindField(node, col);
111                 newpath = (newpath) ? newpath + '-'+ cls : cls;
112
113                 tobj.table = node.table;
114                 tobj.alias = newpath;
115                 _debug('field type is ' + field.type);
116                 if( i == (parts.length - 2) ) break;
117
118                 tobj.join = {};
119                 tobj = tobj.join;
120                 tobj[col] = {};
121                 tobj = tobj[col];
122                 if( field.type == 'link' )
123                         tobj.key = field.key;
124
125                 newpath = newpath + '-'+ col;
126         }
127
128         _debug("built 'from' clause: path="+path+"\n"+formatJSON(js2JSON(obj)));
129         return obj;
130 }
131
132
133 /* removes a specific item from the display window */
134 function oilsDelDisplayItem(val) {
135         oilsDelSelectorItem(oilsRptDisplaySelector, val);
136 }
137
138 /* removes selected items from the display window */
139 function oilsDelSelectedDisplayItems() {
140         var list = oilsDelSelectedItems(oilsRptDisplaySelector);
141
142         /* remove the de-selected columns from the report output */
143         oilsRpt.def.select = grep( oilsRpt.def.select, 
144                 function(i) {
145                         for( var j = 0; j < list.length; j++ ) {
146                                 var d = list[j];
147                                 var col = i.column;
148
149                                 /* if this columsn has a transform, 
150                                         it will be an object { tform => column } */
151                                 if( typeof col != 'string' ) 
152                                         for( var c in col ) col = col[c];
153
154                                 /* if this transform requires params, the column 
155                                         will be the first item in the param set array */
156                                 if( typeof col != 'string' ) col = col[0];
157
158                                 if( oilsRptPathRel(d) == i.relation && oilsRptPathCol(d) == col ) {
159                                         var param = (i.alias) ? i.alias.match(/::PARAM\d*/) : null;
160                                         if( param ) delete oilsRpt.params[param];
161                                         return false;
162                                 }
163                         }
164                         return true;
165                 }
166         );
167
168         if(!oilsRpt.def.select) {
169                 oilsRpt.def.select = [];
170                 oilsReportBuilderReset();
171
172         } else {
173                 for( var j = 0; j < list.length; j++ ) 
174                         oilsRptPruneFromClause(list[j]);
175         }
176
177         oilsRptDebug();
178 }
179
180 /* for each item in the path list, remove the associated data
181 from the "from" clause */
182 function oilsRptPruneFromClause(pathlist) {
183 }
184
185 /* adds an item to the display window */
186 function oilsAddRptFilterItem(val) {
187         oilsAddSelectorItem(oilsRptFilterSelector, val);
188 }
189
190 /* removes a specific item from the display window */
191 function oilsDelFilterItem(val) {
192         oilsDelSelectorItem(oilsRptFilterSelector, val);
193 }
194
195 /* removes selected items from the display window */
196 function oilsDelSelectedFilterItems() {
197         oilsDelSelectedItems(oilsRptFilterSelector);
198 }
199
200
201 /* adds an item to the display window */
202 function oilsAddSelectorItem(sel, val, name) {
203         name = (name) ? name : oilsRptMakeLabel(val);
204         _debug("adding selector item "+name+' = ' +val);
205         for( var i = 0; i < sel.options.length; i++ ) {
206                 var opt = sel.options[i];
207                 if( opt.value == val ) return false;
208         }
209         insertSelectorVal( sel, -1, name, val );
210         return true;
211 }
212
213
214 /* removes a specific item from the display window */
215 function oilsDelSelectorItem(sel, val) {
216         _debug("deleting selector item "+val);
217         var opts = sel.options;
218         for( var i = 0; i < opts.length; i++ ) {
219                 var opt = opts[i];
220                 if( opt.value == val )  {
221                         if( i == opts.length - 1 ) 
222                                 opts[i] = null;
223                         else opts[i] = opts[i+1];
224                         return;
225                 }
226         }
227 }
228
229 /* removes selected items from the display window */
230 function oilsDelSelectedItems(sel) {
231         var list = getSelectedList(sel);
232         for( var i = 0; i < list.length; i++ ) 
233                 oilsDelSelectorItem(sel, list[i]);
234         return list;
235 }
236
237
238 /* hides the different field editor tabs */
239 function oilsRptHideEditorDivs() {
240         hideMe(DOM.oils_rpt_tform_div);
241         hideMe(DOM.oils_rpt_filter_div);
242         hideMe(DOM.oils_rpt_agg_filter_div);
243 }
244
245
246 /**
247   This draws the 3-tabbed window containing the transform,
248   filter, and aggregate filter picker window
249   */
250 function oilsRptDrawDataWindow(path) {
251         var col = oilsRptPathCol(path);
252         var cls = oilsRptPathClass(path);
253         var field = grep(oilsIDL[cls].fields, function(f){return (f.name==col);})[0];
254         _debug("setting update data window for column "+col+' on class '+cls);
255
256         var div = DOM.oils_rpt_column_editor;
257         /* set a preliminary top position so the page won't bounce around */
258         div.setAttribute('style','top:'+oilsMouseX+'px');
259
260         /* unhide the div so we can determine the dimensions */
261         unHideMe(div);
262
263         /* don't let them see the floating div until the position is fully determined */
264         div.style.visibility='hidden'; 
265
266         oilsRptDrawTransformWindow(path, col, cls, field);
267
268         DOM.oils_rpt_column_editor_close_button.onclick = function(){hideMe(div);};
269         buildFloatingDiv(div, 600);
270
271         /* now let them see it */
272         div.style.visibility='visible';
273
274         /* give the tab links behavior */
275         DOM.oils_rpt_tform_tab.onclick = 
276                 function(){oilsRptHideEditorDivs();unHideMe(DOM.oils_rpt_tform_div)};
277         DOM.oils_rpt_filter_tab.onclick = 
278                 function(){oilsRptHideEditorDivs();unHideMe(DOM.oils_rpt_filter_div)};
279         DOM.oils_rpt_agg_filter_tab.onclick = 
280                 function(){oilsRptHideEditorDivs();unHideMe(DOM.oils_rpt_agg_filter_div)};
281
282         DOM.oils_rpt_tform_tab.onclick();
283 }
284
285
286 /* draws the transform window */
287 function oilsRptDrawTransformWindow(path, col, cls, field) {
288         appendClear(DOM.oils_rpt_tform_label, text(oilsRptMakeLabel(path)));
289         DOM.oils_rpt_tform_label_input.value = oilsRptMakeLabel(path);
290         var dtype = field.datatype;
291
292         DOM.oils_rpt_tform_submit.onclick = 
293                 function(){ 
294                         var tform = oilsRptGetTform(dtype);
295                         _debug('found tform: ' + js2JSON(tform));
296                         var params = getRptTformParams(dtype, tform);
297                         _debug('found tform params: ' + js2JSON(params));
298                         tform = (tform == 'raw') ? null : tform;
299                         oilsAddRptDisplayItem(path, DOM.oils_rpt_tform_label_input.value, tform, params ) 
300                 };
301
302         DOM.oils_rpt_tform_label_input.focus();
303         DOM.oils_rpt_tform_label_input.select();
304         oilsRptHideTformFields();
305
306         _debug("Transforming item with datatype "+dtype);
307         unHideMe($('oils_rpt_tform_'+dtype+'_div'));
308         $('oils_rpt_tform_all_raw').checked = true;
309 }
310
311 function oilsRptHideTformFields() {
312         for( var t in oilsRptTransforms ) 
313                 hideMe($('oils_rpt_tform_'+t+'_div'));
314 }
315
316 function oilsRptGetTform(datatype) {
317         for( var i in oilsRptTransforms[datatype] ) 
318                 if( $('oils_rpt_tform_'+datatype+'_'+oilsRptTransforms[datatype][i]).checked )
319                         return oilsRptTransforms[datatype][i];
320         for( var i in oilsRptTransforms.all ) 
321                 if( $('oils_rpt_tform_all_'+oilsRptTransforms.all[i]).checked )
322                         return oilsRptTransforms.all[i];
323         return null;
324 }
325
326 function getRptTformParams(type, tform) {
327         switch(type) {
328                 case 'timestamp':
329                         switch(tform) {
330                                 case 'months_ago':
331                                         return [DOM.oils_rpt_tform_timestamp_months_ago_input.value];
332                                 case 'quarters_ago':
333                                         return [DOM.oils_rpt_tform_timestamp_quarters_ago_input.value];
334                         }
335                 case 'string' :
336                         switch(tform) {
337                                 case 'substring' :
338                                         return [];
339                         }
340         }
341 }
342
343