]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/admin/adminlib.js
LP2045292 Color contrast for AngularJS patron bills
[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 var OILS_WORK_PERMS = {};
6
7 var XML_ELEMENT_NODE = 1;
8 var XML_TEXT_NODE = 3;
9
10 var FETCH_ORG_UNIT = "open-ils.actor:open-ils.actor.org_unit.retrieve";
11
12 function debug(str) { try { dump(str + '\n'); } catch(e){} }
13
14 function fetchUser(session) {
15     if(session == null ) {
16         cgi = new CGI();
17         session = cgi.param('ses');
18         if(!session && (location.protocol == 'chrome:' || location.protocol == 'oils:')) {
19             try {
20                 var CacheClass = Components.classes["@open-ils.org/openils_data_cache;1"].getService();
21                 session = CacheClass.wrappedJSObject.data.session.key;
22             } catch(e) {
23                 console.log("Error loading XUL stash: " + e);
24             }
25         }
26     }
27     if(!session) throw "User session is not defined";
28     SESSION = session;
29     var request = new Request(FETCH_SESSION, session);
30     request.send(true);
31     var user = request.result();
32     if(checkILSEvent(user)) throw user;
33     USER = user;
34     return user;
35 }
36
37 /* if defined, callback will get the user object asynchronously */
38 function fetchFleshedUser(id, callback) {
39     if(id == null) return null;
40     var req = new Request(
41         'open-ils.actor:open-ils.actor.user.fleshed.retrieve', SESSION, id );
42
43     if( callback ) {
44         req.callback( function(r){callback(r.getResultObject());} );
45         req.send();
46
47     } else {
48         req.send(true);
49         return req.result();
50     }
51 }
52
53 /**
54   * Fetches the highest org at for each perm  and stores the value in
55   * PERMS[ permName ].  It also returns the org list to the caller
56   */
57 function fetchHighestPermOrgs( session, userId, perms ) {
58     var req = new RemoteRequest(
59         'open-ils.actor',
60         'open-ils.actor.user.perm.highest_org.batch', 
61         session, userId, perms  );
62     req.send(true);
63     var orgs = req.getResultObject();
64     for( var i = 0; i != orgs.length; i++ ) 
65         PERMS[perms[i]] = orgs[i];
66         //PERMS[ perms[i] ] = ( orgs[i] != null ) ? orgs[i] : -1 ;
67     return orgs;
68 }
69
70 function fetchHighestWorkPermOrgs(session, userId, perms, onload) {
71     var req = new RemoteRequest(
72         'open-ils.actor',
73         'open-ils.actor.user.has_work_perm_at.batch',
74         session, perms);
75     if(onload) {
76         req.setCompleteCallback(function(r){
77             onload(OILS_WORK_PERMS = r.getResultObject());
78         });
79         req.send()
80     } else {
81         req.send(true);
82         return OILS_WORK_PERMS = req.getResultObject();
83     }
84 }
85
86 /*
87  takes org IDs 
88  Finds the lowest relevent org unit between a context org unit and a set of
89  permission orgs.  This defines the sphere of influence for a given action
90  on a specific set of data.  if the context org shares no common nodes with
91  the set of permission orgs, null is returned.
92  returns the orgUnit object
93  */
94 function findReleventRootOrg(permOrgList, contextOrgId) {
95     var contextOrgNode = findOrgUnit(contextOrgId);
96     for(var i = 0; i < permOrgList.length; i++) {
97         var permOrg = findOrgUnit(permOrgList[i]);
98         if(orgIsMine(permOrg, contextOrgNode)) {
99             // perm org is equal to or a parent of the context org, so the context org is the highest
100             return contextOrgNode;
101         } else if(orgIsMine(contextOrgNode, permOrg)) {
102             // perm org is a child if the context org, so permOrg is the highest org
103             return permOrg;
104         }
105     }
106     return null;
107 }
108
109
110 /* offset is the depth of the highest org 
111     in the tree we're building 
112   */
113
114 /* XXX Moved to opac_utils.js */
115
116 /*
117 function buildOrgSel(selector, org, offset) { 
118     insertSelectorVal( selector, -1, 
119         org.name(), org.id(), null, findOrgDepth(org) - offset );
120     for( var c in org.children() )
121         buildOrgSel( selector, org.children()[c], offset);
122 }
123 */
124
125 /** removes all child nodes in 'tbody' that have the attribute 'key' defined */
126 function cleanTbody(tbody, key) {
127     for( var c  = 0; c < tbody.childNodes.length; c++ ) {
128         var child = tbody.childNodes[c];
129         if(child && child.getAttribute(key)) tbody.removeChild(child); 
130     }
131 }
132
133
134 /** Inserts a row into a specified place in a table
135   * tbody is the table body
136   * row is the context row after which the new row is to be inserted
137   * newRow is the new row to insert
138   */
139 function insRow( tbody, row, newRow ) {
140     if(row.nextSibling) tbody.insertBefore( newRow, row.nextSibling );
141     else{ tbody.appendChild(newRow); }
142 }
143
144
145 /** Checks to see if a given node should be enabled
146   * A node should be enabled if the itemOrg is lower in the
147   * org tree than my permissions allow editing
148   * I.e. I can edit the context item because it's "below" me
149   */
150 function checkDisabled( node, itemOrg, perm ) {
151     var itemDepth = findOrgDepth(itemOrg);
152     var mydepth = findOrgDepth(PERMS[perm]);
153     if( mydepth != -1 && mydepth <= itemDepth ) node.disabled = false;
154 }
155
156 /**
157   * If the item-related org unit (owner, etc.) is one of or
158   * or a child of any of the perm-orgs related to the
159   * provided permission, enable the requested node
160   */
161 function checkPermOrgDisabled(node, itemOrg, perm) {
162     var org_list = OILS_WORK_PERMS[perm];
163     if(org_list.length > 0) {
164         for(var i = 0; i < org_list.length; i++) {
165             var highPermOrg = findOrgUnit(org_list[i]);
166             if(orgIsMine(highPermOrg, findOrgUnit(itemOrg))) 
167                 node.disabled = false;
168         }
169     }
170 }
171
172
173 function fetchOrgUnit(id, callback) {
174
175     if(ORG_CACHE[id]) return ORG_CACHE[id];
176     var req = new Request(FETCH_ORG_UNIT, SESSION, id);    
177
178     if(callback) {
179         req.callback(
180             function(r) { 
181                 var org = r.getResultObject();
182                 ORG_CACHE[id] = org;
183                 callback(org); 
184             }
185         );
186         req.send();
187
188     } else {
189         req.send(true);
190         var org = req.result();
191         ORG_CACHE[id] = org;
192         return org;
193     }
194 }