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 || v+'' == 'NaN' ) 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 && (fieldVar +'' != 'NaN') && 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 cleanRemoteRequests(); /* cancel whatever is out there */
430 var uname = G.ui.login.username.value;
431 var passwd = G.ui.login.password.value;
433 var init_request = new Request( LOGIN_INIT, uname );
434 init_request.send(true);
435 var seed = init_request.result();
437 if( ! seed || seed == '0') {
438 alert( "Error Communicating with Authentication Server" );
442 var auth_request = new Request( LOGIN_COMPLETE,
443 uname, hex_md5(seed + hex_md5(passwd)), "opac", getOrigLocation());
446 auth_request.send(true);
447 var auth_result = auth_request.result();
449 var code = checkILSEvent(auth_result);
450 if(code) { alertILSEvent(code); return null; }
452 AUTHTIME = parseInt(auth_result.payload.authtime);
453 var u = grabUser(auth_result.payload.authtoken, true);
454 if(u) runEvt( "common", "locationChanged", u.home_ou(), findOrgDepth(u.home_ou()) );
461 function doLogout(noredirect) {
463 /* cancel everything else */
464 cleanRemoteRequests();
466 /* be nice and delete the session from the server */
467 if(G.user && G.user.session) {
468 var req = new Request(LOGIN_DELETE, G.user.session);
470 try { req.result(); } catch(E){}
474 cookieManager.remove(COOKIE_SES);
475 cookieManager.remove(COOKIE_SKIN);
476 checkUserSkin("default");
480 args[PARAM_TERM] = "";
481 args[PARAM_LOCATION] = globalOrgTree.id();
482 args[PARAM_DEPTH] = findOrgDepth(globalOrgTree);
486 try{ if(isFrontPage) nored = true; } catch(e){nored = false;}
487 if(!nored) goTo(buildOPACLink(args));
491 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); }
492 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
495 /* ----------------------------------------------------------------------- */
496 /* build the org tree */
497 /* ----------------------------------------------------------------------- */
498 function drawOrgTree() {
499 //setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 10 );
500 setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 1 );
504 function buildOrgSelector(node) {
505 var tree = new SlimTree(node,'orgTreeSelector');
506 orgTreeSelector = tree;
507 for( var i in orgArraySearcher ) {
508 var node = orgArraySearcher[i];
509 if( node == null ) continue;
510 if(node.parent_ou() == null)
511 tree.addNode(node.id(), -1, node.name(),
512 "javascript:orgSelect(" + node.id() + ");", node.name());
514 tree.addNode(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(),
515 "javascript:orgSelect(" + node.id() + ");", node.name());
518 hideMe($('org_loading_div'));
519 unHideMe($('org_selector_tip'));
523 function orgSelect(id) {
525 runEvt("common", "locationChanged", id, findOrgDepth(id) );
527 removeChildren(G.ui.common.now_searching);
528 G.ui.common.now_searching.appendChild(text(findOrgUnit(id).name()));
531 var fontCookie = new HTTP.Cookies();
532 function setFontSize(size) {
534 fontCookie.write(COOKIE_FONT, size, '+1y');
538 var resourceFormats = [
541 "sound recording", "software, multimedia",
546 "three dimensional object" ];
549 function modsFormatToMARC(format) {
555 case "sound recording":
557 case "sound recording-nonmusical":
559 case "sound recording-musical":
561 case "software, multimedia":
567 case "mixed material":
569 case "notated music":
571 case "three dimensional object":
578 function MARCFormatToMods(format) {
584 return "moving image";
586 return "sound recording-nonmusical";
588 return "sound recording-musical";
590 return "software, multimedia";
592 return "still images";
595 return "cartographic";
598 return "mixed material";
601 return "notated music";
603 return "three dimensional object";
608 function setResourcePic( img, resource ) {
609 img.setAttribute( "src", "../../../../images/tor/" + resource + ".jpg");
610 img.title = resource;
615 function msg( text ) {
616 try { alert( text ); } catch(e) {}
619 function findRecord(id,type) {
621 for( var i = 0; i != recordsCache.length; i++ ) {
622 var rec = recordsCache[i];
623 if( rec && rec.doc_id() == id ) return rec;
626 var meth = FETCH_RMODS
627 if(type == 'M') meth = FETCH_MRMODS;
628 var req = new Request(meth, id);
633 function Timer(name, node){
638 Timer.prototype.start =
639 function(){_timerRun(this.name);}
640 Timer.prototype.stop =
641 function(){this.done = true;}
642 function _timerRun(tname) {
646 if(_t.count > 100) return;
648 if( (_t.count % 5) == 0 )
650 _t.node.appendChild(text(str));
651 setTimeout("_timerRun('"+tname+"');", 200);
655 function checkILSEvent(obj) {
656 if( obj.ilsevent != null && obj.ilsevent != 0 )
657 return parseInt(obj.ilsevent);
660 function alertILSEvent(code, msg) {
662 alert( msg + '\n' + $('ilsevent.' + code).innerHTML );
667 function AuthTimer(time) {
668 this.time = (time - LOGOUT_WARNING_TIME) * 1000;
672 AuthTimer.prototype.run = function() {
673 setTimeout('_authTimerAlert()', this.time);
676 function _authTimerAlert() {
677 alert( $('auth_session_expiring').innerHTML );
678 if(!grabUser(null, true)) doLogout();
682 function grabUserByBarcode( authtoken, barcode ) {
683 var req = new Request( FETCH_USER_BYBARCODE, authtoken, barcode );
690 goTo(buildOPACLink({page:HOME}));