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 clearSearchParams() {
151 function initCookies() {
152 FONTSIZE = "regular";
153 var font = cookieManager.read(COOKIE_FONT);
155 if(font) FONTSIZE = font;
156 SKIN = cookieManager.read(COOKIE_SKIN);
159 /* URL param accessors */
160 function getTerm(){return TERM;}
161 function getStype(){return STYPE;}
162 function getLocation(){return LOCATION;}
163 function getDepth(){return DEPTH;}
164 function getForm(){return FORM;}
165 function getOffset(){return OFFSET;}
166 function getDisplayCount(){return COUNT;}
167 function getHitCount(){return HITCOUNT;}
168 function getMrid(){return MRID;};
169 function getRid(){return RID;};
170 function getOrigLocation(){return ORIGLOC;}
171 function getAuthtime() { return AUTHTIME; }
172 function getSearchBarExtras(){return SBEXTRAS;}
173 function getFontSize(){return FONTSIZE;};
174 function getSkin(){return SKIN;};
175 function getAdvTerm(){return ADVTERM;}
176 function getAdvType(){return ADVTYPE;}
177 function getRtype(){return RTYPE;}
178 function getSort(){return SORT;}
179 function getSortDir(){return SORT_DIR;}
180 function getDebug(){return DEBUG;}
181 function getCallnumber() { return CALLNUM; }
182 function getLitForm() { return LITFORM; }
183 function getItemForm() { return ITEMFORM; }
184 function getItemType() { return ITEMTYPE; }
185 function getAudience() { return AUDIENCE; }
186 function getSearches() { return SEARCHES; }
187 function getLanguage() { return LANGUAGE; }
190 function findBasePath() {
191 var path = location.pathname;
192 if(!path.match(/.*\.xml$/)) path += "index.xml";
193 var idx = path.indexOf(config.page[findCurrentPage()]);
194 return path.substring(0, idx);
197 function findBaseURL(ssl) {
198 var path = findBasePath();
199 var proto = (ssl) ? "https:" : "http:";
200 return proto + "//" + location.host + path;
201 dump( 'ssl: ' + ssl + 'proto ' + proto );
205 function buildISBNSrc(isbn) {
206 return "http://" + location.host + "/jackets/" + isbn;
210 function buildImageLink(name, ssl) {
211 return findBaseURL(ssl) + "../../../../images/" + name;
214 function buildExtrasLink(name, ssl) {
215 return findBaseURL(ssl) + "../../../../extras/" + name;
219 function _debug(str) {
220 try { dump('dbg: ' + str + '\n'); } catch(e) {}
222 /* potentially useful, but usually just annoying */
225 if(!consoleService) {
227 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
228 this.consoleService = Components.classes['@mozilla.org/consoleservice;1']
229 .getService(Components.interfaces.nsIConsoleService);
235 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
236 consoleService.logStringMessage(str + '\n');
243 function buildOPACLink(args, slim, ssl) {
249 string = findBaseURL(ssl);
250 if(args.page) string += config.page[args.page];
251 else string += config.page[findCurrentPage()];
256 for( var x in args ) {
258 if(x == "page" || v == null || v == undefined || v+'' == 'NaN' ) continue;
259 if(x == PARAM_OFFSET && v == 0) continue;
260 if(x == PARAM_LOCATION && v == 1) continue;
261 if(x == PARAM_DEPTH && v == 0) continue;
262 if(x == PARAM_COUNT && v == 10) continue;
263 if(x == PARAM_FORM && v == 'all' ) continue;
264 if( instanceOf(v, Array) && v.length ) {
265 for( var i = 0; i < v.length; i++ ) {
266 string += "&" + x + "=" + encodeURIComponent(v[i]);
269 string += "&" + x + "=" + encodeURIComponent(v);
274 string += _appendParam(DEBUG, PARAM_DEBUG, args, getDebug, string);
275 if(getOrigLocation() != 1)
276 string += _appendParam(ORIGLOC, PARAM_ORIGLOC, args, getOrigLocation, string);
278 string += _appendParam(TERM, PARAM_TERM, args, getTerm, string);
280 string += _appendParam(STYPE, PARAM_STYPE, args, getStype, string);
281 if(getLocation() != 1)
282 string += _appendParam(LOCATION, PARAM_LOCATION, args, getLocation, string);
284 string += _appendParam(DEPTH, PARAM_DEPTH, args, getDepth, string);
285 if(getForm() && (getForm() != 'all') )
286 string += _appendParam(FORM, PARAM_FORM, args, getForm, string);
288 string += _appendParam(OFFSET, PARAM_OFFSET, args, getOffset, string);
289 if(getDisplayCount() != 10)
290 string += _appendParam(COUNT, PARAM_COUNT, args, getDisplayCount, string);
292 string += _appendParam(HITCOUNT, PARAM_HITCOUNT, args, getHitCount, string);
294 string += _appendParam(MRID, PARAM_MRID, args, getMrid, string);
296 string += _appendParam(RID, PARAM_RID, args, getRid, string);
298 string += _appendParam(AUTHTIME, PARAM_AUTHTIME, args, getAuthtime, string);
300 string += _appendParam(ADVTERM, PARAM_ADVTERM, args, getAdvTerm, string);
302 string += _appendParam(ADVTYPE, PARAM_ADVTYPE, args, getAdvType, string);
304 string += _appendParam(RTYPE, PARAM_RTYPE, args, getRtype, string);
306 string += _appendParam(ITEMFORM, PARAM_ITEMFORM, args, getItemForm, string);
308 string += _appendParam(ITEMTYPE, PARAM_ITEMTYPE, args, getItemType, string);
310 string += _appendParam(LITFORM, PARAM_LITFORM, args, getLitForm, string);
312 string += _appendParam(AUDIENCE, PARAM_AUDIENCE, args, getAudience, string);
314 string += _appendParam(SEARCHES, PARAM_SEARCHES, args, getSearches, string);
316 string += _appendParam(LANGUAGE, PARAM_LANGUAGE, args, getLanguage, string);
318 return string.replace(/\&$/,'').replace(/\?\&/,"?");
321 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
325 if( fieldVar != null &&
326 (fieldVar +'' != 'NaN') &&
327 overrideArgs[fieldName] == null &&
331 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
336 /* ----------------------------------------------------------------------- */
337 function cleanISBN(isbn) {
339 isbn = isbn.toString().replace(/^\s+/,"");
340 var idx = isbn.indexOf(" ");
341 if(idx > -1) { isbn = isbn.substring(0, idx); }
347 /* builds a link that goes to the title listings for a metarecord */
348 function buildTitleLink(rec, link) {
350 link.appendChild(text(normalize(truncate(rec.title(), 65))));
353 args[PARAM_OFFSET] = 0;
354 args[PARAM_MRID] = rec.doc_id();
355 args[PARAM_RTYPE] = RTYPE_MRID;
356 link.setAttribute("href", buildOPACLink(args));
359 function buildTitleDetailLink(rec, link) {
361 link.appendChild(text(normalize(truncate(rec.title(), 65))));
364 args[PARAM_OFFSET] = 0;
365 args[PARAM_RID] = rec.doc_id();
366 link.setAttribute("href", buildOPACLink(args));
369 /* 'type' is one of STYPE_AUTHOR, STYPE_SUBJECT, ... found in config.js
370 'trunc' is the number of characters to show in the string, defaults to 65 */
371 function buildSearchLink(type, string, linknode, trunc) {
372 if(!trunc) trunc = 65;
375 args[PARAM_OFFSET] = 0;
376 args[PARAM_TERM] = string;
377 args[PARAM_STYPE] = type;
378 linknode.appendChild(text(normalize(truncate(string, trunc))));
379 linknode.setAttribute("href", buildOPACLink(args));
383 /* ----------------------------------------------------------------------- */
384 /* user session handling */
385 /* ----------------------------------------------------------------------- */
386 /* session is the login session. If no session is provided, we attempt
387 to find one in the cookies. If 'force' is true we retrieve the
388 user from the server even if there is already a global user present.
389 if ses != G.user.session, we also force a grab */
390 function grabUser(ses, force) {
392 if(!ses && isXUL()) ses = xulG['authtoken'];
395 ses = cookieManager.read(COOKIE_SES);
396 /* https cookies don't show up in http servers.. */
399 if(!ses) return false;
402 if(G.user && G.user.session == ses)
405 /* first make sure the session is valid */
406 var request = new Request(FETCH_SESSION, ses, 1 );
408 var user = request.result();
410 if(checkILSEvent(user)) {
413 return false; /* unable to grab the session */
416 if( !(typeof user == 'object' && user._isfieldmapper) ) {
422 G.user.fleshed = false;
423 G.user.session = ses;
424 cookieManager.write(COOKIE_SES, ses, '+1d');
427 if(G.user.prefs['opac.hits_per_page'])
428 COUNT = parseInt(G.user.prefs['opac.hits_per_page']);
430 if(G.user.prefs[PREF_DEF_FONT])
431 setFontSize(G.user.prefs[PREF_DEF_FONT]);
433 var at = getAuthtime();
434 //if(isXUL()) at = xulG['authtime'];
436 if(at && !isXUL()) new AuthTimer(at).run();
441 /* sets the 'prefs' field of the user object to their preferences
442 and returns the preferences */
443 function grabUserPrefs(user, force) {
444 if(user == null) user = G.user;
445 if(!force && user.prefs) return user.prefs;
446 var req = new Request(FETCH_USER_PREFS, user.session, user.id());
448 user.prefs = req.result();
452 function grabFleshedUser() {
454 if(!G.user || !G.user.session) {
456 if(!G.user || !G.user.session) return null;
459 if(G.user.fleshed) return G.user;
461 var req = new Request(FETCH_FLESHED_USER, G.user.session);
464 G.user = req.result();
466 if(!G.user || G.user.length == 0) {
467 G.user = null; return false;
468 cookieManager.write(COOKIE_SES,"");
471 G.user.session = ses;
472 G.user.fleshed = true;
474 cookieManager.write(COOKIE_SES, ses, '+1y'); /* update the cookie */
478 function checkUserSkin(new_skin) {
480 return; /* XXX do some debugging with this... */
482 var user_skin = getSkin();
483 var cur_skin = grabSkinFromURL();
485 if(new_skin) user_skin = new_skin;
490 if(grabUserPrefs()) {
491 user_skin = G.user.prefs["opac.skin"];
492 cookieManager.write( COOKIE_SKIN, user_skin, '+1y' );
497 if(!user_skin) return;
499 if( cur_skin != user_skin ) {
500 var url = buildOPACLink();
501 goTo(url.replace(cur_skin, user_skin));
505 function updateUserSetting(setting, value, user) {
506 if(user == null) user = G.user;
509 var req = new Request( UPDATE_USER_PREFS, user.session, a );
514 function commitUserPrefs() {
515 var req = new Request(
516 UPDATE_USER_PREFS, G.user.session, null, G.user.prefs );
521 function grabSkinFromURL() {
522 var path = findBasePath();
523 path = path.replace("/xml/", "");
525 for( var i = path.length - 1; i >= 0; i-- ) {
526 var ch = path.charAt(i);
532 for( i = skin.length - 1; i >= 0; i--)
533 skin2 += skin.charAt(i);
539 /* returns a fleshed G.user on success, false on failure */
544 var uname = G.ui.login.username.value;
545 var passwd = G.ui.login.password.value;
547 var init_request = new Request( LOGIN_INIT, uname );
548 init_request.send(true);
549 var seed = init_request.result();
551 if( ! seed || seed == '0') {
552 alert( "Error Communicating with Authentication Server" );
557 password : hex_md5(seed + hex_md5(passwd)),
559 org : getOrigLocation()
562 if( uname.match(REGEX_BARCODE) ) args.barcode = uname; /* make this better */
563 else args.username = uname;
565 var auth_request = new Request( LOGIN_COMPLETE, args );
567 auth_request.send(true);
568 var auth_result = auth_request.result();
570 var code = checkILSEvent(auth_result);
571 if(code) { alertILSEvent(auth_result); return null; }
573 AUTHTIME = parseInt(auth_result.payload.authtime);
574 var u = grabUser(auth_result.payload.authtoken, true);
575 if(u) runEvt( "common", "locationChanged", u.home_ou(), findOrgDepth(u.home_ou()) );
582 function doLogout(noredirect) {
584 /* cancel everything else */
587 /* be nice and delete the session from the server */
588 if(G.user && G.user.session) {
589 var req = new Request(LOGIN_DELETE, G.user.session);
591 try { req.result(); } catch(E){}
595 cookieManager.write(COOKIE_SES, "");
596 cookieManager.write(COOKIE_SKIN, "");
597 checkUserSkin("default");
601 args[PARAM_TERM] = "";
602 args[PARAM_LOCATION] = globalOrgTree.id();
603 args[PARAM_DEPTH] = findOrgDepth(globalOrgTree);
607 try{ if(isFrontPage) nored = true; } catch(e){nored = false;}
608 if(!nored) goTo(buildOPACLink(args));
612 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); }
613 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
616 /* ----------------------------------------------------------------------- */
617 /* build the org tree */
618 /* ----------------------------------------------------------------------- */
619 function drawOrgTree() {
620 //setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 10 );
621 setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 1 );
625 function buildOrgSelector(node) {
626 var tree = new SlimTree(node,'orgTreeSelector');
627 orgTreeSelector = tree;
628 for( var i in orgArraySearcher ) {
629 var node = orgArraySearcher[i];
630 if( node == null ) continue;
631 if(node.parent_ou() == null)
632 tree.addNode(node.id(), -1, node.name(),
633 "javascript:orgSelect(" + node.id() + ");", node.name());
635 tree.addNode(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(),
636 "javascript:orgSelect(" + node.id() + ");", node.name());
639 hideMe($('org_loading_div'));
640 unHideMe($('org_selector_tip'));
644 function orgSelect(id) {
646 runEvt("common", "locationChanged", id, findOrgDepth(id) );
648 removeChildren(G.ui.common.now_searching);
649 G.ui.common.now_searching.appendChild(text(findOrgUnit(id).name()));
652 //var fontCookie = new HTTP.Cookies();
654 function setFontSize(size) {
656 cookieManager.write(COOKIE_FONT, size, '+1y');
660 var resourceFormats = [
663 "sound recording", "software, multimedia",
668 "three dimensional object" ];
671 function modsFormatToMARC(format) {
677 case "sound recording":
679 case "sound recording-nonmusical":
681 case "sound recording-musical":
683 case "software, multimedia":
689 case "mixed material":
691 case "notated music":
693 case "three dimensional object":
700 function MARCFormatToMods(format) {
706 return "moving image";
708 return "sound recording-nonmusical";
710 return "sound recording-musical";
712 return "software, multimedia";
714 return "still images";
717 return "cartographic";
720 return "mixed material";
723 return "notated music";
725 return "three dimensional object";
730 function setResourcePic( img, resource ) {
731 img.setAttribute( "src", "../../../../images/tor/" + resource + ".jpg");
732 img.title = resource;
737 function msg( text ) {
738 try { alert( text ); } catch(e) {}
741 function findRecord(id,type) {
743 for( var i = 0; i != recordsCache.length; i++ ) {
744 var rec = recordsCache[i];
745 if( rec && rec.doc_id() == id ) return rec;
748 var meth = FETCH_RMODS
749 if(type == 'M') meth = FETCH_MRMODS;
750 var req = new Request(meth, id);
755 function Timer(name, node){
760 Timer.prototype.start =
761 function(){_timerRun(this.name);}
762 Timer.prototype.stop =
763 function(){this.done = true;}
764 function _timerRun(tname) {
768 if(_t.count > 100) return;
770 if( (_t.count % 5) == 0 )
772 _t.node.appendChild(text(str));
773 setTimeout("_timerRun('"+tname+"');", 200);
777 function checkILSEvent(obj) {
778 if( obj.ilsevent != null && obj.ilsevent != 0 )
779 return parseInt(obj.ilsevent);
784 function alertILSEvent(code, msg) {
786 alert( msg + '\n' + $('ilsevent.' + code).innerHTML );
790 function alertILSEvent(evt, msg) {
792 alert( msg + '\n' + evt.desc );
797 function AuthTimer(time) {
798 this.time = (time - LOGOUT_WARNING_TIME) * 1000;
802 AuthTimer.prototype.run = function() {
803 setTimeout('_authTimerAlert()', this.time);
806 function _authTimerAlert() {
807 alert( $('auth_session_expiring').innerHTML );
808 if(!grabUser(null, true)) doLogout();
812 function grabUserByBarcode( authtoken, barcode ) {
813 var req = new Request( FETCH_USER_BYBARCODE, authtoken, barcode );
820 goTo(buildOPACLink({page:HOME}));
824 function buildOrgSel(selector, org, offset) {
825 insertSelectorVal( selector, -1,
826 org.name(), org.id(), null, findOrgDepth(org) - offset );
827 for( var c in org.children() )
828 buildOrgSel( selector, org.children()[c], offset);
832 function parseForm(form) {
835 var it = form.replace(/-\w+$/,"");
840 if(form.match(/-/)) itf = form.replace(/^\w+-/,"");
844 for( var i = 0; i < it.length; i++ )
845 item_type.push( it.charAt(i) );
850 for( var i = 0; i < itf.length; i++ )
851 item_form.push( itf.charAt(i) );
854 return {item_type: item_type, item_form:item_form};