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);
114 RDEPTH = cgi.param(PARAM_RDEPTH);
116 /* set up some sane defaults */
117 if(isNaN(LOCATION)) LOCATION = 1;
118 if(isNaN(DEPTH)) DEPTH = 0;
119 if(isNaN(RDEPTH)) RDEPTH = 0;
120 if(isNaN(OFFSET)) OFFSET = 0;
121 if(isNaN(COUNT)) COUNT = 10;
122 if(isNaN(HITCOUNT)) HITCOUNT = 0;
123 if(isNaN(MRID)) MRID = 0;
124 if(isNaN(RID)) RID = 0;
125 if(isNaN(ORIGLOC)) ORIGLOC = 1;
126 if(isNaN(AUTHTIME)) AUTHTIME = 0;
127 if(ADVTERM==null) ADVTERM = "";
130 function clearSearchParams() {
155 function initCookies() {
156 FONTSIZE = "regular";
157 var font = cookieManager.read(COOKIE_FONT);
159 if(font) FONTSIZE = font;
160 SKIN = cookieManager.read(COOKIE_SKIN);
163 /* URL param accessors */
164 function getTerm(){return TERM;}
165 function getStype(){return STYPE;}
166 function getLocation(){return LOCATION;}
167 function getDepth(){return DEPTH;}
168 function getForm(){return FORM;}
169 function getTform(){return TFORM;}
170 function getOffset(){return OFFSET;}
171 function getDisplayCount(){return COUNT;}
172 function getHitCount(){return HITCOUNT;}
173 function getMrid(){return MRID;};
174 function getRid(){return RID;};
175 function getOrigLocation(){return ORIGLOC;}
176 function getAuthtime() { return AUTHTIME; }
177 function getSearchBarExtras(){return SBEXTRAS;}
178 function getFontSize(){return FONTSIZE;};
179 function getSkin(){return SKIN;};
180 function getAdvTerm(){return ADVTERM;}
181 function getAdvType(){return ADVTYPE;}
182 function getRtype(){return RTYPE;}
183 function getSort(){return SORT;}
184 function getSortDir(){return SORT_DIR;}
185 function getDebug(){return DEBUG;}
186 function getCallnumber() { return CALLNUM; }
187 function getLitForm() { return LITFORM; }
188 function getItemForm() { return ITEMFORM; }
189 function getItemType() { return ITEMTYPE; }
190 function getAudience() { return AUDIENCE; }
191 function getSearches() { return SEARCHES; }
192 function getLanguage() { return LANGUAGE; }
193 function getRdepth() { return RDEPTH; }
196 function findBasePath() {
197 var path = location.pathname;
198 if(!path.match(/.*\.xml$/)) path += "index.xml";
199 var idx = path.indexOf(config.page[findCurrentPage()]);
200 return path.substring(0, idx);
203 function findBaseURL(ssl) {
204 var path = findBasePath();
205 var proto = (ssl) ? "https:" : "http:";
206 return proto + "//" + location.host + path;
207 dump( 'ssl: ' + ssl + 'proto ' + proto );
211 function buildISBNSrc(isbn) {
212 return "http://" + location.host + "/jackets/" + isbn;
216 function buildImageLink(name, ssl) {
217 return findBaseURL(ssl) + "../../../../images/" + name;
220 function buildExtrasLink(name, ssl) {
221 return findBaseURL(ssl) + "../../../../extras/" + name;
225 function _debug(str) {
226 try { dump('dbg: ' + str + '\n'); } catch(e) {}
228 /* potentially useful, but usually just annoying */
231 if(!consoleService) {
233 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
234 this.consoleService = Components.classes['@mozilla.org/consoleservice;1']
235 .getService(Components.interfaces.nsIConsoleService);
241 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
242 consoleService.logStringMessage(str + '\n');
249 function buildOPACLink(args, slim, ssl) {
255 string = findBaseURL(ssl);
256 if(args.page) string += config.page[args.page];
257 else string += config.page[findCurrentPage()];
262 for( var x in args ) {
264 if(x == "page" || v == null || v == undefined || v+'' == 'NaN' ) continue;
265 if(x == PARAM_OFFSET && v == 0) continue;
266 if(x == PARAM_LOCATION && v == 1) continue;
267 if(x == PARAM_DEPTH && v == 0) continue;
268 if(x == PARAM_COUNT && v == 10) continue;
269 if(x == PARAM_FORM && v == 'all' ) continue;
270 if( instanceOf(v, Array) && v.length ) {
271 for( var i = 0; i < v.length; i++ ) {
272 string += "&" + x + "=" + encodeURIComponent(v[i]);
275 string += "&" + x + "=" + encodeURIComponent(v);
280 string += _appendParam(DEBUG, PARAM_DEBUG, args, getDebug, string);
281 if(getOrigLocation() != 1)
282 string += _appendParam(ORIGLOC, PARAM_ORIGLOC, args, getOrigLocation, string);
284 string += _appendParam(TERM, PARAM_TERM, args, getTerm, string);
286 string += _appendParam(STYPE, PARAM_STYPE, args, getStype, string);
287 if(getLocation() != 1)
288 string += _appendParam(LOCATION, PARAM_LOCATION, args, getLocation, string);
290 string += _appendParam(DEPTH, PARAM_DEPTH, args, getDepth, string);
291 if(getForm() && (getForm() != 'all') )
292 string += _appendParam(FORM, PARAM_FORM, args, getForm, string);
293 if(getTform() && (getTform() != 'all') )
294 string += _appendParam(TFORM, PARAM_TFORM, args, getTform, string);
296 string += _appendParam(OFFSET, PARAM_OFFSET, args, getOffset, string);
297 if(getDisplayCount() != 10)
298 string += _appendParam(COUNT, PARAM_COUNT, args, getDisplayCount, string);
300 string += _appendParam(HITCOUNT, PARAM_HITCOUNT, args, getHitCount, string);
302 string += _appendParam(MRID, PARAM_MRID, args, getMrid, string);
304 string += _appendParam(RID, PARAM_RID, args, getRid, string);
306 string += _appendParam(AUTHTIME, PARAM_AUTHTIME, args, getAuthtime, string);
308 string += _appendParam(ADVTERM, PARAM_ADVTERM, args, getAdvTerm, string);
310 string += _appendParam(ADVTYPE, PARAM_ADVTYPE, args, getAdvType, string);
312 string += _appendParam(RTYPE, PARAM_RTYPE, args, getRtype, string);
314 string += _appendParam(ITEMFORM, PARAM_ITEMFORM, args, getItemForm, string);
316 string += _appendParam(ITEMTYPE, PARAM_ITEMTYPE, args, getItemType, string);
318 string += _appendParam(LITFORM, PARAM_LITFORM, args, getLitForm, string);
320 string += _appendParam(AUDIENCE, PARAM_AUDIENCE, args, getAudience, string);
322 string += _appendParam(SEARCHES, PARAM_SEARCHES, args, getSearches, string);
324 string += _appendParam(LANGUAGE, PARAM_LANGUAGE, args, getLanguage, string);
325 if(getRdepth() != null)
326 string += _appendParam(RDEPTH, PARAM_RDEPTH, args, getRdepth, string);
328 return string.replace(/\&$/,'').replace(/\?\&/,"?");
332 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
337 if( fieldVar != null &&
338 (fieldVar +'' != 'NaN') &&
339 overrideArgs[fieldName] == null &&
343 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
349 /* ----------------------------------------------------------------------- */
350 function cleanISBN(isbn) {
352 isbn = isbn.toString().replace(/^\s+/,"");
353 var idx = isbn.indexOf(" ");
354 if(idx > -1) { isbn = isbn.substring(0, idx); }
360 /* builds a link that goes to the title listings for a metarecord */
361 function buildTitleLink(rec, link) {
363 link.appendChild(text(normalize(truncate(rec.title(), 65))));
366 args[PARAM_OFFSET] = 0;
367 args[PARAM_MRID] = rec.doc_id();
368 args[PARAM_RTYPE] = RTYPE_MRID;
369 link.setAttribute("href", buildOPACLink(args));
372 function buildTitleDetailLink(rec, link) {
374 link.appendChild(text(normalize(truncate(rec.title(), 65))));
377 args[PARAM_OFFSET] = 0;
378 args[PARAM_RID] = rec.doc_id();
379 link.setAttribute("href", buildOPACLink(args));
382 /* 'type' is one of STYPE_AUTHOR, STYPE_SUBJECT, ... found in config.js
383 'trunc' is the number of characters to show in the string, defaults to 65 */
384 function buildSearchLink(type, string, linknode, trunc) {
385 if(!trunc) trunc = 65;
388 args[PARAM_OFFSET] = 0;
389 args[PARAM_TERM] = string;
390 args[PARAM_STYPE] = type;
391 linknode.appendChild(text(normalize(truncate(string, trunc))));
392 linknode.setAttribute("href", buildOPACLink(args));
396 /* ----------------------------------------------------------------------- */
397 /* user session handling */
398 /* ----------------------------------------------------------------------- */
399 /* session is the login session. If no session is provided, we attempt
400 to find one in the cookies. If 'force' is true we retrieve the
401 user from the server even if there is already a global user present.
402 if ses != G.user.session, we also force a grab */
403 function grabUser(ses, force) {
405 if(!ses && isXUL()) ses = xulG['authtoken'];
408 ses = cookieManager.read(COOKIE_SES);
409 /* https cookies don't show up in http servers.. */
412 if(!ses) return false;
415 if(G.user && G.user.session == ses)
418 /* first make sure the session is valid */
419 var request = new Request(FETCH_SESSION, ses, 1 );
420 request.request.alertEvent = false;
422 var user = request.result();
426 return false; /* unable to grab the session */
429 if( !(typeof user == 'object' && user._isfieldmapper) ) {
435 G.user.fleshed = false;
436 G.user.session = ses;
437 cookieManager.write(COOKIE_SES, ses, '+1d');
441 if(G.user.prefs['opac.hits_per_page'])
442 COUNT = parseInt(G.user.prefs['opac.hits_per_page']);
444 if(G.user.prefs[PREF_DEF_FONT])
445 setFontSize(G.user.prefs[PREF_DEF_FONT]);
447 var at = getAuthtime();
448 //if(isXUL()) at = xulG['authtime'];
450 if(at && !isXUL()) new AuthTimer(at).run();
455 /* sets the 'prefs' field of the user object to their preferences
456 and returns the preferences */
457 function grabUserPrefs(user, force) {
458 if(user == null) user = G.user;
459 if(!force && user.prefs) return user.prefs;
460 var req = new Request(FETCH_USER_PREFS, user.session, user.id());
462 user.prefs = req.result();
466 function grabFleshedUser() {
468 if(!G.user || !G.user.session) {
470 if(!G.user || !G.user.session) return null;
473 if(G.user.fleshed) return G.user;
475 var req = new Request(FETCH_FLESHED_USER, G.user.session);
478 G.user = req.result();
480 if(!G.user || G.user.length == 0) {
481 G.user = null; return false;
482 cookieManager.write(COOKIE_SES,"");
485 G.user.session = ses;
486 G.user.fleshed = true;
488 cookieManager.write(COOKIE_SES, ses, '+1y'); /* update the cookie */
492 function checkUserSkin(new_skin) {
494 return; /* XXX do some debugging with this... */
496 var user_skin = getSkin();
497 var cur_skin = grabSkinFromURL();
499 if(new_skin) user_skin = new_skin;
504 if(grabUserPrefs()) {
505 user_skin = G.user.prefs["opac.skin"];
506 cookieManager.write( COOKIE_SKIN, user_skin, '+1y' );
511 if(!user_skin) return;
513 if( cur_skin != user_skin ) {
514 var url = buildOPACLink();
515 goTo(url.replace(cur_skin, user_skin));
519 function updateUserSetting(setting, value, user) {
520 if(user == null) user = G.user;
523 var req = new Request( UPDATE_USER_PREFS, user.session, a );
528 function commitUserPrefs() {
529 var req = new Request(
530 UPDATE_USER_PREFS, G.user.session, null, G.user.prefs );
535 function grabSkinFromURL() {
536 var path = findBasePath();
537 path = path.replace("/xml/", "");
539 for( var i = path.length - 1; i >= 0; i-- ) {
540 var ch = path.charAt(i);
546 for( i = skin.length - 1; i >= 0; i--)
547 skin2 += skin.charAt(i);
553 /* returns a fleshed G.user on success, false on failure */
554 function doLogin(suppressEvents) {
558 var uname = G.ui.login.username.value;
559 var passwd = G.ui.login.password.value;
561 var init_request = new Request( LOGIN_INIT, uname );
562 init_request.send(true);
563 var seed = init_request.result();
565 if( ! seed || seed == '0') {
566 alert( "Error Communicating with Authentication Server" );
571 password : hex_md5(seed + hex_md5(passwd)),
573 org : getOrigLocation()
576 if( uname.match(REGEX_BARCODE) ) args.barcode = uname; /* make this better */
577 else args.username = uname;
579 var auth_request = new Request( LOGIN_COMPLETE, args );
581 auth_request.send(true);
582 var auth_result = auth_request.result();
583 if(!auth_result) return null;
585 AUTHTIME = parseInt(auth_result.payload.authtime);
586 var u = grabUser(auth_result.payload.authtoken, true);
587 if(u && ! suppressEvents)
588 runEvt( "common", "locationChanged", u.home_ou(), findOrgDepth(u.home_ou()) );
595 function doLogout(noredirect) {
597 /* cancel everything else */
600 /* be nice and delete the session from the server */
601 if(G.user && G.user.session) {
602 var req = new Request(LOGIN_DELETE, G.user.session);
604 try { req.result(); } catch(E){}
608 cookieManager.write(COOKIE_SES, "");
609 cookieManager.write(COOKIE_SKIN, "");
610 checkUserSkin("default");
614 args[PARAM_TERM] = "";
615 args[PARAM_LOCATION] = globalOrgTree.id();
616 args[PARAM_DEPTH] = findOrgDepth(globalOrgTree);
620 try{ if(isFrontPage) nored = true; } catch(e){nored = false;}
621 if(!nored) goTo(buildOPACLink(args));
625 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); }
626 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
629 /* ----------------------------------------------------------------------- */
630 /* build the org tree */
631 /* ----------------------------------------------------------------------- */
632 function drawOrgTree() {
633 //setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 10 );
634 setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 1 );
638 function buildOrgSelector(node) {
639 var tree = new SlimTree(node,'orgTreeSelector');
640 orgTreeSelector = tree;
641 for( var i in orgArraySearcher ) {
642 var node = orgArraySearcher[i];
643 if( node == null ) continue;
644 if(node.parent_ou() == null)
645 tree.addNode(node.id(), -1, node.name(),
646 "javascript:orgSelect(" + node.id() + ");", node.name());
648 tree.addNode(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(),
649 "javascript:orgSelect(" + node.id() + ");", node.name());
652 hideMe($('org_loading_div'));
653 unHideMe($('org_selector_tip'));
657 function orgSelect(id) {
659 runEvt("common", "locationChanged", id, findOrgDepth(id) );
661 removeChildren(G.ui.common.now_searching);
662 G.ui.common.now_searching.appendChild(text(findOrgUnit(id).name()));
665 function setFontSize(size) {
667 cookieManager.write(COOKIE_FONT, size, '+1y');
671 var resourceFormats = [
674 "sound recording", "software, multimedia",
679 "three dimensional object" ];
682 function modsFormatToMARC(format) {
688 case "sound recording":
690 case "sound recording-nonmusical":
692 case "sound recording-musical":
694 case "software, multimedia":
700 case "mixed material":
702 case "notated music":
704 case "three dimensional object":
711 function MARCFormatToMods(format) {
717 return "moving image";
719 return "sound recording-nonmusical";
721 return "sound recording-musical";
723 return "software, multimedia";
725 return "still images";
728 return "cartographic";
731 return "mixed material";
734 return "notated music";
736 return "three dimensional object";
741 function setResourcePic( img, resource ) {
742 img.setAttribute( "src", "../../../../images/tor/" + resource + ".jpg");
743 img.title = resource;
748 function msg( text ) {
749 try { alert( text ); } catch(e) {}
752 function findRecord(id,type) {
754 for( var i = 0; i != recordsCache.length; i++ ) {
755 var rec = recordsCache[i];
756 if( rec && rec.doc_id() == id ) return rec;
759 var meth = FETCH_RMODS
760 if(type == 'M') meth = FETCH_MRMODS;
761 var req = new Request(meth, id);
766 function Timer(name, node){
771 Timer.prototype.start =
772 function(){_timerRun(this.name);}
773 Timer.prototype.stop =
774 function(){this.done = true;}
775 function _timerRun(tname) {
779 if(_t.count > 100) return;
781 if( (_t.count % 5) == 0 )
783 _t.node.appendChild(text(str));
784 setTimeout("_timerRun('"+tname+"');", 200);
788 function checkILSEvent(obj) {
789 if( obj && obj.ilsevent != null && obj.ilsevent != 0 )
790 return parseInt(obj.ilsevent);
795 function alertILSEvent(code, msg) {
797 alert( msg + '\n' + $('ilsevent.' + code).innerHTML );
801 function alertILSEvent(evt, msg) {
803 alert(evt.textcode + '\n' + evt.desc );
808 function AuthTimer(time) {
809 this.time = (time - LOGOUT_WARNING_TIME) * 1000;
813 AuthTimer.prototype.run = function() {
814 setTimeout('_authTimerAlert()', this.time);
817 function _authTimerAlert() {
818 alert( $('auth_session_expiring').innerHTML );
819 if(!grabUser(null, true)) doLogout();
823 function grabUserByBarcode( authtoken, barcode ) {
824 var req = new Request( FETCH_USER_BYBARCODE, authtoken, barcode );
831 goTo(buildOPACLink({page:HOME}));
835 function buildOrgSel(selector, org, offset) {
836 insertSelectorVal( selector, -1,
837 org.name(), org.id(), null, findOrgDepth(org) - offset );
838 for( var c in org.children() )
839 buildOrgSel( selector, org.children()[c], offset);
843 function parseForm(form) {
846 var it = form.replace(/-\w+$/,"");
851 if(form.match(/-/)) itf = form.replace(/^\w+-/,"");
855 for( var i = 0; i < it.length; i++ )
856 item_type.push( it.charAt(i) );
861 for( var i = 0; i < itf.length; i++ )
862 item_form.push( itf.charAt(i) );
865 return {item_type: item_type, item_form:item_form};