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 G.ui.searchbar.text.focus();
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));
84 /* set up some sane defaults */
85 if(isNaN(LOCATION)) LOCATION = 1;
86 if(isNaN(DEPTH)) DEPTH = 0;
87 if(isNaN(OFFSET)) OFFSET = 0;
88 if(isNaN(COUNT)) COUNT = 10;
89 if(isNaN(HITCOUNT)) HITCOUNT = 0;
90 if(isNaN(MRID)) MRID = 0;
91 if(isNaN(RID)) RID = 0;
92 if(isNaN(ORIGLOC)) ORIGLOC = 0;
93 if(isNaN(TOPRANK)) TOPRANK = 1;
94 if(isNaN(AUTHTIME)) AUTHTIME = 1;
97 function initCookies() {
99 var font = fontCookie.get(COOKIE_FONT);
100 if(font) FONTSIZE = font;
101 SKIN = skinCookie.get(COOKIE_SKIN);
104 /* URL param accessors */
105 function getTerm(){return TERM;}
106 function getStype(){return STYPE;}
107 function getLocation(){return LOCATION;}
108 function getDepth(){return DEPTH;}
109 function getForm(){return FORM;}
110 function getOffset(){return OFFSET;}
111 function getDisplayCount(){return COUNT;}
112 function getHitCount(){return HITCOUNT;}
113 function getMrid(){return MRID;};
114 function getRid(){return RID;};
115 function getOrigLocation(){return ORIGLOC;}
116 function getTopRank(){return TOPRANK;}
117 function getAuthtime() { return AUTHTIME; }
119 function getSearchBarExtras(){return SBEXTRAS;}
120 function getFontSize(){return FONTSIZE;};
121 function getSkin(){return SKIN;};
124 function findBasePath() {
125 var path = location.pathname;
126 if(!path.match(/.*\.xml$/)) path += "index.xml";
127 var idx = path.indexOf(config.page[findCurrentPage()]);
128 return path.substring(0, idx);
131 function findBaseURL(ssl) {
132 var path = findBasePath();
133 var proto = (ssl) ? "https:" : "http:";
134 return proto + "//" + location.host + path;
135 dump( 'ssl: ' + ssl + 'proto ' + proto );
139 function buildISBNSrc(isbn) {
140 return "http://" + location.host + "/jackets/" + isbn;
144 function buildImageLink(name, ssl) {
145 return findBaseURL(ssl) + "../../../../images/" + name;
148 function buildOPACLink(args, slim, ssl) {
154 string = findBaseURL(ssl);
155 if(args.page) string += config.page[args.page];
156 else string += config.page[findCurrentPage()];
161 for( var x in args ) {
162 if(x == "page" || args[x] == null) continue;
163 string += "&" + x + "=" + encodeURIComponent(args[x]);
166 string += _appendParam(ORIGLOC, PARAM_ORIGLOC, args, getOrigLocation, string);
167 string += _appendParam(TERM, PARAM_TERM, args, getTerm, string);
168 string += _appendParam(STYPE, PARAM_STYPE, args, getStype, string);
169 string += _appendParam(LOCATION, PARAM_LOCATION, args, getLocation, string);
170 string += _appendParam(DEPTH, PARAM_DEPTH, args, getDepth, string);
171 string += _appendParam(FORM, PARAM_FORM, args, getForm, string);
172 string += _appendParam(OFFSET, PARAM_OFFSET, args, getOffset, string);
173 string += _appendParam(COUNT, PARAM_COUNT, args, getDisplayCount, string);
174 string += _appendParam(HITCOUNT, PARAM_HITCOUNT, args, getHitCount, string);
175 string += _appendParam(MRID, PARAM_MRID, args, getMrid, string);
176 string += _appendParam(RID, PARAM_RID, args, getRid, string);
177 string += _appendParam(TOPRANK, PARAM_TOPRANK, args, getTopRank, string);
178 string += _appendParam(AUTHTIME, PARAM_AUTHTIME, args, getAuthtime, string);
180 return string.replace(/\&$/,'').replace(/\?\&/,"?");
183 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
185 if( fieldVar != null && overrideArgs[fieldName] == null )
186 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
190 /* ----------------------------------------------------------------------- */
191 function cleanISBN(isbn) {
193 isbn = isbn.toString().replace(/^\s+/,"");
194 var idx = isbn.indexOf(" ");
195 if(idx > -1) { isbn = isbn.substring(0, idx); }
201 /* builds a link that goes to the title listings for a metarecord */
202 function buildTitleLink(rec, link) {
204 link.appendChild(text(normalize(truncate(rec.title(), 65))));
207 args[PARAM_OFFSET] = 0;
208 args[PARAM_MRID] = rec.doc_id();
209 link.setAttribute("href", buildOPACLink(args));
212 function buildTitleDetailLink(rec, link) {
214 link.appendChild(text(normalize(truncate(rec.title(), 65))));
217 args[PARAM_OFFSET] = 0;
218 args[PARAM_RID] = rec.doc_id();
219 link.setAttribute("href", buildOPACLink(args));
222 /* 'type' is one of STYPE_AUTHOR, STYPE_SUBJECT, ... found in config.js
223 'trunc' is the number of characters to show in the string, defaults to 65 */
224 function buildSearchLink(type, string, linknode, trunc) {
225 if(!trunc) trunc = 65;
228 args[PARAM_OFFSET] = 0;
229 args[PARAM_TERM] = string;
230 args[PARAM_STYPE] = type;
231 linknode.appendChild(text(normalize(truncate(string, trunc))));
232 linknode.setAttribute("href", buildOPACLink(args));
236 /* ----------------------------------------------------------------------- */
237 /* user session handling */
238 /* ----------------------------------------------------------------------- */
239 /* session is the login session. If no session is provided, we attempt
240 to find one in the cookies. If 'force' is true we retrieve the
241 user from the server even if there is already a global user present.
242 if ses != G.user.session, we also force a grab */
243 var cookie = new cookieObject("ses", 1, "/", COOKIE_SES);
244 function grabUser(ses, force) {
246 if(!ses && isXUL()) ses = xulG['authtoken'];
247 if(!ses) ses = cookie.get(COOKIE_SES);
248 if(!ses) return false;
251 if(G.user && G.user.session == ses)
254 /* first make sure the session is valid */
255 var request = new Request(FETCH_SESSION, ses, 1 );
257 var user = request.result();
259 if(checkILSEvent(user)) {
261 return false; /* unable to grab the session */
264 if( !(typeof user == 'object' && user._isfieldmapper) ) {
270 G.user.fleshed = false;
271 G.user.session = ses;
272 cookie.put(COOKIE_SES, ses);
276 if(G.user.prefs['opac.hits_per_page'])
277 COUNT = parseInt(G.user.prefs['opac.hits_per_page']);
279 var at = getAuthtime();
280 if(isXUL()) at = xulG['authtime'];
282 new AuthTimer(at).run();
288 /* sets the 'prefs' field of the user object to their preferences
289 and returns the preferences */
290 function grabUserPrefs(user, force) {
291 if(user == null) user = G.user;
292 if(!force && user.prefs) return user.prefs;
293 var req = new Request(FETCH_USER_PREFS, user.session, user.id());
295 user.prefs = req.result();
299 function grabFleshedUser() {
301 if(!G.user || !G.user.session) {
303 if(!G.user || !G.user.session) return null;
306 if(G.user.fleshed) return G.user;
308 var req = new Request(FETCH_FLESHED_USER, G.user.session);
311 G.user = req.result();
313 if(!G.user || G.user.length == 0) {
314 G.user = null; return false;
315 cookie.remove(COOKIE_SES);
318 G.user.session = ses;
319 G.user.fleshed = true;
321 cookie.put(COOKIE_SES, ses); /* update the cookie */
327 var skinCookie = new cookieObject("skin", 1, "/", COOKIE_SKIN);
328 function checkUserSkin(new_skin) {
330 return; /* XXX do some debugging with this... */
332 var user_skin = getSkin();
333 var cur_skin = grabSkinFromURL();
335 if(new_skin) user_skin = new_skin;
340 if(grabUserPrefs()) {
341 user_skin = G.user.prefs["opac.skin"];
342 skinCookie.put( COOKIE_SKIN, user_skin );
348 if(!user_skin) return;
350 if( cur_skin != user_skin ) {
351 var url = buildOPACLink();
352 goTo(url.replace(cur_skin, user_skin));
356 function updateUserSetting(setting, value, user) {
357 if(user == null) user = G.user;
360 var req = new Request( UPDATE_USER_PREFS, user.session, a );
365 function commitUserPrefs() {
366 var req = new Request(
367 UPDATE_USER_PREFS, G.user.session, null, G.user.prefs );
372 function grabSkinFromURL() {
373 var path = findBasePath();
374 path = path.replace("/xml/", "");
376 for( var i = path.length - 1; i >= 0; i-- ) {
377 var ch = path.charAt(i);
383 for( i = skin.length - 1; i >= 0; i--)
384 skin2 += skin.charAt(i);
390 /* returns a fleshed G.user on success, false on failure */
393 var uname = G.ui.login.username.value;
394 var passwd = G.ui.login.password.value;
396 var init_request = new Request( LOGIN_INIT, uname );
397 init_request.send(true);
398 var seed = init_request.result();
400 if( ! seed || seed == '0') {
401 alert( "Error Communicating with Authentication Server" );
405 var auth_request = new Request( LOGIN_COMPLETE,
406 uname, hex_md5(seed + hex_md5(passwd)), "opac", getOrigLocation());
409 auth_request.send(true);
410 var auth_result = auth_request.result();
412 var code = checkILSEvent(auth_result);
413 if(code) { alertILSEvent(code); return null; }
415 AUTHTIME = parseInt(auth_result.payload.authtime);
416 var u = grabUser(auth_result.payload.authtoken, true);
417 if(u) runEvt( "common", "locationChanged", u.home_ou(), findOrgDepth(u.home_ou()) );
424 function doLogout() {
426 /* be nice and delete the session from the server */
427 if(G.user && G.user.session) {
428 var req = new Request(LOGIN_DELETE, G.user.session);
430 try { req.result(); } catch(E){}
434 cookie.remove(COOKIE_SES);
435 skinCookie.remove(COOKIE_SKIN);
436 checkUserSkin("default");
440 args[PARAM_TERM] = "";
441 args[PARAM_LOCATION] = globalOrgTree.id();
442 args[PARAM_DEPTH] = findOrgDepth(globalOrgTree);
445 goTo(buildOPACLink(args));
449 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); }
450 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
453 /* ----------------------------------------------------------------------- */
454 /* build the org tree */
455 /* ----------------------------------------------------------------------- */
456 function drawOrgTree() {
457 //G.ui.common.org_tree.innerHTML = buildOrgSelector().toString();
458 //buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);
459 setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 10 );
463 function buildOrgSelector(node) {
464 //var tree = new dTree("orgTreeSelector");
465 var tree = new SlimTree(node,'orgTreeSelector');
466 orgTreeSelector = tree;
467 for( var i in orgArraySearcher ) {
468 var node = orgArraySearcher[i];
469 if( node == null ) continue;
470 if(node.parent_ou() == null)
471 tree.addNode(node.id(), -1, node.name(),
472 "javascript:orgSelect(" + node.id() + ");", node.name());
474 tree.addNode(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(),
475 "javascript:orgSelect(" + node.id() + ");", node.name());
478 hideMe($('org_loading_div'));
479 unHideMe($('org_selector_tip'));
483 function orgSelect(id) {
485 runEvt("common", "locationChanged", id, findOrgDepth(id) );
487 removeChildren(G.ui.common.now_searching);
488 G.ui.common.now_searching.appendChild(text(findOrgUnit(id).name()));
491 var fontCookie = new cookieObject("fonts", 1, "/", COOKIE_FONT);
492 function setFontSize(size) {
494 fontCookie.put(COOKIE_FONT, size);
499 var resourceFormats = [
502 "sound recording", "software, multimedia",
507 "three dimensional object" ];
510 function modsFormatToMARC(format) {
516 case "sound recording":
518 case "sound recording-nonmusical":
520 case "sound recording-musical":
522 case "software, multimedia":
528 case "mixed material":
530 case "notated music":
532 case "three dimensional object":
539 function MARCFormatToMods(format) {
545 return "moving image";
547 return "sound recording-nonmusical";
549 return "sound recording-musical";
551 return "software, multimedia";
553 return "still images";
556 return "cartographic";
559 return "mixed material";
562 return "notated music";
564 return "three dimensional object";
569 function setResourcePic( img, resource ) {
570 img.setAttribute( "src", "../../../../images/tor/" + resource + ".jpg");
571 img.title = resource;
576 function msg( text ) {
577 try { alert( text ); } catch(e) {}
580 function findRecord(id,type) {
582 for( var i = 0; i != recordsCache.length; i++ ) {
583 var rec = recordsCache[i];
584 if( rec && rec.doc_id() == id ) return rec;
587 var meth = FETCH_RMODS
588 if(type == 'M') meth = FETCH_MRMODS;
589 var req = new Request(meth, id);
594 function Timer(name, node){
599 Timer.prototype.start =
600 function(){_timerRun(this.name);}
601 Timer.prototype.stop =
602 function(){this.done = true;}
603 function _timerRun(tname) {
608 if( (_t.count % 5) == 0 )
610 _t.node.appendChild(text(str));
611 setTimeout("_timerRun('"+tname+"');", 200);
615 function checkILSEvent(obj) {
616 if( obj.ilsevent != null && obj.ilsevent != 0 )
617 return parseInt(obj.ilsevent);
620 function alertILSEvent(code, msg) {
621 alert( msg + '\n' + $('ilsevent.' + code).innerHTML );
626 function AuthTimer(time) {
627 this.time = (time - LOGOUT_WARNING_TIME) * 1000;
631 AuthTimer.prototype.run = function() {
632 setTimeout('_authTimerAlert()', this.time);
635 function _authTimerAlert() {
636 if( confirm( $('auth_session_expiring').innerHTML ) )
639 if(!grabUser(null, true)) {
640 alert($('auth_session_expired').innerHTML);
646 function grabUserByBarcode( authtoken, barcode ) {
647 var req = new Request( FETCH_USER_BYBARCODE, authtoken, barcode );