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]);
19 if(!location.href.match(/^https:/))
20 this.request.setSecure(false);
23 for( var x = 1; x!= arguments.length; x++ ) {
25 this.request.addParam(arguments[x]);
30 for( var i = 0; i != p.length; i++ ) {
31 if( i > 0 ) str += ", "
34 _debug('request ' + s[0] + ' ' + s[1] + ' ' + str );
38 Request.prototype.callback = function(cal) {this.request.setCompleteCallback(cal);}
39 Request.prototype.send = function(block){this.request.send(block);}
40 Request.prototype.result = function(){return this.request.getResultObject();}
42 function showCanvas() {
43 for( var x in G.ui.altcanvas ) {
44 hideMe(G.ui.altcanvas[x]);
46 hideMe(G.ui.common.loading);
47 unHideMe(G.ui.common.canvas_main);
48 try{G.ui.searchbar.text.focus();}catch(e){}
51 function swapCanvas(newNode) {
52 for( var x in G.ui.altcanvas )
53 hideMe(G.ui.altcanvas[x]);
55 hideMe(G.ui.common.loading);
56 hideMe(G.ui.common.canvas_main);
60 /* finds the name of the current page */
61 var currentPage = null;
62 function findCurrentPage() {
63 if(currentPage) return currentPage;
66 for( var p in config.page ) pages.push(config.page[p]);
67 pages = pages.sort( function(a,b){ return - (a.length - b.length); } );
69 var path = location.pathname;
70 if(!path.match(/.*\.xml$/))
71 path += "index.xml"; /* in case they go to / */
74 for( var p in pages ) {
75 if( path.indexOf(pages[p]) != -1)
79 for( var p in config.page ) {
80 if(config.page[p] == page) {
89 /* sets all of the params values ----------------------------- */
90 function initParams() {
93 /* handle the location var */
95 var loc = cgi.param(PARAM_LOCATION);
97 org = findOrgUnit(loc);
98 if(!org) org = findOrgUnitSN(loc);
100 LOCATION = (org) ? org.id() : null;
103 loc = cgi.param(PARAM_ORIGLOC);
105 org = findOrgUnit(loc);
106 if(!org) org = findOrgUnitSN(loc);
108 ORIGLOC = (org) ? org.id() : null;
111 DEPTH = parseInt(cgi.param(PARAM_DEPTH));
112 if(isNaN(DEPTH)) DEPTH = null;
115 TERM = cgi.param(PARAM_TERM);
116 STYPE = cgi.param(PARAM_STYPE);
117 FORM = cgi.param(PARAM_FORM);
118 //DEPTH = parseInt(cgi.param(PARAM_DEPTH));
119 OFFSET = parseInt(cgi.param(PARAM_OFFSET));
120 COUNT = parseInt(cgi.param(PARAM_COUNT));
121 HITCOUNT = parseInt(cgi.param(PARAM_HITCOUNT));
122 MRID = parseInt(cgi.param(PARAM_MRID));
123 RID = parseInt(cgi.param(PARAM_RID));
124 AUTHTIME = parseInt(cgi.param(PARAM_AUTHTIME));
125 ADVTERM = cgi.param(PARAM_ADVTERM);
126 ADVTYPE = cgi.param(PARAM_ADVTYPE);
127 RTYPE = cgi.param(PARAM_RTYPE);
128 SORT = cgi.param(PARAM_SORT);
129 SORT_DIR = cgi.param(PARAM_SORT_DIR);
130 DEBUG = cgi.param(PARAM_DEBUG);
131 CALLNUM = cgi.param(PARAM_CN);
132 LITFORM = cgi.param(PARAM_LITFORM);
133 ITEMFORM = cgi.param(PARAM_ITEMFORM);
134 ITEMTYPE = cgi.param(PARAM_ITEMTYPE);
135 AUDIENCE = cgi.param(PARAM_AUDIENCE);
136 SEARCHES = cgi.param(PARAM_SEARCHES);
137 LANGUAGE = cgi.param(PARAM_LANGUAGE);
138 TFORM = cgi.param(PARAM_TFORM);
139 RDEPTH = cgi.param(PARAM_RDEPTH);
141 /* set up some sane defaults */
142 //if(isNaN(DEPTH)) DEPTH = 0;
143 if(isNaN(RDEPTH)) RDEPTH = 0;
144 if(isNaN(OFFSET)) OFFSET = 0;
145 if(isNaN(COUNT)) COUNT = 10;
146 if(isNaN(HITCOUNT)) HITCOUNT = 0;
147 if(isNaN(MRID)) MRID = 0;
148 if(isNaN(RID)) RID = 0;
149 if(isNaN(ORIGLOC)) ORIGLOC = 0; /* so we know it hasn't been set */
150 if(isNaN(AUTHTIME)) AUTHTIME = 0;
151 if(ADVTERM==null) ADVTERM = "";
154 function clearSearchParams() {
179 function initCookies() {
180 FONTSIZE = "regular";
181 var font = cookieManager.read(COOKIE_FONT);
183 if(font) FONTSIZE = font;
184 SKIN = cookieManager.read(COOKIE_SKIN);
187 /* URL param accessors */
188 function getTerm(){return TERM;}
189 function getStype(){return STYPE;}
190 function getLocation(){return LOCATION;}
191 function getDepth(){return DEPTH;}
192 function getForm(){return FORM;}
193 function getTform(){return TFORM;}
194 function getOffset(){return OFFSET;}
195 function getDisplayCount(){return COUNT;}
196 function getHitCount(){return HITCOUNT;}
197 function getMrid(){return MRID;};
198 function getRid(){return RID;};
199 function getOrigLocation(){return ORIGLOC;}
200 function getAuthtime() { return AUTHTIME; }
201 function getSearchBarExtras(){return SBEXTRAS;}
202 function getFontSize(){return FONTSIZE;};
203 function getSkin(){return SKIN;};
204 function getAdvTerm(){return ADVTERM;}
205 function getAdvType(){return ADVTYPE;}
206 function getRtype(){return RTYPE;}
207 function getSort(){return SORT;}
208 function getSortDir(){return SORT_DIR;}
209 function getDebug(){return DEBUG;}
210 function getCallnumber() { return CALLNUM; }
211 function getLitForm() { return LITFORM; }
212 function getItemForm() { return ITEMFORM; }
213 function getItemType() { return ITEMTYPE; }
214 function getAudience() { return AUDIENCE; }
215 function getSearches() { return SEARCHES; }
216 function getLanguage() { return LANGUAGE; }
217 function getRdepth() { return RDEPTH; }
220 function findBasePath() {
221 var path = location.pathname;
222 if(!path.match(/.*\.xml$/)) path += "index.xml";
223 var idx = path.indexOf(config.page[findCurrentPage()]);
224 return path.substring(0, idx);
227 function findBaseURL(ssl) {
228 var path = findBasePath();
229 var proto = (ssl) ? "https:" : "http:";
230 return proto + "//" + location.host + path;
234 function buildISBNSrc(isbn) {
235 return "http://" + location.host + "/jackets/" + isbn;
239 function buildImageLink(name, ssl) {
240 return findBaseURL(ssl) + "../../../../images/" + name;
243 function buildExtrasLink(name, ssl) {
244 return findBaseURL(ssl) + "../../../../extras/" + name;
248 function _debug(str) {
249 try { dump('dbg: ' + str + '\n'); } catch(e) {}
251 /* potentially useful, but usually just annoying */
254 if(!consoleService) {
256 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
257 this.consoleService = Components.classes['@mozilla.org/consoleservice;1']
258 .getService(Components.interfaces.nsIConsoleService);
264 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
265 consoleService.logStringMessage(str + '\n');
272 function buildOPACLink(args, slim, ssl) {
278 string = findBaseURL(ssl);
279 if(args.page) string += config.page[args.page];
280 else string += config.page[findCurrentPage()];
285 for( var x in args ) {
287 if(x == "page" || v == null || v == undefined || v+'' == 'NaN' ) continue;
288 if(x == PARAM_OFFSET && v == 0) continue;
289 if(x == PARAM_COUNT && v == 10) continue;
290 if(x == PARAM_FORM && v == 'all' ) continue;
291 if( instanceOf(v, Array) && v.length ) {
292 for( var i = 0; i < v.length; i++ ) {
293 string += "&" + x + "=" + encodeURIComponent(v[i]);
296 string += "&" + x + "=" + encodeURIComponent(v);
301 string += _appendParam(DEBUG, PARAM_DEBUG, args, getDebug, string);
302 if(getOrigLocation() != 1)
303 string += _appendParam(ORIGLOC, PARAM_ORIGLOC, args, getOrigLocation, string);
305 string += _appendParam(TERM, PARAM_TERM, args, getTerm, string);
307 string += _appendParam(STYPE, PARAM_STYPE, args, getStype, string);
308 if(getLocation() != 1)
309 string += _appendParam(LOCATION, PARAM_LOCATION, args, getLocation, string);
310 if(getDepth() != null)
311 string += _appendParam(DEPTH, PARAM_DEPTH, args, getDepth, string);
312 if(getForm() && (getForm() != 'all') )
313 string += _appendParam(FORM, PARAM_FORM, args, getForm, string);
314 if(getTform() && (getTform() != 'all') )
315 string += _appendParam(TFORM, PARAM_TFORM, args, getTform, string);
317 string += _appendParam(OFFSET, PARAM_OFFSET, args, getOffset, string);
318 if(getDisplayCount() != 10)
319 string += _appendParam(COUNT, PARAM_COUNT, args, getDisplayCount, string);
321 string += _appendParam(HITCOUNT, PARAM_HITCOUNT, args, getHitCount, string);
323 string += _appendParam(MRID, PARAM_MRID, args, getMrid, string);
325 string += _appendParam(RID, PARAM_RID, args, getRid, string);
327 string += _appendParam(AUTHTIME, PARAM_AUTHTIME, args, getAuthtime, string);
329 string += _appendParam(ADVTERM, PARAM_ADVTERM, args, getAdvTerm, string);
331 string += _appendParam(ADVTYPE, PARAM_ADVTYPE, args, getAdvType, string);
333 string += _appendParam(RTYPE, PARAM_RTYPE, args, getRtype, string);
335 string += _appendParam(ITEMFORM, PARAM_ITEMFORM, args, getItemForm, string);
337 string += _appendParam(ITEMTYPE, PARAM_ITEMTYPE, args, getItemType, string);
339 string += _appendParam(LITFORM, PARAM_LITFORM, args, getLitForm, string);
341 string += _appendParam(AUDIENCE, PARAM_AUDIENCE, args, getAudience, string);
343 string += _appendParam(SEARCHES, PARAM_SEARCHES, args, getSearches, string);
345 string += _appendParam(LANGUAGE, PARAM_LANGUAGE, args, getLanguage, string);
346 if(getRdepth() != null)
347 string += _appendParam(RDEPTH, PARAM_RDEPTH, args, getRdepth, string);
348 if(getSort() != null)
349 string += _appendParam(SORT, PARAM_SORT, args, getSort, string);
350 if(getSortDir() != null)
351 string += _appendParam(SORT_DIR, PARAM_SORT_DIR, args, getSortDir, string);
353 return string.replace(/\&$/,'').replace(/\?\&/,"?");
357 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
362 if( fieldVar != null &&
363 (fieldVar +'' != 'NaN') &&
364 overrideArgs[fieldName] == null &&
368 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
374 /* ----------------------------------------------------------------------- */
375 function cleanISBN(isbn) {
377 isbn = isbn.toString().replace(/^\s+/,"");
378 var idx = isbn.indexOf(" ");
379 if(idx > -1) { isbn = isbn.substring(0, idx); }
385 /* builds a link that goes to the title listings for a metarecord */
386 function buildTitleLink(rec, link) {
388 link.appendChild(text(normalize(truncate(rec.title(), 65))));
391 args[PARAM_OFFSET] = 0;
392 args[PARAM_MRID] = rec.doc_id();
393 args[PARAM_RTYPE] = RTYPE_MRID;
394 link.setAttribute("href", buildOPACLink(args));
397 function buildTitleDetailLink(rec, link) {
399 link.appendChild(text(normalize(truncate(rec.title(), 65))));
402 //args[PARAM_OFFSET] = 0;
403 args[PARAM_RID] = rec.doc_id();
404 link.setAttribute("href", buildOPACLink(args));
407 /* 'type' is one of STYPE_AUTHOR, STYPE_SUBJECT, ... found in config.js
408 'trunc' is the number of characters to show in the string, defaults to 65 */
409 function buildSearchLink(type, string, linknode, trunc) {
410 if(!trunc) trunc = 65;
413 args[PARAM_OFFSET] = 0;
414 args[PARAM_TERM] = string;
415 args[PARAM_STYPE] = type;
416 linknode.appendChild(text(normalize(truncate(string, trunc))));
417 linknode.setAttribute("href", buildOPACLink(args));
421 /* ----------------------------------------------------------------------- */
422 /* user session handling */
423 /* ----------------------------------------------------------------------- */
424 /* session is the login session. If no session is provided, we attempt
425 to find one in the cookies. If 'force' is true we retrieve the
426 user from the server even if there is already a global user present.
427 if ses != G.user.session, we also force a grab */
428 function grabUser(ses, force) {
430 if(!ses && isXUL()) ses = xulG['authtoken'];
433 ses = cookieManager.read(COOKIE_SES);
434 /* https cookies don't show up in http servers.. */
437 if(!ses) return false;
440 if(G.user && G.user.session == ses)
443 /* first make sure the session is valid */
444 var request = new Request(FETCH_SESSION, ses, 1 );
445 request.request.alertEvent = false;
447 var user = request.result();
451 return false; /* unable to grab the session */
454 if( !(typeof user == 'object' && user._isfieldmapper) ) {
460 G.user.fleshed = false;
461 G.user.session = ses;
462 cookieManager.write(COOKIE_SES, ses, '+1d');
466 if(G.user.prefs['opac.hits_per_page'])
467 COUNT = parseInt(G.user.prefs['opac.hits_per_page']);
469 if(G.user.prefs[PREF_DEF_FONT])
470 setFontSize(G.user.prefs[PREF_DEF_FONT]);
472 var at = getAuthtime();
473 //if(isXUL()) at = xulG['authtime'];
475 if(at && !isXUL()) new AuthTimer(at).run();
480 /* sets the 'prefs' field of the user object to their preferences
481 and returns the preferences */
482 function grabUserPrefs(user, force) {
483 if(user == null) user = G.user;
484 if(!force && user.prefs) return user.prefs;
485 var req = new Request(FETCH_USER_PREFS, user.session, user.id());
487 user.prefs = req.result();
491 function grabFleshedUser() {
493 if(!G.user || !G.user.session) {
495 if(!G.user || !G.user.session) return null;
498 if(G.user.fleshed) return G.user;
500 var req = new Request(FETCH_FLESHED_USER, G.user.session);
503 G.user = req.result();
505 if(!G.user || G.user.length == 0) {
506 G.user = null; return false;
507 cookieManager.write(COOKIE_SES,"");
510 G.user.session = ses;
511 G.user.fleshed = true;
513 cookieManager.write(COOKIE_SES, ses, '+1y'); /* update the cookie */
517 function checkUserSkin(new_skin) {
519 return; /* XXX do some debugging with this... */
521 var user_skin = getSkin();
522 var cur_skin = grabSkinFromURL();
524 if(new_skin) user_skin = new_skin;
529 if(grabUserPrefs()) {
530 user_skin = G.user.prefs["opac.skin"];
531 cookieManager.write( COOKIE_SKIN, user_skin, '+1y' );
536 if(!user_skin) return;
538 if( cur_skin != user_skin ) {
539 var url = buildOPACLink();
540 goTo(url.replace(cur_skin, user_skin));
544 function updateUserSetting(setting, value, user) {
545 if(user == null) user = G.user;
548 var req = new Request( UPDATE_USER_PREFS, user.session, a );
553 function commitUserPrefs() {
554 var req = new Request(
555 UPDATE_USER_PREFS, G.user.session, null, G.user.prefs );
560 function grabSkinFromURL() {
561 var path = findBasePath();
562 path = path.replace("/xml/", "");
564 for( var i = path.length - 1; i >= 0; i-- ) {
565 var ch = path.charAt(i);
571 for( i = skin.length - 1; i >= 0; i--)
572 skin2 += skin.charAt(i);
578 /* returns a fleshed G.user on success, false on failure */
579 function doLogin(suppressEvents) {
583 var uname = G.ui.login.username.value;
584 var passwd = G.ui.login.password.value;
586 var init_request = new Request( LOGIN_INIT, uname );
587 init_request.send(true);
588 var seed = init_request.result();
590 if( ! seed || seed == '0') {
591 alert( "Error Communicating with Authentication Server" );
596 password : hex_md5(seed + hex_md5(passwd)),
598 org : getOrigLocation()
601 if( uname.match(REGEX_BARCODE) ) args.barcode = uname; /* make this better */
602 else args.username = uname;
604 var auth_request = new Request( LOGIN_COMPLETE, args );
606 auth_request.request.alertEvent = false;
607 auth_request.send(true);
608 var auth_result = auth_request.result();
611 alertId('patron_login_failed');
615 if( checkILSEvent(auth_result) ) {
617 if( auth_result.textcode == 'PATRON_INACTIVE' ) {
618 alertId('patron_inactive_alert');
622 if( auth_result.textcode == 'PATRON_CARD_INACTIVE' ) {
623 alertId('patron_card_inactive_alert');
627 if( auth_result.textcode == 'LOGIN_FAILED' ||
628 auth_result.textcode == 'PERM_FAILURE' ) {
629 alertId('patron_login_failed');
635 AUTHTIME = parseInt(auth_result.payload.authtime);
636 var u = grabUser(auth_result.payload.authtoken, true);
637 if(u && ! suppressEvents)
638 runEvt( "common", "locationChanged", u.ws_ou(), findOrgDepth(u.ws_ou()) );
645 function doLogout(noredirect) {
647 /* cancel everything else */
650 /* be nice and delete the session from the server */
651 if(G.user && G.user.session) {
652 var req = new Request(LOGIN_DELETE, G.user.session);
654 try { req.result(); } catch(E){}
659 /* remove any cached data */
660 //cookieManager.remove(COOKIE_IDS);
661 //cookieManager.remove(COOKIE_SRIDS);
662 cookieManager.remove(COOKIE_RIDS);
663 cookieManager.remove(COOKIE_SES);
664 cookieManager.remove(COOKIE_SKIN);
666 checkUserSkin("default");
671 args[PARAM_TERM] = "";
672 args[PARAM_LOCATION] = getOrigLocation();
673 args[PARAM_DEPTH] = findOrgDepth(getOrigLocation());
678 try{ if(isFrontPage) nored = true; } catch(e){nored = false;}
679 if(!nored) goTo(buildOPACLink(args));
683 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); }
684 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
687 /* ----------------------------------------------------------------------- */
688 /* build the org tree */
689 /* ----------------------------------------------------------------------- */
690 function drawOrgTree() {
691 //setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 10 );
692 setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 1 );
696 function buildOrgSelector(node) {
697 var tree = new SlimTree(node,'orgTreeSelector');
698 orgTreeSelector = tree;
699 for( var i in orgArraySearcher ) {
700 var node = orgArraySearcher[i];
701 if( node == null ) continue;
702 if(node.parent_ou() == null)
703 tree.addNode(node.id(), -1, node.name(),
704 "javascript:orgSelect(" + node.id() + ");", node.name());
706 tree.addNode(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(),
707 "javascript:orgSelect(" + node.id() + ");", node.name());
710 hideMe($('org_loading_div'));
711 unHideMe($('org_selector_tip'));
715 function orgSelect(id) {
717 runEvt("common", "locationChanged", id, findOrgDepth(id) );
719 removeChildren(G.ui.common.now_searching);
720 G.ui.common.now_searching.appendChild(text(findOrgUnit(id).name()));
723 function setFontSize(size) {
725 cookieManager.write(COOKIE_FONT, size, '+1y');
729 var resourceFormats = [
732 "sound recording", "software, multimedia",
737 "three dimensional object" ];
740 function modsFormatToMARC(format) {
746 case "sound recording":
748 case "sound recording-nonmusical":
750 case "sound recording-musical":
752 case "software, multimedia":
758 case "mixed material":
760 case "notated music":
762 case "three dimensional object":
769 function MARCFormatToMods(format) {
775 return "moving image";
777 return "sound recording-nonmusical";
779 return "sound recording-musical";
781 return "software, multimedia";
783 return "still images";
786 return "cartographic";
789 return "mixed material";
792 return "notated music";
794 return "three dimensional object";
799 function MARCTypeToFriendly(format) {
800 var words = $('format_words');
803 case 't' : return $n(words, 'at').innerHTML;
805 var node = $n(words,format);
806 if( node ) return node.innerHTML;
811 function setResourcePic( img, resource ) {
812 img.setAttribute( "src", "../../../../images/tor/" + resource + ".jpg");
813 img.title = resource;
818 function msg( text ) {
819 try { alert( text ); } catch(e) {}
822 function findRecord(id,type) {
824 for( var i = 0; i != recordsCache.length; i++ ) {
825 var rec = recordsCache[i];
826 if( rec && rec.doc_id() == id ) return rec;
829 var meth = FETCH_RMODS
830 if(type == 'M') meth = FETCH_MRMODS;
831 var req = new Request(meth, id);
832 req.request.alertEvent = false;
834 var res = req.result();
835 if( checkILSEvent(res) ) return null;
838 function Timer(name, node){
843 Timer.prototype.start =
844 function(){_timerRun(this.name);}
845 Timer.prototype.stop =
846 function(){this.done = true;}
847 function _timerRun(tname) {
851 if(_t.count > 100) return;
853 if( (_t.count % 5) == 0 )
855 _t.node.appendChild(text(str));
856 setTimeout("_timerRun('"+tname+"');", 200);
860 function checkILSEvent(obj) {
861 if( obj && obj.ilsevent != null && obj.ilsevent != 0 )
862 return parseInt(obj.ilsevent);
867 function alertILSEvent(evt, msg) {
870 alert(msg +'\n' + evt.textcode + '\n' + evt.desc );
872 alert(evt.textcode + '\n' + evt.desc );
877 function AuthTimer(time) {
878 this.time = (time - LOGOUT_WARNING_TIME) * 1000;
882 AuthTimer.prototype.run = function() {
883 setTimeout('_authTimerAlert()', this.time);
886 function _authTimerAlert() {
887 alert( $('auth_session_expiring').innerHTML );
888 if(!grabUser(null, true)) doLogout();
892 function grabUserByBarcode( authtoken, barcode ) {
893 var req = new Request( FETCH_USER_BYBARCODE, authtoken, barcode );
900 goTo(buildOPACLink({page:HOME}));
904 function buildOrgSel(selector, org, offset) {
905 insertSelectorVal( selector, -1,
906 org.name(), org.id(), null, findOrgDepth(org) - offset );
907 for( var c in org.children() )
908 buildOrgSel( selector, org.children()[c], offset);
912 function parseForm(form) {
915 var it = form.replace(/-\w+$/,"");
920 if(form.match(/-/)) itf = form.replace(/^\w+-/,"");
924 for( var i = 0; i < it.length; i++ )
925 item_type.push( it.charAt(i) );
930 for( var i = 0; i < itf.length; i++ )
931 item_form.push( itf.charAt(i) );
934 return {item_type: item_type, item_form:item_form};
938 function isTrue(x) { return ( x && x != "0" && !(x+'').match(/^f$/i) ); }
940 function fetchPermOrgs() {
941 var a = []; /* why does arguments come accross as an object and not an array? */
942 for( var i = 0; i < arguments.length; i++ )
945 var preq = new Request(FETCH_HIGHEST_PERM_ORG,
946 G.user.session, G.user.id(), a );
948 return preq.result();