]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/reports/oils_rpt_builder.js
core report types are broken out into a top level report type dropdown
[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         oilsRpt = new oilsReport();
17         oilsRptDisplaySelector  = DOM.oils_rpt_display_selector;
18         oilsRptFilterSelector   = DOM.oils_rpt_filter_selector;
19         removeChildren(oilsRptDisplaySelector);
20         removeChildren(oilsRptFilterSelector);
21         oilsRptDebug();
22 }
23
24 /* returns just the column name */
25 function oilsRptPathCol(path) {
26         var parts = path.split(/-/);
27         return parts.pop();
28 }
29
30 /* returns the IDL class of the selected column */
31 function oilsRptPathClass(path) {
32         var parts = path.split(/-/);
33         parts.pop();
34         return parts.pop();
35 }
36
37 /* returns everything prior to the column name */
38 function oilsRptPathRel(path) {
39         var parts = path.split(/-/);
40         parts.pop();
41         return parts.join('-');
42 }
43
44 /* creates a label "path" based on the column path */
45 function oilsRptMakeLabel(path) {
46         var parts = path.split(/-/);
47         var str = '';
48         for( var i = 0; i < parts.length; i++ ) {
49                 if(i%2 == 0) {
50                         if( i == 0 )
51                                 str += oilsIDL[parts[i]].label;
52                 } else {
53                         var column = parts[i];
54                         var data = oilsIDL[parts[i-1]];
55                         var f = grep(data.fields, 
56                                 function(j){return (j.name == column); })[0];
57                         str += ":"+f.label;
58                 }
59         }
60         return str;
61 }
62
63
64 /* adds an item to the display window */
65 function oilsAddRptDisplayItem(path, name) {
66         if( ! oilsAddSelectorItem(oilsRptDisplaySelector, path, name) ) 
67                 return;
68
69         /* add the selected columns to the report output */
70         name = (name) ? name : oilsRptPathCol(path);
71         var param = oilsRptNextParam();
72
73         /* add this item to the select blob */
74         oilsRpt.def.select.push( {
75                 relation:oilsRptPathRel(path), 
76                 column:oilsRptPathCol(path), 
77                 alias:param
78         });
79
80         mergeObjects( oilsRpt.def.from, oilsRptBuildFromClause(path));
81         oilsRpt.params[param] = name;
82         oilsRptDebug();
83 }
84
85 /* takes a column path and builds a from-clause object for the path */
86 function oilsRptBuildFromClause(path) {
87         var parts = path.split(/-/);
88         //var obj = {from : {}};
89         var obj = {};
90         var tobj = obj;
91         var newpath = "";
92         for( var i = 0; i < parts.length; i += 2 ) {
93                 var cls = parts[i];
94                 var col = parts[i+1];
95                 var node = oilsIDL[parts[i]];
96                 var field = oilsRptFindField(node, col);
97                 newpath = (newpath) ? newpath + '-'+ cls : cls;
98
99                 tobj.table = node.table;
100                 tobj.alias = newpath;
101                 _debug('field type is ' + field.type);
102                 if( i == (parts.length - 2) ) break;
103
104                 tobj.join = {};
105                 tobj = tobj.join;
106                 tobj[col] = {};
107                 tobj = tobj[col];
108                 if( field.type == 'link' )
109                         tobj.key = field.key;
110
111                 newpath = newpath + '-'+ col;
112         }
113
114         _debug("built 'from' clause: path="+path+"\n"+formatJSON(js2JSON(obj)));
115         return obj;
116 }
117
118
119 /* removes a specific item from the display window */
120 function oilsDelDisplayItem(val) {
121         oilsDelSelectorItem(oilsRptDisplaySelector, val);
122 }
123
124 /* removes selected items from the display window */
125 function oilsDelSelectedDisplayItems() {
126         var list = oilsDelSelectedItems(oilsRptDisplaySelector);
127
128         /* remove the de-selected columns from the report output */
129         oilsRpt.def.select = grep( oilsRpt.def.select, 
130                 function(i) {
131                         for( var j = 0; j < list.length; j++ ) {
132                                 var d = list[j];
133                                 if( oilsRptPathRel(d) == i.relation 
134                                                 && oilsRptPathCol(d) == i.column ) {
135                                         var param = (i.alias) ? i.alias.match(/::PARAM\d*/) : null;
136                                         if( param ) delete oilsRpt.params[param];
137                                         return false;
138                                 }
139                         }
140                         return true;
141                 }
142         );
143         if(!oilsRpt.def.select) oilsRpt.def.select = [];
144
145         for( var j = 0; j < list.length; j++ ) 
146                 oilsRptPruneFromClause(list[j]);
147
148         oilsRptDebug();
149 }
150
151 /* for each item in the path list, remove the associated data
152 from the "from" clause */
153 function oilsRptPruneFromClause(pathlist) {
154 }
155
156 /* adds an item to the display window */
157 function oilsAddRptFilterItem(val) {
158         oilsAddSelectorItem(oilsRptFilterSelector, val);
159 }
160
161 /* removes a specific item from the display window */
162 function oilsDelFilterItem(val) {
163         oilsDelSelectorItem(oilsRptFilterSelector, val);
164 }
165
166 /* removes selected items from the display window */
167 function oilsDelSelectedFilterItems() {
168         oilsDelSelectedItems(oilsRptFilterSelector);
169 }
170
171
172 /* adds an item to the display window */
173 function oilsAddSelectorItem(sel, val, name) {
174         name = (name) ? name : oilsRptMakeLabel(val);
175         _debug("adding selector item "+name+' = ' +val);
176         for( var i = 0; i < sel.options.length; i++ ) {
177                 var opt = sel.options[i];
178                 if( opt.value == val ) return false;
179         }
180         insertSelectorVal( sel, -1, name, val );
181         return true;
182 }
183
184
185 /* removes a specific item from the display window */
186 function oilsDelSelectorItem(sel, val) {
187         _debug("deleting selector item "+val);
188         var opts = sel.options;
189         for( var i = 0; i < opts.length; i++ ) {
190                 var opt = opts[i];
191                 if( opt.value == val )  {
192                         if( i == opts.length - 1 ) 
193                                 opts[i] = null;
194                         else opts[i] = opts[i+1];
195                         return;
196                 }
197         }
198 }
199
200 /* removes selected items from the display window */
201 function oilsDelSelectedItems(sel) {
202         var list = getSelectedList(sel);
203         for( var i = 0; i < list.length; i++ ) 
204                 oilsDelSelectorItem(sel, list[i]);
205         return list;
206 }
207
208
209 /* hides the different field editor tabs */
210 function oilsRptHideEditorDivs() {
211         hideMe(DOM.oils_rpt_tform_div);
212         hideMe(DOM.oils_rpt_filter_div);
213         hideMe(DOM.oils_rpt_agg_filter_div);
214 }
215
216
217 /**
218   This draws the 3-tabbed window containing the transform,
219   filter, and aggregate filter picker window
220   */
221 function oilsRptDrawDataWindow(path) {
222         var col = oilsRptPathCol(path);
223         var cls = oilsRptPathClass(path);
224         var field = grep(oilsIDL[cls].fields, function(f){return (f.name==col);})[0];
225         _debug("setting update data window for column "+col+' on class '+cls);
226
227         var div = DOM.oils_rpt_column_editor;
228         /* set a preliminary top position so the page won't bounce around */
229         div.setAttribute('style','top:'+oilsMouseX+'px');
230
231         /* unhide the div so we can determine the dimensions */
232         unHideMe(div);
233
234         /* don't let them see the floating div until the position is fully determined */
235         div.style.visibility='hidden'; 
236
237         oilsRptDrawTransformWindow(path, col, cls, field);
238
239         DOM.oils_rpt_column_editor_close_button.onclick = function(){hideMe(div);};
240         buildFloatingDiv(div, 600);
241
242         /* now let them see it */
243         div.style.visibility='visible';
244
245         /* give the tab links behavior */
246         DOM.oils_rpt_tform_tab.onclick = 
247                 function(){oilsRptHideEditorDivs();unHideMe(DOM.oils_rpt_tform_div)};
248         DOM.oils_rpt_filter_tab.onclick = 
249                 function(){oilsRptHideEditorDivs();unHideMe(DOM.oils_rpt_filter_div)};
250         DOM.oils_rpt_agg_filter_tab.onclick = 
251                 function(){oilsRptHideEditorDivs();unHideMe(DOM.oils_rpt_agg_filter_div)};
252 }
253
254
255 /* draws the transform window */
256 function oilsRptDrawTransformWindow(path, col, cls, field) {
257         appendClear(DOM.oils_rpt_tform_label, text(oilsRptMakeLabel(path)));
258         DOM.oils_rpt_tform_label_input.value = oilsRptMakeLabel(path);
259
260         DOM.oils_rpt_tform_submit.onclick = 
261                 function(){ oilsAddRptDisplayItem(path, DOM.oils_rpt_tform_label_input.value) };
262
263         DOM.oils_rpt_tform_label_input.focus();
264         DOM.oils_rpt_tform_label_input.select();
265
266         if( field.datatype == 'timestamp' )
267                 unHideMe(DOM.oils_rpt_tform_date_div);
268 }
269
270
271