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);
107 LITFORM = cgi.param(PARAM_LITFORM);
108 ITEMFORM = cgi.param(PARAM_ITEMFORM);
109 ITEMTYPE = cgi.param(PARAM_ITEMTYPE);
110 AUDIENCE = cgi.param(PARAM_AUDIENCE);
111 SEARCHES = cgi.param(PARAM_SEARCHES);
112 LANGUAGE = cgi.param(PARAM_LANGUAGE);
113 TFORM = cgi.param(PARAM_TFORM);
115 /* set up some sane defaults */
116 if(isNaN(LOCATION)) LOCATION = 1;
117 if(isNaN(DEPTH)) DEPTH = 0;
118 if(isNaN(OFFSET)) OFFSET = 0;
119 if(isNaN(COUNT)) COUNT = 10;
120 if(isNaN(HITCOUNT)) HITCOUNT = 0;
121 if(isNaN(MRID)) MRID = 0;
122 if(isNaN(RID)) RID = 0;
123 if(isNaN(ORIGLOC)) ORIGLOC = 1;
124 if(isNaN(AUTHTIME)) AUTHTIME = 0;
125 if(ADVTERM==null) ADVTERM = "";
128 function clearSearchParams() {
152 function initCookies() {
153 FONTSIZE = "regular";
154 var font = cookieManager.read(COOKIE_FONT);
156 if(font) FONTSIZE = font;
157 SKIN = cookieManager.read(COOKIE_SKIN);
160 /* URL param accessors */
161 function getTerm(){return TERM;}
162 function getStype(){return STYPE;}
163 function getLocation(){return LOCATION;}
164 function getDepth(){return DEPTH;}
165 function getForm(){return FORM;}
166 function getTform(){return TFORM;}
167 function getOffset(){return OFFSET;}
168 function getDisplayCount(){return COUNT;}
169 function getHitCount(){return HITCOUNT;}
170 function getMrid(){return MRID;};
171 function getRid(){return RID;};
172 function getOrigLocation(){return ORIGLOC;}
173 function getAuthtime() { return AUTHTIME; }
174 function getSearchBarExtras(){return SBEXTRAS;}
175 function getFontSize(){return FONTSIZE;};
176 function getSkin(){return SKIN;};
177 function getAdvTerm(){return ADVTERM;}
178 function getAdvType(){return ADVTYPE;}
179 function getRtype(){return RTYPE;}
180 function getSort(){return SORT;}
181 function getSortDir(){return SORT_DIR;}
182 function getDebug(){return DEBUG;}
183 function getCallnumber() { return CALLNUM; }
184 function getLitForm() { return LITFORM; }
185 function getItemForm() { return ITEMFORM; }
186 function getItemType() { return ITEMTYPE; }
187 function getAudience() { return AUDIENCE; }
188 function getSearches() { return SEARCHES; }
189 function getLanguage() { return LANGUAGE; }
192 function findBasePath() {
193 var path = location.pathname;
194 if(!path.match(/.*\.xml$/)) path += "index.xml";
195 var idx = path.indexOf(config.page[findCurrentPage()]);
196 return path.substring(0, idx);
199 function findBaseURL(ssl) {
200 var path = findBasePath();
201 var proto = (ssl) ? "https:" : "http:";
202 return proto + "//" + location.host + path;
203 dump( 'ssl: ' + ssl + 'proto ' + proto );
207 function buildISBNSrc(isbn) {
208 return "http://" + location.host + "/jackets/" + isbn;
212 function buildImageLink(name, ssl) {
213 return findBaseURL(ssl) + "../../../../images/" + name;
216 function buildExtrasLink(name, ssl) {
217 return findBaseURL(ssl) + "../../../../extras/" + name;
221 function _debug(str) {
222 try { dump('dbg: ' + str + '\n'); } catch(e) {}
224 /* potentially useful, but usually just annoying */
227 if(!consoleService) {
229 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
230 this.consoleService = Components.classes['@mozilla.org/consoleservice;1']
231 .getService(Components.interfaces.nsIConsoleService);
237 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
238 consoleService.logStringMessage(str + '\n');
245 function buildOPACLink(args, slim, ssl) {
251 string = findBaseURL(ssl);
252 if(args.page) string += config.page[args.page];
253 else string += config.page[findCurrentPage()];
258 for( var x in args ) {
260 if(x == "page" || v == null || v == undefined || v+'' == 'NaN' ) continue;
261 if(x == PARAM_OFFSET && v == 0) continue;
262 if(x == PARAM_LOCATION && v == 1) continue;
263 if(x == PARAM_DEPTH && v == 0) continue;
264 if(x == PARAM_COUNT && v == 10) continue;
265 if(x == PARAM_FORM && v == 'all' ) continue;
266 if( instanceOf(v, Array) && v.length ) {
267 for( var i = 0; i < v.length; i++ ) {
268 string += "&" + x + "=" + encodeURIComponent(v[i]);
271 string += "&" + x + "=" + encodeURIComponent(v);
276 string += _appendParam(DEBUG, PARAM_DEBUG, args, getDebug, string);
277 if(getOrigLocation() != 1)
278 string += _appendParam(ORIGLOC, PARAM_ORIGLOC, args, getOrigLocation, string);
280 string += _appendParam(TERM, PARAM_TERM, args, getTerm, string);
282 string += _appendParam(STYPE, PARAM_STYPE, args, getStype, string);
283 if(getLocation() != 1)
284 string += _appendParam(LOCATION, PARAM_LOCATION, args, getLocation, string);
286 string += _appendParam(DEPTH, PARAM_DEPTH, args, getDepth, string);
287 if(getForm() && (getForm() != 'all') )
288 string += _appendParam(FORM, PARAM_FORM, args, getForm, string);
289 if(getTform() && (getTform() != 'all') )
290 string += _appendParam(TFORM, PARAM_TFORM, args, getTform, string);
292 string += _appendParam(OFFSET, PARAM_OFFSET, args, getOffset, string);
293 if(getDisplayCount() != 10)
294 string += _appendParam(COUNT, PARAM_COUNT, args, getDisplayCount, string);
296 string += _appendParam(HITCOUNT, PARAM_HITCOUNT, args, getHitCount, string);
298 string += _appendParam(MRID, PARAM_MRID, args, getMrid, string);
300 string += _appendParam(RID, PARAM_RID, args, getRid, string);
302 string += _appendParam(AUTHTIME, PARAM_AUTHTIME, args, getAuthtime, string);
304 string += _appendParam(ADVTERM, PARAM_ADVTERM, args, getAdvTerm, string);
306 string += _appendParam(ADVTYPE, PARAM_ADVTYPE, args, getAdvType, string);
308 string += _appendParam(RTYPE, PARAM_RTYPE, args, getRtype, string);
310 string += _appendParam(ITEMFORM, PARAM_ITEMFORM, args, getItemForm, string);
312 string += _appendParam(ITEMTYPE, PARAM_ITEMTYPE, args, getItemType, string);
314 string += _appendParam(LITFORM, PARAM_LITFORM, args, getLitForm, string);
316 string += _appendParam(AUDIENCE, PARAM_AUDIENCE, args, getAudience, string);
318 string += _appendParam(SEARCHES, PARAM_SEARCHES, args, getSearches, string);
320 string += _appendParam(LANGUAGE, PARAM_LANGUAGE, args, getLanguage, string);
322 return string.replace(/\&$/,'').replace(/\?\&/,"?");
325 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
329 if( fieldVar != null &&
330 (fieldVar +'' != 'NaN') &&
331 overrideArgs[fieldName] == null &&
335 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
340 /* ----------------------------------------------------------------------- */
341 function cleanISBN(isbn) {
343 isbn = isbn.toString().replace(/^\s+/,"");
344 var idx = isbn.indexOf(" ");
345 if(idx > -1) { isbn = isbn.substring(0, idx); }
351 /* builds a link that goes to the title listings for a metarecord */
352 function buildTitleLink(rec, link) {
354 link.appendChild(text(normalize(truncate(rec.title(), 65))));
357 args[PARAM_OFFSET] = 0;
358 args[PARAM_MRID] = rec.doc_id();
359 args[PARAM_RTYPE] = RTYPE_MRID;
360 link.setAttribute("href", buildOPACLink(args));
363 function buildTitleDetailLink(rec, link) {
365 link.appendChild(text(normalize(truncate(rec.title(), 65))));
368 args[PARAM_OFFSET] = 0;
369 args[PARAM_RID] = rec.doc_id();
370 link.setAttribute("href", buildOPACLink(args));
373 /* 'type' is one of STYPE_AUTHOR, STYPE_SUBJECT, ... found in config.js
374 'trunc' is the number of characters to show in the string, defaults to 65 */
375 function buildSearchLink(type, string, linknode, trunc) {
376 if(!trunc) trunc = 65;
379 args[PARAM_OFFSET] = 0;
380 args[PARAM_TERM] = string;
381 args[PARAM_STYPE] = type;
382 linknode.appendChild(text(normalize(truncate(string, trunc))));
383 linknode.setAttribute("href", buildOPACLink(args));
387 /* ----------------------------------------------------------------------- */
388 /* user session handling */
389 /* ----------------------------------------------------------------------- */
390 /* session is the login session. If no session is provided, we attempt
391 to find one in the cookies. If 'force' is true we retrieve the
392 user from the server even if there is already a global user present.
393 if ses != G.user.session, we also force a grab */
394 function grabUser(ses, force) {
396 if(!ses && isXUL()) ses = xulG['authtoken'];
399 ses = cookieManager.read(COOKIE_SES);
400 /* https cookies don't show up in http servers.. */
403 if(!ses) return false;
406 if(G.user && G.user.session == ses)
409 /* first make sure the session is valid */
410 var request = new Request(FETCH_SESSION, ses, 1 );
411 request.request.alertEvent = false;
413 var user = request.result();
417 return false; /* unable to grab the session */
420 if( !(typeof user == 'object' && user._isfieldmapper) ) {
426 G.user.fleshed = false;
427 G.user.session = ses;
428 cookieManager.write(COOKIE_SES, ses, '+1d');
432 if(G.user.prefs['opac.hits_per_page'])
433 COUNT = parseInt(G.user.prefs['opac.hits_per_page']);
435 if(G.user.prefs[PREF_DEF_FONT])
436 setFontSize(G.user.prefs[PREF_DEF_FONT]);
438 var at = getAuthtime();
439 //if(isXUL()) at = xulG['authtime'];
441 if(at && !isXUL()) new AuthTimer(at).run();
446 /* sets the 'prefs' field of the user object to their preferences
447 and returns the preferences */
448 function grabUserPrefs(user, force) {
449 if(user == null) user = G.user;
450 if(!force && user.prefs) return user.prefs;
451 var req = new Request(FETCH_USER_PREFS, user.session, user.id());
453 user.prefs = req.result();
457 function grabFleshedUser() {
459 if(!G.user || !G.user.session) {
461 if(!G.user || !G.user.session) return null;
464 if(G.user.fleshed) return G.user;
466 var req = new Request(FETCH_FLESHED_USER, G.user.session);
469 G.user = req.result();
471 if(!G.user || G.user.length == 0) {
472 G.user = null; return false;
473 cookieManager.write(COOKIE_SES,"");
476 G.user.session = ses;
477 G.user.fleshed = true;
479 cookieManager.write(COOKIE_SES, ses, '+1y'); /* update the cookie */
483 function checkUserSkin(new_skin) {
485 return; /* XXX do some debugging with this... */
487 var user_skin = getSkin();
488 var cur_skin = grabSkinFromURL();
490 if(new_skin) user_skin = new_skin;
495 if(grabUserPrefs()) {
496 user_skin = G.user.prefs["opac.skin"];
497 cookieManager.write( COOKIE_SKIN, user_skin, '+1y' );
502 if(!user_skin) return;
504 if( cur_skin != user_skin ) {
505 var url = buildOPACLink();
506 goTo(url.replace(cur_skin, user_skin));
510 function updateUserSetting(setting, value, user) {
511 if(user == null) user = G.user;
514 var req = new Request( UPDATE_USER_PREFS, user.session, a );
519 function commitUserPrefs() {
520 var req = new Request(
521 UPDATE_USER_PREFS, G.user.session, null, G.user.prefs );
526 function grabSkinFromURL() {
527 var path = findBasePath();
528 path = path.replace("/xml/", "");
530 for( var i = path.length - 1; i >= 0; i-- ) {
531 var ch = path.charAt(i);
537 for( i = skin.length - 1; i >= 0; i--)
538 skin2 += skin.charAt(i);
544 /* returns a fleshed G.user on success, false on failure */
545 function doLogin(suppressEvents) {
549 var uname = G.ui.login.username.value;
550 var passwd = G.ui.login.password.value;
552 var init_request = new Request( LOGIN_INIT, uname );
553 init_request.send(true);
554 var seed = init_request.result();
556 if( ! seed || seed == '0') {
557 alert( "Error Communicating with Authentication Server" );
562 password : hex_md5(seed + hex_md5(passwd)),
564 org : getOrigLocation()
567 if( uname.match(REGEX_BARCODE) ) args.barcode = uname; /* make this better */
568 else args.username = uname;
570 var auth_request = new Request( LOGIN_COMPLETE, args );
572 auth_request.send(true);
573 var auth_result = auth_request.result();
574 if(!auth_result) return null;
576 AUTHTIME = parseInt(auth_result.payload.authtime);
577 var u = grabUser(auth_result.payload.authtoken, true);
578 if(u && ! suppressEvents)
579 runEvt( "common", "locationChanged", u.home_ou(), findOrgDepth(u.home_ou()) );
586 function doLogout(noredirect) {
588 /* cancel everything else */
591 /* be nice and delete the session from the server */
592 if(G.user && G.user.session) {
593 var req = new Request(LOGIN_DELETE, G.user.session);
595 try { req.result(); } catch(E){}
599 cookieManager.write(COOKIE_SES, "");
600 cookieManager.write(COOKIE_SKIN, "");
601 checkUserSkin("default");
605 args[PARAM_TERM] = "";
606 args[PARAM_LOCATION] = globalOrgTree.id();
607 args[PARAM_DEPTH] = findOrgDepth(globalOrgTree);
611 try{ if(isFrontPage) nored = true; } catch(e){nored = false;}
612 if(!nored) goTo(buildOPACLink(args));
616 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); }
617 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
620 /* ----------------------------------------------------------------------- */
621 /* build the org tree */
622 /* ----------------------------------------------------------------------- */
623 function drawOrgTree() {
624 //setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 10 );
625 setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 1 );
629 function buildOrgSelector(node) {
630 var tree = new SlimTree(node,'orgTreeSelector');
631 orgTreeSelector = tree;
632 for( var i in orgArraySearcher ) {
633 var node = orgArraySearcher[i];
634 if( node == null ) continue;
635 if(node.parent_ou() == null)
636 tree.addNode(node.id(), -1, node.name(),
637 "javascript:orgSelect(" + node.id() + ");", node.name());
639 tree.addNode(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(),
640 "javascript:orgSelect(" + node.id() + ");", node.name());
643 hideMe($('org_loading_div'));
644 unHideMe($('org_selector_tip'));
648 function orgSelect(id) {
650 runEvt("common", "locationChanged", id, findOrgDepth(id) );
652 removeChildren(G.ui.common.now_searching);
653 G.ui.common.now_searching.appendChild(text(findOrgUnit(id).name()));
656 function setFontSize(size) {
658 cookieManager.write(COOKIE_FONT, size, '+1y');
662 var resourceFormats = [
665 "sound recording", "software, multimedia",
670 "three dimensional object" ];
673 function modsFormatToMARC(format) {
679 case "sound recording":
681 case "sound recording-nonmusical":
683 case "sound recording-musical":
685 case "software, multimedia":
691 case "mixed material":
693 case "notated music":
695 case "three dimensional object":
702 function MARCFormatToMods(format) {
708 return "moving image";
710 return "sound recording-nonmusical";
712 return "sound recording-musical";
714 return "software, multimedia";
716 return "still images";
719 return "cartographic";
722 return "mixed material";
725 return "notated music";
727 return "three dimensional object";
732 function setResourcePic( img, resource ) {
733 img.setAttribute( "src", "../../../../images/tor/" + resource + ".jpg");
734 img.title = resource;
739 function msg( text ) {
740 try { alert( text ); } catch(e) {}
743 function findRecord(id,type) {
745 for( var i = 0; i != recordsCache.length; i++ ) {
746 var rec = recordsCache[i];
747 if( rec && rec.doc_id() == id ) return rec;
750 var meth = FETCH_RMODS
751 if(type == 'M') meth = FETCH_MRMODS;
752 var req = new Request(meth, id);
757 function Timer(name, node){
762 Timer.prototype.start =
763 function(){_timerRun(this.name);}
764 Timer.prototype.stop =
765 function(){this.done = true;}
766 function _timerRun(tname) {
770 if(_t.count > 100) return;
772 if( (_t.count % 5) == 0 )
774 _t.node.appendChild(text(str));
775 setTimeout("_timerRun('"+tname+"');", 200);
779 function checkILSEvent(obj) {
780 if( obj && obj.ilsevent != null && obj.ilsevent != 0 )
781 return parseInt(obj.ilsevent);
786 function alertILSEvent(code, msg) {
788 alert( msg + '\n' + $('ilsevent.' + code).innerHTML );
792 function alertILSEvent(evt, msg) {
794 alert(evt.textcode + '\n' + evt.desc );
799 function AuthTimer(time) {
800 this.time = (time - LOGOUT_WARNING_TIME) * 1000;
804 AuthTimer.prototype.run = function() {
805 setTimeout('_authTimerAlert()', this.time);
808 function _authTimerAlert() {
809 alert( $('auth_session_expiring').innerHTML );
810 if(!grabUser(null, true)) doLogout();
814 function grabUserByBarcode( authtoken, barcode ) {
815 var req = new Request( FETCH_USER_BYBARCODE, authtoken, barcode );
822 goTo(buildOPACLink({page:HOME}));
826 function buildOrgSel(selector, org, offset) {
827 insertSelectorVal( selector, -1,
828 org.name(), org.id(), null, findOrgDepth(org) - offset );
829 for( var c in org.children() )
830 buildOrgSel( selector, org.children()[c], offset);
834 function parseForm(form) {
837 var it = form.replace(/-\w+$/,"");
842 if(form.match(/-/)) itf = form.replace(/^\w+-/,"");
846 for( var i = 0; i < it.length; i++ )
847 item_type.push( it.charAt(i) );
852 for( var i = 0; i < itf.length; i++ )
853 item_form.push( itf.charAt(i) );
856 return {item_type: item_type, item_form:item_form};