]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/javascript/lib/js/opac/opac_utils.js
632b0407c923f11e368385cf3e46801fb49d999f
[working/Evergreen.git] / Open-ILS / src / javascript / lib / js / opac / opac_utils.js
1 var IAMXUL = false;
2 function isXUL() { return IAMXUL; }
3
4
5 /* - Request ------------------------------------------------------------- */
6 function Request(type) {
7         var s = type.split(":");
8         if(s[2] == "1" && isXUL()) s[1] += ".staff";
9         this.request = new RemoteRequest(s[0], s[1]);
10         for( var x = 1; x!= arguments.length; x++ ) 
11                 this.request.addParam(arguments[x]);
12 }
13
14 Request.prototype.callback = function(cal) { this.request.setCompleteCallback(cal); }
15 Request.prototype.send          = function(block){this.request.send(block);}
16 Request.prototype.result        = function(){return this.request.getResultObject();}
17 /* ----------------------------------------------------------------------- */
18
19
20 /* ----------------------------------------------------------------------- */
21 /* Functions for showing the canvas (and hiding any other shown stuff) */
22 /* ----------------------------------------------------------------------- */
23 function showCanvas() { setTimeout(_showCanvas, 200); }
24 function _showCanvas() {
25         for( var x in G.ui.altcanvas ) {
26                 hideMe(G.ui.altcanvas[x]);
27         }
28         hideMe(G.ui.common.loading);
29         unHideMe(G.ui.common.canvas_main);
30         G.ui.searchbar.text.focus(); /* focus the searchbar */
31 }
32
33
34 var newCanvasNode;
35 function swapCanvas(newNode) { newCanvasNode = newNode; setTimeout(_swapCanvas, 200); }
36 function _swapCanvas() {
37         for( var x in G.ui.altcanvas ) 
38                 hideMe(G.ui.altcanvas[x]);
39
40         hideMe(G.ui.common.loading);
41         hideMe(G.ui.common.canvas_main);
42         unHideMe(newCanvasNode);
43 }
44 /* ----------------------------------------------------------------------- */
45
46
47 /* finds the name of the current page */
48 function findCurrentPage() {
49         for( var p in config.page ) {
50                 var path = location.pathname;
51
52                 if(!path.match(/.*\.xml$/))
53                         path += "index.xml"; /* in case they go to  / */
54
55                 if( config.page[p] == path)
56                         return p;
57         }
58         return null;
59 }
60
61
62 /* builds an opac URL.  If no page is defined, the current page is used
63         if slim, then only everything after the ? is returned (no host portion)
64  */
65 function  buildOPACLink(args, slim) {
66
67         if(!args) args = {};
68
69         if(!slim) {
70                 var string = location.protocol + "//" + location.host;
71                 if(args.page) string += config.page[args.page];
72                 else string += config.page[findCurrentPage()];
73         }
74
75         string += "?";
76
77         for( var x in args ) {
78                 if(x == "page" || args[x] == null) continue;
79                 string += "&" + x + "=" + encodeURIComponent(args[x]);
80         }
81
82         string += _appendParam(TERM,            PARAM_TERM, args, getTerm, string);
83         string += _appendParam(STYPE,           PARAM_STYPE, args, getStype, string);
84         string += _appendParam(LOCATION, PARAM_LOCATION, args, getLocation, string);
85         string += _appendParam(DEPTH,           PARAM_DEPTH, args, getDepth, string);
86         string += _appendParam(FORM,            PARAM_FORM, args, getForm, string);
87         string += _appendParam(OFFSET,  PARAM_OFFSET, args, getOffset, string);
88         string += _appendParam(COUNT,           PARAM_COUNT, args, getDisplayCount, string);
89         string += _appendParam(HITCOUNT, PARAM_HITCOUNT, args, getHitCount, string);
90         string += _appendParam(MRID,            PARAM_MRID, args, getMrid, string);
91         string += _appendParam(RID,             PARAM_RID, args, getRid, string);
92         return string.replace(/\&$/,'').replace(/\?\&/,"?");    
93 }
94
95 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
96         var ret = "";
97         if( fieldVar != null && overrideArgs[fieldName] == null ) 
98                 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
99         return ret;
100 }
101
102
103
104
105
106 /* ----------------------------------------------------------------------- */
107 /* some useful exceptions */
108 function EX(message) { this.init(message); }
109
110 EX.prototype.init = function(message) {
111    this.message = message;
112 }
113
114 EX.prototype.toString = function() {
115    return "\n *** Exception Occured \n" + this.message;
116 }  
117
118 EXCommunication.prototype              = new EX();
119 EXCommunication.prototype.constructor  = EXCommunication;
120 EXCommunication.baseClass              = EX.prototype.constructor;
121
122 function EXCommunication(message) {
123    this.init("EXCommunication: " + message);
124 }                          
125 /* ----------------------------------------------------------------------- */
126
127 function cleanISBN(isbn) {
128    if(isbn) {
129       isbn = isbn.toString().replace(/^\s+/,"");
130       var idx = isbn.indexOf(" ");
131       if(idx > -1) { isbn = isbn.substring(0, idx); }
132    } else isbn = "";
133    return isbn;
134 }       
135
136
137
138
139 /* ----------------------------------------------------------------------- */
140 /* builds a link that goes to the title listings for a metarecord */
141 function buildTitleLink(rec, link) {
142         if(!rec) return;
143         link.appendChild(text(normalize(truncate(rec.title(), 65))));
144         var args = {};
145         args.page = RRESULT;
146         args[PARAM_OFFSET] = 0;
147         args[PARAM_MRID] = rec.doc_id();
148         link.setAttribute("href", buildOPACLink(args));
149 }
150
151 function buildTitleDetailLink(rec, link) {
152         if(!rec) return;
153         link.appendChild(text(normalize(truncate(rec.title(), 65))));
154         var args = {};
155         args.page = RDETAIL;
156         args[PARAM_OFFSET] = 0;
157         args[PARAM_RID] = rec.doc_id();
158         link.setAttribute("href", buildOPACLink(args));
159 }
160
161 /* 'type' is one of STYPE_AUTHOR, STYPE_SUBJECT, ... found in config.js 
162         'trunc' is the number of characters to show in the string, defaults to 65 */
163 function buildSearchLink(type, string, linknode, trunc) {
164         if(!trunc) trunc = 65;
165         var args = {};
166         args.page = MRESULT;
167         args[PARAM_OFFSET] = 0;
168         args[PARAM_TERM] = string;
169         args[PARAM_STYPE] = type;
170         linknode.appendChild(text(normalize(truncate(string, trunc))));
171         linknode.setAttribute("href", buildOPACLink(args));
172 }
173
174 /* ----------------------------------------------------------------------- */
175
176
177
178 /* ----------------------------------------------------------------------- */
179 /* user session handling */
180 /* ----------------------------------------------------------------------- */
181
182 /* session is the login session.  If no session is provided, we attempt
183         to find one in the cookies.  
184         If 'force' is true we retrieve the 
185         user from the server even if there is already a global user present.
186         if ses != G.user.session, we also force a grab */
187 var cookie = new cookieObject("ses", 1, "/", COOKIE_SES);
188 function grabUser(ses, force) {
189
190         if(!ses) ses = cookie.get(COOKIE_SES);
191         if(!ses) return false;
192
193         if(!force) 
194                 if(G.user && G.user.session == ses)
195                         return G.user;
196
197
198         /* first make sure the session is valid */
199         var request = new Request(FETCH_SESSION, ses );
200         request.send(true);
201         var user = request.result();
202         if( !(typeof user == 'object' && user._isfieldmapper) ) {
203                 return false;
204         }
205
206         G.user = user;
207         G.user.fleshed = false;
208         G.user.session = ses;
209         cookie.put(COOKIE_SES, ses);
210         cookie.write();
211
212         return G.user;
213
214 }
215
216 function grabFleshedUser() {
217
218         if(!G.user || !G.user.session) {
219                 grabUser();     
220                 if(!G.user || !G.user.session) return null;
221         }
222
223         if(G.user.fleshed) return G.user;
224
225    var req = new Request(FETCH_FLESHED_USER, G.user.session);
226         req.send(true);
227
228         G.user = req.result();
229
230         if(!G.user || G.user.length == 0) { 
231                 G.user = null; return false; 
232                 cookie.remove(COOKIE_SES);
233         }
234
235         G.user.session = ses;
236         G.user.fleshed = true;
237
238         cookie.put(COOKIE_SES, ses); /*  update the cookie */
239         cookie.write();
240
241         return G.user;
242 }
243
244
245 /* returns a fleshed G.user on success, false on failure */
246 function doLogin() {
247
248         var uname = G.ui.login.username.value;
249         var passwd = G.ui.login.password.value; 
250
251         var init_request = new Request( LOGIN_INIT, uname );
252    init_request.send(true);
253    var seed = init_request.result();
254
255    if( ! seed || seed == '0') {
256       alert( "Error Communicating with Authentication Server" );
257       return null;
258    }
259
260    var auth_request = new Request( LOGIN_COMPLETE, 
261                 uname, hex_md5(seed + hex_md5(passwd)), "opac");
262
263    auth_request.send(true);
264    var auth_result = auth_request.result();
265
266    if(auth_result == '0' || auth_result == null || auth_result.length == 0) { return false; }
267
268         var u = grabUser(auth_result, true);
269         if(u) updateLoc(u.home_ou(), findOrgDepth(u.home_ou()));
270
271         return u;
272 }
273
274 function doLogout() {
275
276         /* be nice and delete the session from the server */
277         if(G.user && G.user.session) { 
278                 var req = new Request(LOGIN_DELETE, G.user.session);
279       req.send(true);
280                 try { req.result(); } catch(E){}
281     }
282
283         G.user = null;
284         cookie.remove(COOKIE_SES);
285
286         hideMe(G.ui.sidebar.logoutbox);
287         unHideMe(G.ui.sidebar.loginbox);
288         hideMe(G.ui.sidebar.logged_in_as);
289
290 }
291
292
293 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); } 
294 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
295
296
297 /* ----------------------------------------------------------------------- */
298 /* build the org tree */
299 /* ----------------------------------------------------------------------- */
300         
301 var orgTreeSelector;
302 function buildOrgSelector() {
303         var tree = new dTree("orgTreeSelector"); 
304         for( var i in orgArraySearcher ) { 
305                 var node = orgArraySearcher[i];
306                 if( node == null ) continue;
307                 if(node.parent_ou() == null)
308                         tree.add(node.id(), -1, node.name(), 
309                                 "javascript:orgSelect(" + node.id() + ");", node.name());
310                 else {
311                         tree.add(node.id(), node.parent_ou().id(), node.name(), 
312                                 "javascript:orgSelect(" + node.id() + ");", node.name());
313                 }
314         }
315         orgTreeSelector = tree;
316         return tree;
317 }
318
319 function orgSelect(id) {
320         showCanvas();
321         updateLoc(id, findOrgDepth(id));
322 }
323
324
325 /* ------------------------------------------------------------------------------------------------------ */
326 /* org tree utilities */
327 /* ------------------------------------------------------------------------------------------------------ */
328
329 /* takes an org unit or id and return the numeric depth */
330 function findOrgDepth(org_id_or_node) {
331         return findOrgType(findOrgUnit(org_id_or_node).ou_type()).depth();
332 }
333
334 /* takes the org type id from orgunit.ou_type() field and returns
335         the org type object */
336 function findOrgType(type_id) {
337         if(typeof type_id == 'object') return type_id;
338         for(var type in globalOrgTypes) {
339                 var t =globalOrgTypes[type]; 
340                 if( t.id() == type_id || t.id() == parseInt(type_id) ) 
341                         return t;
342         }
343         return null;
344 }
345
346
347 /* returns an org unit by id.  if an object is passed in as the id,
348         then the object is assumed to be an org unit and is returned */
349 function findOrgUnit(org_id) {
350         return (typeof org_id == 'object') ? org_id : orgArraySearcher[org_id];
351 }
352
353
354 /* builds a trail from the top of the org tree to the node provide.
355         basically fleshes out 'my orgs' 
356         Returns an array of [org0, org1, ..., myorg] */
357 function orgNodeTrail(node) {
358         var na = new Array();
359         while( node ) {
360                 na.push(node);
361                 node = findOrgUnit(node.parent_ou());
362         }
363         return na.reverse();
364 }
365
366
367 /* returns an array of sibling org units */
368 function findSiblingOrgs(node) { return findOrgUnit(node.parent_ou()).children(); }
369 /* ------------------------------------------------------------------------------------------------------ */
370
371
372
373
374
375
376