]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/admin/adminlib.js
c4d166afe54c41e65069679eb9f24689c9caffed
[Evergreen.git] / Open-ILS / xul / staff_client / server / admin / adminlib.js
1 var USER;
2 var SESSION;
3 var PERMS = {};
4 var ORG_CACHE = {};
5
6 var XML_ELEMENT_NODE = 1;
7 var XML_TEXT_NODE = 3;
8
9 var FETCH_ORG_UNIT = "open-ils.actor:open-ils.actor.org_unit.retrieve";
10
11 function debug(str) { try { dump(str + '\n'); } catch(e){} }
12
13 function fetchUser(session) {
14         if(session == null ) {
15                 cgi = new CGI();
16                 session = cgi.param('ses');
17         }
18         if(!session) throw "User session is not defined";
19         SESSION = session;
20         var request = new Request(FETCH_SESSION, session, 1);
21         request.send(true);
22         var user = request.result();
23         if(checkILSEvent(user)) throw user;
24         USER = user;
25         return user;
26 }
27
28 /* if defined, callback will get the user object asynchronously */
29 function fetchFleshedUser(id, callback) {
30         if(id == null) return null;
31         var req = new Request(
32                 'open-ils.actor:open-ils.actor.user.fleshed.retrieve', SESSION, id );
33
34         if( callback ) {
35                 req.callback( function(r){callback(r.getResultObject());} );
36                 req.send();
37
38         } else {
39                 req.send(true);
40                 return req.result();
41         }
42 }
43
44 /**
45   * Fetches the highest org at for each perm  and stores the value in
46   * PERMS[ permName ].  It also returns the org list to the caller
47   */
48 function fetchHighestPermOrgs( session, userId, perms ) {
49         var req = new RemoteRequest(
50                 'open-ils.actor',
51                 'open-ils.actor.user.perm.highest_org.batch', 
52                 session, userId, perms  );
53         req.send(true);
54         var orgs = req.getResultObject();
55         for( var i = 0; i != orgs.length; i++ ) 
56                 PERMS[perms[i]] = orgs[i];
57                 //PERMS[ perms[i] ] = ( orgs[i] != null ) ? orgs[i] : -1 ;
58         return orgs;
59 }
60
61 /* offset is the depth of the highest org 
62         in the tree we're building 
63   */
64
65 /* XXX Moved to opac_utils.js */
66
67 /*
68 function buildOrgSel(selector, org, offset) { 
69         insertSelectorVal( selector, -1, 
70                 org.name(), org.id(), null, findOrgDepth(org) - offset );
71         for( var c in org.children() )
72                 buildOrgSel( selector, org.children()[c], offset);
73 }
74 */
75
76 /** removes all child nodes in 'tbody' that have the attribute 'key' defined */
77 function cleanTbody(tbody, key) {
78         for( var c  = 0; c < tbody.childNodes.length; c++ ) {
79                 var child = tbody.childNodes[c];
80                 if(child && child.getAttribute(key)) tbody.removeChild(child); 
81         }
82 }
83
84
85 /** Inserts a row into a specified place in a table
86   * tbody is the table body
87   * row is the context row after which the new row is to be inserted
88   * newRow is the new row to insert
89   */
90 function insRow( tbody, row, newRow ) {
91         if(row.nextSibling) tbody.insertBefore( newRow, row.nextSibling );
92         else{ tbody.appendChild(newRow); }
93 }
94
95
96 /** Checks to see if a given node should be enabled
97   * A node should be enabled if the itemOrg is lower in the
98   * org tree than my permissions allow editing
99   * I.e. I can edit the context item because it's "below" me
100   */
101 function checkDisabled( node, itemOrg, perm ) {
102         var itemDepth = findOrgDepth(itemOrg);
103         var mydepth = findOrgDepth(PERMS[perm]);
104         if( mydepth != -1 && mydepth <= itemDepth ) node.disabled = false;
105 }
106
107
108 function fetchOrgUnit(id, callback) {
109
110         if(ORG_CACHE[id]) return ORG_CACHE[id];
111         var req = new Request(FETCH_ORG_UNIT, SESSION, id);     
112
113         if(callback) {
114                 req.callback(
115                         function(r) { 
116                                 var org = r.getResultObject();
117                                 ORG_CACHE[id] = org;
118                                 callback(org); 
119                         }
120                 );
121                 req.send();
122
123         } else {
124                 req.send(true);
125                 var org = req.result();
126                 ORG_CACHE[id] = org;
127                 return org;
128         }
129 }