]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/opac/common/js/opac_utils.js
changed copy details layout, still some tweaks to be done
[working/Evergreen.git] / Open-ILS / web / opac / common / js / opac_utils.js
1
2
3 /* - Request ------------------------------------------------------------- */
4 function Request(type) {
5         var s = type.split(":");
6         if(s[2] == "1" && isXUL()) s[1] += ".staff";
7         this.request = new RemoteRequest(s[0], s[1]);
8         for( var x = 1; x!= arguments.length; x++ ) 
9                 this.request.addParam(arguments[x]);
10 }
11
12 Request.prototype.callback = function(cal) { this.request.setCompleteCallback(cal); }
13 Request.prototype.send          = function(block){this.request.send(block);}
14 Request.prototype.result        = function(){return this.request.getResultObject();}
15 /* ----------------------------------------------------------------------- */
16
17 function showCanvas() { setTimeout(_showCanvas, 200); }
18 function _showCanvas() {
19         for( var x in G.ui.altcanvas ) {
20                 hideMe(G.ui.altcanvas[x]);
21         }
22         hideMe(G.ui.common.loading);
23         unHideMe(G.ui.common.canvas_main);
24         G.ui.searchbar.text.focus(); 
25 }
26
27 function swapCanvas(newNode) {
28         for( var x in G.ui.altcanvas ) 
29                 hideMe(G.ui.altcanvas[x]);
30
31         hideMe(G.ui.common.loading);
32         hideMe(G.ui.common.canvas_main);
33         unHideMe(newNode);
34 }
35
36 /* finds the name of the current page */
37 var currentPage = null;
38 function findCurrentPage() {
39         if(currentPage) return currentPage;
40
41         var pages = [];
42         for( var p in config.page ) pages.push(config.page[p]);
43         pages = pages.sort( function(a,b){ return - (a.length - b.length); } );
44
45         var path = location.pathname;
46         if(!path.match(/.*\.xml$/))
47                 path += "index.xml"; /* in case they go to  / */
48
49         var page = null;
50         for( var p in pages ) {
51                 if( path.indexOf(pages[p]) != -1)
52                         page = pages[p];
53         }
54
55         for( var p in config.page ) {
56                 if(config.page[p] == page) {
57                         currentPage = p;
58                         return p;
59                 }
60         }
61
62         return null;
63 }
64
65
66 /* sets all of the params values  ----------------------------- */
67 function initParams() {
68         var cgi = new CGI();    
69
70         TERM    = cgi.param(PARAM_TERM);
71         STYPE   = cgi.param(PARAM_STYPE);
72         FORM    = cgi.param(PARAM_FORM);
73
74         LOCATION        = parseInt(cgi.param(PARAM_LOCATION));
75         ORIGLOC = parseInt(cgi.param(PARAM_ORIGLOC));
76         DEPTH           = parseInt(cgi.param(PARAM_DEPTH));
77         OFFSET  = parseInt(cgi.param(PARAM_OFFSET));
78         COUNT           = parseInt(cgi.param(PARAM_COUNT));
79         HITCOUNT        = parseInt(cgi.param(PARAM_HITCOUNT));
80         MRID            = parseInt(cgi.param(PARAM_MRID));
81         RID             = parseInt(cgi.param(PARAM_RID));
82
83         /* set up some sane defaults */
84         if(isNaN(LOCATION))     LOCATION        = 1;
85         if(isNaN(DEPTH))                DEPTH           = 0;
86         if(isNaN(OFFSET))               OFFSET  = 0;
87         if(isNaN(COUNT))                COUNT           = 10;
88         if(isNaN(HITCOUNT))     HITCOUNT        = 0;
89         if(isNaN(MRID))         MRID            = 0;
90         if(isNaN(RID))                  RID             = 0;
91         if(isNaN(ORIGLOC))      ORIGLOC = 0;
92 }
93
94 function initCookies() {
95         FONTSIZE = "medium";
96         var font = fontCookie.get(COOKIE_FONT);
97         if(font) FONTSIZE = font;
98         SKIN = skinCookie.get(COOKIE_SKIN);
99 }
100
101 /* URL param accessors */
102 function getTerm(){return TERM;}
103 function getStype(){return STYPE;}
104 function getLocation(){return LOCATION;}
105 function getDepth(){return DEPTH;}
106 function getForm(){return FORM;}
107 function getOffset(){return OFFSET;}
108 function getDisplayCount(){return COUNT;}
109 function getHitCount(){return HITCOUNT;}
110 function getMrid(){return MRID;};
111 function getRid(){return RID;};
112 function getOrigLocation(){return ORIGLOC;}
113
114 function getSearchBarExtras(){return SBEXTRAS;}
115 function getFontSize(){return FONTSIZE;};
116 function getSkin(){return SKIN;};
117
118
119
120 /* builds an opac URL.  If no page is defined, the current page is used
121         if slim, then only everything after the ? is returned (no host portion) */
122 function findBasePath() {
123         var path = location.pathname;
124         if(!path.match(/.*\.xml$/)) path += "index.xml"; 
125         var idx = path.indexOf(config.page[findCurrentPage()]);
126         return path.substring(0, idx);
127 }
128
129 function findBaseURL(ssl) {
130         var path = findBasePath();
131         var proto = location.protocol;
132         if(ssl) proto = "https:";
133         return proto + "//" + location.host + path;
134 }
135
136 function buildImageLink(name, ssl) {
137         return findBaseURL(ssl) + "../../../images/" + name;
138 }
139
140 function  buildOPACLink(args, slim, ssl) {
141
142         if(!args) args = {};
143         var string = "";
144
145         if(!slim) {
146                 string = findBaseURL(ssl);
147                 if(args.page) string += config.page[args.page];
148                 else string += config.page[findCurrentPage()];
149         }
150
151         string += "?";
152
153         for( var x in args ) {
154                 if(x == "page" || args[x] == null) continue;
155                 string += "&" + x + "=" + encodeURIComponent(args[x]);
156         }
157
158         string += _appendParam(TERM,            PARAM_TERM, args, getTerm, string);
159         string += _appendParam(STYPE,           PARAM_STYPE, args, getStype, string);
160         string += _appendParam(LOCATION, PARAM_LOCATION, args, getLocation, string);
161         string += _appendParam(DEPTH,           PARAM_DEPTH, args, getDepth, string);
162         string += _appendParam(FORM,            PARAM_FORM, args, getForm, string);
163         string += _appendParam(OFFSET,  PARAM_OFFSET, args, getOffset, string);
164         string += _appendParam(COUNT,           PARAM_COUNT, args, getDisplayCount, string);
165         string += _appendParam(HITCOUNT, PARAM_HITCOUNT, args, getHitCount, string);
166         string += _appendParam(MRID,            PARAM_MRID, args, getMrid, string);
167         string += _appendParam(RID,             PARAM_RID, args, getRid, string);
168         return string.replace(/\&$/,'').replace(/\?\&/,"?");    
169 }
170
171 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
172         var ret = "";
173         if( fieldVar != null && overrideArgs[fieldName] == null ) 
174                 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
175         return ret;
176 }
177
178
179 /*
180 function EX(message) { this.init(message); }
181 EX.prototype.init = function(message) { this.message = message; }
182 EX.prototype.toString = function() { return "\n *** Exception Occured \n" + this.message; }  
183 EXCommunication.prototype              = new EX();
184 EXCommunication.prototype.constructor  = EXCommunication;
185 EXCommunication.baseClass              = EX.prototype.constructor;
186 function EXCommunication(message) { this.init("EXCommunication: " + message); }                          
187 */
188
189 /* ----------------------------------------------------------------------- */
190 function cleanISBN(isbn) {
191    if(isbn) {
192       isbn = isbn.toString().replace(/^\s+/,"");
193       var idx = isbn.indexOf(" ");
194       if(idx > -1) { isbn = isbn.substring(0, idx); }
195    } else isbn = "";
196    return isbn;
197 }       
198
199
200
201
202 /* builds a link that goes to the title listings for a metarecord */
203 function buildTitleLink(rec, link) {
204         if(!rec) return;
205         link.appendChild(text(normalize(truncate(rec.title(), 65))));
206         var args = {};
207         args.page = RRESULT;
208         args[PARAM_OFFSET] = 0;
209         args[PARAM_MRID] = rec.doc_id();
210         link.setAttribute("href", buildOPACLink(args));
211 }
212
213 function buildTitleDetailLink(rec, link) {
214         if(!rec) return;
215         link.appendChild(text(normalize(truncate(rec.title(), 65))));
216         var args = {};
217         args.page = RDETAIL;
218         args[PARAM_OFFSET] = 0;
219         args[PARAM_RID] = rec.doc_id();
220         link.setAttribute("href", buildOPACLink(args));
221 }
222
223 /* 'type' is one of STYPE_AUTHOR, STYPE_SUBJECT, ... found in config.js 
224         'trunc' is the number of characters to show in the string, defaults to 65 */
225 function buildSearchLink(type, string, linknode, trunc) {
226         if(!trunc) trunc = 65;
227         var args = {};
228         args.page = MRESULT;
229         args[PARAM_OFFSET] = 0;
230         args[PARAM_TERM] = string;
231         args[PARAM_STYPE] = type;
232         linknode.appendChild(text(normalize(truncate(string, trunc))));
233         linknode.setAttribute("href", buildOPACLink(args));
234 }
235
236
237 /* ----------------------------------------------------------------------- */
238 /* user session handling */
239 /* ----------------------------------------------------------------------- */
240
241 /* session is the login session.  If no session is provided, we attempt
242         to find one in the cookies.  If 'force' is true we retrieve the 
243         user from the server even if there is already a global user present.
244         if ses != G.user.session, we also force a grab */
245 var cookie = new cookieObject("ses", 1, "/", COOKIE_SES);
246 function grabUser(ses, force) {
247
248         if(!ses) ses = cookie.get(COOKIE_SES);
249         if(!ses) return false;
250
251         if(!force) 
252                 if(G.user && G.user.session == ses)
253                         return G.user;
254
255         /* first make sure the session is valid */
256         var request = new Request(FETCH_SESSION, ses );
257         request.send(true);
258         var user = request.result();
259         if( !(typeof user == 'object' && user._isfieldmapper) ) {
260                 return false;
261         }
262
263         G.user = user;
264         G.user.fleshed = false;
265         G.user.session = ses;
266         cookie.put(COOKIE_SES, ses);
267         cookie.write();
268
269         return G.user;
270
271 }
272
273 /* sets the 'prefs' field of the user object to their preferences 
274         and returns the preferences */
275 function grabUserPrefs(user, force) {
276         if(user == null) user = G.user;
277         if(!force && user.prefs) return user.prefs;     
278         var req = new Request(FETCH_USER_PREFS, user.session, user.id());
279         req.send(true);
280         user.prefs = req.result();
281         return user.prefs;
282 }
283
284 function grabFleshedUser() {
285
286         if(!G.user || !G.user.session) {
287                 grabUser();     
288                 if(!G.user || !G.user.session) return null;
289         }
290
291         if(G.user.fleshed) return G.user;
292
293    var req = new Request(FETCH_FLESHED_USER, G.user.session);
294         req.send(true);
295
296         G.user = req.result();
297
298         if(!G.user || G.user.length == 0) { 
299                 G.user = null; return false; 
300                 cookie.remove(COOKIE_SES);
301         }
302
303         G.user.session = ses;
304         G.user.fleshed = true;
305
306         cookie.put(COOKIE_SES, ses); /*  update the cookie */
307         cookie.write();
308
309         return G.user;
310 }
311
312 var skinCookie = new cookieObject("skin", 1, "/", COOKIE_SKIN);
313 function checkUserSkin(new_skin) {
314
315         var user_skin = getSkin();
316         var cur_skin = grabSkinFromURL();
317
318         if(new_skin) user_skin = new_skin;
319
320         if(isNull(user_skin)) {
321
322                 if(grabUser()) {
323                         if(grabUserPrefs()) {
324                                 user_skin = G.user.prefs["opac.skin"];
325                                 skinCookie.put( COOKIE_SKIN, user_skin );
326                                 skinCookie.write();
327                         }
328                 }
329         }
330
331         if(isNull(user_skin)) return;
332
333         if( cur_skin != user_skin ) {
334                 var url = buildOPACLink();
335                 goTo(url.replace(cur_skin, user_skin));
336         }
337 }
338
339 function updateUserSetting(setting, value, user) {
340         if(user == null) user = G.user;
341         var a = {};
342         a[setting] = value;
343         var req = new Request( UPDATE_USER_PREFS, user.session, a );
344         req.send(true);
345         return req.result();
346 }
347
348 function grabSkinFromURL() {
349         var path = findBasePath();
350         path = path.replace("/xml/", "");
351         var skin = "";
352         for( var i = path.length - 1; i >= 0; i-- ) {
353                 var ch = path.charAt(i);
354                 if(ch == "/") break;
355                 skin += ch;
356         }
357
358         var skin2 = "";
359         for( i = skin.length - 1; i >= 0; i--)
360                 skin2 += skin.charAt(i);
361
362         return skin2;
363 }
364
365
366 /* returns a fleshed G.user on success, false on failure */
367 function doLogin() {
368
369         var uname = G.ui.login.username.value;
370         var passwd = G.ui.login.password.value; 
371
372         var init_request = new Request( LOGIN_INIT, uname );
373    init_request.send(true);
374    var seed = init_request.result();
375
376    if( ! seed || seed == '0') {
377       alert( "Error Communicating with Authentication Server" );
378       return null;
379    }
380
381    var auth_request = new Request( LOGIN_COMPLETE, 
382                 uname, hex_md5(seed + hex_md5(passwd)), "opac");
383
384    auth_request.send(true);
385    var auth_result = auth_request.result();
386
387    if(auth_result == '0' || auth_result == null || auth_result.length == 0) { return false; }
388
389         var u = grabUser(auth_result, true);
390         if(u) updateLoc(u.home_ou(), findOrgDepth(u.home_ou()));
391
392         checkUserSkin();
393
394         return u;
395 }
396
397 function doLogout() {
398
399         /* be nice and delete the session from the server */
400         if(G.user && G.user.session) { 
401                 var req = new Request(LOGIN_DELETE, G.user.session);
402       req.send(true);
403                 try { req.result(); } catch(E){}
404     }
405
406         G.user = null;
407         cookie.remove(COOKIE_SES);
408
409         hideMe(G.ui.sidebar.logoutbox);
410         unHideMe(G.ui.sidebar.loginbox);
411         hideMe(G.ui.sidebar.logged_in_as);
412         skinCookie.remove(COOKIE_SKIN);
413
414         checkUserSkin("default");
415 }
416
417
418 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); } 
419 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
420
421
422 /* ----------------------------------------------------------------------- */
423 /* build the org tree */
424 /* ----------------------------------------------------------------------- */
425
426
427 function drawOrgTree() {
428         G.ui.common.org_tree.innerHTML = buildOrgSelector().toString();
429 }
430         
431 var orgTreeSelector;
432 function buildOrgSelector() {
433         var tree = new dTree("orgTreeSelector"); 
434         for( var i in orgArraySearcher ) { 
435                 var node = orgArraySearcher[i];
436                 if( node == null ) continue;
437                 if(node.parent_ou() == null)
438                         tree.add(node.id(), -1, node.name(), 
439                                 "javascript:orgSelect(" + node.id() + ");", node.name());
440                 else {
441                         tree.add(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(), 
442                                 "javascript:orgSelect(" + node.id() + ");", node.name());
443                 }
444         }
445         orgTreeSelector = tree;
446         return tree;
447 }
448
449 function orgSelect(id) {
450         showCanvas();
451         updateLoc(id, findOrgDepth(id));
452 }
453
454 var fontCookie = new cookieObject("fonts", 1, "/", COOKIE_FONT);
455 function setFontSize(size) {
456         scaleFonts(size);
457         fontCookie.put(COOKIE_FONT, size);
458         fontCookie.write();
459 }
460
461
462
463
464
465
466
467