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