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);
108 /* set up some sane defaults */
109 if(isNaN(LOCATION)) LOCATION = 1;
110 if(isNaN(DEPTH)) DEPTH = 0;
111 if(isNaN(OFFSET)) OFFSET = 0;
112 if(isNaN(COUNT)) COUNT = 10;
113 if(isNaN(HITCOUNT)) HITCOUNT = 0;
114 if(isNaN(MRID)) MRID = 0;
115 if(isNaN(RID)) RID = 0;
116 if(isNaN(ORIGLOC)) ORIGLOC = 1;
117 if(isNaN(AUTHTIME)) AUTHTIME = 0;
118 if(ADVTERM==null) ADVTERM = "";
121 function initCookies() {
122 FONTSIZE = "regular";
123 var font = cookieManager.read(COOKIE_FONT);
125 if(font) FONTSIZE = font;
126 SKIN = cookieManager.read(COOKIE_SKIN);
129 /* URL param accessors */
130 function getTerm(){return TERM;}
131 function getStype(){return STYPE;}
132 function getLocation(){return LOCATION;}
133 function getDepth(){return DEPTH;}
134 function getForm(){return FORM;}
135 function getOffset(){return OFFSET;}
136 function getDisplayCount(){return COUNT;}
137 function getHitCount(){return HITCOUNT;}
138 function getMrid(){return MRID;};
139 function getRid(){return RID;};
140 function getOrigLocation(){return ORIGLOC;}
141 function getAuthtime() { return AUTHTIME; }
142 function getSearchBarExtras(){return SBEXTRAS;}
143 function getFontSize(){return FONTSIZE;};
144 function getSkin(){return SKIN;};
145 function getAdvTerm(){return ADVTERM;}
146 function getAdvType(){return ADVTYPE;}
147 function getRtype(){return RTYPE;}
148 function getSort(){return SORT;}
149 function getSortDir(){return SORT_DIR;}
150 function getDebug(){return DEBUG;}
151 function getCallnumber() { return CALLNUM; }
154 function findBasePath() {
155 var path = location.pathname;
156 if(!path.match(/.*\.xml$/)) path += "index.xml";
157 var idx = path.indexOf(config.page[findCurrentPage()]);
158 return path.substring(0, idx);
161 function findBaseURL(ssl) {
162 var path = findBasePath();
163 var proto = (ssl) ? "https:" : "http:";
164 return proto + "//" + location.host + path;
165 dump( 'ssl: ' + ssl + 'proto ' + proto );
169 function buildISBNSrc(isbn) {
170 return "http://" + location.host + "/jackets/" + isbn;
174 function buildImageLink(name, ssl) {
175 return findBaseURL(ssl) + "../../../../images/" + name;
178 function buildExtrasLink(name, ssl) {
179 return findBaseURL(ssl) + "../../../../extras/" + name;
183 function _debug(str) {
184 try { dump(str + '\n'); } catch(e) {}
187 if(!consoleService) {
189 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
190 this.consoleService = Components.classes['@mozilla.org/consoleservice;1']
191 .getService(Components.interfaces.nsIConsoleService);
197 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
198 consoleService.logStringMessage(str + '\n');
204 function buildOPACLink(args, slim, ssl) {
210 string = findBaseURL(ssl);
211 if(args.page) string += config.page[args.page];
212 else string += config.page[findCurrentPage()];
217 for( var x in args ) {
219 if(x == "page" || v == null || v == undefined || v+'' == 'NaN' ) continue;
220 if(x == PARAM_OFFSET && v == 0) continue;
221 if(x == PARAM_LOCATION && v == 1) continue;
222 if(x == PARAM_DEPTH && v == 0) continue;
223 if(x == PARAM_COUNT && v == 10) continue;
224 if(x == PARAM_FORM && v == 'all' ) continue;
225 if( instanceOf(v, Array) && v.length ) {
226 for( var i = 0; i < v.length; i++ ) {
227 string += "&" + x + "=" + encodeURIComponent(v[i]);
230 string += "&" + x + "=" + encodeURIComponent(v);
235 string += _appendParam(DEBUG, PARAM_DEBUG, args, getDebug, string);
236 if(getOrigLocation() != 1)
237 string += _appendParam(ORIGLOC, PARAM_ORIGLOC, args, getOrigLocation, string);
239 string += _appendParam(TERM, PARAM_TERM, args, getTerm, string);
241 string += _appendParam(STYPE, PARAM_STYPE, args, getStype, string);
242 if(getLocation() != 1)
243 string += _appendParam(LOCATION, PARAM_LOCATION, args, getLocation, string);
245 string += _appendParam(DEPTH, PARAM_DEPTH, args, getDepth, string);
246 if(getForm() && (getForm() != 'all') )
247 string += _appendParam(FORM, PARAM_FORM, args, getForm, string);
249 string += _appendParam(OFFSET, PARAM_OFFSET, args, getOffset, string);
250 if(getDisplayCount() != 10)
251 string += _appendParam(COUNT, PARAM_COUNT, args, getDisplayCount, string);
253 string += _appendParam(HITCOUNT, PARAM_HITCOUNT, args, getHitCount, string);
255 string += _appendParam(MRID, PARAM_MRID, args, getMrid, string);
257 string += _appendParam(RID, PARAM_RID, args, getRid, string);
259 string += _appendParam(AUTHTIME, PARAM_AUTHTIME, args, getAuthtime, string);
261 string += _appendParam(ADVTERM, PARAM_ADVTERM, args, getAdvTerm, string);
263 string += _appendParam(ADVTYPE, PARAM_ADVTYPE, args, getAdvType, string);
265 string += _appendParam(RTYPE, PARAM_RTYPE, args, getRtype, string);
267 return string.replace(/\&$/,'').replace(/\?\&/,"?");
270 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
272 if( fieldVar != null && (fieldVar +'' != 'NaN') && overrideArgs[fieldName] == null )
273 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
277 /* ----------------------------------------------------------------------- */
278 function cleanISBN(isbn) {
280 isbn = isbn.toString().replace(/^\s+/,"");
281 var idx = isbn.indexOf(" ");
282 if(idx > -1) { isbn = isbn.substring(0, idx); }
288 /* builds a link that goes to the title listings for a metarecord */
289 function buildTitleLink(rec, link) {
291 link.appendChild(text(normalize(truncate(rec.title(), 65))));
294 args[PARAM_OFFSET] = 0;
295 args[PARAM_MRID] = rec.doc_id();
296 args[PARAM_RTYPE] = RTYPE_MRID;
297 link.setAttribute("href", buildOPACLink(args));
300 function buildTitleDetailLink(rec, link) {
302 link.appendChild(text(normalize(truncate(rec.title(), 65))));
305 args[PARAM_OFFSET] = 0;
306 args[PARAM_RID] = rec.doc_id();
307 link.setAttribute("href", buildOPACLink(args));
310 /* 'type' is one of STYPE_AUTHOR, STYPE_SUBJECT, ... found in config.js
311 'trunc' is the number of characters to show in the string, defaults to 65 */
312 function buildSearchLink(type, string, linknode, trunc) {
313 if(!trunc) trunc = 65;
316 args[PARAM_OFFSET] = 0;
317 args[PARAM_TERM] = string;
318 args[PARAM_STYPE] = type;
319 linknode.appendChild(text(normalize(truncate(string, trunc))));
320 linknode.setAttribute("href", buildOPACLink(args));
324 /* ----------------------------------------------------------------------- */
325 /* user session handling */
326 /* ----------------------------------------------------------------------- */
327 /* session is the login session. If no session is provided, we attempt
328 to find one in the cookies. If 'force' is true we retrieve the
329 user from the server even if there is already a global user present.
330 if ses != G.user.session, we also force a grab */
331 function grabUser(ses, force) {
333 if(!ses && isXUL()) ses = xulG['authtoken'];
334 if(!ses) ses = cookieManager.read(COOKIE_SES);
335 if(!ses) return false;
338 if(G.user && G.user.session == ses)
341 /* first make sure the session is valid */
342 var request = new Request(FETCH_SESSION, ses, 1 );
344 var user = request.result();
346 if(checkILSEvent(user)) {
349 return false; /* unable to grab the session */
352 if( !(typeof user == 'object' && user._isfieldmapper) ) {
358 G.user.fleshed = false;
359 G.user.session = ses;
360 cookieManager.write(COOKIE_SES, ses, '+1y');
363 if(G.user.prefs['opac.hits_per_page'])
364 COUNT = parseInt(G.user.prefs['opac.hits_per_page']);
366 if(G.user.prefs[PREF_DEF_FONT])
367 setFontSize(G.user.prefs[PREF_DEF_FONT]);
369 var at = getAuthtime();
370 if(isXUL()) at = xulG['authtime'];
372 if(at) new AuthTimer(at).run();
377 /* sets the 'prefs' field of the user object to their preferences
378 and returns the preferences */
379 function grabUserPrefs(user, force) {
380 if(user == null) user = G.user;
381 if(!force && user.prefs) return user.prefs;
382 var req = new Request(FETCH_USER_PREFS, user.session, user.id());
384 user.prefs = req.result();
388 function grabFleshedUser() {
390 if(!G.user || !G.user.session) {
392 if(!G.user || !G.user.session) return null;
395 if(G.user.fleshed) return G.user;
397 var req = new Request(FETCH_FLESHED_USER, G.user.session);
400 G.user = req.result();
402 if(!G.user || G.user.length == 0) {
403 G.user = null; return false;
404 cookieManager.remove(COOKIE_SES);
407 G.user.session = ses;
408 G.user.fleshed = true;
410 cookieManager.write(COOKIE_SES, ses, '+1y'); /* update the cookie */
414 function checkUserSkin(new_skin) {
416 return; /* XXX do some debugging with this... */
418 var user_skin = getSkin();
419 var cur_skin = grabSkinFromURL();
421 if(new_skin) user_skin = new_skin;
426 if(grabUserPrefs()) {
427 user_skin = G.user.prefs["opac.skin"];
428 cookieManager.write( COOKIE_SKIN, user_skin, '+1y' );
433 if(!user_skin) return;
435 if( cur_skin != user_skin ) {
436 var url = buildOPACLink();
437 goTo(url.replace(cur_skin, user_skin));
441 function updateUserSetting(setting, value, user) {
442 if(user == null) user = G.user;
445 var req = new Request( UPDATE_USER_PREFS, user.session, a );
450 function commitUserPrefs() {
451 var req = new Request(
452 UPDATE_USER_PREFS, G.user.session, null, G.user.prefs );
457 function grabSkinFromURL() {
458 var path = findBasePath();
459 path = path.replace("/xml/", "");
461 for( var i = path.length - 1; i >= 0; i-- ) {
462 var ch = path.charAt(i);
468 for( i = skin.length - 1; i >= 0; i--)
469 skin2 += skin.charAt(i);
475 /* returns a fleshed G.user on success, false on failure */
480 var uname = G.ui.login.username.value;
481 var passwd = G.ui.login.password.value;
483 var init_request = new Request( LOGIN_INIT, uname );
484 init_request.send(true);
485 var seed = init_request.result();
487 if( ! seed || seed == '0') {
488 alert( "Error Communicating with Authentication Server" );
493 password : hex_md5(seed + hex_md5(passwd)),
495 org : getOrigLocation()
498 if( uname.match(REGEX_BARCODE) ) args.barcode = uname; /* make this better */
499 else args.username = uname;
501 var auth_request = new Request( LOGIN_COMPLETE, args );
503 auth_request.send(true);
504 var auth_result = auth_request.result();
506 var code = checkILSEvent(auth_result);
507 if(code) { alertILSEvent(code); return null; }
509 AUTHTIME = parseInt(auth_result.payload.authtime);
510 var u = grabUser(auth_result.payload.authtoken, true);
511 if(u) runEvt( "common", "locationChanged", u.home_ou(), findOrgDepth(u.home_ou()) );
518 function doLogout(noredirect) {
520 /* cancel everything else */
523 /* be nice and delete the session from the server */
524 if(G.user && G.user.session) {
525 var req = new Request(LOGIN_DELETE, G.user.session);
527 try { req.result(); } catch(E){}
531 cookieManager.remove(COOKIE_SES);
532 cookieManager.remove(COOKIE_SKIN);
533 checkUserSkin("default");
537 args[PARAM_TERM] = "";
538 args[PARAM_LOCATION] = globalOrgTree.id();
539 args[PARAM_DEPTH] = findOrgDepth(globalOrgTree);
543 try{ if(isFrontPage) nored = true; } catch(e){nored = false;}
544 if(!nored) goTo(buildOPACLink(args));
548 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); }
549 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
552 /* ----------------------------------------------------------------------- */
553 /* build the org tree */
554 /* ----------------------------------------------------------------------- */
555 function drawOrgTree() {
556 //setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 10 );
557 setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 1 );
561 function buildOrgSelector(node) {
562 var tree = new SlimTree(node,'orgTreeSelector');
563 orgTreeSelector = tree;
564 for( var i in orgArraySearcher ) {
565 var node = orgArraySearcher[i];
566 if( node == null ) continue;
567 if(node.parent_ou() == null)
568 tree.addNode(node.id(), -1, node.name(),
569 "javascript:orgSelect(" + node.id() + ");", node.name());
571 tree.addNode(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(),
572 "javascript:orgSelect(" + node.id() + ");", node.name());
575 hideMe($('org_loading_div'));
576 unHideMe($('org_selector_tip'));
580 function orgSelect(id) {
582 runEvt("common", "locationChanged", id, findOrgDepth(id) );
584 removeChildren(G.ui.common.now_searching);
585 G.ui.common.now_searching.appendChild(text(findOrgUnit(id).name()));
588 //var fontCookie = new HTTP.Cookies();
590 function setFontSize(size) {
592 cookieManager.write(COOKIE_FONT, size, '+1y');
596 var resourceFormats = [
599 "sound recording", "software, multimedia",
604 "three dimensional object" ];
607 function modsFormatToMARC(format) {
613 case "sound recording":
615 case "sound recording-nonmusical":
617 case "sound recording-musical":
619 case "software, multimedia":
625 case "mixed material":
627 case "notated music":
629 case "three dimensional object":
636 function MARCFormatToMods(format) {
642 return "moving image";
644 return "sound recording-nonmusical";
646 return "sound recording-musical";
648 return "software, multimedia";
650 return "still images";
653 return "cartographic";
656 return "mixed material";
659 return "notated music";
661 return "three dimensional object";
666 function setResourcePic( img, resource ) {
667 img.setAttribute( "src", "../../../../images/tor/" + resource + ".jpg");
668 img.title = resource;
673 function msg( text ) {
674 try { alert( text ); } catch(e) {}
677 function findRecord(id,type) {
679 for( var i = 0; i != recordsCache.length; i++ ) {
680 var rec = recordsCache[i];
681 if( rec && rec.doc_id() == id ) return rec;
684 var meth = FETCH_RMODS
685 if(type == 'M') meth = FETCH_MRMODS;
686 var req = new Request(meth, id);
691 function Timer(name, node){
696 Timer.prototype.start =
697 function(){_timerRun(this.name);}
698 Timer.prototype.stop =
699 function(){this.done = true;}
700 function _timerRun(tname) {
704 if(_t.count > 100) return;
706 if( (_t.count % 5) == 0 )
708 _t.node.appendChild(text(str));
709 setTimeout("_timerRun('"+tname+"');", 200);
713 function checkILSEvent(obj) {
714 if( obj.ilsevent != null && obj.ilsevent != 0 )
715 return parseInt(obj.ilsevent);
718 function alertILSEvent(code, msg) {
720 alert( msg + '\n' + $('ilsevent.' + code).innerHTML );
725 function AuthTimer(time) {
726 this.time = (time - LOGOUT_WARNING_TIME) * 1000;
730 AuthTimer.prototype.run = function() {
731 setTimeout('_authTimerAlert()', this.time);
734 function _authTimerAlert() {
735 alert( $('auth_session_expiring').innerHTML );
736 if(!grabUser(null, true)) doLogout();
740 function grabUserByBarcode( authtoken, barcode ) {
741 var req = new Request( FETCH_USER_BYBARCODE, authtoken, barcode );
748 goTo(buildOPACLink({page:HOME}));
752 function buildOrgSel(selector, org, offset) {
753 insertSelectorVal( selector, -1,
754 org.name(), org.id(), null, findOrgDepth(org) - offset );
755 for( var c in org.children() )
756 buildOrgSel( selector, org.children()[c], offset);