]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/reports/xul/source-setup.js
Improve Firefox/XULRunner Support
[Evergreen.git] / Open-ILS / web / reports / xul / source-setup.js
1 dojo.requireLocalization("openils.reports", "reports");
2 dojo.require('openils.XUL');
3
4 var rpt_strings = dojo.i18n.getLocalization("openils.reports", "reports");
5 var idlNS       = "http://opensrf.org/spec/IDL/base/v1";
6 var persistNS   = "http://open-ils.org/spec/opensrf/IDL/persistence/v1";
7 var objNS       = "http://open-ils.org/spec/opensrf/IDL/objects/v1";
8 var rptNS       = "http://open-ils.org/spec/opensrf/IDL/reporter/v1";
9 var gwNS        = "http://opensrf.org/-/namespaces/gateway/v1";
10 var xulNS       = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
11
12 var oilsIDL;
13 var rpt_rel_cache = {};
14
15 function sortHashLabels (a,b) { return a.label.toLowerCase() < b.label.toLowerCase() ? -1 : 1; }
16
17 function sortNames (a,b) {
18         var aname =  a.name.toLowerCase();
19         if (!aname) aname = a.idlclass.toLowerCase();
20
21         var bname =  b.name.toLowerCase();
22         if (!bname) bname = b.idlclass.toLowerCase();
23
24         return aname < bname ? -1 : 1;
25 }
26
27 function sortLabels (a,b) {
28         var aname =  a.getAttributeNS(rptNS, 'label').toLowerCase();
29         if (!aname) aname = a.getAttribute('name');
30         if (!aname) aname = a.getAttribute('id');
31
32         var bname =  b.getAttributeNS(rptNS, 'label').toLowerCase();
33         if (!bname) bname = b.getAttribute('name');
34         if (!bname) bname = b.getAttribute('id');
35
36         return aname < bname ? -1 : 1;
37 }
38
39
40 function loadTemplate(id) {
41         var cgi = new CGI();
42         var session = cgi.param('ses');
43     if(!session && openils.XUL.isXUL()) {
44         var stash = openils.XUL.getStash();
45         session = stash.session.key;
46     }
47         var r = new Request('open-ils.reporter:open-ils.reporter.template.retrieve', session, id);
48
49         r.callback(
50                 function(res) {
51                         var tmpl = res.getResultObject();
52                         var template = JSON2js( tmpl.data() );
53
54                         resetUI( template.core_class );
55
56                         $('template-name').value = tmpl.name() + ' (clone)';
57                         $('template-description').value = tmpl.description();
58
59                         rpt_rel_cache = template.rel_cache;
60                         renderSources();
61                 }
62         );
63
64         r.send();
65 }
66
67
68 function loadIDL() {
69         var req = new XMLHttpRequest();
70         req.open('GET', '../fm_IDL.xml', true);
71         req.onreadystatechange = function() {
72                 if( req.readyState == 4 ) {
73                         oilsIDL = req.responseXML;
74                         populateSourcesMenu(
75                                 filterByAttributeNS( oilsIDL.getElementsByTagName('class'), rptNS, 'core', 'true' )
76                         );
77
78                         var cgi = new CGI();
79                         var template_id = cgi.param('ct');
80                         if (template_id) loadTemplate(template_id);
81                 }
82         }
83         req.send(null);
84
85 }
86
87 function getIDLClass (id) { return filterByAttribute( oilsIDL.getElementsByTagName('class'), 'id', id )[0] }
88 function getIDLField (classNode,field) { return filterByAttribute( classNode.getElementsByTagName('field'), 'name', field )[0] }
89 function getIDLLink (classNode,field) { return filterByAttribute( classNode.getElementsByTagName('link'), 'field', field )[0] }
90
91 function resetUI (idlclass) {
92         if (getKeys(rpt_rel_cache).length > 0) {
93                 if (!confirm( rpt_strings.SOURCE_SETUP_CONFIRM_EXIT)) return false;
94         }
95
96         rpt_rel_cache = {};
97         try { renderSources(); } catch (e) {}
98
99         populateSourcesTree( idlclass );
100
101         var tree = $('sources-treetop').parentNode;
102         tree.focus();
103         tree.view.selection.select(0);
104         tree.click();
105 }
106
107 function populateSourcesMenu (classList) {
108         classList.sort( sortLabels );
109
110         var menu = $('source-menu');
111
112         menu.appendChild(
113                 createMenuItem(
114                         { label : rpt_strings.SOURCE_SETUP_CORE_SOURCES,
115                           disabled : 'true',
116                           style : 'color: black; text-decoration: underline;'
117                         }
118                 )
119         );
120
121
122         for (var i in classList) {
123
124                 var name = classList[i].getAttributeNS(rptNS,'label');
125                 var id = classList[i].getAttribute('id');
126                 if (!name) name = id;
127
128                 menu.appendChild(
129                         createMenuItem(
130                                 { container : 'true',
131                                   idlclass : id,
132                                   label : name,
133                                   onmouseup : 'resetUI( "' + id + '");'
134                                 }
135                         )
136                 );
137
138         }
139
140         menu.appendChild( createMenuSeparator() );
141
142         var _m = createMenu(
143                 { label : rpt_strings.SOURCE_SETUP_ALL_AVAIL_SOURCES },
144                 createMenuPopup(
145                         {},
146                         createMenuItem(
147                                 { label : rpt_strings.SOURCE_SETUP_ALL_AVAIL_SOURCES,
148                                   disabled : 'true',
149                                   style : 'color: black; '
150                                 }
151                         ),
152                         createMenuSeparator()
153                 )
154         );
155
156         menu.appendChild( _m );
157         menu = _m.firstChild;
158
159         var all = map(function(x){return x;}, oilsIDL.getElementsByTagNameNS(idlNS,'class'));
160         all.sort( sortLabels );
161
162         for (var i = 0; i < all.length; i++) {
163
164                 if (all[i].getAttributeNS(persistNS,'virtual') == 'true') continue;
165
166                 var name = all[i].getAttributeNS(rptNS,'label');
167                 var id = all[i].getAttribute('id');
168                 if (!name) name = id;
169
170                 menu.appendChild(
171                         createMenuItem(
172                                 { container : 'true',
173                                   idlclass : id,
174                                   label : name,
175                                   onmouseup : 'resetUI( "' + id + '");'
176                                 }
177                         )
178                 );
179
180         }
181
182
183 }
184
185 function populateSourcesTree (idlclass) {
186
187         var tcNode = $('sources-treetop');
188         while (tcNode.childNodes.length) tcNode.removeChild(tcNode.lastChild);
189
190         var c = getIDLClass( idlclass );
191         var name = c.getAttributeNS(rptNS,'label');
192         if (!name) name = idlclass;
193
194         tcNode.appendChild(
195                 createTreeItem(
196                         { container : 'true',
197                           idlclass : idlclass,
198                           fullpath : idlclass
199                         },
200                         createTreeRow(
201                                 { },
202                                 createTreeCell( { label : '' } ),
203                                 createTreeCell( { label : name } )
204                         ),
205                         createTreeChildren( { alternatingbackground : true } )
206                 )
207         );
208 }
209
210 function populateSourcesSubtree (tcNode, classList) {
211         classList.sort(sortNames);
212         for (var i in classList) {
213                 var obj = classList[i];
214
215                 var p = getIDLClass( obj.idlclass );
216                 var cont = p.getElementsByTagName('link').length ? 'true' : 'false';
217
218                 tcNode.appendChild(
219                         createTreeItem(
220                                 { container : cont,
221                                   idlclass : obj.idlclass,
222                                   map : obj.map,
223                                   key : obj.key,
224                                   field : obj.field,
225                                   link : obj.link,
226                                   join : obj['join'],
227                                   reltype : obj.reltype,
228                                   fullpath : obj.fullpath
229                                 },
230                                 createTreeRow(
231                                         { },
232                                         createTreeCell( { label : obj.nullable } ),
233                                         createTreeCell( { label : obj.name } )
234                                 ),
235                                 createTreeChildren( { alternatingbackground : true } )
236                         )
237                 );
238
239
240         }
241 }
242
243 function populateDetailTree (tcNode, c, item) {
244         var fullpath = item.getAttribute('fullpath');
245         var reltype = item.getAttribute('reltype');
246
247         var fields = nodelistToArray(c.getElementsByTagName('field'));
248         fields.sort( sortLabels );
249
250         var id = c.getAttribute('id');
251         var path_label = [];
252
253         var steps = fullpath.split('.');
254         for (var k in steps) {
255
256                 if (!steps[k]) continue;
257
258                 var atom = steps[k].split('-');
259                 var classNode = getIDLClass(atom[0]);
260
261                 var _cname = classNode.getAttributeNS(rptNS, 'label');
262                 if (!_cname) _cname = classNode.getAttribute('id');
263
264                 var _label = _cname; 
265
266                 if (atom.length > 1 && k == steps.length - 1) {
267                         var field_name = atom[1];
268                         var join_type = field_name;
269
270                         field_name = field_name.split(/>/)[0];
271                         join_type = join_type.split(/>/)[1];
272
273                         var _f = getIDLField(classNode, field_name);
274                         var _fname = _f.getAttributeNS(rptNS, 'label');
275                         if (!_fname) _fname = _f.getAttribute('name');
276                         if (_fname) _label += ' :: ' + _fname; 
277                         if (join_type) _label += ' (' + join_type + ')'; 
278
279                 } else if (atom[1]) {
280                         var field_name = atom[1];
281                         var join_type = field_name;
282
283                         switch (join_type) {
284                                 case 'right':
285                                         _label += ' (' + rpt_strings.LINK_NULLABLE_RIGHT + ')'; 
286                                         break;
287                                 case 'left':
288                                         _label += ' (' + rpt_strings.LINK_NULLABLE_LEFT + ')'; 
289                                         break;
290                                 case 'inner':
291                                         _label += ' (' + rpt_strings.LINK_NULLABLE_NONE + ')'; 
292                                         break;
293                         }
294                 }
295
296                 path_label.push(_label); 
297         }
298
299         $('path-label').value = path_label.join(' -> ');
300         $('path-label').setAttribute('reltype',reltype);
301
302         for (var i in fields) {
303
304                 var type = fields[i].getAttributeNS(rptNS, 'datatype');
305                 //if (!type) type = 'text';
306
307                 var label = fields[i].getAttributeNS(rptNS, 'label');
308                 var name = fields[i].getAttribute('name');
309                 if (!label) label = name;
310
311                 tcNode.appendChild(
312                         createTreeItem(
313                                 { idlclass : id,
314                                   idlfield : name,
315                                   datatype : type,
316                                   fullpath : fullpath
317                                 },
318                                 createTreeRow(
319                                         { },
320                                         createTreeCell( { label : label } ),
321                                         createTreeCell( { label : type } )
322                                 )
323                         )
324                 );
325         }
326 }
327
328