1 /* - Request ------------------------------------------------------------- */
3 /* define it again here for pages that don't load RemoteRequest */
4 function isXUL() { try { if(IAMXUL) return true;}catch(e){return false;}; }
6 var cookieManager = new HTTP.Cookies();
8 var __ilsEvent; /* the last event the occurred */
10 function Request(type) {
11 var s = type.split(":");
12 if(s[2] == "1" && isXUL()) s[1] += ".staff";
13 this.request = new RemoteRequest(s[0], s[1]);
14 for( var x = 1; x!= arguments.length; x++ )
15 this.request.addParam(arguments[x]);
18 Request.prototype.callback = function(cal) { this.request.setCompleteCallback(cal); }
19 Request.prototype.send = function(block){this.request.send(block);}
20 Request.prototype.result = function(){return this.request.getResultObject();}
22 function showCanvas() {
23 for( var x in G.ui.altcanvas ) {
24 hideMe(G.ui.altcanvas[x]);
26 hideMe(G.ui.common.loading);
27 unHideMe(G.ui.common.canvas_main);
28 try{G.ui.searchbar.text.focus();}catch(e){}
31 function swapCanvas(newNode) {
32 for( var x in G.ui.altcanvas )
33 hideMe(G.ui.altcanvas[x]);
35 hideMe(G.ui.common.loading);
36 hideMe(G.ui.common.canvas_main);
40 /* finds the name of the current page */
41 var currentPage = null;
42 function findCurrentPage() {
43 if(currentPage) return currentPage;
46 for( var p in config.page ) pages.push(config.page[p]);
47 pages = pages.sort( function(a,b){ return - (a.length - b.length); } );
49 var path = location.pathname;
50 if(!path.match(/.*\.xml$/))
51 path += "index.xml"; /* in case they go to / */
54 for( var p in pages ) {
55 if( path.indexOf(pages[p]) != -1)
59 for( var p in config.page ) {
60 if(config.page[p] == page) {
69 /* sets all of the params values ----------------------------- */
70 function initParams() {
73 TERM = cgi.param(PARAM_TERM);
74 STYPE = cgi.param(PARAM_STYPE);
75 FORM = cgi.param(PARAM_FORM);
77 LOCATION = parseInt(cgi.param(PARAM_LOCATION));
78 ORIGLOC = parseInt(cgi.param(PARAM_ORIGLOC));
79 DEPTH = parseInt(cgi.param(PARAM_DEPTH));
80 OFFSET = parseInt(cgi.param(PARAM_OFFSET));
81 COUNT = parseInt(cgi.param(PARAM_COUNT));
82 HITCOUNT = parseInt(cgi.param(PARAM_HITCOUNT));
83 MRID = parseInt(cgi.param(PARAM_MRID));
84 RID = parseInt(cgi.param(PARAM_RID));
85 TOPRANK = parseFloat(cgi.param(PARAM_TOPRANK));
86 AUTHTIME = parseInt(cgi.param(PARAM_AUTHTIME));
87 ADVTERM = cgi.param(PARAM_ADVTERM);
88 ADVTYPE = cgi.param(PARAM_ADVTYPE);
90 /* set up some sane defaults */
91 if(isNaN(LOCATION)) LOCATION = 1;
92 if(isNaN(DEPTH)) DEPTH = 0;
93 if(isNaN(OFFSET)) OFFSET = 0;
94 if(isNaN(COUNT)) COUNT = 10;
95 if(isNaN(HITCOUNT)) HITCOUNT = 0;
96 if(isNaN(MRID)) MRID = 0;
97 if(isNaN(RID)) RID = 0;
98 if(isNaN(ORIGLOC)) ORIGLOC = 0;
99 if(isNaN(TOPRANK)) TOPRANK = 1;
100 if(isNaN(AUTHTIME)) AUTHTIME = 1;
101 if(ADVTERM==null) ADVTERM = "";
104 function initCookies() {
106 var font = cookieManager.read(COOKIE_FONT);
107 if(font) FONTSIZE = font;
108 SKIN = cookieManager.read(COOKIE_SKIN);
111 /* URL param accessors */
112 function getTerm(){return TERM;}
113 function getStype(){return STYPE;}
114 function getLocation(){return LOCATION;}
115 function getDepth(){return DEPTH;}
116 function getForm(){return FORM;}
117 function getOffset(){return OFFSET;}
118 function getDisplayCount(){return COUNT;}
119 function getHitCount(){return HITCOUNT;}
120 function getMrid(){return MRID;};
121 function getRid(){return RID;};
122 function getOrigLocation(){return ORIGLOC;}
123 function getTopRank(){return TOPRANK;}
124 function getAuthtime() { return AUTHTIME; }
125 function getSearchBarExtras(){return SBEXTRAS;}
126 function getFontSize(){return FONTSIZE;};
127 function getSkin(){return SKIN;};
128 function getAdvTerm(){return ADVTERM;}
129 function getAdvType(){return ADVTYPE;}
132 function findBasePath() {
133 var path = location.pathname;
134 if(!path.match(/.*\.xml$/)) path += "index.xml";
135 var idx = path.indexOf(config.page[findCurrentPage()]);
136 return path.substring(0, idx);
139 function findBaseURL(ssl) {
140 var path = findBasePath();
141 var proto = (ssl) ? "https:" : "http:";
142 return proto + "//" + location.host + path;
143 dump( 'ssl: ' + ssl + 'proto ' + proto );
147 function buildISBNSrc(isbn) {
148 return "http://" + location.host + "/jackets/" + isbn;
152 function buildImageLink(name, ssl) {
153 return findBaseURL(ssl) + "../../../../images/" + name;
156 function buildExtrasLink(name, ssl) {
157 return findBaseURL(ssl) + "../../../../extras/" + name;
160 function buildOPACLink(args, slim, ssl) {
166 string = findBaseURL(ssl);
167 if(args.page) string += config.page[args.page];
168 else string += config.page[findCurrentPage()];
173 for( var x in args ) {
174 if(x == "page" || args[x] == null) continue;
175 string += "&" + x + "=" + encodeURIComponent(args[x]);
178 string += _appendParam(ORIGLOC, PARAM_ORIGLOC, args, getOrigLocation, string);
179 string += _appendParam(TERM, PARAM_TERM, args, getTerm, string);
180 string += _appendParam(STYPE, PARAM_STYPE, args, getStype, string);
181 string += _appendParam(LOCATION, PARAM_LOCATION, args, getLocation, string);
182 string += _appendParam(DEPTH, PARAM_DEPTH, args, getDepth, string);
183 string += _appendParam(FORM, PARAM_FORM, args, getForm, string);
184 string += _appendParam(OFFSET, PARAM_OFFSET, args, getOffset, string);
185 string += _appendParam(COUNT, PARAM_COUNT, args, getDisplayCount, string);
186 string += _appendParam(HITCOUNT, PARAM_HITCOUNT, args, getHitCount, string);
187 string += _appendParam(MRID, PARAM_MRID, args, getMrid, string);
188 string += _appendParam(RID, PARAM_RID, args, getRid, string);
189 string += _appendParam(TOPRANK, PARAM_TOPRANK, args, getTopRank, string);
190 string += _appendParam(AUTHTIME, PARAM_AUTHTIME, args, getAuthtime, string);
191 string += _appendParam(ADVTERM, PARAM_ADVTERM, args, getAdvTerm, string);
192 string += _appendParam(ADVTYPE, PARAM_ADVTYPE, args, getAdvType, string);
194 return string.replace(/\&$/,'').replace(/\?\&/,"?");
197 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
199 if( fieldVar != null && overrideArgs[fieldName] == null )
200 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
204 /* ----------------------------------------------------------------------- */
205 function cleanISBN(isbn) {
207 isbn = isbn.toString().replace(/^\s+/,"");
208 var idx = isbn.indexOf(" ");
209 if(idx > -1) { isbn = isbn.substring(0, idx); }
215 /* builds a link that goes to the title listings for a metarecord */
216 function buildTitleLink(rec, link) {
218 link.appendChild(text(normalize(truncate(rec.title(), 65))));
221 args[PARAM_OFFSET] = 0;
222 args[PARAM_MRID] = rec.doc_id();
223 link.setAttribute("href", buildOPACLink(args));
226 function buildTitleDetailLink(rec, link) {
228 link.appendChild(text(normalize(truncate(rec.title(), 65))));
231 args[PARAM_OFFSET] = 0;
232 args[PARAM_RID] = rec.doc_id();
233 link.setAttribute("href", buildOPACLink(args));
236 /* 'type' is one of STYPE_AUTHOR, STYPE_SUBJECT, ... found in config.js
237 'trunc' is the number of characters to show in the string, defaults to 65 */
238 function buildSearchLink(type, string, linknode, trunc) {
239 if(!trunc) trunc = 65;
242 args[PARAM_OFFSET] = 0;
243 args[PARAM_TERM] = string;
244 args[PARAM_STYPE] = type;
245 linknode.appendChild(text(normalize(truncate(string, trunc))));
246 linknode.setAttribute("href", buildOPACLink(args));
250 /* ----------------------------------------------------------------------- */
251 /* user session handling */
252 /* ----------------------------------------------------------------------- */
253 /* session is the login session. If no session is provided, we attempt
254 to find one in the cookies. If 'force' is true we retrieve the
255 user from the server even if there is already a global user present.
256 if ses != G.user.session, we also force a grab */
257 function grabUser(ses, force) {
259 if(!ses && isXUL()) ses = xulG['authtoken'];
260 if(!ses) ses = cookieManager.read(COOKIE_SES);
261 if(!ses) return false;
264 if(G.user && G.user.session == ses)
267 /* first make sure the session is valid */
268 var request = new Request(FETCH_SESSION, ses, 1 );
270 var user = request.result();
272 if(checkILSEvent(user)) {
275 return false; /* unable to grab the session */
278 if( !(typeof user == 'object' && user._isfieldmapper) ) {
284 G.user.fleshed = false;
285 G.user.session = ses;
286 cookieManager.write(COOKIE_SES, ses, '+1y');
289 if(G.user.prefs['opac.hits_per_page'])
290 COUNT = parseInt(G.user.prefs['opac.hits_per_page']);
292 var at = getAuthtime();
293 if(isXUL()) at = xulG['authtime'];
295 new AuthTimer(at).run(); /* needs debugging... */
301 /* sets the 'prefs' field of the user object to their preferences
302 and returns the preferences */
303 function grabUserPrefs(user, force) {
304 if(user == null) user = G.user;
305 if(!force && user.prefs) return user.prefs;
306 var req = new Request(FETCH_USER_PREFS, user.session, user.id());
308 user.prefs = req.result();
312 function grabFleshedUser() {
314 if(!G.user || !G.user.session) {
316 if(!G.user || !G.user.session) return null;
319 if(G.user.fleshed) return G.user;
321 var req = new Request(FETCH_FLESHED_USER, G.user.session);
324 G.user = req.result();
326 if(!G.user || G.user.length == 0) {
327 G.user = null; return false;
328 cookieManager.remove(COOKIE_SES);
331 G.user.session = ses;
332 G.user.fleshed = true;
334 cookieManager.write(COOKIE_SES, ses, '+1y'); /* update the cookie */
338 function checkUserSkin(new_skin) {
340 return; /* XXX do some debugging with this... */
342 var user_skin = getSkin();
343 var cur_skin = grabSkinFromURL();
345 if(new_skin) user_skin = new_skin;
350 if(grabUserPrefs()) {
351 user_skin = G.user.prefs["opac.skin"];
352 cookieManager.write( COOKIE_SKIN, user_skin, '+1y' );
357 if(!user_skin) return;
359 if( cur_skin != user_skin ) {
360 var url = buildOPACLink();
361 goTo(url.replace(cur_skin, user_skin));
365 function updateUserSetting(setting, value, user) {
366 if(user == null) user = G.user;
369 var req = new Request( UPDATE_USER_PREFS, user.session, a );
374 function commitUserPrefs() {
375 var req = new Request(
376 UPDATE_USER_PREFS, G.user.session, null, G.user.prefs );
381 function grabSkinFromURL() {
382 var path = findBasePath();
383 path = path.replace("/xml/", "");
385 for( var i = path.length - 1; i >= 0; i-- ) {
386 var ch = path.charAt(i);
392 for( i = skin.length - 1; i >= 0; i--)
393 skin2 += skin.charAt(i);
399 /* returns a fleshed G.user on success, false on failure */
402 var uname = G.ui.login.username.value;
403 var passwd = G.ui.login.password.value;
405 var init_request = new Request( LOGIN_INIT, uname );
406 init_request.send(true);
407 var seed = init_request.result();
409 if( ! seed || seed == '0') {
410 alert( "Error Communicating with Authentication Server" );
414 var auth_request = new Request( LOGIN_COMPLETE,
415 uname, hex_md5(seed + hex_md5(passwd)), "opac", getOrigLocation());
418 auth_request.send(true);
419 var auth_result = auth_request.result();
421 var code = checkILSEvent(auth_result);
422 if(code) { alertILSEvent(code); return null; }
424 AUTHTIME = parseInt(auth_result.payload.authtime);
425 var u = grabUser(auth_result.payload.authtoken, true);
426 if(u) runEvt( "common", "locationChanged", u.home_ou(), findOrgDepth(u.home_ou()) );
433 function doLogout(noredirect) {
435 /* be nice and delete the session from the server */
436 if(G.user && G.user.session) {
437 var req = new Request(LOGIN_DELETE, G.user.session);
439 try { req.result(); } catch(E){}
443 cookieManager.remove(COOKIE_SES);
444 cookieManager.remove(COOKIE_SKIN);
445 checkUserSkin("default");
449 args[PARAM_TERM] = "";
450 args[PARAM_LOCATION] = globalOrgTree.id();
451 args[PARAM_DEPTH] = findOrgDepth(globalOrgTree);
455 try{ if(isFrontPage) nored = true; } catch(e){nored = false;}
456 if(!nored) goTo(buildOPACLink(args));
460 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); }
461 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
464 /* ----------------------------------------------------------------------- */
465 /* build the org tree */
466 /* ----------------------------------------------------------------------- */
467 function drawOrgTree() {
468 //setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 10 );
469 setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 1 );
473 function buildOrgSelector(node) {
474 var tree = new SlimTree(node,'orgTreeSelector');
475 orgTreeSelector = tree;
476 for( var i in orgArraySearcher ) {
477 var node = orgArraySearcher[i];
478 if( node == null ) continue;
479 if(node.parent_ou() == null)
480 tree.addNode(node.id(), -1, node.name(),
481 "javascript:orgSelect(" + node.id() + ");", node.name());
483 tree.addNode(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(),
484 "javascript:orgSelect(" + node.id() + ");", node.name());
487 hideMe($('org_loading_div'));
488 unHideMe($('org_selector_tip'));
492 function orgSelect(id) {
494 runEvt("common", "locationChanged", id, findOrgDepth(id) );
496 removeChildren(G.ui.common.now_searching);
497 G.ui.common.now_searching.appendChild(text(findOrgUnit(id).name()));
500 var fontCookie = new HTTP.Cookies();
501 function setFontSize(size) {
503 fontCookie.write(COOKIE_FONT, size, '+1y');
507 var resourceFormats = [
510 "sound recording", "software, multimedia",
515 "three dimensional object" ];
518 function modsFormatToMARC(format) {
524 case "sound recording":
526 case "sound recording-nonmusical":
528 case "sound recording-musical":
530 case "software, multimedia":
536 case "mixed material":
538 case "notated music":
540 case "three dimensional object":
547 function MARCFormatToMods(format) {
553 return "moving image";
555 return "sound recording-nonmusical";
557 return "sound recording-musical";
559 return "software, multimedia";
561 return "still images";
564 return "cartographic";
567 return "mixed material";
570 return "notated music";
572 return "three dimensional object";
577 function setResourcePic( img, resource ) {
578 img.setAttribute( "src", "../../../../images/tor/" + resource + ".jpg");
579 img.title = resource;
584 function msg( text ) {
585 try { alert( text ); } catch(e) {}
588 function findRecord(id,type) {
590 for( var i = 0; i != recordsCache.length; i++ ) {
591 var rec = recordsCache[i];
592 if( rec && rec.doc_id() == id ) return rec;
595 var meth = FETCH_RMODS
596 if(type == 'M') meth = FETCH_MRMODS;
597 var req = new Request(meth, id);
602 function Timer(name, node){
607 Timer.prototype.start =
608 function(){_timerRun(this.name);}
609 Timer.prototype.stop =
610 function(){this.done = true;}
611 function _timerRun(tname) {
615 if(_t.count > 100) return;
617 if( (_t.count % 5) == 0 )
619 _t.node.appendChild(text(str));
620 setTimeout("_timerRun('"+tname+"');", 200);
624 function checkILSEvent(obj) {
625 if( obj.ilsevent != null && obj.ilsevent != 0 )
626 return parseInt(obj.ilsevent);
629 function alertILSEvent(code, msg) {
631 alert( msg + '\n' + $('ilsevent.' + code).innerHTML );
636 function AuthTimer(time) {
637 this.time = (time - LOGOUT_WARNING_TIME) * 1000;
641 AuthTimer.prototype.run = function() {
642 setTimeout('_authTimerAlert()', this.time);
645 function _authTimerAlert() {
646 alert( $('auth_session_expiring').innerHTML );
647 if(!grabUser(null, true)) doLogout();
651 function grabUserByBarcode( authtoken, barcode ) {
652 var req = new Request( FETCH_USER_BYBARCODE, authtoken, barcode );
659 goTo(buildOPACLink({page:HOME}));