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 if(at) new AuthTimer(at).run();
326 /* sets the 'prefs' field of the user object to their preferences
327 and returns the preferences */
328 function grabUserPrefs(user, force) {
329 if(user == null) user = G.user;
330 if(!force && user.prefs) return user.prefs;
331 var req = new Request(FETCH_USER_PREFS, user.session, user.id());
333 user.prefs = req.result();
337 function grabFleshedUser() {
339 if(!G.user || !G.user.session) {
341 if(!G.user || !G.user.session) return null;
344 if(G.user.fleshed) return G.user;
346 var req = new Request(FETCH_FLESHED_USER, G.user.session);
349 G.user = req.result();
351 if(!G.user || G.user.length == 0) {
352 G.user = null; return false;
353 cookieManager.remove(COOKIE_SES);
356 G.user.session = ses;
357 G.user.fleshed = true;
359 cookieManager.write(COOKIE_SES, ses, '+1y'); /* update the cookie */
363 function checkUserSkin(new_skin) {
365 return; /* XXX do some debugging with this... */
367 var user_skin = getSkin();
368 var cur_skin = grabSkinFromURL();
370 if(new_skin) user_skin = new_skin;
375 if(grabUserPrefs()) {
376 user_skin = G.user.prefs["opac.skin"];
377 cookieManager.write( COOKIE_SKIN, user_skin, '+1y' );
382 if(!user_skin) return;
384 if( cur_skin != user_skin ) {
385 var url = buildOPACLink();
386 goTo(url.replace(cur_skin, user_skin));
390 function updateUserSetting(setting, value, user) {
391 if(user == null) user = G.user;
394 var req = new Request( UPDATE_USER_PREFS, user.session, a );
399 function commitUserPrefs() {
400 var req = new Request(
401 UPDATE_USER_PREFS, G.user.session, null, G.user.prefs );
406 function grabSkinFromURL() {
407 var path = findBasePath();
408 path = path.replace("/xml/", "");
410 for( var i = path.length - 1; i >= 0; i-- ) {
411 var ch = path.charAt(i);
417 for( i = skin.length - 1; i >= 0; i--)
418 skin2 += skin.charAt(i);
424 /* returns a fleshed G.user on success, false on failure */
429 var uname = G.ui.login.username.value;
430 var passwd = G.ui.login.password.value;
432 var init_request = new Request( LOGIN_INIT, uname );
433 init_request.send(true);
434 var seed = init_request.result();
436 if( ! seed || seed == '0') {
437 alert( "Error Communicating with Authentication Server" );
441 var auth_request = new Request( LOGIN_COMPLETE,
442 uname, hex_md5(seed + hex_md5(passwd)), "opac", getOrigLocation());
445 auth_request.send(true);
446 var auth_result = auth_request.result();
448 var code = checkILSEvent(auth_result);
449 if(code) { alertILSEvent(code); return null; }
451 AUTHTIME = parseInt(auth_result.payload.authtime);
452 var u = grabUser(auth_result.payload.authtoken, true);
453 if(u) runEvt( "common", "locationChanged", u.home_ou(), findOrgDepth(u.home_ou()) );
460 function doLogout(noredirect) {
462 /* cancel everything else */
465 /* be nice and delete the session from the server */
466 if(G.user && G.user.session) {
467 var req = new Request(LOGIN_DELETE, G.user.session);
469 try { req.result(); } catch(E){}
473 cookieManager.remove(COOKIE_SES);
474 cookieManager.remove(COOKIE_SKIN);
475 checkUserSkin("default");
479 args[PARAM_TERM] = "";
480 args[PARAM_LOCATION] = globalOrgTree.id();
481 args[PARAM_DEPTH] = findOrgDepth(globalOrgTree);
485 try{ if(isFrontPage) nored = true; } catch(e){nored = false;}
486 if(!nored) goTo(buildOPACLink(args));
490 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); }
491 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
494 /* ----------------------------------------------------------------------- */
495 /* build the org tree */
496 /* ----------------------------------------------------------------------- */
497 function drawOrgTree() {
498 //setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 10 );
499 setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 1 );
503 function buildOrgSelector(node) {
504 var tree = new SlimTree(node,'orgTreeSelector');
505 orgTreeSelector = tree;
506 for( var i in orgArraySearcher ) {
507 var node = orgArraySearcher[i];
508 if( node == null ) continue;
509 if(node.parent_ou() == null)
510 tree.addNode(node.id(), -1, node.name(),
511 "javascript:orgSelect(" + node.id() + ");", node.name());
513 tree.addNode(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(),
514 "javascript:orgSelect(" + node.id() + ");", node.name());
517 hideMe($('org_loading_div'));
518 unHideMe($('org_selector_tip'));
522 function orgSelect(id) {
524 runEvt("common", "locationChanged", id, findOrgDepth(id) );
526 removeChildren(G.ui.common.now_searching);
527 G.ui.common.now_searching.appendChild(text(findOrgUnit(id).name()));
530 var fontCookie = new HTTP.Cookies();
531 function setFontSize(size) {
533 fontCookie.write(COOKIE_FONT, size, '+1y');
537 var resourceFormats = [
540 "sound recording", "software, multimedia",
545 "three dimensional object" ];
548 function modsFormatToMARC(format) {
554 case "sound recording":
556 case "sound recording-nonmusical":
558 case "sound recording-musical":
560 case "software, multimedia":
566 case "mixed material":
568 case "notated music":
570 case "three dimensional object":
577 function MARCFormatToMods(format) {
583 return "moving image";
585 return "sound recording-nonmusical";
587 return "sound recording-musical";
589 return "software, multimedia";
591 return "still images";
594 return "cartographic";
597 return "mixed material";
600 return "notated music";
602 return "three dimensional object";
607 function setResourcePic( img, resource ) {
608 img.setAttribute( "src", "../../../../images/tor/" + resource + ".jpg");
609 img.title = resource;
614 function msg( text ) {
615 try { alert( text ); } catch(e) {}
618 function findRecord(id,type) {
620 for( var i = 0; i != recordsCache.length; i++ ) {
621 var rec = recordsCache[i];
622 if( rec && rec.doc_id() == id ) return rec;
625 var meth = FETCH_RMODS
626 if(type == 'M') meth = FETCH_MRMODS;
627 var req = new Request(meth, id);
632 function Timer(name, node){
637 Timer.prototype.start =
638 function(){_timerRun(this.name);}
639 Timer.prototype.stop =
640 function(){this.done = true;}
641 function _timerRun(tname) {
645 if(_t.count > 100) return;
647 if( (_t.count % 5) == 0 )
649 _t.node.appendChild(text(str));
650 setTimeout("_timerRun('"+tname+"');", 200);
654 function checkILSEvent(obj) {
655 if( obj.ilsevent != null && obj.ilsevent != 0 )
656 return parseInt(obj.ilsevent);
659 function alertILSEvent(code, msg) {
661 alert( msg + '\n' + $('ilsevent.' + code).innerHTML );
666 function AuthTimer(time) {
667 this.time = (time - LOGOUT_WARNING_TIME) * 1000;
671 AuthTimer.prototype.run = function() {
672 setTimeout('_authTimerAlert()', this.time);
675 function _authTimerAlert() {
676 alert( $('auth_session_expiring').innerHTML );
677 if(!grabUser(null, true)) doLogout();
681 function grabUserByBarcode( authtoken, barcode ) {
682 var req = new Request( FETCH_USER_BYBARCODE, authtoken, barcode );
689 goTo(buildOPACLink({page:HOME}));