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_LOCATION && v == 1) continue;*/
290 if(x == PARAM_DEPTH && v == 0) continue;
291 if(x == PARAM_COUNT && v == 10) continue;
292 if(x == PARAM_FORM && v == 'all' ) continue;
293 if( instanceOf(v, Array) && v.length ) {
294 for( var i = 0; i < v.length; i++ ) {
295 string += "&" + x + "=" + encodeURIComponent(v[i]);
298 string += "&" + x + "=" + encodeURIComponent(v);
303 string += _appendParam(DEBUG, PARAM_DEBUG, args, getDebug, string);
304 if(getOrigLocation() != 1)
305 string += _appendParam(ORIGLOC, PARAM_ORIGLOC, args, getOrigLocation, string);
307 string += _appendParam(TERM, PARAM_TERM, args, getTerm, string);
309 string += _appendParam(STYPE, PARAM_STYPE, args, getStype, string);
310 if(getLocation() != 1)
311 string += _appendParam(LOCATION, PARAM_LOCATION, args, getLocation, string);
313 string += _appendParam(DEPTH, PARAM_DEPTH, args, getDepth, string);
314 if(getForm() && (getForm() != 'all') )
315 string += _appendParam(FORM, PARAM_FORM, args, getForm, string);
316 if(getTform() && (getTform() != 'all') )
317 string += _appendParam(TFORM, PARAM_TFORM, args, getTform, string);
319 string += _appendParam(OFFSET, PARAM_OFFSET, args, getOffset, string);
320 if(getDisplayCount() != 10)
321 string += _appendParam(COUNT, PARAM_COUNT, args, getDisplayCount, string);
323 string += _appendParam(HITCOUNT, PARAM_HITCOUNT, args, getHitCount, string);
325 string += _appendParam(MRID, PARAM_MRID, args, getMrid, string);
327 string += _appendParam(RID, PARAM_RID, args, getRid, string);
329 string += _appendParam(AUTHTIME, PARAM_AUTHTIME, args, getAuthtime, string);
331 string += _appendParam(ADVTERM, PARAM_ADVTERM, args, getAdvTerm, string);
333 string += _appendParam(ADVTYPE, PARAM_ADVTYPE, args, getAdvType, string);
335 string += _appendParam(RTYPE, PARAM_RTYPE, args, getRtype, string);
337 string += _appendParam(ITEMFORM, PARAM_ITEMFORM, args, getItemForm, string);
339 string += _appendParam(ITEMTYPE, PARAM_ITEMTYPE, args, getItemType, string);
341 string += _appendParam(LITFORM, PARAM_LITFORM, args, getLitForm, string);
343 string += _appendParam(AUDIENCE, PARAM_AUDIENCE, args, getAudience, string);
345 string += _appendParam(SEARCHES, PARAM_SEARCHES, args, getSearches, string);
347 string += _appendParam(LANGUAGE, PARAM_LANGUAGE, args, getLanguage, string);
348 if(getRdepth() != null)
349 string += _appendParam(RDEPTH, PARAM_RDEPTH, args, getRdepth, string);
350 if(getSort() != null)
351 string += _appendParam(SORT, PARAM_SORT, args, getSort, string);
352 if(getSortDir() != null)
353 string += _appendParam(SORT_DIR, PARAM_SORT_DIR, args, getSortDir, string);
355 return string.replace(/\&$/,'').replace(/\?\&/,"?");
359 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
364 if( fieldVar != null &&
365 (fieldVar +'' != 'NaN') &&
366 overrideArgs[fieldName] == null &&
370 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
376 /* ----------------------------------------------------------------------- */
377 function cleanISBN(isbn) {
379 isbn = isbn.toString().replace(/^\s+/,"");
380 var idx = isbn.indexOf(" ");
381 if(idx > -1) { isbn = isbn.substring(0, idx); }
387 /* builds a link that goes to the title listings for a metarecord */
388 function buildTitleLink(rec, link) {
390 link.appendChild(text(normalize(truncate(rec.title(), 65))));
393 args[PARAM_OFFSET] = 0;
394 args[PARAM_MRID] = rec.doc_id();
395 args[PARAM_RTYPE] = RTYPE_MRID;
396 link.setAttribute("href", buildOPACLink(args));
399 function buildTitleDetailLink(rec, link) {
401 link.appendChild(text(normalize(truncate(rec.title(), 65))));
404 //args[PARAM_OFFSET] = 0;
405 args[PARAM_RID] = rec.doc_id();
406 link.setAttribute("href", buildOPACLink(args));
409 /* 'type' is one of STYPE_AUTHOR, STYPE_SUBJECT, ... found in config.js
410 'trunc' is the number of characters to show in the string, defaults to 65 */
411 function buildSearchLink(type, string, linknode, trunc) {
412 if(!trunc) trunc = 65;
415 args[PARAM_OFFSET] = 0;
416 args[PARAM_TERM] = string;
417 args[PARAM_STYPE] = type;
418 linknode.appendChild(text(normalize(truncate(string, trunc))));
419 linknode.setAttribute("href", buildOPACLink(args));
423 /* ----------------------------------------------------------------------- */
424 /* user session handling */
425 /* ----------------------------------------------------------------------- */
426 /* session is the login session. If no session is provided, we attempt
427 to find one in the cookies. If 'force' is true we retrieve the
428 user from the server even if there is already a global user present.
429 if ses != G.user.session, we also force a grab */
430 function grabUser(ses, force) {
432 if(!ses && isXUL()) ses = xulG['authtoken'];
435 ses = cookieManager.read(COOKIE_SES);
436 /* https cookies don't show up in http servers.. */
439 if(!ses) return false;
442 if(G.user && G.user.session == ses)
445 /* first make sure the session is valid */
446 var request = new Request(FETCH_SESSION, ses, 1 );
447 request.request.alertEvent = false;
449 var user = request.result();
453 return false; /* unable to grab the session */
456 if( !(typeof user == 'object' && user._isfieldmapper) ) {
462 G.user.fleshed = false;
463 G.user.session = ses;
464 cookieManager.write(COOKIE_SES, ses, '+1d');
468 if(G.user.prefs['opac.hits_per_page'])
469 COUNT = parseInt(G.user.prefs['opac.hits_per_page']);
471 if(G.user.prefs[PREF_DEF_FONT])
472 setFontSize(G.user.prefs[PREF_DEF_FONT]);
474 var at = getAuthtime();
475 //if(isXUL()) at = xulG['authtime'];
477 if(at && !isXUL()) new AuthTimer(at).run();
482 /* sets the 'prefs' field of the user object to their preferences
483 and returns the preferences */
484 function grabUserPrefs(user, force) {
485 if(user == null) user = G.user;
486 if(!force && user.prefs) return user.prefs;
487 var req = new Request(FETCH_USER_PREFS, user.session, user.id());
489 user.prefs = req.result();
493 function grabFleshedUser() {
495 if(!G.user || !G.user.session) {
497 if(!G.user || !G.user.session) return null;
500 if(G.user.fleshed) return G.user;
502 var req = new Request(FETCH_FLESHED_USER, G.user.session);
505 G.user = req.result();
507 if(!G.user || G.user.length == 0) {
508 G.user = null; return false;
509 cookieManager.write(COOKIE_SES,"");
512 G.user.session = ses;
513 G.user.fleshed = true;
515 cookieManager.write(COOKIE_SES, ses, '+1y'); /* update the cookie */
519 function checkUserSkin(new_skin) {
521 return; /* XXX do some debugging with this... */
523 var user_skin = getSkin();
524 var cur_skin = grabSkinFromURL();
526 if(new_skin) user_skin = new_skin;
531 if(grabUserPrefs()) {
532 user_skin = G.user.prefs["opac.skin"];
533 cookieManager.write( COOKIE_SKIN, user_skin, '+1y' );
538 if(!user_skin) return;
540 if( cur_skin != user_skin ) {
541 var url = buildOPACLink();
542 goTo(url.replace(cur_skin, user_skin));
546 function updateUserSetting(setting, value, user) {
547 if(user == null) user = G.user;
550 var req = new Request( UPDATE_USER_PREFS, user.session, a );
555 function commitUserPrefs() {
556 var req = new Request(
557 UPDATE_USER_PREFS, G.user.session, null, G.user.prefs );
562 function grabSkinFromURL() {
563 var path = findBasePath();
564 path = path.replace("/xml/", "");
566 for( var i = path.length - 1; i >= 0; i-- ) {
567 var ch = path.charAt(i);
573 for( i = skin.length - 1; i >= 0; i--)
574 skin2 += skin.charAt(i);
580 /* returns a fleshed G.user on success, false on failure */
581 function doLogin(suppressEvents) {
585 var uname = G.ui.login.username.value;
586 var passwd = G.ui.login.password.value;
588 var init_request = new Request( LOGIN_INIT, uname );
589 init_request.send(true);
590 var seed = init_request.result();
592 if( ! seed || seed == '0') {
593 alert( "Error Communicating with Authentication Server" );
598 password : hex_md5(seed + hex_md5(passwd)),
600 org : getOrigLocation()
603 if( uname.match(REGEX_BARCODE) ) args.barcode = uname; /* make this better */
604 else args.username = uname;
606 var auth_request = new Request( LOGIN_COMPLETE, args );
608 auth_request.request.alertEvent = false;
609 auth_request.send(true);
610 var auth_result = auth_request.result();
613 alertId('patron_login_failed');
617 if( checkILSEvent(auth_result) ) {
619 if( auth_result.textcode == 'PATRON_INACTIVE' ) {
620 alertId('patron_inactive_alert');
624 if( auth_result.textcode == 'PATRON_CARD_INACTIVE' ) {
625 alertId('patron_card_inactive_alert');
629 if( auth_result.textcode == 'LOGIN_FAILED' ||
630 auth_result.textcode == 'PERM_FAILURE' ) {
631 alertId('patron_login_failed');
637 AUTHTIME = parseInt(auth_result.payload.authtime);
638 var u = grabUser(auth_result.payload.authtoken, true);
639 if(u && ! suppressEvents)
640 runEvt( "common", "locationChanged", u.ws_ou(), findOrgDepth(u.ws_ou()) );
647 function doLogout(noredirect) {
649 /* cancel everything else */
652 /* be nice and delete the session from the server */
653 if(G.user && G.user.session) {
654 var req = new Request(LOGIN_DELETE, G.user.session);
656 try { req.result(); } catch(E){}
661 /* remove any cached data */
662 //cookieManager.remove(COOKIE_IDS);
663 //cookieManager.remove(COOKIE_SRIDS);
664 cookieManager.remove(COOKIE_RIDS);
665 cookieManager.remove(COOKIE_SES);
666 cookieManager.remove(COOKIE_SKIN);
668 checkUserSkin("default");
673 args[PARAM_TERM] = "";
674 args[PARAM_LOCATION] = getOrigLocation();
675 args[PARAM_DEPTH] = findOrgDepth(getOrigLocation());
680 try{ if(isFrontPage) nored = true; } catch(e){nored = false;}
681 if(!nored) goTo(buildOPACLink(args));
685 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); }
686 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
689 /* ----------------------------------------------------------------------- */
690 /* build the org tree */
691 /* ----------------------------------------------------------------------- */
692 function drawOrgTree() {
693 //setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 10 );
694 setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 1 );
698 function buildOrgSelector(node) {
699 var tree = new SlimTree(node,'orgTreeSelector');
700 orgTreeSelector = tree;
701 for( var i in orgArraySearcher ) {
702 var node = orgArraySearcher[i];
703 if( node == null ) continue;
704 if(node.parent_ou() == null)
705 tree.addNode(node.id(), -1, node.name(),
706 "javascript:orgSelect(" + node.id() + ");", node.name());
708 tree.addNode(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(),
709 "javascript:orgSelect(" + node.id() + ");", node.name());
712 hideMe($('org_loading_div'));
713 unHideMe($('org_selector_tip'));
717 function orgSelect(id) {
719 runEvt("common", "locationChanged", id, findOrgDepth(id) );
721 removeChildren(G.ui.common.now_searching);
722 G.ui.common.now_searching.appendChild(text(findOrgUnit(id).name()));
725 function setFontSize(size) {
727 cookieManager.write(COOKIE_FONT, size, '+1y');
731 var resourceFormats = [
734 "sound recording", "software, multimedia",
739 "three dimensional object" ];
742 function modsFormatToMARC(format) {
748 case "sound recording":
750 case "sound recording-nonmusical":
752 case "sound recording-musical":
754 case "software, multimedia":
760 case "mixed material":
762 case "notated music":
764 case "three dimensional object":
771 function MARCFormatToMods(format) {
777 return "moving image";
779 return "sound recording-nonmusical";
781 return "sound recording-musical";
783 return "software, multimedia";
785 return "still images";
788 return "cartographic";
791 return "mixed material";
794 return "notated music";
796 return "three dimensional object";
801 function MARCTypeToFriendly(format) {
802 var words = $('format_words');
805 case 't' : return $n(words, 'at').innerHTML;
807 var node = $n(words,format);
808 if( node ) return node.innerHTML;
813 function setResourcePic( img, resource ) {
814 img.setAttribute( "src", "../../../../images/tor/" + resource + ".jpg");
815 img.title = resource;
820 function msg( text ) {
821 try { alert( text ); } catch(e) {}
824 function findRecord(id,type) {
826 for( var i = 0; i != recordsCache.length; i++ ) {
827 var rec = recordsCache[i];
828 if( rec && rec.doc_id() == id ) return rec;
831 var meth = FETCH_RMODS
832 if(type == 'M') meth = FETCH_MRMODS;
833 var req = new Request(meth, id);
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();