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