]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/util/ils_utils.js
our little opac is growing up
[Evergreen.git] / Open-ILS / src / javascript / util / ils_utils.js
1 /* */
2
3
4 /* these are the types of resource provided my MODS - used in virtual records */
5 var resourceFormats = [ 
6         "text", 
7         "moving image",
8         "sound recording",
9         "software, multimedia",
10         "still images",
11         "cartographic",
12         "mixed material",
13         "notated music",
14         "three dimensional object" ];
15
16
17
18 function findOrgDepth(type_id) {
19
20         if(type_id == null || globalOrgTypes == null)
21                 return null;
22
23         var t = findOrgType(type_id);
24         if(t != null)
25                 return t.depth();
26
27         return null;
28 }
29
30 function findOrgType(type_id) {
31
32         if(type_id == null || globalOrgTypes == null)
33                 return null;
34
35         if(typeof type_id == 'object')
36                 return type_id;
37
38         for(var type in globalOrgTypes) {
39                 var t =globalOrgTypes[type]; 
40                 if( t.id() == type_id || t.id() == parseInt(type_id) ) 
41                         return t;
42         }
43         return null;
44 }
45
46
47 /* locates a specific org unit */
48 var orgArraySearcher = null;
49
50 /* flatten the org tree for faster searching */
51 function _flattenOrgs(node) { 
52
53         if(node == null) {
54                 node = globalOrgTree;
55                 orgArraySearcher = new Object();
56         }
57
58         orgArraySearcher[node.id()] = node;
59         for(var idx in node.children()) {
60                 _flattenOrgs(node.children()[idx]);
61         }
62 }
63
64 var singleOrgCache = new Object();
65 function findOrgUnit(org_id, branch) {
66
67         if(org_id == null) return null;
68         if(typeof org_id == 'object') return org_id;
69
70         /* if we don't have the global org tree, grab the org unit from the server */
71         var tree_exists = false;
72         try{if(globalOrgTree != null) tree_exists = true;}catch(E){}
73
74         if(!tree_exists) {
75                 var org = singleOrgCache[org_id];
76                 if(org) return org;
77                 var r = new RemoteRequest(
78                         "open-ils.actor",
79                         "open-ils.actor.org_unit.retrieve", null, org_id);
80                 r.send(true);
81                 return r.getResultObject();
82         }
83
84         if(orgArraySearcher == null)
85                 _flattenOrgs();
86
87         return orgArraySearcher[org_id];
88 }
89
90
91 function getOrgById(id, node) {
92         if(node == null) node = globalOrgTree;
93         if( node.id() == id) return node;
94         for( var ind in node.children() ) {
95                 var ret = getOrgById(id, node.children()[ind] );
96                 if( ret != null )
97                         return ret;
98         }
99         return null;
100 }
101
102
103
104 function orgNodeTrail(node) {
105         var nodeArray = new Array();
106         while( node ) {
107                 nodeArray.push(node);
108                 node = findOrgUnit(node.parent_ou());
109         }
110         nodeArray = nodeArray.reverse();
111         return nodeArray;
112 }
113
114 function findSiblingOrgs(node) {
115         return findOrgUnit(node.parent_ou()).children();
116 }
117
118
119 function grabCopyLocations() {
120
121         if(globalCopyLocations != null) return;
122         debug("Grabbing copy locations");
123
124         var req = new RemoteRequest(
125                 "open-ils.search",
126                 "open-ils.search.config.copy_location.retrieve.all" );
127
128         req.send(true);
129         globalCopyLocations = req.getResultObject();
130         return globalCopyLocations;
131
132 }
133
134 function findCopyLocation(id) {
135
136         grabCopyLocations();
137         if(typeof id == 'object') return id;
138
139         if(globalCopyLocations == null) 
140                 throw new EXLogic("globalCopyLocations is NULL");
141
142         for( var x = 0; x!= globalCopyLocations.length; x++) {
143                 if(globalCopyLocations[x].id() == id)
144                         return globalCopyLocations[x];
145         }
146         return null;
147 }
148
149
150 function modsFormatToMARC(format) {
151         switch(format) {
152                 case "text":
153                         return "at";
154                 case "moving image":
155                         return "g";
156                 case "sound recording":
157                         return "ij";
158                 case "software, multimedia":
159                         return "m";
160                 case "still images":
161                         return "k";
162                 case "cartographic":
163                         return "ef";
164                 case "mixed material":
165                         return "op";
166                 case "notated music":
167                         return "cd";
168                 case "three dimensional object":
169                         return "r";
170         }
171         throw new EXLogic("Invalid format provided form modsFormatToMARC: " + format);
172 }
173
174 function MARCFormatToMods(format) {
175         switch(format) {
176
177                 case "a":
178                 case "t":
179                         return "text";
180
181                 case "g":
182                         return "moving image";
183
184                 case "i":
185                 case "j":
186                         return "sound recording";
187
188                 case "m":
189                         return "software, multimedia";
190
191                 case "k":
192                         return "still images";
193
194                 case "e":
195                 case "f":
196                         return "cartographic";
197
198                 case "o":
199                 case "p":
200                         return "mixed material";
201
202                 case "c":
203                 case "d":
204                         return "notated music";
205
206                 case "r":
207                         return "three dimensional object";
208         }
209         throw new EXLogic("Invalid format provided for MARCFormatToMods: " + format);
210 }
211
212
213
214 /* if callback exists, call is asynchronous and 
215         the returned item is passed to the callback... */
216 function fetchRecord(id, callback) {
217
218         var req = new RemoteRequest(
219                 "open-ils.search",
220                 "open-ils.search.biblio.record.mods_slim.retrieve",
221                 id );
222
223         if(callback) {
224                 req.setCompleteCallback(
225                         function(req) {callback(req.getResultObject())});
226                 req.send();
227         } else {
228                 req.send(true);
229                 return req.getResultObject();
230         }
231 }
232
233 /* if callback exists, call is asynchronous and 
234         the returned item is passed to the callback... */
235 function fetchMetaRecord(id, callback) {
236         var req = new RemoteRequest(
237                 "open-ils.search",
238                 "open-ils.search.biblio.metarecord.mods_slim.retrieve",
239                 id );
240
241         if(callback) {
242                 req.setCompleteCallback(
243                         function(req) {callback(req.getResultObject())});
244                 req.send();
245         } else {
246                 req.send(true);
247                 return req.getResultObject();
248         }
249 }
250
251 /* if callback exists, call is asynchronous and 
252         the returned item is passed to the callback... */
253 /* XXX no method yet... */
254 function fetchVolume(id, callback) {
255         var req = new RemoteRequest(
256                 "open-ils.search",
257                 "open-ils.search.biblio.metarecord.mods_slim.retrieve",
258                 id );
259
260         if(callback) {
261                 req.setCompleteCallback(
262                         function(req) {callback(req.getResultObject())});
263                 req.send();
264         } else {
265                 req.send(true);
266                 return req.getResultObject();
267         }
268 }
269
270 /* if callback exists, call is asynchronous and 
271         the returned item is passed to the callback... */
272 function fetchCopy(id, callback) {
273         var req = new RemoteRequest(
274                 "open-ils.search",
275                 "open-ils.search.asset.copy.fleshed.retrieve",
276                 id );
277
278         if(callback) {
279                 req.setCompleteCallback(
280                         function(req) {callback(req.getResultObject())});
281                 req.send();
282         } else {
283                 req.send(true);
284                 return req.getResultObject();
285         }
286 }
287
288 function mkResourceImage(resource) {
289         var pic = elem("img");
290         pic.setAttribute("src", "/images/" + resource + ".jpg");
291         pic.setAttribute("width", "20");
292         pic.setAttribute("height", "20");
293         pic.setAttribute("title", resource);
294         return pic;
295 }
296
297
298
299 function doLogout() {
300
301         /* remove cookie so browser know's we're logged out */
302         deleteCookie("ils_ses");
303
304         var user = UserSession.instance();
305         if( user.session_id ) {
306                 var request = new RemoteRequest( "open-ils.auth",
307                         "open-ils.auth.session.delete", user.session_id );
308                 request.send(true);
309                 var response = request.getResultObject();
310                 if(! response ) {
311                         //alert("error logging out"); /* exception */
312                 }
313         }
314
315         /* completely destroy this user object */
316         user.destroy();
317 }
318
319
320 function cleanISBN(isbn) {
321         if(isbn) {
322                 isbn = isbn.replace(/^\s+/,"");
323                 var idx = isbn.indexOf(" ");
324                 if(idx > -1) {
325                         isbn = isbn.substring(0, idx);
326                 }
327
328         } else isbn = "";
329         return isbn
330
331 }
332
333
334 function grabUserByBarcode(barcode) {
335         if(!barcode) return null;
336         var req = new RemoteRequest(
337                 "open-ils.actor",
338                 "open-ils.actor.user.fleshed.retrieve_by_barcode",
339                 barcode );
340
341         return req.send(true).getResultObject();
342 }
343
344
345