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);
76 LOCATION = parseInt(cgi.param(PARAM_LOCATION));
77 ORIGLOC = parseInt(cgi.param(PARAM_ORIGLOC));
78 DEPTH = parseInt(cgi.param(PARAM_DEPTH));
79 OFFSET = parseInt(cgi.param(PARAM_OFFSET));
80 COUNT = parseInt(cgi.param(PARAM_COUNT));
81 HITCOUNT = parseInt(cgi.param(PARAM_HITCOUNT));
82 MRID = parseInt(cgi.param(PARAM_MRID));
83 RID = parseInt(cgi.param(PARAM_RID));
84 AUTHTIME = parseInt(cgi.param(PARAM_AUTHTIME));
85 ADVTERM = cgi.param(PARAM_ADVTERM);
86 ADVTYPE = cgi.param(PARAM_ADVTYPE);
87 RTYPE = cgi.param(PARAM_RTYPE);
88 SORT = cgi.param(PARAM_SORT);
89 SORT_DIR = cgi.param(PARAM_SORT_DIR);
91 /* set up some sane defaults */
92 if(isNaN(LOCATION)) LOCATION = 1;
93 if(isNaN(DEPTH)) DEPTH = 0;
94 if(isNaN(OFFSET)) OFFSET = 0;
95 if(isNaN(COUNT)) COUNT = 10;
96 if(isNaN(HITCOUNT)) HITCOUNT = 0;
97 if(isNaN(MRID)) MRID = 0;
98 if(isNaN(RID)) RID = 0;
99 if(isNaN(ORIGLOC)) ORIGLOC = 1;
100 if(isNaN(AUTHTIME)) AUTHTIME = 0;
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 getAuthtime() { return AUTHTIME; }
124 function getSearchBarExtras(){return SBEXTRAS;}
125 function getFontSize(){return FONTSIZE;};
126 function getSkin(){return SKIN;};
127 function getAdvTerm(){return ADVTERM;}
128 function getAdvType(){return ADVTYPE;}
129 function getRtype(){return RTYPE;}
130 function getSort(){return SORT;}
131 function getSortDir(){return SORT_DIR;}
134 function findBasePath() {
135 var path = location.pathname;
136 if(!path.match(/.*\.xml$/)) path += "index.xml";
137 var idx = path.indexOf(config.page[findCurrentPage()]);
138 return path.substring(0, idx);
141 function findBaseURL(ssl) {
142 var path = findBasePath();
143 var proto = (ssl) ? "https:" : "http:";
144 return proto + "//" + location.host + path;
145 dump( 'ssl: ' + ssl + 'proto ' + proto );
149 function buildISBNSrc(isbn) {
150 return "http://" + location.host + "/jackets/" + isbn;
154 function buildImageLink(name, ssl) {
155 return findBaseURL(ssl) + "../../../../images/" + name;
158 function buildExtrasLink(name, ssl) {
159 return findBaseURL(ssl) + "../../../../extras/" + name;
162 function _debug(str) { try { dump(str + '\n'); } catch(e) {} }
164 function buildOPACLink(args, slim, ssl) {
170 string = findBaseURL(ssl);
171 if(args.page) string += config.page[args.page];
172 else string += config.page[findCurrentPage()];
177 for( var x in args ) {
179 if(x == "page" || v == null || v == undefined ) continue;
180 if(x == PARAM_OFFSET && v == 0) continue;
181 if(x == PARAM_LOCATION && v == 1) continue;
182 if(x == PARAM_DEPTH && v == 0) continue;
183 if(x == PARAM_COUNT && v == 10) continue;
184 if(x == PARAM_FORM && v == 'all' ) continue;
185 string += "&" + x + "=" + encodeURIComponent(v);
188 if(getOrigLocation() != 1)
189 string += _appendParam(ORIGLOC, PARAM_ORIGLOC, args, getOrigLocation, string);
191 string += _appendParam(TERM, PARAM_TERM, args, getTerm, string);
193 string += _appendParam(STYPE, PARAM_STYPE, args, getStype, string);
194 if(getLocation() != 1)
195 string += _appendParam(LOCATION, PARAM_LOCATION, args, getLocation, string);
197 string += _appendParam(DEPTH, PARAM_DEPTH, args, getDepth, string);
198 if(getForm() && (getForm() != 'all') )
199 string += _appendParam(FORM, PARAM_FORM, args, getForm, string);
201 string += _appendParam(OFFSET, PARAM_OFFSET, args, getOffset, string);
202 if(getDisplayCount() != 10)
203 string += _appendParam(COUNT, PARAM_COUNT, args, getDisplayCount, string);
205 string += _appendParam(HITCOUNT, PARAM_HITCOUNT, args, getHitCount, string);
207 string += _appendParam(MRID, PARAM_MRID, args, getMrid, string);
209 string += _appendParam(RID, PARAM_RID, args, getRid, string);
211 string += _appendParam(AUTHTIME, PARAM_AUTHTIME, args, getAuthtime, string);
213 string += _appendParam(ADVTERM, PARAM_ADVTERM, args, getAdvTerm, string);
215 string += _appendParam(ADVTYPE, PARAM_ADVTYPE, args, getAdvType, string);
217 string += _appendParam(RTYPE, PARAM_RTYPE, args, getRtype, string);
219 return string.replace(/\&$/,'').replace(/\?\&/,"?");
222 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
224 if( fieldVar != null && overrideArgs[fieldName] == null )
225 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
229 /* ----------------------------------------------------------------------- */
230 function cleanISBN(isbn) {
232 isbn = isbn.toString().replace(/^\s+/,"");
233 var idx = isbn.indexOf(" ");
234 if(idx > -1) { isbn = isbn.substring(0, idx); }
240 /* builds a link that goes to the title listings for a metarecord */
241 function buildTitleLink(rec, link) {
243 link.appendChild(text(normalize(truncate(rec.title(), 65))));
246 args[PARAM_OFFSET] = 0;
247 args[PARAM_MRID] = rec.doc_id();
248 args[PARAM_RTYPE] = RTYPE_MRID;
249 link.setAttribute("href", buildOPACLink(args));
252 function buildTitleDetailLink(rec, link) {
254 link.appendChild(text(normalize(truncate(rec.title(), 65))));
257 args[PARAM_OFFSET] = 0;
258 args[PARAM_RID] = rec.doc_id();
259 link.setAttribute("href", buildOPACLink(args));
262 /* 'type' is one of STYPE_AUTHOR, STYPE_SUBJECT, ... found in config.js
263 'trunc' is the number of characters to show in the string, defaults to 65 */
264 function buildSearchLink(type, string, linknode, trunc) {
265 if(!trunc) trunc = 65;
268 args[PARAM_OFFSET] = 0;
269 args[PARAM_TERM] = string;
270 args[PARAM_STYPE] = type;
271 linknode.appendChild(text(normalize(truncate(string, trunc))));
272 linknode.setAttribute("href", buildOPACLink(args));
276 /* ----------------------------------------------------------------------- */
277 /* user session handling */
278 /* ----------------------------------------------------------------------- */
279 /* session is the login session. If no session is provided, we attempt
280 to find one in the cookies. If 'force' is true we retrieve the
281 user from the server even if there is already a global user present.
282 if ses != G.user.session, we also force a grab */
283 function grabUser(ses, force) {
285 if(!ses && isXUL()) ses = xulG['authtoken'];
286 if(!ses) ses = cookieManager.read(COOKIE_SES);
287 if(!ses) return false;
290 if(G.user && G.user.session == ses)
293 /* first make sure the session is valid */
294 var request = new Request(FETCH_SESSION, ses, 1 );
296 var user = request.result();
298 if(checkILSEvent(user)) {
301 return false; /* unable to grab the session */
304 if( !(typeof user == 'object' && user._isfieldmapper) ) {
310 G.user.fleshed = false;
311 G.user.session = ses;
312 cookieManager.write(COOKIE_SES, ses, '+1y');
315 if(G.user.prefs['opac.hits_per_page'])
316 COUNT = parseInt(G.user.prefs['opac.hits_per_page']);
318 var at = getAuthtime();
319 if(isXUL()) at = xulG['authtime'];
321 new AuthTimer(at).run(); /* needs debugging... */
327 /* sets the 'prefs' field of the user object to their preferences
328 and returns the preferences */
329 function grabUserPrefs(user, force) {
330 if(user == null) user = G.user;
331 if(!force && user.prefs) return user.prefs;
332 var req = new Request(FETCH_USER_PREFS, user.session, user.id());
334 user.prefs = req.result();
338 function grabFleshedUser() {
340 if(!G.user || !G.user.session) {
342 if(!G.user || !G.user.session) return null;
345 if(G.user.fleshed) return G.user;
347 var req = new Request(FETCH_FLESHED_USER, G.user.session);
350 G.user = req.result();
352 if(!G.user || G.user.length == 0) {
353 G.user = null; return false;
354 cookieManager.remove(COOKIE_SES);
357 G.user.session = ses;
358 G.user.fleshed = true;
360 cookieManager.write(COOKIE_SES, ses, '+1y'); /* update the cookie */
364 function checkUserSkin(new_skin) {
366 return; /* XXX do some debugging with this... */
368 var user_skin = getSkin();
369 var cur_skin = grabSkinFromURL();
371 if(new_skin) user_skin = new_skin;
376 if(grabUserPrefs()) {
377 user_skin = G.user.prefs["opac.skin"];
378 cookieManager.write( COOKIE_SKIN, user_skin, '+1y' );
383 if(!user_skin) return;
385 if( cur_skin != user_skin ) {
386 var url = buildOPACLink();
387 goTo(url.replace(cur_skin, user_skin));
391 function updateUserSetting(setting, value, user) {
392 if(user == null) user = G.user;
395 var req = new Request( UPDATE_USER_PREFS, user.session, a );
400 function commitUserPrefs() {
401 var req = new Request(
402 UPDATE_USER_PREFS, G.user.session, null, G.user.prefs );
407 function grabSkinFromURL() {
408 var path = findBasePath();
409 path = path.replace("/xml/", "");
411 for( var i = path.length - 1; i >= 0; i-- ) {
412 var ch = path.charAt(i);
418 for( i = skin.length - 1; i >= 0; i--)
419 skin2 += skin.charAt(i);
425 /* returns a fleshed G.user on success, false on failure */
428 var uname = G.ui.login.username.value;
429 var passwd = G.ui.login.password.value;
431 var init_request = new Request( LOGIN_INIT, uname );
432 init_request.send(true);
433 var seed = init_request.result();
435 if( ! seed || seed == '0') {
436 alert( "Error Communicating with Authentication Server" );
440 var auth_request = new Request( LOGIN_COMPLETE,
441 uname, hex_md5(seed + hex_md5(passwd)), "opac", getOrigLocation());
444 auth_request.send(true);
445 var auth_result = auth_request.result();
447 var code = checkILSEvent(auth_result);
448 if(code) { alertILSEvent(code); return null; }
450 AUTHTIME = parseInt(auth_result.payload.authtime);
451 var u = grabUser(auth_result.payload.authtoken, true);
452 if(u) runEvt( "common", "locationChanged", u.home_ou(), findOrgDepth(u.home_ou()) );
459 function doLogout(noredirect) {
461 /* be nice and delete the session from the server */
462 if(G.user && G.user.session) {
463 var req = new Request(LOGIN_DELETE, G.user.session);
465 try { req.result(); } catch(E){}
469 cookieManager.remove(COOKIE_SES);
470 cookieManager.remove(COOKIE_SKIN);
471 checkUserSkin("default");
475 args[PARAM_TERM] = "";
476 args[PARAM_LOCATION] = globalOrgTree.id();
477 args[PARAM_DEPTH] = findOrgDepth(globalOrgTree);
481 try{ if(isFrontPage) nored = true; } catch(e){nored = false;}
482 if(!nored) goTo(buildOPACLink(args));
486 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); }
487 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
490 /* ----------------------------------------------------------------------- */
491 /* build the org tree */
492 /* ----------------------------------------------------------------------- */
493 function drawOrgTree() {
494 //setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 10 );
495 setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 1 );
499 function buildOrgSelector(node) {
500 var tree = new SlimTree(node,'orgTreeSelector');
501 orgTreeSelector = tree;
502 for( var i in orgArraySearcher ) {
503 var node = orgArraySearcher[i];
504 if( node == null ) continue;
505 if(node.parent_ou() == null)
506 tree.addNode(node.id(), -1, node.name(),
507 "javascript:orgSelect(" + node.id() + ");", node.name());
509 tree.addNode(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(),
510 "javascript:orgSelect(" + node.id() + ");", node.name());
513 hideMe($('org_loading_div'));
514 unHideMe($('org_selector_tip'));
518 function orgSelect(id) {
520 runEvt("common", "locationChanged", id, findOrgDepth(id) );
522 removeChildren(G.ui.common.now_searching);
523 G.ui.common.now_searching.appendChild(text(findOrgUnit(id).name()));
526 var fontCookie = new HTTP.Cookies();
527 function setFontSize(size) {
529 fontCookie.write(COOKIE_FONT, size, '+1y');
533 var resourceFormats = [
536 "sound recording", "software, multimedia",
541 "three dimensional object" ];
544 function modsFormatToMARC(format) {
550 case "sound recording":
552 case "sound recording-nonmusical":
554 case "sound recording-musical":
556 case "software, multimedia":
562 case "mixed material":
564 case "notated music":
566 case "three dimensional object":
573 function MARCFormatToMods(format) {
579 return "moving image";
581 return "sound recording-nonmusical";
583 return "sound recording-musical";
585 return "software, multimedia";
587 return "still images";
590 return "cartographic";
593 return "mixed material";
596 return "notated music";
598 return "three dimensional object";
603 function setResourcePic( img, resource ) {
604 img.setAttribute( "src", "../../../../images/tor/" + resource + ".jpg");
605 img.title = resource;
610 function msg( text ) {
611 try { alert( text ); } catch(e) {}
614 function findRecord(id,type) {
616 for( var i = 0; i != recordsCache.length; i++ ) {
617 var rec = recordsCache[i];
618 if( rec && rec.doc_id() == id ) return rec;
621 var meth = FETCH_RMODS
622 if(type == 'M') meth = FETCH_MRMODS;
623 var req = new Request(meth, id);
628 function Timer(name, node){
633 Timer.prototype.start =
634 function(){_timerRun(this.name);}
635 Timer.prototype.stop =
636 function(){this.done = true;}
637 function _timerRun(tname) {
641 if(_t.count > 100) return;
643 if( (_t.count % 5) == 0 )
645 _t.node.appendChild(text(str));
646 setTimeout("_timerRun('"+tname+"');", 200);
650 function checkILSEvent(obj) {
651 if( obj.ilsevent != null && obj.ilsevent != 0 )
652 return parseInt(obj.ilsevent);
655 function alertILSEvent(code, msg) {
657 alert( msg + '\n' + $('ilsevent.' + code).innerHTML );
662 function AuthTimer(time) {
663 this.time = (time - LOGOUT_WARNING_TIME) * 1000;
667 AuthTimer.prototype.run = function() {
668 setTimeout('_authTimerAlert()', this.time);
671 function _authTimerAlert() {
672 alert( $('auth_session_expiring').innerHTML );
673 if(!grabUser(null, true)) doLogout();
677 function grabUserByBarcode( authtoken, barcode ) {
678 var req = new Request( FETCH_USER_BYBARCODE, authtoken, barcode );
685 goTo(buildOPACLink({page:HOME}));