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 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]);
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();}
18 function showCanvas() {
19 for( var x in G.ui.altcanvas ) {
20 hideMe(G.ui.altcanvas[x]);
22 hideMe(G.ui.common.loading);
23 unHideMe(G.ui.common.canvas_main);
24 try{G.ui.searchbar.text.focus();}catch(e){}
27 function swapCanvas(newNode) {
28 for( var x in G.ui.altcanvas )
29 hideMe(G.ui.altcanvas[x]);
31 hideMe(G.ui.common.loading);
32 hideMe(G.ui.common.canvas_main);
36 /* finds the name of the current page */
37 var currentPage = null;
38 function findCurrentPage() {
39 if(currentPage) return currentPage;
42 for( var p in config.page ) pages.push(config.page[p]);
43 pages = pages.sort( function(a,b){ return - (a.length - b.length); } );
45 var path = location.pathname;
46 if(!path.match(/.*\.xml$/))
47 path += "index.xml"; /* in case they go to / */
50 for( var p in pages ) {
51 if( path.indexOf(pages[p]) != -1)
55 for( var p in config.page ) {
56 if(config.page[p] == page) {
65 /* sets all of the params values ----------------------------- */
66 function initParams() {
69 TERM = cgi.param(PARAM_TERM);
70 STYPE = cgi.param(PARAM_STYPE);
71 FORM = cgi.param(PARAM_FORM);
73 LOCATION = parseInt(cgi.param(PARAM_LOCATION));
74 ORIGLOC = parseInt(cgi.param(PARAM_ORIGLOC));
75 DEPTH = parseInt(cgi.param(PARAM_DEPTH));
76 OFFSET = parseInt(cgi.param(PARAM_OFFSET));
77 COUNT = parseInt(cgi.param(PARAM_COUNT));
78 HITCOUNT = parseInt(cgi.param(PARAM_HITCOUNT));
79 MRID = parseInt(cgi.param(PARAM_MRID));
80 RID = parseInt(cgi.param(PARAM_RID));
81 TOPRANK = parseFloat(cgi.param(PARAM_TOPRANK));
82 AUTHTIME = parseInt(cgi.param(PARAM_AUTHTIME));
83 ADVTERM = cgi.param(PARAM_ADVTERM);
84 ADVTYPE = cgi.param(PARAM_ADVTYPE);
86 /* set up some sane defaults */
87 if(isNaN(LOCATION)) LOCATION = 1;
88 if(isNaN(DEPTH)) DEPTH = 0;
89 if(isNaN(OFFSET)) OFFSET = 0;
90 if(isNaN(COUNT)) COUNT = 10;
91 if(isNaN(HITCOUNT)) HITCOUNT = 0;
92 if(isNaN(MRID)) MRID = 0;
93 if(isNaN(RID)) RID = 0;
94 if(isNaN(ORIGLOC)) ORIGLOC = 0;
95 if(isNaN(TOPRANK)) TOPRANK = 1;
96 if(isNaN(AUTHTIME)) AUTHTIME = 1;
97 if(ADVTERM==null) ADVTERM = "";
100 function initCookies() {
102 var font = fontCookie.get(COOKIE_FONT);
103 if(font) FONTSIZE = font;
104 SKIN = skinCookie.get(COOKIE_SKIN);
107 /* URL param accessors */
108 function getTerm(){return TERM;}
109 function getStype(){return STYPE;}
110 function getLocation(){return LOCATION;}
111 function getDepth(){return DEPTH;}
112 function getForm(){return FORM;}
113 function getOffset(){return OFFSET;}
114 function getDisplayCount(){return COUNT;}
115 function getHitCount(){return HITCOUNT;}
116 function getMrid(){return MRID;};
117 function getRid(){return RID;};
118 function getOrigLocation(){return ORIGLOC;}
119 function getTopRank(){return TOPRANK;}
120 function getAuthtime() { return AUTHTIME; }
121 function getSearchBarExtras(){return SBEXTRAS;}
122 function getFontSize(){return FONTSIZE;};
123 function getSkin(){return SKIN;};
124 function getAdvTerm(){return ADVTERM;}
125 function getAdvType(){return ADVTYPE;}
128 function findBasePath() {
129 var path = location.pathname;
130 if(!path.match(/.*\.xml$/)) path += "index.xml";
131 var idx = path.indexOf(config.page[findCurrentPage()]);
132 return path.substring(0, idx);
135 function findBaseURL(ssl) {
136 var path = findBasePath();
137 var proto = (ssl) ? "https:" : "http:";
138 return proto + "//" + location.host + path;
139 dump( 'ssl: ' + ssl + 'proto ' + proto );
143 function buildISBNSrc(isbn) {
144 return "http://" + location.host + "/jackets/" + isbn;
148 function buildImageLink(name, ssl) {
149 return findBaseURL(ssl) + "../../../../images/" + name;
152 function buildOPACLink(args, slim, ssl) {
158 string = findBaseURL(ssl);
159 if(args.page) string += config.page[args.page];
160 else string += config.page[findCurrentPage()];
165 for( var x in args ) {
166 if(x == "page" || args[x] == null) continue;
167 string += "&" + x + "=" + encodeURIComponent(args[x]);
170 string += _appendParam(ORIGLOC, PARAM_ORIGLOC, args, getOrigLocation, string);
171 string += _appendParam(TERM, PARAM_TERM, args, getTerm, string);
172 string += _appendParam(STYPE, PARAM_STYPE, args, getStype, string);
173 string += _appendParam(LOCATION, PARAM_LOCATION, args, getLocation, string);
174 string += _appendParam(DEPTH, PARAM_DEPTH, args, getDepth, string);
175 string += _appendParam(FORM, PARAM_FORM, args, getForm, string);
176 string += _appendParam(OFFSET, PARAM_OFFSET, args, getOffset, string);
177 string += _appendParam(COUNT, PARAM_COUNT, args, getDisplayCount, string);
178 string += _appendParam(HITCOUNT, PARAM_HITCOUNT, args, getHitCount, string);
179 string += _appendParam(MRID, PARAM_MRID, args, getMrid, string);
180 string += _appendParam(RID, PARAM_RID, args, getRid, string);
181 string += _appendParam(TOPRANK, PARAM_TOPRANK, args, getTopRank, string);
182 string += _appendParam(AUTHTIME, PARAM_AUTHTIME, args, getAuthtime, string);
183 string += _appendParam(ADVTERM, PARAM_ADVTERM, args, getAdvTerm, string);
184 string += _appendParam(ADVTYPE, PARAM_ADVTYPE, args, getAdvType, string);
186 return string.replace(/\&$/,'').replace(/\?\&/,"?");
189 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
191 if( fieldVar != null && overrideArgs[fieldName] == null )
192 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
196 /* ----------------------------------------------------------------------- */
197 function cleanISBN(isbn) {
199 isbn = isbn.toString().replace(/^\s+/,"");
200 var idx = isbn.indexOf(" ");
201 if(idx > -1) { isbn = isbn.substring(0, idx); }
207 /* builds a link that goes to the title listings for a metarecord */
208 function buildTitleLink(rec, link) {
210 link.appendChild(text(normalize(truncate(rec.title(), 65))));
213 args[PARAM_OFFSET] = 0;
214 args[PARAM_MRID] = rec.doc_id();
215 link.setAttribute("href", buildOPACLink(args));
218 function buildTitleDetailLink(rec, link) {
220 link.appendChild(text(normalize(truncate(rec.title(), 65))));
223 args[PARAM_OFFSET] = 0;
224 args[PARAM_RID] = rec.doc_id();
225 link.setAttribute("href", buildOPACLink(args));
228 /* 'type' is one of STYPE_AUTHOR, STYPE_SUBJECT, ... found in config.js
229 'trunc' is the number of characters to show in the string, defaults to 65 */
230 function buildSearchLink(type, string, linknode, trunc) {
231 if(!trunc) trunc = 65;
234 args[PARAM_OFFSET] = 0;
235 args[PARAM_TERM] = string;
236 args[PARAM_STYPE] = type;
237 linknode.appendChild(text(normalize(truncate(string, trunc))));
238 linknode.setAttribute("href", buildOPACLink(args));
242 /* ----------------------------------------------------------------------- */
243 /* user session handling */
244 /* ----------------------------------------------------------------------- */
245 /* session is the login session. If no session is provided, we attempt
246 to find one in the cookies. If 'force' is true we retrieve the
247 user from the server even if there is already a global user present.
248 if ses != G.user.session, we also force a grab */
249 var cookie = new cookieObject("ses", 1, "/", COOKIE_SES);
250 function grabUser(ses, force) {
252 if(!ses && isXUL()) ses = xulG['authtoken'];
253 if(!ses) ses = cookie.get(COOKIE_SES);
254 if(!ses) return false;
257 if(G.user && G.user.session == ses)
260 /* first make sure the session is valid */
261 var request = new Request(FETCH_SESSION, ses, 1 );
263 var user = request.result();
265 if(checkILSEvent(user)) {
267 return false; /* unable to grab the session */
270 if( !(typeof user == 'object' && user._isfieldmapper) ) {
276 G.user.fleshed = false;
277 G.user.session = ses;
278 cookie.put(COOKIE_SES, ses);
282 if(G.user.prefs['opac.hits_per_page'])
283 COUNT = parseInt(G.user.prefs['opac.hits_per_page']);
285 var at = getAuthtime();
286 if(isXUL()) at = xulG['authtime'];
288 //new AuthTimer(at).run(); /* needs debugging... */
294 /* sets the 'prefs' field of the user object to their preferences
295 and returns the preferences */
296 function grabUserPrefs(user, force) {
297 if(user == null) user = G.user;
298 if(!force && user.prefs) return user.prefs;
299 var req = new Request(FETCH_USER_PREFS, user.session, user.id());
301 user.prefs = req.result();
305 function grabFleshedUser() {
307 if(!G.user || !G.user.session) {
309 if(!G.user || !G.user.session) return null;
312 if(G.user.fleshed) return G.user;
314 var req = new Request(FETCH_FLESHED_USER, G.user.session);
317 G.user = req.result();
319 if(!G.user || G.user.length == 0) {
320 G.user = null; return false;
321 cookie.remove(COOKIE_SES);
324 G.user.session = ses;
325 G.user.fleshed = true;
327 cookie.put(COOKIE_SES, ses); /* update the cookie */
333 var skinCookie = new cookieObject("skin", 1, "/", COOKIE_SKIN);
334 function checkUserSkin(new_skin) {
336 return; /* XXX do some debugging with this... */
338 var user_skin = getSkin();
339 var cur_skin = grabSkinFromURL();
341 if(new_skin) user_skin = new_skin;
346 if(grabUserPrefs()) {
347 user_skin = G.user.prefs["opac.skin"];
348 skinCookie.put( COOKIE_SKIN, user_skin );
354 if(!user_skin) return;
356 if( cur_skin != user_skin ) {
357 var url = buildOPACLink();
358 goTo(url.replace(cur_skin, user_skin));
362 function updateUserSetting(setting, value, user) {
363 if(user == null) user = G.user;
366 var req = new Request( UPDATE_USER_PREFS, user.session, a );
371 function commitUserPrefs() {
372 var req = new Request(
373 UPDATE_USER_PREFS, G.user.session, null, G.user.prefs );
378 function grabSkinFromURL() {
379 var path = findBasePath();
380 path = path.replace("/xml/", "");
382 for( var i = path.length - 1; i >= 0; i-- ) {
383 var ch = path.charAt(i);
389 for( i = skin.length - 1; i >= 0; i--)
390 skin2 += skin.charAt(i);
396 /* returns a fleshed G.user on success, false on failure */
399 var uname = G.ui.login.username.value;
400 var passwd = G.ui.login.password.value;
402 var init_request = new Request( LOGIN_INIT, uname );
403 init_request.send(true);
404 var seed = init_request.result();
406 if( ! seed || seed == '0') {
407 alert( "Error Communicating with Authentication Server" );
411 var auth_request = new Request( LOGIN_COMPLETE,
412 uname, hex_md5(seed + hex_md5(passwd)), "opac", getOrigLocation());
415 auth_request.send(true);
416 var auth_result = auth_request.result();
418 var code = checkILSEvent(auth_result);
419 if(code) { alertILSEvent(code); return null; }
421 AUTHTIME = parseInt(auth_result.payload.authtime);
422 var u = grabUser(auth_result.payload.authtoken, true);
423 if(u) runEvt( "common", "locationChanged", u.home_ou(), findOrgDepth(u.home_ou()) );
430 function doLogout() {
432 /* be nice and delete the session from the server */
433 if(G.user && G.user.session) {
434 var req = new Request(LOGIN_DELETE, G.user.session);
436 try { req.result(); } catch(E){}
440 cookie.remove(COOKIE_SES);
441 skinCookie.remove(COOKIE_SKIN);
442 checkUserSkin("default");
446 args[PARAM_TERM] = "";
447 args[PARAM_LOCATION] = globalOrgTree.id();
448 args[PARAM_DEPTH] = findOrgDepth(globalOrgTree);
451 goTo(buildOPACLink(args));
455 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); }
456 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
459 /* ----------------------------------------------------------------------- */
460 /* build the org tree */
461 /* ----------------------------------------------------------------------- */
462 function drawOrgTree() {
463 //G.ui.common.org_tree.innerHTML = buildOrgSelector().toString();
464 //buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);
465 setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 10 );
469 function buildOrgSelector(node) {
470 //var tree = new dTree("orgTreeSelector");
471 var tree = new SlimTree(node,'orgTreeSelector');
472 orgTreeSelector = tree;
473 for( var i in orgArraySearcher ) {
474 var node = orgArraySearcher[i];
475 if( node == null ) continue;
476 if(node.parent_ou() == null)
477 tree.addNode(node.id(), -1, node.name(),
478 "javascript:orgSelect(" + node.id() + ");", node.name());
480 tree.addNode(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(),
481 "javascript:orgSelect(" + node.id() + ");", node.name());
484 hideMe($('org_loading_div'));
485 unHideMe($('org_selector_tip'));
489 function orgSelect(id) {
491 runEvt("common", "locationChanged", id, findOrgDepth(id) );
493 removeChildren(G.ui.common.now_searching);
494 G.ui.common.now_searching.appendChild(text(findOrgUnit(id).name()));
497 var fontCookie = new cookieObject("fonts", 1, "/", COOKIE_FONT);
498 function setFontSize(size) {
500 fontCookie.put(COOKIE_FONT, size);
505 var resourceFormats = [
508 "sound recording", "software, multimedia",
513 "three dimensional object" ];
516 function modsFormatToMARC(format) {
522 case "sound recording":
524 case "sound recording-nonmusical":
526 case "sound recording-musical":
528 case "software, multimedia":
534 case "mixed material":
536 case "notated music":
538 case "three dimensional object":
545 function MARCFormatToMods(format) {
551 return "moving image";
553 return "sound recording-nonmusical";
555 return "sound recording-musical";
557 return "software, multimedia";
559 return "still images";
562 return "cartographic";
565 return "mixed material";
568 return "notated music";
570 return "three dimensional object";
575 function setResourcePic( img, resource ) {
576 img.setAttribute( "src", "../../../../images/tor/" + resource + ".jpg");
577 img.title = resource;
582 function msg( text ) {
583 try { alert( text ); } catch(e) {}
586 function findRecord(id,type) {
588 for( var i = 0; i != recordsCache.length; i++ ) {
589 var rec = recordsCache[i];
590 if( rec && rec.doc_id() == id ) return rec;
593 var meth = FETCH_RMODS
594 if(type == 'M') meth = FETCH_MRMODS;
595 var req = new Request(meth, id);
600 function Timer(name, node){
605 Timer.prototype.start =
606 function(){_timerRun(this.name);}
607 Timer.prototype.stop =
608 function(){this.done = true;}
609 function _timerRun(tname) {
613 if(_t.count > 100) return;
615 if( (_t.count % 5) == 0 )
617 _t.node.appendChild(text(str));
618 setTimeout("_timerRun('"+tname+"');", 200);
622 function checkILSEvent(obj) {
623 if( obj.ilsevent != null && obj.ilsevent != 0 )
624 return parseInt(obj.ilsevent);
627 function alertILSEvent(code, msg) {
628 alert( msg + '\n' + $('ilsevent.' + code).innerHTML );
633 function AuthTimer(time) {
634 this.time = (time - LOGOUT_WARNING_TIME) * 1000;
638 AuthTimer.prototype.run = function() {
639 setTimeout('_authTimerAlert()', this.time);
642 function _authTimerAlert() {
643 if( confirm( $('auth_session_expiring').innerHTML ) )
646 if(!grabUser(null, true)) {
647 alert($('auth_session_expired').innerHTML);
653 function grabUserByBarcode( authtoken, barcode ) {
654 var req = new Request( FETCH_USER_BYBARCODE, authtoken, barcode );