]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/reports/oils_rpt_builder.js
adding a top level index-style file
[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) {
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                 sel.column[tform] = oilsRptPathCol(path); 
85         } else { sel.column = oilsRptPathCol(path); }
86
87         oilsRpt.def.select.push(sel);
88
89         mergeObjects( oilsRpt.def.from, oilsRptBuildFromClause(path));
90         oilsRpt.params[param] = name;
91         oilsRptDebug();
92 }
93
94 /* takes a column path and builds a from-clause object for the path */
95 function oilsRptBuildFromClause(path) {
96         var parts = path.split(/-/);
97         var obj = {};
98         var tobj = obj;
99         var newpath = "";
100
101         for( var i = 0; i < parts.length; i += 2 ) {
102                 var cls = parts[i];
103                 var col = parts[i+1];
104                 var node = oilsIDL[parts[i]];
105                 var field = oilsRptFindField(node, col);
106                 newpath = (newpath) ? newpath + '-'+ cls : cls;
107
108                 tobj.table = node.table;
109                 tobj.alias = newpath;
110                 _debug('field type is ' + field.type);
111                 if( i == (parts.length - 2) ) break;
112
113                 tobj.join = {};
114                 tobj = tobj.join;
115                 tobj[col] = {};
116                 tobj = tobj[col];
117                 if( field.type == 'link' )
118                         tobj.key = field.key;
119
120                 newpath = newpath + '-'+ col;
121         }
122
123         _debug("built 'from' clause: path="+path+"\n"+formatJSON(js2JSON(obj)));
124         return obj;
125 }
126
127
128 /* removes a specific item from the display window */
129 function oilsDelDisplayItem(val) {
130         oilsDelSelectorItem(oilsRptDisplaySelector, val);
131 }
132
133 /* removes selected items from the display window */
134 function oilsDelSelectedDisplayItems() {
135         var list = oilsDelSelectedItems(oilsRptDisplaySelector);
136
137         /* remove the de-selected columns from the report output */
138         oilsRpt.def.select = grep( oilsRpt.def.select, 
139                 function(i) {
140                         for( var j = 0; j < list.length; j++ ) {
141                                 var d = list[j];
142                                 var col = i.column;
143
144                                 /* if this columsn has a transform, it will be an object { tform => column } */
145                                 if( typeof col != 'string' ) 
146                                         for( var c in col ) col = col[c];
147
148                                 if( oilsRptPathRel(d) == i.relation && oilsRptPathCol(d) == col ) {
149                                         var param = (i.alias) ? i.alias.match(/::PARAM\d*/) : null;
150                                         if( param ) delete oilsRpt.params[param];
151                                         return false;
152                                 }
153                         }
154                         return true;
155                 }
156         );
157
158         if(!oilsRpt.def.select) {
159                 oilsRpt.def.select = [];
160                 oilsReportBuilderReset();
161
162         } else {
163                 for( var j = 0; j < list.length; j++ ) 
164                         oilsRptPruneFromClause(list[j]);
165         }
166
167         oilsRptDebug();
168 }
169
170 /* for each item in the path list, remove the associated data
171 from the "from" clause */
172 function oilsRptPruneFromClause(pathlist) {
173 }
174
175 /* adds an item to the display window */
176 function oilsAddRptFilterItem(val) {
177         oilsAddSelectorItem(oilsRptFilterSelector, val);
178 }
179
180 /* removes a specific item from the display window */
181 function oilsDelFilterItem(val) {
182         oilsDelSelectorItem(oilsRptFilterSelector, val);
183 }
184
185 /* removes selected items from the display window */
186 function oilsDelSelectedFilterItems() {
187         oilsDelSelectedItems(oilsRptFilterSelector);
188 }
189
190
191 /* adds an item to the display window */
192 function oilsAddSelectorItem(sel, val, name) {
193         name = (name) ? name : oilsRptMakeLabel(val);
194         _debug("adding selector item "+name+' = ' +val);
195         for( var i = 0; i < sel.options.length; i++ ) {
196                 var opt = sel.options[i];
197                 if( opt.value == val ) return false;
198         }
199         insertSelectorVal( sel, -1, name, val );
200         return true;
201 }
202
203
204 /* removes a specific item from the display window */
205 function oilsDelSelectorItem(sel, val) {
206         _debug("deleting selector item "+val);
207         var opts = sel.options;
208         for( var i = 0; i < opts.length; i++ ) {
209                 var opt = opts[i];
210                 if( opt.value == val )  {
211                         if( i == opts.length - 1 ) 
212                                 opts[i] = null;
213                         else opts[i] = opts[i+1];
214                         return;
215                 }
216         }
217 }
218
219 /* removes selected items from the display window */
220 function oilsDelSelectedItems(sel) {
221         var list = getSelectedList(sel);
222         for( var i = 0; i < list.length; i++ ) 
223                 oilsDelSelectorItem(sel, list[i]);
224         return list;
225 }
226
227
228 /* hides the different field editor tabs */
229 function oilsRptHideEditorDivs() {
230         hideMe(DOM.oils_rpt_tform_div);
231         hideMe(DOM.oils_rpt_filter_div);
232         hideMe(DOM.oils_rpt_agg_filter_div);
233 }
234
235
236 /**
237   This draws the 3-tabbed window containing the transform,
238   filter, and aggregate filter picker window
239   */
240 function oilsRptDrawDataWindow(path) {
241         var col = oilsRptPathCol(path);
242         var cls = oilsRptPathClass(path);
243         var field = grep(oilsIDL[cls].fields, function(f){return (f.name==col);})[0];
244         _debug("setting update data window for column "+col+' on class '+cls);
245
246         var div = DOM.oils_rpt_column_editor;
247         /* set a preliminary top position so the page won't bounce around */
248         div.setAttribute('style','top:'+oilsMouseX+'px');
249
250         /* unhide the div so we can determine the dimensions */
251         unHideMe(div);
252
253         /* don't let them see the floating div until the position is fully determined */
254         div.style.visibility='hidden'; 
255
256         oilsRptDrawTransformWindow(path, col, cls, field);
257
258         DOM.oils_rpt_column_editor_close_button.onclick = function(){hideMe(div);};
259         buildFloatingDiv(div, 600);
260
261         /* now let them see it */
262         div.style.visibility='visible';
263
264         /* give the tab links behavior */
265         DOM.oils_rpt_tform_tab.onclick = 
266                 function(){oilsRptHideEditorDivs();unHideMe(DOM.oils_rpt_tform_div)};
267         DOM.oils_rpt_filter_tab.onclick = 
268                 function(){oilsRptHideEditorDivs();unHideMe(DOM.oils_rpt_filter_div)};
269         DOM.oils_rpt_agg_filter_tab.onclick = 
270                 function(){oilsRptHideEditorDivs();unHideMe(DOM.oils_rpt_agg_filter_div)};
271
272         DOM.oils_rpt_tform_tab.onclick();
273 }
274
275
276 /* draws the transform window */
277 function oilsRptDrawTransformWindow(path, col, cls, field) {
278         appendClear(DOM.oils_rpt_tform_label, text(oilsRptMakeLabel(path)));
279         DOM.oils_rpt_tform_label_input.value = oilsRptMakeLabel(path);
280
281         DOM.oils_rpt_tform_submit.onclick = 
282                 function(){ 
283                         var tform = oilsRptGetTform();
284                         tform = (tform == 'raw') ? null : tform;
285                         oilsAddRptDisplayItem(path, DOM.oils_rpt_tform_label_input.value, tform) 
286                 };
287
288         DOM.oils_rpt_tform_label_input.focus();
289         DOM.oils_rpt_tform_label_input.select();
290         oilsRptHideTformFields();
291
292         _debug("Transforming item with datatype "+field.datatype);
293         unHideMe($('oils_rpt_tform_'+field.datatype+'_div'));
294         unHideMe($('oils_rpt_tform_'+field.datatype+'_raw'));
295         $('oils_rpt_tform_'+field.datatype+'_raw').checked = true;
296 }
297
298 function oilsRptHideTformFields() {
299         for( var t in oilsRptTransforms ) {
300                 hideMe($('oils_rpt_tform_'+t+'_div'));
301                 for( var i in oilsRptTransforms[t] ) {
302                         _debug('oils_rpt_tform_'+t+'_'+oilsRptTransforms[t][i]);
303                         $('oils_rpt_tform_'+t+'_'+oilsRptTransforms[t][i]).checked = false;
304                 }
305         }
306 }
307
308 function oilsRptGetTform() {
309         for( var t in oilsRptTransforms ) 
310                 for( var i in oilsRptTransforms[t] ) 
311                         if( $('oils_rpt_tform_'+t+'_'+oilsRptTransforms[t][i]).checked )
312                                 return oilsRptTransforms[t][i];
313         return null;
314 }
315
316 ;