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;}; }
7 var cookieManager = new HTTP.Cookies();
9 var __ilsEvent; /* the last event the occurred */
11 function Request(type) {
13 var s = type.split(":");
14 if(s[2] == "1" && isXUL()) s[1] += ".staff";
15 this.request = new RemoteRequest(s[0], s[1]);
18 for( var x = 1; x!= arguments.length; x++ ) {
20 this.request.addParam(arguments[x]);
25 for( var i = 0; i != p.length; i++ ) {
26 if( i > 0 ) str += ", "
29 _debug('request ' + s[0] + ' ' + s[1] + ' ' + str );
33 Request.prototype.callback = function(cal) {this.request.setCompleteCallback(cal);}
34 Request.prototype.send = function(block){this.request.send(block);}
35 Request.prototype.result = function(){return this.request.getResultObject();}
37 function showCanvas() {
38 for( var x in G.ui.altcanvas ) {
39 hideMe(G.ui.altcanvas[x]);
41 hideMe(G.ui.common.loading);
42 unHideMe(G.ui.common.canvas_main);
43 try{G.ui.searchbar.text.focus();}catch(e){}
46 function swapCanvas(newNode) {
47 for( var x in G.ui.altcanvas )
48 hideMe(G.ui.altcanvas[x]);
50 hideMe(G.ui.common.loading);
51 hideMe(G.ui.common.canvas_main);
55 /* finds the name of the current page */
56 var currentPage = null;
57 function findCurrentPage() {
58 if(currentPage) return currentPage;
61 for( var p in config.page ) pages.push(config.page[p]);
62 pages = pages.sort( function(a,b){ return - (a.length - b.length); } );
64 var path = location.pathname;
65 if(!path.match(/.*\.xml$/))
66 path += "index.xml"; /* in case they go to / */
69 for( var p in pages ) {
70 if( path.indexOf(pages[p]) != -1)
74 for( var p in config.page ) {
75 if(config.page[p] == page) {
84 /* sets all of the params values ----------------------------- */
85 function initParams() {
88 TERM = cgi.param(PARAM_TERM);
89 STYPE = cgi.param(PARAM_STYPE);
90 FORM = cgi.param(PARAM_FORM);
91 LOCATION = parseInt(cgi.param(PARAM_LOCATION));
92 ORIGLOC = parseInt(cgi.param(PARAM_ORIGLOC));
93 DEPTH = parseInt(cgi.param(PARAM_DEPTH));
94 OFFSET = parseInt(cgi.param(PARAM_OFFSET));
95 COUNT = parseInt(cgi.param(PARAM_COUNT));
96 HITCOUNT = parseInt(cgi.param(PARAM_HITCOUNT));
97 MRID = parseInt(cgi.param(PARAM_MRID));
98 RID = parseInt(cgi.param(PARAM_RID));
99 AUTHTIME = parseInt(cgi.param(PARAM_AUTHTIME));
100 ADVTERM = cgi.param(PARAM_ADVTERM);
101 ADVTYPE = cgi.param(PARAM_ADVTYPE);
102 RTYPE = cgi.param(PARAM_RTYPE);
103 SORT = cgi.param(PARAM_SORT);
104 SORT_DIR = cgi.param(PARAM_SORT_DIR);
105 DEBUG = cgi.param(PARAM_DEBUG);
106 CALLNUM = cgi.param(PARAM_CN);
108 /* set up some sane defaults */
109 if(isNaN(LOCATION)) LOCATION = 1;
110 if(isNaN(DEPTH)) DEPTH = 0;
111 if(isNaN(OFFSET)) OFFSET = 0;
112 if(isNaN(COUNT)) COUNT = 10;
113 if(isNaN(HITCOUNT)) HITCOUNT = 0;
114 if(isNaN(MRID)) MRID = 0;
115 if(isNaN(RID)) RID = 0;
116 if(isNaN(ORIGLOC)) ORIGLOC = 1;
117 if(isNaN(AUTHTIME)) AUTHTIME = 0;
118 if(ADVTERM==null) ADVTERM = "";
121 function initCookies() {
123 var font = cookieManager.read(COOKIE_FONT);
124 if(font) FONTSIZE = font;
125 SKIN = cookieManager.read(COOKIE_SKIN);
128 /* URL param accessors */
129 function getTerm(){return TERM;}
130 function getStype(){return STYPE;}
131 function getLocation(){return LOCATION;}
132 function getDepth(){return DEPTH;}
133 function getForm(){return FORM;}
134 function getOffset(){return OFFSET;}
135 function getDisplayCount(){return COUNT;}
136 function getHitCount(){return HITCOUNT;}
137 function getMrid(){return MRID;};
138 function getRid(){return RID;};
139 function getOrigLocation(){return ORIGLOC;}
140 function getAuthtime() { return AUTHTIME; }
141 function getSearchBarExtras(){return SBEXTRAS;}
142 function getFontSize(){return FONTSIZE;};
143 function getSkin(){return SKIN;};
144 function getAdvTerm(){return ADVTERM;}
145 function getAdvType(){return ADVTYPE;}
146 function getRtype(){return RTYPE;}
147 function getSort(){return SORT;}
148 function getSortDir(){return SORT_DIR;}
149 function getDebug(){return DEBUG;}
150 function getCallnumber() { return CALLNUM; }
153 function findBasePath() {
154 var path = location.pathname;
155 if(!path.match(/.*\.xml$/)) path += "index.xml";
156 var idx = path.indexOf(config.page[findCurrentPage()]);
157 return path.substring(0, idx);
160 function findBaseURL(ssl) {
161 var path = findBasePath();
162 var proto = (ssl) ? "https:" : "http:";
163 return proto + "//" + location.host + path;
164 dump( 'ssl: ' + ssl + 'proto ' + proto );
168 function buildISBNSrc(isbn) {
169 return "http://" + location.host + "/jackets/" + isbn;
173 function buildImageLink(name, ssl) {
174 return findBaseURL(ssl) + "../../../../images/" + name;
177 function buildExtrasLink(name, ssl) {
178 return findBaseURL(ssl) + "../../../../extras/" + name;
181 function _debug(str) { try { dump(str + '\n'); } catch(e) {} }
183 function buildOPACLink(args, slim, ssl) {
189 string = findBaseURL(ssl);
190 if(args.page) string += config.page[args.page];
191 else string += config.page[findCurrentPage()];
196 for( var x in args ) {
198 if(x == "page" || v == null || v == undefined || v+'' == 'NaN' ) continue;
199 if(x == PARAM_OFFSET && v == 0) continue;
200 if(x == PARAM_LOCATION && v == 1) continue;
201 if(x == PARAM_DEPTH && v == 0) continue;
202 if(x == PARAM_COUNT && v == 10) continue;
203 if(x == PARAM_FORM && v == 'all' ) continue;
204 if( instanceOf(v, Array) && v.length ) {
205 for( var i = 0; i < v.length; i++ ) {
206 string += "&" + x + "=" + encodeURIComponent(v[i]);
209 string += "&" + x + "=" + encodeURIComponent(v);
214 string += _appendParam(DEBUG, PARAM_DEBUG, args, getDebug, string);
215 if(getOrigLocation() != 1)
216 string += _appendParam(ORIGLOC, PARAM_ORIGLOC, args, getOrigLocation, string);
218 string += _appendParam(TERM, PARAM_TERM, args, getTerm, string);
220 string += _appendParam(STYPE, PARAM_STYPE, args, getStype, string);
221 if(getLocation() != 1)
222 string += _appendParam(LOCATION, PARAM_LOCATION, args, getLocation, string);
224 string += _appendParam(DEPTH, PARAM_DEPTH, args, getDepth, string);
225 if(getForm() && (getForm() != 'all') )
226 string += _appendParam(FORM, PARAM_FORM, args, getForm, string);
228 string += _appendParam(OFFSET, PARAM_OFFSET, args, getOffset, string);
229 if(getDisplayCount() != 10)
230 string += _appendParam(COUNT, PARAM_COUNT, args, getDisplayCount, string);
232 string += _appendParam(HITCOUNT, PARAM_HITCOUNT, args, getHitCount, string);
234 string += _appendParam(MRID, PARAM_MRID, args, getMrid, string);
236 string += _appendParam(RID, PARAM_RID, args, getRid, string);
238 string += _appendParam(AUTHTIME, PARAM_AUTHTIME, args, getAuthtime, string);
240 string += _appendParam(ADVTERM, PARAM_ADVTERM, args, getAdvTerm, string);
242 string += _appendParam(ADVTYPE, PARAM_ADVTYPE, args, getAdvType, string);
244 string += _appendParam(RTYPE, PARAM_RTYPE, args, getRtype, string);
246 return string.replace(/\&$/,'').replace(/\?\&/,"?");
249 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
251 if( fieldVar != null && (fieldVar +'' != 'NaN') && overrideArgs[fieldName] == null )
252 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
256 /* ----------------------------------------------------------------------- */
257 function cleanISBN(isbn) {
259 isbn = isbn.toString().replace(/^\s+/,"");
260 var idx = isbn.indexOf(" ");
261 if(idx > -1) { isbn = isbn.substring(0, idx); }
267 /* builds a link that goes to the title listings for a metarecord */
268 function buildTitleLink(rec, link) {
270 link.appendChild(text(normalize(truncate(rec.title(), 65))));
273 args[PARAM_OFFSET] = 0;
274 args[PARAM_MRID] = rec.doc_id();
275 args[PARAM_RTYPE] = RTYPE_MRID;
276 link.setAttribute("href", buildOPACLink(args));
279 function buildTitleDetailLink(rec, link) {
281 link.appendChild(text(normalize(truncate(rec.title(), 65))));
284 args[PARAM_OFFSET] = 0;
285 args[PARAM_RID] = rec.doc_id();
286 link.setAttribute("href", buildOPACLink(args));
289 /* 'type' is one of STYPE_AUTHOR, STYPE_SUBJECT, ... found in config.js
290 'trunc' is the number of characters to show in the string, defaults to 65 */
291 function buildSearchLink(type, string, linknode, trunc) {
292 if(!trunc) trunc = 65;
295 args[PARAM_OFFSET] = 0;
296 args[PARAM_TERM] = string;
297 args[PARAM_STYPE] = type;
298 linknode.appendChild(text(normalize(truncate(string, trunc))));
299 linknode.setAttribute("href", buildOPACLink(args));
303 /* ----------------------------------------------------------------------- */
304 /* user session handling */
305 /* ----------------------------------------------------------------------- */
306 /* session is the login session. If no session is provided, we attempt
307 to find one in the cookies. If 'force' is true we retrieve the
308 user from the server even if there is already a global user present.
309 if ses != G.user.session, we also force a grab */
310 function grabUser(ses, force) {
312 if(!ses && isXUL()) ses = xulG['authtoken'];
313 if(!ses) ses = cookieManager.read(COOKIE_SES);
314 if(!ses) return false;
317 if(G.user && G.user.session == ses)
320 /* first make sure the session is valid */
321 var request = new Request(FETCH_SESSION, ses, 1 );
323 var user = request.result();
325 if(checkILSEvent(user)) {
328 return false; /* unable to grab the session */
331 if( !(typeof user == 'object' && user._isfieldmapper) ) {
337 G.user.fleshed = false;
338 G.user.session = ses;
339 cookieManager.write(COOKIE_SES, ses, '+1y');
342 if(G.user.prefs['opac.hits_per_page'])
343 COUNT = parseInt(G.user.prefs['opac.hits_per_page']);
345 var at = getAuthtime();
346 if(isXUL()) at = xulG['authtime'];
348 if(at) new AuthTimer(at).run();
353 /* sets the 'prefs' field of the user object to their preferences
354 and returns the preferences */
355 function grabUserPrefs(user, force) {
356 if(user == null) user = G.user;
357 if(!force && user.prefs) return user.prefs;
358 var req = new Request(FETCH_USER_PREFS, user.session, user.id());
360 user.prefs = req.result();
364 function grabFleshedUser() {
366 if(!G.user || !G.user.session) {
368 if(!G.user || !G.user.session) return null;
371 if(G.user.fleshed) return G.user;
373 var req = new Request(FETCH_FLESHED_USER, G.user.session);
376 G.user = req.result();
378 if(!G.user || G.user.length == 0) {
379 G.user = null; return false;
380 cookieManager.remove(COOKIE_SES);
383 G.user.session = ses;
384 G.user.fleshed = true;
386 cookieManager.write(COOKIE_SES, ses, '+1y'); /* update the cookie */
390 function checkUserSkin(new_skin) {
392 return; /* XXX do some debugging with this... */
394 var user_skin = getSkin();
395 var cur_skin = grabSkinFromURL();
397 if(new_skin) user_skin = new_skin;
402 if(grabUserPrefs()) {
403 user_skin = G.user.prefs["opac.skin"];
404 cookieManager.write( COOKIE_SKIN, user_skin, '+1y' );
409 if(!user_skin) return;
411 if( cur_skin != user_skin ) {
412 var url = buildOPACLink();
413 goTo(url.replace(cur_skin, user_skin));
417 function updateUserSetting(setting, value, user) {
418 if(user == null) user = G.user;
421 var req = new Request( UPDATE_USER_PREFS, user.session, a );
426 function commitUserPrefs() {
427 var req = new Request(
428 UPDATE_USER_PREFS, G.user.session, null, G.user.prefs );
433 function grabSkinFromURL() {
434 var path = findBasePath();
435 path = path.replace("/xml/", "");
437 for( var i = path.length - 1; i >= 0; i-- ) {
438 var ch = path.charAt(i);
444 for( i = skin.length - 1; i >= 0; i--)
445 skin2 += skin.charAt(i);
451 /* returns a fleshed G.user on success, false on failure */
456 var uname = G.ui.login.username.value;
457 var passwd = G.ui.login.password.value;
459 var init_request = new Request( LOGIN_INIT, uname );
460 init_request.send(true);
461 var seed = init_request.result();
463 if( ! seed || seed == '0') {
464 alert( "Error Communicating with Authentication Server" );
469 password : hex_md5(seed + hex_md5(passwd)),
471 org : getOrigLocation()
474 if( uname.match(REGEX_BARCODE) ) args.barcode = uname; /* make this better */
475 else args.username = uname;
477 var auth_request = new Request( LOGIN_COMPLETE, args );
479 auth_request.send(true);
480 var auth_result = auth_request.result();
482 var code = checkILSEvent(auth_result);
483 if(code) { alertILSEvent(code); return null; }
485 AUTHTIME = parseInt(auth_result.payload.authtime);
486 var u = grabUser(auth_result.payload.authtoken, true);
487 if(u) runEvt( "common", "locationChanged", u.home_ou(), findOrgDepth(u.home_ou()) );
494 function doLogout(noredirect) {
496 /* cancel everything else */
499 /* be nice and delete the session from the server */
500 if(G.user && G.user.session) {
501 var req = new Request(LOGIN_DELETE, G.user.session);
503 try { req.result(); } catch(E){}
507 cookieManager.remove(COOKIE_SES);
508 cookieManager.remove(COOKIE_SKIN);
509 checkUserSkin("default");
513 args[PARAM_TERM] = "";
514 args[PARAM_LOCATION] = globalOrgTree.id();
515 args[PARAM_DEPTH] = findOrgDepth(globalOrgTree);
519 try{ if(isFrontPage) nored = true; } catch(e){nored = false;}
520 if(!nored) goTo(buildOPACLink(args));
524 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); }
525 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
528 /* ----------------------------------------------------------------------- */
529 /* build the org tree */
530 /* ----------------------------------------------------------------------- */
531 function drawOrgTree() {
532 //setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 10 );
533 setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 1 );
537 function buildOrgSelector(node) {
538 var tree = new SlimTree(node,'orgTreeSelector');
539 orgTreeSelector = tree;
540 for( var i in orgArraySearcher ) {
541 var node = orgArraySearcher[i];
542 if( node == null ) continue;
543 if(node.parent_ou() == null)
544 tree.addNode(node.id(), -1, node.name(),
545 "javascript:orgSelect(" + node.id() + ");", node.name());
547 tree.addNode(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(),
548 "javascript:orgSelect(" + node.id() + ");", node.name());
551 hideMe($('org_loading_div'));
552 unHideMe($('org_selector_tip'));
556 function orgSelect(id) {
558 runEvt("common", "locationChanged", id, findOrgDepth(id) );
560 removeChildren(G.ui.common.now_searching);
561 G.ui.common.now_searching.appendChild(text(findOrgUnit(id).name()));
564 var fontCookie = new HTTP.Cookies();
565 function setFontSize(size) {
567 fontCookie.write(COOKIE_FONT, size, '+1y');
571 var resourceFormats = [
574 "sound recording", "software, multimedia",
579 "three dimensional object" ];
582 function modsFormatToMARC(format) {
588 case "sound recording":
590 case "sound recording-nonmusical":
592 case "sound recording-musical":
594 case "software, multimedia":
600 case "mixed material":
602 case "notated music":
604 case "three dimensional object":
611 function MARCFormatToMods(format) {
617 return "moving image";
619 return "sound recording-nonmusical";
621 return "sound recording-musical";
623 return "software, multimedia";
625 return "still images";
628 return "cartographic";
631 return "mixed material";
634 return "notated music";
636 return "three dimensional object";
641 function setResourcePic( img, resource ) {
642 img.setAttribute( "src", "../../../../images/tor/" + resource + ".jpg");
643 img.title = resource;
648 function msg( text ) {
649 try { alert( text ); } catch(e) {}
652 function findRecord(id,type) {
654 for( var i = 0; i != recordsCache.length; i++ ) {
655 var rec = recordsCache[i];
656 if( rec && rec.doc_id() == id ) return rec;
659 var meth = FETCH_RMODS
660 if(type == 'M') meth = FETCH_MRMODS;
661 var req = new Request(meth, id);
666 function Timer(name, node){
671 Timer.prototype.start =
672 function(){_timerRun(this.name);}
673 Timer.prototype.stop =
674 function(){this.done = true;}
675 function _timerRun(tname) {
679 if(_t.count > 100) return;
681 if( (_t.count % 5) == 0 )
683 _t.node.appendChild(text(str));
684 setTimeout("_timerRun('"+tname+"');", 200);
688 function checkILSEvent(obj) {
689 if( obj.ilsevent != null && obj.ilsevent != 0 )
690 return parseInt(obj.ilsevent);
693 function alertILSEvent(code, msg) {
695 alert( msg + '\n' + $('ilsevent.' + code).innerHTML );
700 function AuthTimer(time) {
701 this.time = (time - LOGOUT_WARNING_TIME) * 1000;
705 AuthTimer.prototype.run = function() {
706 setTimeout('_authTimerAlert()', this.time);
709 function _authTimerAlert() {
710 alert( $('auth_session_expiring').innerHTML );
711 if(!grabUser(null, true)) doLogout();
715 function grabUserByBarcode( authtoken, barcode ) {
716 var req = new Request( FETCH_USER_BYBARCODE, authtoken, barcode );
723 goTo(buildOPACLink({page:HOME}));