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');
31 aname = aname.toLowerCase();
33 aname = a.getAttribute('name') || a.getAttribute('id');
36 var bname = b.getAttributeNS(rptNS, 'label');
39 bname = bname.toLowerCase();
41 bname = b.getAttribute('name') || b.getAttribute('id');
44 return aname < bname ? -1 : 1;
48 function loadTemplate(id) {
50 var session = cgi.param('ses');
51 if(!session && openils.XUL.isXUL()) {
52 var stash = openils.XUL.getStash();
53 session = stash.session.key;
55 var r = new Request('open-ils.reporter:open-ils.reporter.template.retrieve', session, id);
59 var tmpl = res.getResultObject();
60 var template = JSON2js( tmpl.data() );
62 resetUI( template.core_class );
64 $('template-name').value = tmpl.name() + ' (clone)';
65 $('template-description').value = tmpl.description();
67 rpt_rel_cache = template.rel_cache;
77 var req = new XMLHttpRequest();
78 req.open('GET', '../fm_IDL.xml', true);
79 req.onreadystatechange = function() {
80 if( req.readyState == 4 ) {
81 oilsIDL = req.responseXML;
83 filterByAttributeNS( oilsIDL.getElementsByTagName('class'), rptNS, 'core', 'true' )
87 var template_id = cgi.param('ct');
88 if (template_id) loadTemplate(template_id);
95 function getIDLClass (id) { return filterByAttribute( oilsIDL.getElementsByTagName('class'), 'id', id )[0] }
96 function getIDLField (classNode,field) { return filterByAttribute( classNode.getElementsByTagName('field'), 'name', field )[0] }
97 function getIDLLink (classNode,field) { return filterByAttribute( classNode.getElementsByTagName('link'), 'field', field )[0] }
99 function resetUI (idlclass) {
100 if (getKeys(rpt_rel_cache).length > 0) {
101 if (!confirm( rpt_strings.SOURCE_SETUP_CONFIRM_EXIT)) return false;
105 try { renderSources(); } catch (e) {}
107 populateSourcesTree( idlclass );
109 var tree = $('sources-treetop').parentNode;
111 tree.view.selection.select(0);
115 function populateSourcesMenu (classList) {
116 classList.sort( sortLabels );
118 var menu = $('source-menu');
122 { label : rpt_strings.SOURCE_SETUP_CORE_SOURCES,
124 style : 'color: black; text-decoration: underline;'
130 for (var i in classList) {
132 var name = classList[i].getAttributeNS(rptNS,'label');
133 var id = classList[i].getAttribute('id');
134 if (!name) name = id;
138 { container : 'true',
141 onmouseup : 'resetUI( "' + id + '");'
148 menu.appendChild( createMenuSeparator() );
151 { label : rpt_strings.SOURCE_SETUP_ALL_AVAIL_SOURCES },
155 { label : rpt_strings.SOURCE_SETUP_ALL_AVAIL_SOURCES,
157 style : 'color: black; '
160 createMenuSeparator()
164 menu.appendChild( _m );
165 menu = _m.firstChild;
167 var all = map(function(x){return x;}, oilsIDL.getElementsByTagNameNS(idlNS,'class'));
168 all.sort( sortLabels );
170 for (var i = 0; i < all.length; i++) {
172 if (all[i].getAttributeNS(persistNS,'virtual') == 'true') continue;
174 var name = all[i].getAttributeNS(rptNS,'label');
175 var id = all[i].getAttribute('id');
176 if (!name) name = id;
180 { container : 'true',
183 onmouseup : 'resetUI( "' + id + '");'
193 function populateSourcesTree (idlclass) {
195 var tcNode = $('sources-treetop');
196 while (tcNode.childNodes.length) tcNode.removeChild(tcNode.lastChild);
198 var c = getIDLClass( idlclass );
199 var name = c.getAttributeNS(rptNS,'label');
200 if (!name) name = idlclass;
204 { container : 'true',
210 createTreeCell( { label : '' } ),
211 createTreeCell( { label : name } )
213 createTreeChildren( { alternatingbackground : true } )
218 function populateSourcesSubtree (tcNode, classList) {
219 classList.sort(sortNames);
220 for (var i in classList) {
221 var obj = classList[i];
223 var p = getIDLClass( obj.idlclass );
224 var cont = p.getElementsByTagName('link').length ? 'true' : 'false';
229 idlclass : obj.idlclass,
235 reltype : obj.reltype,
236 fullpath : obj.fullpath
240 createTreeCell( { label : obj.nullable } ),
241 createTreeCell( { label : obj.name } )
243 createTreeChildren( { alternatingbackground : true } )
251 function populateDetailTree (tcNode, c, item) {
252 var fullpath = item.getAttribute('fullpath');
253 var reltype = item.getAttribute('reltype');
255 var fields = nodelistToArray(c.getElementsByTagName('field'));
256 fields.sort( sortLabels );
258 var id = c.getAttribute('id');
261 var steps = fullpath.split('.');
262 for (var k in steps) {
264 if (!steps[k]) continue;
266 var atom = steps[k].split('-');
267 var classNode = getIDLClass(atom[0]);
269 var _cname = classNode.getAttributeNS(rptNS, 'label');
270 if (!_cname) _cname = classNode.getAttribute('id');
274 if (atom.length > 1 && k == steps.length - 1) {
275 var field_name = atom[1];
276 var join_type = field_name;
278 field_name = field_name.split(/>/)[0];
279 join_type = join_type.split(/>/)[1];
281 var _f = getIDLField(classNode, field_name);
282 var _fname = _f.getAttributeNS(rptNS, 'label');
283 if (!_fname) _fname = _f.getAttribute('name');
284 if (_fname) _label += ' :: ' + _fname;
285 if (join_type) _label += ' (' + join_type + ')';
287 } else if (atom[1]) {
288 var field_name = atom[1];
289 var join_type = field_name;
293 _label += ' (' + rpt_strings.LINK_NULLABLE_RIGHT + ')';
296 _label += ' (' + rpt_strings.LINK_NULLABLE_LEFT + ')';
299 _label += ' (' + rpt_strings.LINK_NULLABLE_NONE + ')';
304 path_label.push(_label);
307 $('path-label').value = path_label.join(' -> ');
308 $('path-label').setAttribute('reltype',reltype);
310 for (var i in fields) {
312 var type = fields[i].getAttributeNS(rptNS, 'datatype');
313 //if (!type) type = 'text';
315 var suppress = fields[i].getAttribute('suppress_controller');
316 if (suppress && suppress.indexOf('open-ils.reporter-store') > -1) continue;
318 var label = fields[i].getAttributeNS(rptNS, 'label');
319 var name = fields[i].getAttribute('name');
320 if (!label) label = name;
331 createTreeCell( { label : label } ),
332 createTreeCell( { label : type } )