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