]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/javascript/util/ils_utils.js
removing old opac images and css
[working/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 "sound recording-nonmusical":
159                         return "i";
160                 case "sound recording-musical":
161                         return "j";
162                 case "software, multimedia":
163                         return "m";
164                 case "still images":
165                         return "k";
166                 case "cartographic":
167                         return "ef";
168                 case "mixed material":
169                         return "op";
170                 case "notated music":
171                         return "cd";
172                 case "three dimensional object":
173                         return "r";
174         }
175
176         return "at";
177         //throw new EXLogic("Invalid format provided form modsFormatToMARC: " + format);
178 }
179
180 function MARCFormatToMods(format) {
181         switch(format) {
182
183                 case "a":
184                 case "t":
185                         return "text";
186
187                 case "g":
188                         return "moving image";
189
190                 case "i":
191                         return "sound recording-nonmusical";
192
193                 case "j":
194                         return "sound recording-musical";
195
196                 case "m":
197                         return "software, multimedia";
198
199                 case "k":
200                         return "still images";
201
202                 case "e":
203                 case "f":
204                         return "cartographic";
205
206                 case "o":
207                 case "p":
208                         return "mixed material";
209
210                 case "c":
211                 case "d":
212                         return "notated music";
213
214                 case "r":
215                         return "three dimensional object";
216         }
217
218         return "text";
219
220         //throw new EXLogic("Invalid format provided for MARCFormatToMods: " + format);
221 }
222
223
224
225 /* if callback exists, call is asynchronous and 
226         the returned item is passed to the callback... */
227 function fetchRecord(id, callback) {
228
229         var req = new RemoteRequest(
230                 "open-ils.search",
231                 "open-ils.search.biblio.record.mods_slim.retrieve",
232                 id );
233
234         if(callback) {
235                 req.setCompleteCallback(
236                         function(req) {callback(req.getResultObject())});
237                 req.send();
238         } else {
239                 req.send(true);
240                 return req.getResultObject();
241         }
242 }
243
244 /* if callback exists, call is asynchronous and 
245         the returned item is passed to the callback... */
246 function fetchMetaRecord(id, callback) {
247         var req = new RemoteRequest(
248                 "open-ils.search",
249                 "open-ils.search.biblio.metarecord.mods_slim.retrieve",
250                 id );
251
252         if(callback) {
253                 req.setCompleteCallback(
254                         function(req) {callback(req.getResultObject())});
255                 req.send();
256         } else {
257                 req.send(true);
258                 return req.getResultObject();
259         }
260 }
261
262 /* if callback exists, call is asynchronous and 
263         the returned item is passed to the callback... */
264 /* XXX no method yet... */
265 function fetchVolume(id, callback) {
266         var req = new RemoteRequest(
267                 "open-ils.search",
268                 "open-ils.search.biblio.metarecord.mods_slim.retrieve",
269                 id );
270
271         if(callback) {
272                 req.setCompleteCallback(
273                         function(req) {callback(req.getResultObject())});
274                 req.send();
275         } else {
276                 req.send(true);
277                 return req.getResultObject();
278         }
279 }
280
281 /* if callback exists, call is asynchronous and 
282         the returned item is passed to the callback... */
283 function fetchCopy(id, callback) {
284         var req = new RemoteRequest(
285                 "open-ils.search",
286                 "open-ils.search.asset.copy.fleshed.retrieve",
287                 id );
288
289         if(callback) {
290                 req.setCompleteCallback(
291                         function(req) {callback(req.getResultObject())});
292                 req.send();
293         } else {
294                 req.send(true);
295                 return req.getResultObject();
296         }
297 }
298
299 function mkResourceImage(resource) {
300         var pic = elem("img");
301         pic.setAttribute("src", "/images/" + resource + ".jpg");
302         pic.setAttribute("width", "20");
303         pic.setAttribute("height", "20");
304         pic.setAttribute("title", resource);
305         return pic;
306 }
307
308
309
310 function doLogout() {
311
312         /* remove cookie so browser know's we're logged out */
313         deleteCookie("ils_ses");
314
315         var user = UserSession.instance();
316         if( user.session_id ) {
317                 var request = new RemoteRequest( "open-ils.auth",
318                         "open-ils.auth.session.delete", user.session_id );
319                 request.send(true);
320                 var response = request.getResultObject();
321                 if(! response ) {
322                         //alert("error logging out"); /* exception */
323                 }
324         }
325
326         /* completely destroy this user object */
327         user.destroy();
328 }
329
330
331 function cleanISBN(isbn) {
332         if(isbn) {
333                 isbn = isbn.replace(/^\s+/,"");
334                 var idx = isbn.indexOf(" ");
335                 if(idx > -1) {
336                         isbn = isbn.substring(0, idx);
337                 }
338
339         } else isbn = "";
340         return isbn
341
342 }
343
344
345 function grabUserByBarcode(login, barcode) {
346         if(!barcode) return null;
347         var req = new RemoteRequest(
348                 "open-ils.actor",
349                 "open-ils.actor.user.fleshed.retrieve_by_barcode",
350                 login, barcode );
351
352         req.send(true);
353         return req.getResultObject();
354 }
355
356
357