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