]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/admin/adminlib.js
new and improved classic. Pull lists and hold lists should probably include filters...
[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] != null ) ? orgs[i] : -1 ;
57         return orgs;
58 }
59
60 /* offset is the depth of the highest org 
61         in the tree we're building 
62   */
63
64 /* XXX Moved to opac_utils.js */
65
66 /*
67 function buildOrgSel(selector, org, offset) { 
68         insertSelectorVal( selector, -1, 
69                 org.name(), org.id(), null, findOrgDepth(org) - offset );
70         for( var c in org.children() )
71                 buildOrgSel( selector, org.children()[c], offset);
72 }
73 */
74
75 /** removes all child nodes in 'tbody' that have the attribute 'key' defined */
76 function cleanTbody(tbody, key) {
77         for( var c  = 0; c < tbody.childNodes.length; c++ ) {
78                 var child = tbody.childNodes[c];
79                 if(child && child.getAttribute(key)) tbody.removeChild(child); 
80         }
81 }
82
83
84 /** Inserts a row into a specified place in a table
85   * tbody is the table body
86   * row is the context row after which the new row is to be inserted
87   * newRow is the new row to insert
88   */
89 function insRow( tbody, row, newRow ) {
90         if(row.nextSibling) tbody.insertBefore( newRow, row.nextSibling );
91         else{ tbody.appendChild(newRow); }
92 }
93
94
95 /** Checks to see if a given node should be enabled
96   * A node should be enabled if the itemOrg is lower in the
97   * org tree than my permissions allow editing
98   * I.e. I can edit the context item because it's "below" me
99   */
100 function checkDisabled( node, itemOrg, perm ) {
101         var itemDepth = findOrgDepth(itemOrg);
102         var mydepth = findOrgDepth(PERMS[perm]);
103         if( mydepth != -1 && mydepth <= itemDepth ) node.disabled = false;
104 }
105
106
107 function fetchOrgUnit(id, callback) {
108
109         if(ORG_CACHE[id]) return ORG_CACHE[id];
110         var req = new Request(FETCH_ORG_UNIT, SESSION, id);     
111
112         if(callback) {
113                 req.callback(
114                         function(r) { 
115                                 var org = r.getResultObject();
116                                 ORG_CACHE[id] = org;
117                                 callback(org); 
118                         }
119                 );
120                 req.send();
121
122         } else {
123                 req.send(true);
124                 var org = req.result();
125                 ORG_CACHE[id] = org;
126                 return org;
127         }
128 }