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