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();
285 /* sets the 'prefs' field of the user object to their preferences
286 and returns the preferences */
287 function grabUserPrefs(user, force) {
288 if(user == null) user = G.user;
289 if(!force && user.prefs) return user.prefs;
290 var req = new Request(FETCH_USER_PREFS, user.session, user.id());
292 user.prefs = req.result();
296 function grabFleshedUser() {
298 if(!G.user || !G.user.session) {
300 if(!G.user || !G.user.session) return null;
303 if(G.user.fleshed) return G.user;
305 var req = new Request(FETCH_FLESHED_USER, G.user.session);
308 G.user = req.result();
310 if(!G.user || G.user.length == 0) {
311 G.user = null; return false;
312 cookie.remove(COOKIE_SES);
315 G.user.session = ses;
316 G.user.fleshed = true;
318 cookie.put(COOKIE_SES, ses); /* update the cookie */
324 var skinCookie = new cookieObject("skin", 1, "/", COOKIE_SKIN);
325 function checkUserSkin(new_skin) {
327 return; /* XXX do some debugging with this... */
329 var user_skin = getSkin();
330 var cur_skin = grabSkinFromURL();
332 if(new_skin) user_skin = new_skin;
337 if(grabUserPrefs()) {
338 user_skin = G.user.prefs["opac.skin"];
339 skinCookie.put( COOKIE_SKIN, user_skin );
345 if(!user_skin) return;
347 if( cur_skin != user_skin ) {
348 var url = buildOPACLink();
349 goTo(url.replace(cur_skin, user_skin));
353 function updateUserSetting(setting, value, user) {
354 if(user == null) user = G.user;
357 var req = new Request( UPDATE_USER_PREFS, user.session, a );
362 function commitUserPrefs() {
363 var req = new Request(
364 UPDATE_USER_PREFS, G.user.session, G.user.prefs );
369 function grabSkinFromURL() {
370 var path = findBasePath();
371 path = path.replace("/xml/", "");
373 for( var i = path.length - 1; i >= 0; i-- ) {
374 var ch = path.charAt(i);
380 for( i = skin.length - 1; i >= 0; i--)
381 skin2 += skin.charAt(i);
387 /* returns a fleshed G.user on success, false on failure */
390 var uname = G.ui.login.username.value;
391 var passwd = G.ui.login.password.value;
393 var init_request = new Request( LOGIN_INIT, uname );
394 init_request.send(true);
395 var seed = init_request.result();
397 if( ! seed || seed == '0') {
398 alert( "Error Communicating with Authentication Server" );
402 var auth_request = new Request( LOGIN_COMPLETE,
403 uname, hex_md5(seed + hex_md5(passwd)), "opac", getOrigLocation());
406 auth_request.send(true);
407 var auth_result = auth_request.result();
409 var code = checkILSEvent(auth_result);
410 if(code) { alertILSEvent(code); return null; }
412 var u = grabUser(auth_result.payload.authtoken, true);
413 AUTHTIME = parseInt(auth_result.payload.authtime);
414 if(u) runEvt( "common", "locationChanged", u.home_ou(), findOrgDepth(u.home_ou()) );
421 function doLogout() {
423 /* be nice and delete the session from the server */
424 if(G.user && G.user.session) {
425 var req = new Request(LOGIN_DELETE, G.user.session);
427 try { req.result(); } catch(E){}
431 cookie.remove(COOKIE_SES);
432 skinCookie.remove(COOKIE_SKIN);
433 checkUserSkin("default");
437 args[PARAM_TERM] = "";
438 args[PARAM_LOCATION] = globalOrgTree.id();
439 args[PARAM_DEPTH] = findOrgDepth(globalOrgTree);
442 goTo(buildOPACLink(args));
446 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); }
447 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
450 /* ----------------------------------------------------------------------- */
451 /* build the org tree */
452 /* ----------------------------------------------------------------------- */
453 function drawOrgTree() {
454 //G.ui.common.org_tree.innerHTML = buildOrgSelector().toString();
455 //buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);
456 setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 10 );
460 function buildOrgSelector(node) {
461 //var tree = new dTree("orgTreeSelector");
462 var tree = new SlimTree(node,'orgTreeSelector');
463 orgTreeSelector = tree;
464 for( var i in orgArraySearcher ) {
465 var node = orgArraySearcher[i];
466 if( node == null ) continue;
467 if(node.parent_ou() == null)
468 tree.addNode(node.id(), -1, node.name(),
469 "javascript:orgSelect(" + node.id() + ");", node.name());
471 tree.addNode(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(),
472 "javascript:orgSelect(" + node.id() + ");", node.name());
475 hideMe($('org_loading_div'));
476 unHideMe($('org_selector_tip'));
480 function orgSelect(id) {
482 runEvt("common", "locationChanged", id, findOrgDepth(id) );
484 removeChildren(G.ui.common.now_searching);
485 G.ui.common.now_searching.appendChild(text(findOrgUnit(id).name()));
488 var fontCookie = new cookieObject("fonts", 1, "/", COOKIE_FONT);
489 function setFontSize(size) {
491 fontCookie.put(COOKIE_FONT, size);
496 var resourceFormats = [
499 "sound recording", "software, multimedia",
504 "three dimensional object" ];
507 function modsFormatToMARC(format) {
513 case "sound recording":
515 case "sound recording-nonmusical":
517 case "sound recording-musical":
519 case "software, multimedia":
525 case "mixed material":
527 case "notated music":
529 case "three dimensional object":
536 function MARCFormatToMods(format) {
542 return "moving image";
544 return "sound recording-nonmusical";
546 return "sound recording-musical";
548 return "software, multimedia";
550 return "still images";
553 return "cartographic";
556 return "mixed material";
559 return "notated music";
561 return "three dimensional object";
566 function setResourcePic( img, resource ) {
567 img.setAttribute( "src", "../../../images/tor/" + resource + ".jpg");
568 img.title = resource;
573 function msg( text ) {
579 function findRecord(id,type) {
581 for( var i = 0; i != recordsCache.length; i++ ) {
582 var rec = recordsCache[i];
583 if( rec && rec.doc_id() == id ) return rec;
586 var meth = FETCH_RMODS
587 if(type == 'M') meth = FETCH_MRMODS;
588 var req = new Request(meth, id);
593 function Timer(name, node){
598 Timer.prototype.start =
599 function(){_timerRun(this.name);}
600 Timer.prototype.stop =
601 function(){this.done = true;}
602 function _timerRun(tname) {
607 if( (_t.count % 5) == 0 )
609 _t.node.appendChild(text(str));
610 setTimeout("_timerRun('"+tname+"');", 200);
614 function checkILSEvent(obj) {
615 if( obj.ilsevent != null && obj.ilsevent != 0 )
616 return parseInt(obj.ilsevent);
619 function alertILSEvent(code, msg) {
620 alert( msg + '\n' + $('ilsevent.' + code).innerHTML );
625 function AuthTimer(time) {
626 this.time = (time - LOGOUT_WARNING_TIME) * 1000;
630 AuthTimer.prototype.run = function() {
631 setTimeout('_authTimerAlert()', this.time);
634 function _authTimerAlert() {
635 if( confirm( $('auth_session_expiring').innerHTML ) )
638 if(!grabUser(null, true)) {
639 alert($('auth_session_expired').innerHTML);
645 function grabUserByBarcode( authtoken, barcode ) {
646 var req = new Request( FETCH_USER_BYBARCODE, authtoken, barcode );