1 /* - Request ------------------------------------------------------------- */
2 function Request(type) {
3 var s = type.split(":");
4 if(s[2] == "1" && isXUL()) s[1] += ".staff";
5 this.request = new RemoteRequest(s[0], s[1]);
6 for( var x = 1; x!= arguments.length; x++ )
7 this.request.addParam(arguments[x]);
10 Request.prototype.callback = function(cal) { this.request.setCompleteCallback(cal); }
11 Request.prototype.send = function(block){this.request.send(block);}
12 Request.prototype.result = function(){return this.request.getResultObject();}
14 function showCanvas() {
15 for( var x in G.ui.altcanvas ) {
16 hideMe(G.ui.altcanvas[x]);
18 hideMe(G.ui.common.loading);
19 unHideMe(G.ui.common.canvas_main);
20 G.ui.searchbar.text.focus();
23 function swapCanvas(newNode) {
24 for( var x in G.ui.altcanvas )
25 hideMe(G.ui.altcanvas[x]);
27 hideMe(G.ui.common.loading);
28 hideMe(G.ui.common.canvas_main);
32 /* finds the name of the current page */
33 var currentPage = null;
34 function findCurrentPage() {
35 if(currentPage) return currentPage;
38 for( var p in config.page ) pages.push(config.page[p]);
39 pages = pages.sort( function(a,b){ return - (a.length - b.length); } );
41 var path = location.pathname;
42 if(!path.match(/.*\.xml$/))
43 path += "index.xml"; /* in case they go to / */
46 for( var p in pages ) {
47 if( path.indexOf(pages[p]) != -1)
51 for( var p in config.page ) {
52 if(config.page[p] == page) {
61 /* sets all of the params values ----------------------------- */
62 function initParams() {
65 TERM = cgi.param(PARAM_TERM);
66 STYPE = cgi.param(PARAM_STYPE);
67 FORM = cgi.param(PARAM_FORM);
69 LOCATION = parseInt(cgi.param(PARAM_LOCATION));
70 ORIGLOC = parseInt(cgi.param(PARAM_ORIGLOC));
71 DEPTH = parseInt(cgi.param(PARAM_DEPTH));
72 OFFSET = parseInt(cgi.param(PARAM_OFFSET));
73 COUNT = parseInt(cgi.param(PARAM_COUNT));
74 HITCOUNT = parseInt(cgi.param(PARAM_HITCOUNT));
75 MRID = parseInt(cgi.param(PARAM_MRID));
76 RID = parseInt(cgi.param(PARAM_RID));
77 TOPRANK = parseFloat(cgi.param(PARAM_TOPRANK));
78 AUTHTIME = parseInt(cgi.param(PARAM_AUTHTIME));
80 /* set up some sane defaults */
81 if(isNaN(LOCATION)) LOCATION = 1;
82 if(isNaN(DEPTH)) DEPTH = 0;
83 if(isNaN(OFFSET)) OFFSET = 0;
84 if(isNaN(COUNT)) COUNT = 10;
85 if(isNaN(HITCOUNT)) HITCOUNT = 0;
86 if(isNaN(MRID)) MRID = 0;
87 if(isNaN(RID)) RID = 0;
88 if(isNaN(ORIGLOC)) ORIGLOC = 0;
89 if(isNaN(TOPRANK)) TOPRANK = 1;
90 if(isNaN(AUTHTIME)) AUTHTIME = 1;
93 function initCookies() {
95 var font = fontCookie.get(COOKIE_FONT);
96 if(font) FONTSIZE = font;
97 SKIN = skinCookie.get(COOKIE_SKIN);
100 /* URL param accessors */
101 function getTerm(){return TERM;}
102 function getStype(){return STYPE;}
103 function getLocation(){return LOCATION;}
104 function getDepth(){return DEPTH;}
105 function getForm(){return FORM;}
106 function getOffset(){return OFFSET;}
107 function getDisplayCount(){return COUNT;}
108 function getHitCount(){return HITCOUNT;}
109 function getMrid(){return MRID;};
110 function getRid(){return RID;};
111 function getOrigLocation(){return ORIGLOC;}
112 function getTopRank(){return TOPRANK;}
113 function getAuthtime() { return AUTHTIME; }
115 function getSearchBarExtras(){return SBEXTRAS;}
116 function getFontSize(){return FONTSIZE;};
117 function getSkin(){return SKIN;};
120 function findBasePath() {
121 var path = location.pathname;
122 if(!path.match(/.*\.xml$/)) path += "index.xml";
123 var idx = path.indexOf(config.page[findCurrentPage()]);
124 return path.substring(0, idx);
127 function findBaseURL(ssl) {
128 var path = findBasePath();
129 var proto = (ssl) ? "https:" : "http:";
130 return proto + "//" + location.host + path;
131 dump( 'ssl: ' + ssl + 'proto ' + proto );
135 function buildISBNSrc(isbn) {
136 return "http://" + location.host + "/jackets/" + isbn;
140 function buildImageLink(name, ssl) {
141 return findBaseURL(ssl) + "../../../images/" + name;
144 function buildOPACLink(args, slim, ssl) {
150 string = findBaseURL(ssl);
151 if(args.page) string += config.page[args.page];
152 else string += config.page[findCurrentPage()];
157 for( var x in args ) {
158 if(x == "page" || args[x] == null) continue;
159 string += "&" + x + "=" + encodeURIComponent(args[x]);
162 string += _appendParam(ORIGLOC, PARAM_ORIGLOC, args, getOrigLocation, string);
163 string += _appendParam(TERM, PARAM_TERM, args, getTerm, string);
164 string += _appendParam(STYPE, PARAM_STYPE, args, getStype, string);
165 string += _appendParam(LOCATION, PARAM_LOCATION, args, getLocation, string);
166 string += _appendParam(DEPTH, PARAM_DEPTH, args, getDepth, string);
167 string += _appendParam(FORM, PARAM_FORM, args, getForm, string);
168 string += _appendParam(OFFSET, PARAM_OFFSET, args, getOffset, string);
169 string += _appendParam(COUNT, PARAM_COUNT, args, getDisplayCount, string);
170 string += _appendParam(HITCOUNT, PARAM_HITCOUNT, args, getHitCount, string);
171 string += _appendParam(MRID, PARAM_MRID, args, getMrid, string);
172 string += _appendParam(RID, PARAM_RID, args, getRid, string);
173 string += _appendParam(TOPRANK, PARAM_TOPRANK, args, getTopRank, string);
174 string += _appendParam(AUTHTIME, PARAM_AUTHTIME, args, getAuthtime, string);
176 return string.replace(/\&$/,'').replace(/\?\&/,"?");
179 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
181 if( fieldVar != null && overrideArgs[fieldName] == null )
182 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
186 /* ----------------------------------------------------------------------- */
187 function cleanISBN(isbn) {
189 isbn = isbn.toString().replace(/^\s+/,"");
190 var idx = isbn.indexOf(" ");
191 if(idx > -1) { isbn = isbn.substring(0, idx); }
197 /* builds a link that goes to the title listings for a metarecord */
198 function buildTitleLink(rec, link) {
200 link.appendChild(text(normalize(truncate(rec.title(), 65))));
203 args[PARAM_OFFSET] = 0;
204 args[PARAM_MRID] = rec.doc_id();
205 link.setAttribute("href", buildOPACLink(args));
208 function buildTitleDetailLink(rec, link) {
210 link.appendChild(text(normalize(truncate(rec.title(), 65))));
213 args[PARAM_OFFSET] = 0;
214 args[PARAM_RID] = rec.doc_id();
215 link.setAttribute("href", buildOPACLink(args));
218 /* 'type' is one of STYPE_AUTHOR, STYPE_SUBJECT, ... found in config.js
219 'trunc' is the number of characters to show in the string, defaults to 65 */
220 function buildSearchLink(type, string, linknode, trunc) {
221 if(!trunc) trunc = 65;
224 args[PARAM_OFFSET] = 0;
225 args[PARAM_TERM] = string;
226 args[PARAM_STYPE] = type;
227 linknode.appendChild(text(normalize(truncate(string, trunc))));
228 linknode.setAttribute("href", buildOPACLink(args));
232 /* ----------------------------------------------------------------------- */
233 /* user session handling */
234 /* ----------------------------------------------------------------------- */
235 /* session is the login session. If no session is provided, we attempt
236 to find one in the cookies. If 'force' is true we retrieve the
237 user from the server even if there is already a global user present.
238 if ses != G.user.session, we also force a grab */
239 var cookie = new cookieObject("ses", 1, "/", COOKIE_SES);
240 function grabUser(ses, force) {
242 if(!ses) ses = cookie.get(COOKIE_SES);
243 try{if(!ses && isXUL()) ses = xulG['auth_ses'][0];}catch(e){}
244 if(!ses) return false;
249 if(G.user && G.user.session == ses)
252 /* first make sure the session is valid */
253 var request = new Request(FETCH_SESSION, ses, 1 );
255 var user = request.result();
256 //if(checkILSEvent(user)) throw user; /* unable to grab the session */
258 if(checkILSEvent(user)) {
260 return false; /* unable to grab the session */
263 if( !(typeof user == 'object' && user._isfieldmapper) ) {
269 G.user.fleshed = false;
270 G.user.session = ses;
271 cookie.put(COOKIE_SES, ses);
275 if(G.user.prefs['opac.hits_per_page'])
276 COUNT = parseInt(G.user.prefs['opac.hits_per_page']);
278 //new AuthTimer(getAuthtime()).run();
279 new AuthTimer(20).run();
286 /* sets the 'prefs' field of the user object to their preferences
287 and returns the preferences */
288 function grabUserPrefs(user, force) {
289 if(user == null) user = G.user;
290 if(!force && user.prefs) return user.prefs;
291 var req = new Request(FETCH_USER_PREFS, user.session, user.id());
293 user.prefs = req.result();
297 function grabFleshedUser() {
299 if(!G.user || !G.user.session) {
301 if(!G.user || !G.user.session) return null;
304 if(G.user.fleshed) return G.user;
306 var req = new Request(FETCH_FLESHED_USER, G.user.session);
309 G.user = req.result();
311 if(!G.user || G.user.length == 0) {
312 G.user = null; return false;
313 cookie.remove(COOKIE_SES);
316 G.user.session = ses;
317 G.user.fleshed = true;
319 cookie.put(COOKIE_SES, ses); /* update the cookie */
325 var skinCookie = new cookieObject("skin", 1, "/", COOKIE_SKIN);
326 function checkUserSkin(new_skin) {
328 return; /* XXX do some debugging with this... */
330 var user_skin = getSkin();
331 var cur_skin = grabSkinFromURL();
333 if(new_skin) user_skin = new_skin;
338 if(grabUserPrefs()) {
339 user_skin = G.user.prefs["opac.skin"];
340 skinCookie.put( COOKIE_SKIN, user_skin );
346 if(!user_skin) return;
348 if( cur_skin != user_skin ) {
349 var url = buildOPACLink();
350 goTo(url.replace(cur_skin, user_skin));
354 function updateUserSetting(setting, value, user) {
355 if(user == null) user = G.user;
358 var req = new Request( UPDATE_USER_PREFS, user.session, a );
363 function commitUserPrefs() {
364 var req = new Request(
365 UPDATE_USER_PREFS, G.user.session, G.user.prefs );
370 function grabSkinFromURL() {
371 var path = findBasePath();
372 path = path.replace("/xml/", "");
374 for( var i = path.length - 1; i >= 0; i-- ) {
375 var ch = path.charAt(i);
381 for( i = skin.length - 1; i >= 0; i--)
382 skin2 += skin.charAt(i);
388 /* returns a fleshed G.user on success, false on failure */
391 var uname = G.ui.login.username.value;
392 var passwd = G.ui.login.password.value;
394 var init_request = new Request( LOGIN_INIT, uname );
395 init_request.send(true);
396 var seed = init_request.result();
398 if( ! seed || seed == '0') {
399 alert( "Error Communicating with Authentication Server" );
403 var auth_request = new Request( LOGIN_COMPLETE,
404 uname, hex_md5(seed + hex_md5(passwd)), "opac", getOrigLocation());
407 auth_request.send(true);
408 var auth_result = auth_request.result();
410 var code = checkILSEvent(auth_result);
411 if(code) { alertILSEvent(code); return null; }
413 var u = grabUser(auth_result.payload.authtoken, true);
414 AUTHTIME = parseInt(auth_result.payload.authtime);
415 if(u) runEvt( "common", "locationChanged", u.home_ou(), findOrgDepth(u.home_ou()) );
422 function doLogout() {
424 /* be nice and delete the session from the server */
425 if(G.user && G.user.session) {
426 var req = new Request(LOGIN_DELETE, G.user.session);
428 try { req.result(); } catch(E){}
432 cookie.remove(COOKIE_SES);
433 skinCookie.remove(COOKIE_SKIN);
434 checkUserSkin("default");
438 args[PARAM_TERM] = "";
439 args[PARAM_LOCATION] = globalOrgTree.id();
440 args[PARAM_DEPTH] = findOrgDepth(globalOrgTree);
443 goTo(buildOPACLink(args));
447 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); }
448 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
451 /* ----------------------------------------------------------------------- */
452 /* build the org tree */
453 /* ----------------------------------------------------------------------- */
454 function drawOrgTree() {
455 //G.ui.common.org_tree.innerHTML = buildOrgSelector().toString();
456 //buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);
457 setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 10 );
461 function buildOrgSelector(node) {
462 //var tree = new dTree("orgTreeSelector");
463 var tree = new SlimTree(node,'orgTreeSelector');
464 orgTreeSelector = tree;
465 for( var i in orgArraySearcher ) {
466 var node = orgArraySearcher[i];
467 if( node == null ) continue;
468 if(node.parent_ou() == null)
469 tree.addNode(node.id(), -1, node.name(),
470 "javascript:orgSelect(" + node.id() + ");", node.name());
472 tree.addNode(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(),
473 "javascript:orgSelect(" + node.id() + ");", node.name());
476 hideMe($('org_loading_div'));
477 unHideMe($('org_selector_tip'));
481 function orgSelect(id) {
483 runEvt("common", "locationChanged", id, findOrgDepth(id) );
485 removeChildren(G.ui.common.now_searching);
486 G.ui.common.now_searching.appendChild(text(findOrgUnit(id).name()));
489 var fontCookie = new cookieObject("fonts", 1, "/", COOKIE_FONT);
490 function setFontSize(size) {
492 fontCookie.put(COOKIE_FONT, size);
497 var resourceFormats = [
500 "sound recording", "software, multimedia",
505 "three dimensional object" ];
508 function modsFormatToMARC(format) {
514 case "sound recording":
516 case "sound recording-nonmusical":
518 case "sound recording-musical":
520 case "software, multimedia":
526 case "mixed material":
528 case "notated music":
530 case "three dimensional object":
537 function MARCFormatToMods(format) {
543 return "moving image";
545 return "sound recording-nonmusical";
547 return "sound recording-musical";
549 return "software, multimedia";
551 return "still images";
554 return "cartographic";
557 return "mixed material";
560 return "notated music";
562 return "three dimensional object";
567 function setResourcePic( img, resource ) {
568 img.setAttribute( "src", "../../../images/tor/" + resource + ".jpg");
569 img.title = resource;
574 function msg( text ) {
580 function findRecord(id,type) {
582 for( var i = 0; i != recordsCache.length; i++ ) {
583 var rec = recordsCache[i];
584 if( rec && rec.doc_id() == id ) return rec;
587 var meth = FETCH_RMODS
588 if(type == 'M') meth = FETCH_MRMODS;
589 var req = new Request(meth, id);
594 function Timer(name, node){
599 Timer.prototype.start =
600 function(){_timerRun(this.name);}
601 Timer.prototype.stop =
602 function(){this.done = true;}
603 function _timerRun(tname) {
608 if( (_t.count % 5) == 0 )
610 _t.node.appendChild(text(str));
611 setTimeout("_timerRun('"+tname+"');", 200);
615 function checkILSEvent(obj) {
616 if( obj.ilsevent != null && obj.ilsevent != 0 )
617 return parseInt(obj.ilsevent);
620 function alertILSEvent(code) {
621 alert( $('ilsevent.' + code).innerHTML );
626 function AuthTimer(time) {
627 this.time = (time - LOGOUT_WARNING_TIME) * 1000;
631 AuthTimer.prototype.run = function() {
632 setTimeout('_authTimerAlert()', this.time);
635 function _authTimerAlert() {
636 if( confirm( $('auth_session_expiring').innerHTML ) )
638 else grabUser(null, true);