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