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 if( instanceOf(v, Array) ) {
186 for( var i = 0; i != v.length; i++ ) {
187 string += "&" + x + "=" + encodeURIComponent(v[i]);
190 string += "&" + x + "=" + encodeURIComponent(v);
194 if(getOrigLocation() != 1)
195 string += _appendParam(ORIGLOC, PARAM_ORIGLOC, args, getOrigLocation, string);
197 string += _appendParam(TERM, PARAM_TERM, args, getTerm, string);
199 string += _appendParam(STYPE, PARAM_STYPE, args, getStype, string);
200 if(getLocation() != 1)
201 string += _appendParam(LOCATION, PARAM_LOCATION, args, getLocation, string);
203 string += _appendParam(DEPTH, PARAM_DEPTH, args, getDepth, string);
204 if(getForm() && (getForm() != 'all') )
205 string += _appendParam(FORM, PARAM_FORM, args, getForm, string);
207 string += _appendParam(OFFSET, PARAM_OFFSET, args, getOffset, string);
208 if(getDisplayCount() != 10)
209 string += _appendParam(COUNT, PARAM_COUNT, args, getDisplayCount, string);
211 string += _appendParam(HITCOUNT, PARAM_HITCOUNT, args, getHitCount, string);
213 string += _appendParam(MRID, PARAM_MRID, args, getMrid, string);
215 string += _appendParam(RID, PARAM_RID, args, getRid, string);
217 string += _appendParam(AUTHTIME, PARAM_AUTHTIME, args, getAuthtime, string);
219 string += _appendParam(ADVTERM, PARAM_ADVTERM, args, getAdvTerm, string);
221 string += _appendParam(ADVTYPE, PARAM_ADVTYPE, args, getAdvType, string);
223 string += _appendParam(RTYPE, PARAM_RTYPE, args, getRtype, string);
225 return string.replace(/\&$/,'').replace(/\?\&/,"?");
228 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
230 if( fieldVar != null && (fieldVar +'' != 'NaN') && overrideArgs[fieldName] == null )
231 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
235 /* ----------------------------------------------------------------------- */
236 function cleanISBN(isbn) {
238 isbn = isbn.toString().replace(/^\s+/,"");
239 var idx = isbn.indexOf(" ");
240 if(idx > -1) { isbn = isbn.substring(0, idx); }
246 /* builds a link that goes to the title listings for a metarecord */
247 function buildTitleLink(rec, link) {
249 link.appendChild(text(normalize(truncate(rec.title(), 65))));
252 args[PARAM_OFFSET] = 0;
253 args[PARAM_MRID] = rec.doc_id();
254 args[PARAM_RTYPE] = RTYPE_MRID;
255 link.setAttribute("href", buildOPACLink(args));
258 function buildTitleDetailLink(rec, link) {
260 link.appendChild(text(normalize(truncate(rec.title(), 65))));
263 args[PARAM_OFFSET] = 0;
264 args[PARAM_RID] = rec.doc_id();
265 link.setAttribute("href", buildOPACLink(args));
268 /* 'type' is one of STYPE_AUTHOR, STYPE_SUBJECT, ... found in config.js
269 'trunc' is the number of characters to show in the string, defaults to 65 */
270 function buildSearchLink(type, string, linknode, trunc) {
271 if(!trunc) trunc = 65;
274 args[PARAM_OFFSET] = 0;
275 args[PARAM_TERM] = string;
276 args[PARAM_STYPE] = type;
277 linknode.appendChild(text(normalize(truncate(string, trunc))));
278 linknode.setAttribute("href", buildOPACLink(args));
282 /* ----------------------------------------------------------------------- */
283 /* user session handling */
284 /* ----------------------------------------------------------------------- */
285 /* session is the login session. If no session is provided, we attempt
286 to find one in the cookies. If 'force' is true we retrieve the
287 user from the server even if there is already a global user present.
288 if ses != G.user.session, we also force a grab */
289 function grabUser(ses, force) {
291 if(!ses && isXUL()) ses = xulG['authtoken'];
292 if(!ses) ses = cookieManager.read(COOKIE_SES);
293 if(!ses) return false;
296 if(G.user && G.user.session == ses)
299 /* first make sure the session is valid */
300 var request = new Request(FETCH_SESSION, ses, 1 );
302 var user = request.result();
304 if(checkILSEvent(user)) {
307 return false; /* unable to grab the session */
310 if( !(typeof user == 'object' && user._isfieldmapper) ) {
316 G.user.fleshed = false;
317 G.user.session = ses;
318 cookieManager.write(COOKIE_SES, ses, '+1y');
321 if(G.user.prefs['opac.hits_per_page'])
322 COUNT = parseInt(G.user.prefs['opac.hits_per_page']);
324 var at = getAuthtime();
325 if(isXUL()) at = xulG['authtime'];
327 if(at) new AuthTimer(at).run();
332 /* sets the 'prefs' field of the user object to their preferences
333 and returns the preferences */
334 function grabUserPrefs(user, force) {
335 if(user == null) user = G.user;
336 if(!force && user.prefs) return user.prefs;
337 var req = new Request(FETCH_USER_PREFS, user.session, user.id());
339 user.prefs = req.result();
343 function grabFleshedUser() {
345 if(!G.user || !G.user.session) {
347 if(!G.user || !G.user.session) return null;
350 if(G.user.fleshed) return G.user;
352 var req = new Request(FETCH_FLESHED_USER, G.user.session);
355 G.user = req.result();
357 if(!G.user || G.user.length == 0) {
358 G.user = null; return false;
359 cookieManager.remove(COOKIE_SES);
362 G.user.session = ses;
363 G.user.fleshed = true;
365 cookieManager.write(COOKIE_SES, ses, '+1y'); /* update the cookie */
369 function checkUserSkin(new_skin) {
371 return; /* XXX do some debugging with this... */
373 var user_skin = getSkin();
374 var cur_skin = grabSkinFromURL();
376 if(new_skin) user_skin = new_skin;
381 if(grabUserPrefs()) {
382 user_skin = G.user.prefs["opac.skin"];
383 cookieManager.write( COOKIE_SKIN, user_skin, '+1y' );
388 if(!user_skin) return;
390 if( cur_skin != user_skin ) {
391 var url = buildOPACLink();
392 goTo(url.replace(cur_skin, user_skin));
396 function updateUserSetting(setting, value, user) {
397 if(user == null) user = G.user;
400 var req = new Request( UPDATE_USER_PREFS, user.session, a );
405 function commitUserPrefs() {
406 var req = new Request(
407 UPDATE_USER_PREFS, G.user.session, null, G.user.prefs );
412 function grabSkinFromURL() {
413 var path = findBasePath();
414 path = path.replace("/xml/", "");
416 for( var i = path.length - 1; i >= 0; i-- ) {
417 var ch = path.charAt(i);
423 for( i = skin.length - 1; i >= 0; i--)
424 skin2 += skin.charAt(i);
430 /* returns a fleshed G.user on success, false on failure */
435 var uname = G.ui.login.username.value;
436 var passwd = G.ui.login.password.value;
438 var init_request = new Request( LOGIN_INIT, uname );
439 init_request.send(true);
440 var seed = init_request.result();
442 if( ! seed || seed == '0') {
443 alert( "Error Communicating with Authentication Server" );
447 var auth_request = new Request( LOGIN_COMPLETE,
448 uname, hex_md5(seed + hex_md5(passwd)), "opac", getOrigLocation());
451 auth_request.send(true);
452 var auth_result = auth_request.result();
454 var code = checkILSEvent(auth_result);
455 if(code) { alertILSEvent(code); return null; }
457 AUTHTIME = parseInt(auth_result.payload.authtime);
458 var u = grabUser(auth_result.payload.authtoken, true);
459 if(u) runEvt( "common", "locationChanged", u.home_ou(), findOrgDepth(u.home_ou()) );
466 function doLogout(noredirect) {
468 /* cancel everything else */
471 /* be nice and delete the session from the server */
472 if(G.user && G.user.session) {
473 var req = new Request(LOGIN_DELETE, G.user.session);
475 try { req.result(); } catch(E){}
479 cookieManager.remove(COOKIE_SES);
480 cookieManager.remove(COOKIE_SKIN);
481 checkUserSkin("default");
485 args[PARAM_TERM] = "";
486 args[PARAM_LOCATION] = globalOrgTree.id();
487 args[PARAM_DEPTH] = findOrgDepth(globalOrgTree);
491 try{ if(isFrontPage) nored = true; } catch(e){nored = false;}
492 if(!nored) goTo(buildOPACLink(args));
496 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); }
497 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
500 /* ----------------------------------------------------------------------- */
501 /* build the org tree */
502 /* ----------------------------------------------------------------------- */
503 function drawOrgTree() {
504 //setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 10 );
505 setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 1 );
509 function buildOrgSelector(node) {
510 var tree = new SlimTree(node,'orgTreeSelector');
511 orgTreeSelector = tree;
512 for( var i in orgArraySearcher ) {
513 var node = orgArraySearcher[i];
514 if( node == null ) continue;
515 if(node.parent_ou() == null)
516 tree.addNode(node.id(), -1, node.name(),
517 "javascript:orgSelect(" + node.id() + ");", node.name());
519 tree.addNode(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(),
520 "javascript:orgSelect(" + node.id() + ");", node.name());
523 hideMe($('org_loading_div'));
524 unHideMe($('org_selector_tip'));
528 function orgSelect(id) {
530 runEvt("common", "locationChanged", id, findOrgDepth(id) );
532 removeChildren(G.ui.common.now_searching);
533 G.ui.common.now_searching.appendChild(text(findOrgUnit(id).name()));
536 var fontCookie = new HTTP.Cookies();
537 function setFontSize(size) {
539 fontCookie.write(COOKIE_FONT, size, '+1y');
543 var resourceFormats = [
546 "sound recording", "software, multimedia",
551 "three dimensional object" ];
554 function modsFormatToMARC(format) {
560 case "sound recording":
562 case "sound recording-nonmusical":
564 case "sound recording-musical":
566 case "software, multimedia":
572 case "mixed material":
574 case "notated music":
576 case "three dimensional object":
583 function MARCFormatToMods(format) {
589 return "moving image";
591 return "sound recording-nonmusical";
593 return "sound recording-musical";
595 return "software, multimedia";
597 return "still images";
600 return "cartographic";
603 return "mixed material";
606 return "notated music";
608 return "three dimensional object";
613 function setResourcePic( img, resource ) {
614 img.setAttribute( "src", "../../../../images/tor/" + resource + ".jpg");
615 img.title = resource;
620 function msg( text ) {
621 try { alert( text ); } catch(e) {}
624 function findRecord(id,type) {
626 for( var i = 0; i != recordsCache.length; i++ ) {
627 var rec = recordsCache[i];
628 if( rec && rec.doc_id() == id ) return rec;
631 var meth = FETCH_RMODS
632 if(type == 'M') meth = FETCH_MRMODS;
633 var req = new Request(meth, id);
638 function Timer(name, node){
643 Timer.prototype.start =
644 function(){_timerRun(this.name);}
645 Timer.prototype.stop =
646 function(){this.done = true;}
647 function _timerRun(tname) {
651 if(_t.count > 100) return;
653 if( (_t.count % 5) == 0 )
655 _t.node.appendChild(text(str));
656 setTimeout("_timerRun('"+tname+"');", 200);
660 function checkILSEvent(obj) {
661 if( obj.ilsevent != null && obj.ilsevent != 0 )
662 return parseInt(obj.ilsevent);
665 function alertILSEvent(code, msg) {
667 alert( msg + '\n' + $('ilsevent.' + code).innerHTML );
672 function AuthTimer(time) {
673 this.time = (time - LOGOUT_WARNING_TIME) * 1000;
677 AuthTimer.prototype.run = function() {
678 setTimeout('_authTimerAlert()', this.time);
681 function _authTimerAlert() {
682 alert( $('auth_session_expiring').innerHTML );
683 if(!grabUser(null, true)) doLogout();
687 function grabUserByBarcode( authtoken, barcode ) {
688 var req = new Request( FETCH_USER_BYBARCODE, authtoken, barcode );
695 goTo(buildOPACLink({page:HOME}));