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(ORIGLOC)) ORIGLOC = 0; /* so we know it hasn't been set */
127 if(isNaN(AUTHTIME)) AUTHTIME = 0;
128 if(ADVTERM==null) ADVTERM = "";
131 function clearSearchParams() {
156 function initCookies() {
157 FONTSIZE = "regular";
158 var font = cookieManager.read(COOKIE_FONT);
160 if(font) FONTSIZE = font;
161 SKIN = cookieManager.read(COOKIE_SKIN);
164 /* URL param accessors */
165 function getTerm(){return TERM;}
166 function getStype(){return STYPE;}
167 function getLocation(){return LOCATION;}
168 function getDepth(){return DEPTH;}
169 function getForm(){return FORM;}
170 function getTform(){return TFORM;}
171 function getOffset(){return OFFSET;}
172 function getDisplayCount(){return COUNT;}
173 function getHitCount(){return HITCOUNT;}
174 function getMrid(){return MRID;};
175 function getRid(){return RID;};
176 function getOrigLocation(){return ORIGLOC;}
177 function getAuthtime() { return AUTHTIME; }
178 function getSearchBarExtras(){return SBEXTRAS;}
179 function getFontSize(){return FONTSIZE;};
180 function getSkin(){return SKIN;};
181 function getAdvTerm(){return ADVTERM;}
182 function getAdvType(){return ADVTYPE;}
183 function getRtype(){return RTYPE;}
184 function getSort(){return SORT;}
185 function getSortDir(){return SORT_DIR;}
186 function getDebug(){return DEBUG;}
187 function getCallnumber() { return CALLNUM; }
188 function getLitForm() { return LITFORM; }
189 function getItemForm() { return ITEMFORM; }
190 function getItemType() { return ITEMTYPE; }
191 function getAudience() { return AUDIENCE; }
192 function getSearches() { return SEARCHES; }
193 function getLanguage() { return LANGUAGE; }
194 function getRdepth() { return RDEPTH; }
197 function findBasePath() {
198 var path = location.pathname;
199 if(!path.match(/.*\.xml$/)) path += "index.xml";
200 var idx = path.indexOf(config.page[findCurrentPage()]);
201 return path.substring(0, idx);
204 function findBaseURL(ssl) {
205 var path = findBasePath();
206 var proto = (ssl) ? "https:" : "http:";
207 return proto + "//" + location.host + path;
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);
327 if(getSort() != null)
328 string += _appendParam(SORT, PARAM_SORT, args, getSort, string);
329 if(getSortDir() != null)
330 string += _appendParam(SORT_DIR, PARAM_SORT_DIR, args, getSortDir, string);
332 return string.replace(/\&$/,'').replace(/\?\&/,"?");
336 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
341 if( fieldVar != null &&
342 (fieldVar +'' != 'NaN') &&
343 overrideArgs[fieldName] == null &&
347 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
353 /* ----------------------------------------------------------------------- */
354 function cleanISBN(isbn) {
356 isbn = isbn.toString().replace(/^\s+/,"");
357 var idx = isbn.indexOf(" ");
358 if(idx > -1) { isbn = isbn.substring(0, idx); }
364 /* builds a link that goes to the title listings for a metarecord */
365 function buildTitleLink(rec, link) {
367 link.appendChild(text(normalize(truncate(rec.title(), 65))));
370 args[PARAM_OFFSET] = 0;
371 args[PARAM_MRID] = rec.doc_id();
372 args[PARAM_RTYPE] = RTYPE_MRID;
373 link.setAttribute("href", buildOPACLink(args));
376 function buildTitleDetailLink(rec, link) {
378 link.appendChild(text(normalize(truncate(rec.title(), 65))));
381 //args[PARAM_OFFSET] = 0;
382 args[PARAM_RID] = rec.doc_id();
383 link.setAttribute("href", buildOPACLink(args));
386 /* 'type' is one of STYPE_AUTHOR, STYPE_SUBJECT, ... found in config.js
387 'trunc' is the number of characters to show in the string, defaults to 65 */
388 function buildSearchLink(type, string, linknode, trunc) {
389 if(!trunc) trunc = 65;
392 args[PARAM_OFFSET] = 0;
393 args[PARAM_TERM] = string;
394 args[PARAM_STYPE] = type;
395 linknode.appendChild(text(normalize(truncate(string, trunc))));
396 linknode.setAttribute("href", buildOPACLink(args));
400 /* ----------------------------------------------------------------------- */
401 /* user session handling */
402 /* ----------------------------------------------------------------------- */
403 /* session is the login session. If no session is provided, we attempt
404 to find one in the cookies. If 'force' is true we retrieve the
405 user from the server even if there is already a global user present.
406 if ses != G.user.session, we also force a grab */
407 function grabUser(ses, force) {
409 if(!ses && isXUL()) ses = xulG['authtoken'];
412 ses = cookieManager.read(COOKIE_SES);
413 /* https cookies don't show up in http servers.. */
416 if(!ses) return false;
419 if(G.user && G.user.session == ses)
422 /* first make sure the session is valid */
423 var request = new Request(FETCH_SESSION, ses, 1 );
424 request.request.alertEvent = false;
426 var user = request.result();
430 return false; /* unable to grab the session */
433 if( !(typeof user == 'object' && user._isfieldmapper) ) {
439 G.user.fleshed = false;
440 G.user.session = ses;
441 cookieManager.write(COOKIE_SES, ses, '+1d');
445 if(G.user.prefs['opac.hits_per_page'])
446 COUNT = parseInt(G.user.prefs['opac.hits_per_page']);
448 if(G.user.prefs[PREF_DEF_FONT])
449 setFontSize(G.user.prefs[PREF_DEF_FONT]);
451 var at = getAuthtime();
452 //if(isXUL()) at = xulG['authtime'];
454 if(at && !isXUL()) new AuthTimer(at).run();
459 /* sets the 'prefs' field of the user object to their preferences
460 and returns the preferences */
461 function grabUserPrefs(user, force) {
462 if(user == null) user = G.user;
463 if(!force && user.prefs) return user.prefs;
464 var req = new Request(FETCH_USER_PREFS, user.session, user.id());
466 user.prefs = req.result();
470 function grabFleshedUser() {
472 if(!G.user || !G.user.session) {
474 if(!G.user || !G.user.session) return null;
477 if(G.user.fleshed) return G.user;
479 var req = new Request(FETCH_FLESHED_USER, G.user.session);
482 G.user = req.result();
484 if(!G.user || G.user.length == 0) {
485 G.user = null; return false;
486 cookieManager.write(COOKIE_SES,"");
489 G.user.session = ses;
490 G.user.fleshed = true;
492 cookieManager.write(COOKIE_SES, ses, '+1y'); /* update the cookie */
496 function checkUserSkin(new_skin) {
498 return; /* XXX do some debugging with this... */
500 var user_skin = getSkin();
501 var cur_skin = grabSkinFromURL();
503 if(new_skin) user_skin = new_skin;
508 if(grabUserPrefs()) {
509 user_skin = G.user.prefs["opac.skin"];
510 cookieManager.write( COOKIE_SKIN, user_skin, '+1y' );
515 if(!user_skin) return;
517 if( cur_skin != user_skin ) {
518 var url = buildOPACLink();
519 goTo(url.replace(cur_skin, user_skin));
523 function updateUserSetting(setting, value, user) {
524 if(user == null) user = G.user;
527 var req = new Request( UPDATE_USER_PREFS, user.session, a );
532 function commitUserPrefs() {
533 var req = new Request(
534 UPDATE_USER_PREFS, G.user.session, null, G.user.prefs );
539 function grabSkinFromURL() {
540 var path = findBasePath();
541 path = path.replace("/xml/", "");
543 for( var i = path.length - 1; i >= 0; i-- ) {
544 var ch = path.charAt(i);
550 for( i = skin.length - 1; i >= 0; i--)
551 skin2 += skin.charAt(i);
557 /* returns a fleshed G.user on success, false on failure */
558 function doLogin(suppressEvents) {
562 var uname = G.ui.login.username.value;
563 var passwd = G.ui.login.password.value;
565 var init_request = new Request( LOGIN_INIT, uname );
566 init_request.send(true);
567 var seed = init_request.result();
569 if( ! seed || seed == '0') {
570 alert( "Error Communicating with Authentication Server" );
575 password : hex_md5(seed + hex_md5(passwd)),
577 org : getOrigLocation()
580 if( uname.match(REGEX_BARCODE) ) args.barcode = uname; /* make this better */
581 else args.username = uname;
583 var auth_request = new Request( LOGIN_COMPLETE, args );
585 auth_request.send(true);
586 var auth_result = auth_request.result();
587 if(!auth_result) return null;
589 AUTHTIME = parseInt(auth_result.payload.authtime);
590 var u = grabUser(auth_result.payload.authtoken, true);
591 if(u && ! suppressEvents)
592 runEvt( "common", "locationChanged", u.ws_ou(), findOrgDepth(u.ws_ou()) );
599 function doLogout(noredirect) {
601 /* cancel everything else */
604 /* be nice and delete the session from the server */
605 if(G.user && G.user.session) {
606 var req = new Request(LOGIN_DELETE, G.user.session);
608 try { req.result(); } catch(E){}
613 /* remove any cached data */
614 //cookieManager.remove(COOKIE_IDS);
615 //cookieManager.remove(COOKIE_SRIDS);
616 cookieManager.remove(COOKIE_RIDS);
617 cookieManager.remove(COOKIE_SES);
618 cookieManager.remove(COOKIE_SKIN);
620 checkUserSkin("default");
625 args[PARAM_TERM] = "";
626 args[PARAM_LOCATION] = globalOrgTree.id();
627 args[PARAM_DEPTH] = findOrgDepth(globalOrgTree);
632 try{ if(isFrontPage) nored = true; } catch(e){nored = false;}
633 if(!nored) goTo(buildOPACLink(args));
637 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); }
638 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
641 /* ----------------------------------------------------------------------- */
642 /* build the org tree */
643 /* ----------------------------------------------------------------------- */
644 function drawOrgTree() {
645 //setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 10 );
646 setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 1 );
650 function buildOrgSelector(node) {
651 var tree = new SlimTree(node,'orgTreeSelector');
652 orgTreeSelector = tree;
653 for( var i in orgArraySearcher ) {
654 var node = orgArraySearcher[i];
655 if( node == null ) continue;
656 if(node.parent_ou() == null)
657 tree.addNode(node.id(), -1, node.name(),
658 "javascript:orgSelect(" + node.id() + ");", node.name());
660 tree.addNode(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(),
661 "javascript:orgSelect(" + node.id() + ");", node.name());
664 hideMe($('org_loading_div'));
665 unHideMe($('org_selector_tip'));
669 function orgSelect(id) {
671 runEvt("common", "locationChanged", id, findOrgDepth(id) );
673 removeChildren(G.ui.common.now_searching);
674 G.ui.common.now_searching.appendChild(text(findOrgUnit(id).name()));
677 function setFontSize(size) {
679 cookieManager.write(COOKIE_FONT, size, '+1y');
683 var resourceFormats = [
686 "sound recording", "software, multimedia",
691 "three dimensional object" ];
694 function modsFormatToMARC(format) {
700 case "sound recording":
702 case "sound recording-nonmusical":
704 case "sound recording-musical":
706 case "software, multimedia":
712 case "mixed material":
714 case "notated music":
716 case "three dimensional object":
723 function MARCFormatToMods(format) {
729 return "moving image";
731 return "sound recording-nonmusical";
733 return "sound recording-musical";
735 return "software, multimedia";
737 return "still images";
740 return "cartographic";
743 return "mixed material";
746 return "notated music";
748 return "three dimensional object";
753 function MARCTypeToFriendly(format) {
754 var words = $('format_words');
757 case 't' : return $n(words, 'at').innerHTML;
759 var node = $n(words,format);
760 if( node ) return node.innerHTML;
765 function setResourcePic( img, resource ) {
766 img.setAttribute( "src", "../../../../images/tor/" + resource + ".jpg");
767 img.title = resource;
772 function msg( text ) {
773 try { alert( text ); } catch(e) {}
776 function findRecord(id,type) {
778 for( var i = 0; i != recordsCache.length; i++ ) {
779 var rec = recordsCache[i];
780 if( rec && rec.doc_id() == id ) return rec;
783 var meth = FETCH_RMODS
784 if(type == 'M') meth = FETCH_MRMODS;
785 var req = new Request(meth, id);
790 function Timer(name, node){
795 Timer.prototype.start =
796 function(){_timerRun(this.name);}
797 Timer.prototype.stop =
798 function(){this.done = true;}
799 function _timerRun(tname) {
803 if(_t.count > 100) return;
805 if( (_t.count % 5) == 0 )
807 _t.node.appendChild(text(str));
808 setTimeout("_timerRun('"+tname+"');", 200);
812 function checkILSEvent(obj) {
813 if( obj && obj.ilsevent != null && obj.ilsevent != 0 )
814 return parseInt(obj.ilsevent);
819 function alertILSEvent(evt, msg) {
822 alert(msg +'\n' + evt.textcode + '\n' + evt.desc );
824 alert(evt.textcode + '\n' + evt.desc );
829 function AuthTimer(time) {
830 this.time = (time - LOGOUT_WARNING_TIME) * 1000;
834 AuthTimer.prototype.run = function() {
835 setTimeout('_authTimerAlert()', this.time);
838 function _authTimerAlert() {
839 alert( $('auth_session_expiring').innerHTML );
840 if(!grabUser(null, true)) doLogout();
844 function grabUserByBarcode( authtoken, barcode ) {
845 var req = new Request( FETCH_USER_BYBARCODE, authtoken, barcode );
852 goTo(buildOPACLink({page:HOME}));
856 function buildOrgSel(selector, org, offset) {
857 insertSelectorVal( selector, -1,
858 org.name(), org.id(), null, findOrgDepth(org) - offset );
859 for( var c in org.children() )
860 buildOrgSel( selector, org.children()[c], offset);
864 function parseForm(form) {
867 var it = form.replace(/-\w+$/,"");
872 if(form.match(/-/)) itf = form.replace(/^\w+-/,"");
876 for( var i = 0; i < it.length; i++ )
877 item_type.push( it.charAt(i) );
882 for( var i = 0; i < itf.length; i++ )
883 item_form.push( itf.charAt(i) );
886 return {item_type: item_type, item_form:item_form};
890 function isTrue(x) { return ( x && x != "0" && x != 'f' ); }