1 dojo.requireLocalization("openils.reports", "reports");
2 dojo.require('openils.XUL');
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";
13 var rpt_rel_cache = {};
15 function sortHashLabels (a,b) { return a.label.toLowerCase() < b.label.toLowerCase() ? -1 : 1; }
17 function sortNames (a,b) {
18 var aname = a.name.toLowerCase();
19 if (!aname) aname = a.idlclass.toLowerCase();
21 var bname = b.name.toLowerCase();
22 if (!bname) bname = b.idlclass.toLowerCase();
24 return aname < bname ? -1 : 1;
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');
32 var bname = b.getAttributeNS(rptNS, 'label').toLowerCase();
33 if (!bname) bname = b.getAttribute('name');
34 if (!bname) bname = b.getAttribute('id');
36 return aname < bname ? -1 : 1;
40 function loadTemplate(id) {
42 var session = cgi.param('ses');
43 if(!session && openils.XUL.isXUL()) {
44 var stash = openils.XUL.getStash();
45 session = stash.session.key;
47 var r = new Request('open-ils.reporter:open-ils.reporter.template.retrieve', session, id);
51 var tmpl = res.getResultObject();
52 var template = JSON2js( tmpl.data() );
54 resetUI( template.core_class );
56 $('template-name').value = tmpl.name() + ' (clone)';
57 $('template-description').value = tmpl.description();
59 rpt_rel_cache = template.rel_cache;
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;
75 filterByAttributeNS( oilsIDL.getElementsByTagName('class'), rptNS, 'core', 'true' )
79 var template_id = cgi.param('ct');
80 if (template_id) loadTemplate(template_id);
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] }
91 function resetUI (idlclass) {
92 if (getKeys(rpt_rel_cache).length > 0) {
93 if (!confirm( rpt_strings.SOURCE_SETUP_CONFIRM_EXIT)) return false;
97 try { renderSources(); } catch (e) {}
99 populateSourcesTree( idlclass );
101 var tree = $('sources-treetop').parentNode;
103 tree.view.selection.select(0);
107 function populateSourcesMenu (classList) {
108 classList.sort( sortLabels );
110 var menu = $('source-menu');
114 { label : rpt_strings.SOURCE_SETUP_CORE_SOURCES,
116 style : 'color: black; text-decoration: underline;'
122 for (var i in classList) {
124 var name = classList[i].getAttributeNS(rptNS,'label');
125 var id = classList[i].getAttribute('id');
126 if (!name) name = id;
130 { container : 'true',
133 onmouseup : 'resetUI( "' + id + '");'
140 menu.appendChild( createMenuSeparator() );
143 { label : rpt_strings.SOURCE_SETUP_ALL_AVAIL_SOURCES },
147 { label : rpt_strings.SOURCE_SETUP_ALL_AVAIL_SOURCES,
149 style : 'color: black; '
152 createMenuSeparator()
156 menu.appendChild( _m );
157 menu = _m.firstChild;
159 var all = map(function(x){return x;}, oilsIDL.getElementsByTagNameNS(idlNS,'class'));
160 all.sort( sortLabels );
162 for (var i = 0; i < all.length; i++) {
164 if (all[i].getAttributeNS(persistNS,'virtual') == 'true') continue;
166 var name = all[i].getAttributeNS(rptNS,'label');
167 var id = all[i].getAttribute('id');
168 if (!name) name = id;
172 { container : 'true',
175 onmouseup : 'resetUI( "' + id + '");'
185 function populateSourcesTree (idlclass) {
187 var tcNode = $('sources-treetop');
188 while (tcNode.childNodes.length) tcNode.removeChild(tcNode.lastChild);
190 var c = getIDLClass( idlclass );
191 var name = c.getAttributeNS(rptNS,'label');
192 if (!name) name = idlclass;
196 { container : 'true',
202 createTreeCell( { label : '' } ),
203 createTreeCell( { label : name } )
205 createTreeChildren( { alternatingbackground : true } )
210 function populateSourcesSubtree (tcNode, classList) {
211 classList.sort(sortNames);
212 for (var i in classList) {
213 var obj = classList[i];
215 var p = getIDLClass( obj.idlclass );
216 var cont = p.getElementsByTagName('link').length ? 'true' : 'false';
221 idlclass : obj.idlclass,
227 reltype : obj.reltype,
228 fullpath : obj.fullpath
232 createTreeCell( { label : obj.nullable } ),
233 createTreeCell( { label : obj.name } )
235 createTreeChildren( { alternatingbackground : true } )
243 function populateDetailTree (tcNode, c, item) {
244 var fullpath = item.getAttribute('fullpath');
245 var reltype = item.getAttribute('reltype');
247 var fields = nodelistToArray(c.getElementsByTagName('field'));
248 fields.sort( sortLabels );
250 var id = c.getAttribute('id');
253 var steps = fullpath.split('.');
254 for (var k in steps) {
256 if (!steps[k]) continue;
258 var atom = steps[k].split('-');
259 var classNode = getIDLClass(atom[0]);
261 var _cname = classNode.getAttributeNS(rptNS, 'label');
262 if (!_cname) _cname = classNode.getAttribute('id');
266 if (atom.length > 1 && k == steps.length - 1) {
267 var field_name = atom[1];
268 var join_type = field_name;
270 field_name = field_name.split(/>/)[0];
271 join_type = join_type.split(/>/)[1];
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 + ')';
279 } else if (atom[1]) {
280 var field_name = atom[1];
281 var join_type = field_name;
285 _label += ' (' + rpt_strings.LINK_NULLABLE_RIGHT + ')';
288 _label += ' (' + rpt_strings.LINK_NULLABLE_LEFT + ')';
291 _label += ' (' + rpt_strings.LINK_NULLABLE_NONE + ')';
296 path_label.push(_label);
299 $('path-label').value = path_label.join(' -> ');
300 $('path-label').setAttribute('reltype',reltype);
302 for (var i in fields) {
304 var type = fields[i].getAttributeNS(rptNS, 'datatype');
305 //if (!type) type = 'text';
307 var label = fields[i].getAttributeNS(rptNS, 'label');
308 var name = fields[i].getAttribute('name');
309 if (!label) label = name;
320 createTreeCell( { label : label } ),
321 createTreeCell( { label : type } )