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);
114 /* set up some sane defaults */
115 if(isNaN(LOCATION)) LOCATION = 1;
116 if(isNaN(DEPTH)) DEPTH = 0;
117 if(isNaN(OFFSET)) OFFSET = 0;
118 if(isNaN(COUNT)) COUNT = 10;
119 if(isNaN(HITCOUNT)) HITCOUNT = 0;
120 if(isNaN(MRID)) MRID = 0;
121 if(isNaN(RID)) RID = 0;
122 if(isNaN(ORIGLOC)) ORIGLOC = 1;
123 if(isNaN(AUTHTIME)) AUTHTIME = 0;
124 if(ADVTERM==null) ADVTERM = "";
127 function initCookies() {
128 FONTSIZE = "regular";
129 var font = cookieManager.read(COOKIE_FONT);
131 if(font) FONTSIZE = font;
132 SKIN = cookieManager.read(COOKIE_SKIN);
135 /* URL param accessors */
136 function getTerm(){return TERM;}
137 function getStype(){return STYPE;}
138 function getLocation(){return LOCATION;}
139 function getDepth(){return DEPTH;}
140 function getForm(){return FORM;}
141 function getOffset(){return OFFSET;}
142 function getDisplayCount(){return COUNT;}
143 function getHitCount(){return HITCOUNT;}
144 function getMrid(){return MRID;};
145 function getRid(){return RID;};
146 function getOrigLocation(){return ORIGLOC;}
147 function getAuthtime() { return AUTHTIME; }
148 function getSearchBarExtras(){return SBEXTRAS;}
149 function getFontSize(){return FONTSIZE;};
150 function getSkin(){return SKIN;};
151 function getAdvTerm(){return ADVTERM;}
152 function getAdvType(){return ADVTYPE;}
153 function getRtype(){return RTYPE;}
154 function getSort(){return SORT;}
155 function getSortDir(){return SORT_DIR;}
156 function getDebug(){return DEBUG;}
157 function getCallnumber() { return CALLNUM; }
158 function getLitForm() { return LITFORM; }
159 function getItemForm() { return ITEMFORM; }
160 function getItemType() { return ITEMTYPE; }
161 function getAudience() { return AUDIENCE; }
162 function getSearches() { return SEARCHES; }
163 function getLanguage() { return LANGUAGE; }
166 function findBasePath() {
167 var path = location.pathname;
168 if(!path.match(/.*\.xml$/)) path += "index.xml";
169 var idx = path.indexOf(config.page[findCurrentPage()]);
170 return path.substring(0, idx);
173 function findBaseURL(ssl) {
174 var path = findBasePath();
175 var proto = (ssl) ? "https:" : "http:";
176 return proto + "//" + location.host + path;
177 dump( 'ssl: ' + ssl + 'proto ' + proto );
181 function buildISBNSrc(isbn) {
182 return "http://" + location.host + "/jackets/" + isbn;
186 function buildImageLink(name, ssl) {
187 return findBaseURL(ssl) + "../../../../images/" + name;
190 function buildExtrasLink(name, ssl) {
191 return findBaseURL(ssl) + "../../../../extras/" + name;
195 function _debug(str) {
196 try { dump(str + '\n'); } catch(e) {}
198 /* potentially useful, but usually just annoying */
201 if(!consoleService) {
203 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
204 this.consoleService = Components.classes['@mozilla.org/consoleservice;1']
205 .getService(Components.interfaces.nsIConsoleService);
211 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
212 consoleService.logStringMessage(str + '\n');
219 function buildOPACLink(args, slim, ssl) {
225 string = findBaseURL(ssl);
226 if(args.page) string += config.page[args.page];
227 else string += config.page[findCurrentPage()];
232 for( var x in args ) {
234 if(x == "page" || v == null || v == undefined || v+'' == 'NaN' ) continue;
235 if(x == PARAM_OFFSET && v == 0) continue;
236 if(x == PARAM_LOCATION && v == 1) continue;
237 if(x == PARAM_DEPTH && v == 0) continue;
238 if(x == PARAM_COUNT && v == 10) continue;
239 if(x == PARAM_FORM && v == 'all' ) continue;
240 if( instanceOf(v, Array) && v.length ) {
241 for( var i = 0; i < v.length; i++ ) {
242 string += "&" + x + "=" + encodeURIComponent(v[i]);
245 string += "&" + x + "=" + encodeURIComponent(v);
250 string += _appendParam(DEBUG, PARAM_DEBUG, args, getDebug, string);
251 if(getOrigLocation() != 1)
252 string += _appendParam(ORIGLOC, PARAM_ORIGLOC, args, getOrigLocation, string);
254 string += _appendParam(TERM, PARAM_TERM, args, getTerm, string);
256 string += _appendParam(STYPE, PARAM_STYPE, args, getStype, string);
257 if(getLocation() != 1)
258 string += _appendParam(LOCATION, PARAM_LOCATION, args, getLocation, string);
260 string += _appendParam(DEPTH, PARAM_DEPTH, args, getDepth, string);
261 if(getForm() && (getForm() != 'all') )
262 string += _appendParam(FORM, PARAM_FORM, args, getForm, string);
264 string += _appendParam(OFFSET, PARAM_OFFSET, args, getOffset, string);
265 if(getDisplayCount() != 10)
266 string += _appendParam(COUNT, PARAM_COUNT, args, getDisplayCount, string);
268 string += _appendParam(HITCOUNT, PARAM_HITCOUNT, args, getHitCount, string);
270 string += _appendParam(MRID, PARAM_MRID, args, getMrid, string);
272 string += _appendParam(RID, PARAM_RID, args, getRid, string);
274 string += _appendParam(AUTHTIME, PARAM_AUTHTIME, args, getAuthtime, string);
276 string += _appendParam(ADVTERM, PARAM_ADVTERM, args, getAdvTerm, string);
278 string += _appendParam(ADVTYPE, PARAM_ADVTYPE, args, getAdvType, string);
280 string += _appendParam(RTYPE, PARAM_RTYPE, args, getRtype, string);
282 string += _appendParam(ITEMFORM, PARAM_ITEMFORM, args, getItemForm, string);
284 string += _appendParam(ITEMTYPE, PARAM_ITEMTYPE, args, getItemType, string);
286 string += _appendParam(LITFORM, PARAM_LITFORM, args, getLitForm, string);
288 string += _appendParam(AUDIENCE, PARAM_AUDIENCE, args, getAudience, string);
290 string += _appendParam(SEARCHES, PARAM_SEARCHES, args, getSearches, string);
292 string += _appendParam(LANGUAGE, PARAM_LANGUAGE, args, getLanguage, string);
294 return string.replace(/\&$/,'').replace(/\?\&/,"?");
297 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
299 if( fieldVar != null && (fieldVar +'' != 'NaN') && overrideArgs[fieldName] == null )
300 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
304 /* ----------------------------------------------------------------------- */
305 function cleanISBN(isbn) {
307 isbn = isbn.toString().replace(/^\s+/,"");
308 var idx = isbn.indexOf(" ");
309 if(idx > -1) { isbn = isbn.substring(0, idx); }
315 /* builds a link that goes to the title listings for a metarecord */
316 function buildTitleLink(rec, link) {
318 link.appendChild(text(normalize(truncate(rec.title(), 65))));
321 args[PARAM_OFFSET] = 0;
322 args[PARAM_MRID] = rec.doc_id();
323 args[PARAM_RTYPE] = RTYPE_MRID;
324 link.setAttribute("href", buildOPACLink(args));
327 function buildTitleDetailLink(rec, link) {
329 link.appendChild(text(normalize(truncate(rec.title(), 65))));
332 args[PARAM_OFFSET] = 0;
333 args[PARAM_RID] = rec.doc_id();
334 link.setAttribute("href", buildOPACLink(args));
337 /* 'type' is one of STYPE_AUTHOR, STYPE_SUBJECT, ... found in config.js
338 'trunc' is the number of characters to show in the string, defaults to 65 */
339 function buildSearchLink(type, string, linknode, trunc) {
340 if(!trunc) trunc = 65;
343 args[PARAM_OFFSET] = 0;
344 args[PARAM_TERM] = string;
345 args[PARAM_STYPE] = type;
346 linknode.appendChild(text(normalize(truncate(string, trunc))));
347 linknode.setAttribute("href", buildOPACLink(args));
351 /* ----------------------------------------------------------------------- */
352 /* user session handling */
353 /* ----------------------------------------------------------------------- */
354 /* session is the login session. If no session is provided, we attempt
355 to find one in the cookies. If 'force' is true we retrieve the
356 user from the server even if there is already a global user present.
357 if ses != G.user.session, we also force a grab */
358 function grabUser(ses, force) {
360 if(!ses && isXUL()) ses = xulG['authtoken'];
363 ses = cookieManager.read(COOKIE_SES);
364 /* https cookies don't show up in http servers.. */
367 if(!ses) return false;
370 if(G.user && G.user.session == ses)
373 /* first make sure the session is valid */
374 var request = new Request(FETCH_SESSION, ses, 1 );
376 var user = request.result();
378 if(checkILSEvent(user)) {
381 return false; /* unable to grab the session */
384 if( !(typeof user == 'object' && user._isfieldmapper) ) {
390 G.user.fleshed = false;
391 G.user.session = ses;
392 cookieManager.write(COOKIE_SES, ses, '+1d');
395 if(G.user.prefs['opac.hits_per_page'])
396 COUNT = parseInt(G.user.prefs['opac.hits_per_page']);
398 if(G.user.prefs[PREF_DEF_FONT])
399 setFontSize(G.user.prefs[PREF_DEF_FONT]);
401 var at = getAuthtime();
402 //if(isXUL()) at = xulG['authtime'];
404 if(at && !isXUL()) new AuthTimer(at).run();
409 /* sets the 'prefs' field of the user object to their preferences
410 and returns the preferences */
411 function grabUserPrefs(user, force) {
412 if(user == null) user = G.user;
413 if(!force && user.prefs) return user.prefs;
414 var req = new Request(FETCH_USER_PREFS, user.session, user.id());
416 user.prefs = req.result();
420 function grabFleshedUser() {
422 if(!G.user || !G.user.session) {
424 if(!G.user || !G.user.session) return null;
427 if(G.user.fleshed) return G.user;
429 var req = new Request(FETCH_FLESHED_USER, G.user.session);
432 G.user = req.result();
434 if(!G.user || G.user.length == 0) {
435 G.user = null; return false;
436 cookieManager.write(COOKIE_SES,"");
439 G.user.session = ses;
440 G.user.fleshed = true;
442 cookieManager.write(COOKIE_SES, ses, '+1y'); /* update the cookie */
446 function checkUserSkin(new_skin) {
448 return; /* XXX do some debugging with this... */
450 var user_skin = getSkin();
451 var cur_skin = grabSkinFromURL();
453 if(new_skin) user_skin = new_skin;
458 if(grabUserPrefs()) {
459 user_skin = G.user.prefs["opac.skin"];
460 cookieManager.write( COOKIE_SKIN, user_skin, '+1y' );
465 if(!user_skin) return;
467 if( cur_skin != user_skin ) {
468 var url = buildOPACLink();
469 goTo(url.replace(cur_skin, user_skin));
473 function updateUserSetting(setting, value, user) {
474 if(user == null) user = G.user;
477 var req = new Request( UPDATE_USER_PREFS, user.session, a );
482 function commitUserPrefs() {
483 var req = new Request(
484 UPDATE_USER_PREFS, G.user.session, null, G.user.prefs );
489 function grabSkinFromURL() {
490 var path = findBasePath();
491 path = path.replace("/xml/", "");
493 for( var i = path.length - 1; i >= 0; i-- ) {
494 var ch = path.charAt(i);
500 for( i = skin.length - 1; i >= 0; i--)
501 skin2 += skin.charAt(i);
507 /* returns a fleshed G.user on success, false on failure */
512 var uname = G.ui.login.username.value;
513 var passwd = G.ui.login.password.value;
515 var init_request = new Request( LOGIN_INIT, uname );
516 init_request.send(true);
517 var seed = init_request.result();
519 if( ! seed || seed == '0') {
520 alert( "Error Communicating with Authentication Server" );
525 password : hex_md5(seed + hex_md5(passwd)),
527 org : getOrigLocation()
530 if( uname.match(REGEX_BARCODE) ) args.barcode = uname; /* make this better */
531 else args.username = uname;
533 var auth_request = new Request( LOGIN_COMPLETE, args );
535 auth_request.send(true);
536 var auth_result = auth_request.result();
538 var code = checkILSEvent(auth_result);
539 if(code) { alertILSEvent(code); return null; }
541 AUTHTIME = parseInt(auth_result.payload.authtime);
542 var u = grabUser(auth_result.payload.authtoken, true);
543 if(u) runEvt( "common", "locationChanged", u.home_ou(), findOrgDepth(u.home_ou()) );
550 function doLogout(noredirect) {
552 /* cancel everything else */
555 /* be nice and delete the session from the server */
556 if(G.user && G.user.session) {
557 var req = new Request(LOGIN_DELETE, G.user.session);
559 try { req.result(); } catch(E){}
563 cookieManager.write(COOKIE_SES, "");
564 cookieManager.write(COOKIE_SKIN, "");
565 checkUserSkin("default");
569 args[PARAM_TERM] = "";
570 args[PARAM_LOCATION] = globalOrgTree.id();
571 args[PARAM_DEPTH] = findOrgDepth(globalOrgTree);
575 try{ if(isFrontPage) nored = true; } catch(e){nored = false;}
576 if(!nored) goTo(buildOPACLink(args));
580 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); }
581 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
584 /* ----------------------------------------------------------------------- */
585 /* build the org tree */
586 /* ----------------------------------------------------------------------- */
587 function drawOrgTree() {
588 //setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 10 );
589 setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 1 );
593 function buildOrgSelector(node) {
594 var tree = new SlimTree(node,'orgTreeSelector');
595 orgTreeSelector = tree;
596 for( var i in orgArraySearcher ) {
597 var node = orgArraySearcher[i];
598 if( node == null ) continue;
599 if(node.parent_ou() == null)
600 tree.addNode(node.id(), -1, node.name(),
601 "javascript:orgSelect(" + node.id() + ");", node.name());
603 tree.addNode(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(),
604 "javascript:orgSelect(" + node.id() + ");", node.name());
607 hideMe($('org_loading_div'));
608 unHideMe($('org_selector_tip'));
612 function orgSelect(id) {
614 runEvt("common", "locationChanged", id, findOrgDepth(id) );
616 removeChildren(G.ui.common.now_searching);
617 G.ui.common.now_searching.appendChild(text(findOrgUnit(id).name()));
620 //var fontCookie = new HTTP.Cookies();
622 function setFontSize(size) {
624 cookieManager.write(COOKIE_FONT, size, '+1y');
628 var resourceFormats = [
631 "sound recording", "software, multimedia",
636 "three dimensional object" ];
639 function modsFormatToMARC(format) {
645 case "sound recording":
647 case "sound recording-nonmusical":
649 case "sound recording-musical":
651 case "software, multimedia":
657 case "mixed material":
659 case "notated music":
661 case "three dimensional object":
668 function MARCFormatToMods(format) {
674 return "moving image";
676 return "sound recording-nonmusical";
678 return "sound recording-musical";
680 return "software, multimedia";
682 return "still images";
685 return "cartographic";
688 return "mixed material";
691 return "notated music";
693 return "three dimensional object";
698 function setResourcePic( img, resource ) {
699 img.setAttribute( "src", "../../../../images/tor/" + resource + ".jpg");
700 img.title = resource;
705 function msg( text ) {
706 try { alert( text ); } catch(e) {}
709 function findRecord(id,type) {
711 for( var i = 0; i != recordsCache.length; i++ ) {
712 var rec = recordsCache[i];
713 if( rec && rec.doc_id() == id ) return rec;
716 var meth = FETCH_RMODS
717 if(type == 'M') meth = FETCH_MRMODS;
718 var req = new Request(meth, id);
723 function Timer(name, node){
728 Timer.prototype.start =
729 function(){_timerRun(this.name);}
730 Timer.prototype.stop =
731 function(){this.done = true;}
732 function _timerRun(tname) {
736 if(_t.count > 100) return;
738 if( (_t.count % 5) == 0 )
740 _t.node.appendChild(text(str));
741 setTimeout("_timerRun('"+tname+"');", 200);
745 function checkILSEvent(obj) {
746 if( obj.ilsevent != null && obj.ilsevent != 0 )
747 return parseInt(obj.ilsevent);
750 function alertILSEvent(code, msg) {
752 alert( msg + '\n' + $('ilsevent.' + code).innerHTML );
757 function AuthTimer(time) {
758 this.time = (time - LOGOUT_WARNING_TIME) * 1000;
762 AuthTimer.prototype.run = function() {
763 setTimeout('_authTimerAlert()', this.time);
766 function _authTimerAlert() {
767 alert( $('auth_session_expiring').innerHTML );
768 if(!grabUser(null, true)) doLogout();
772 function grabUserByBarcode( authtoken, barcode ) {
773 var req = new Request( FETCH_USER_BYBARCODE, authtoken, barcode );
780 goTo(buildOPACLink({page:HOME}));
784 function buildOrgSel(selector, org, offset) {
785 insertSelectorVal( selector, -1,
786 org.name(), org.id(), null, findOrgDepth(org) - offset );
787 for( var c in org.children() )
788 buildOrgSel( selector, org.children()[c], offset);
792 function parseForm(form) {
795 var it = form.replace(/-\w+$/,"");
800 if(form.match(/-/)) itf = form.replace(/^\w+-/,"");
804 for( var i = 0; i < it.length; i++ )
805 item_type.push( it.charAt(i) );
810 for( var i = 0; i < itf.length; i++ )
811 item_form.push( itf.charAt(i) );
814 return {item_type: item_type, item_form:item_form};