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