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 );
255 var user = request.result();
256 if( !(typeof user == 'object' && user._isfieldmapper) ) {
261 G.user.fleshed = false;
262 G.user.session = ses;
263 cookie.put(COOKIE_SES, ses);
267 if(G.user.prefs['opac.hits_per_page'])
268 COUNT = parseInt(G.user.prefs['opac.hits_per_page']);
275 /* sets the 'prefs' field of the user object to their preferences
276 and returns the preferences */
277 function grabUserPrefs(user, force) {
278 if(user == null) user = G.user;
279 if(!force && user.prefs) return user.prefs;
280 var req = new Request(FETCH_USER_PREFS, user.session, user.id());
282 user.prefs = req.result();
286 function grabFleshedUser() {
288 if(!G.user || !G.user.session) {
290 if(!G.user || !G.user.session) return null;
293 if(G.user.fleshed) return G.user;
295 var req = new Request(FETCH_FLESHED_USER, G.user.session);
298 G.user = req.result();
300 if(!G.user || G.user.length == 0) {
301 G.user = null; return false;
302 cookie.remove(COOKIE_SES);
305 G.user.session = ses;
306 G.user.fleshed = true;
308 cookie.put(COOKIE_SES, ses); /* update the cookie */
314 var skinCookie = new cookieObject("skin", 1, "/", COOKIE_SKIN);
315 function checkUserSkin(new_skin) {
317 return; /* XXX do some debugging with this... */
319 var user_skin = getSkin();
320 var cur_skin = grabSkinFromURL();
322 if(new_skin) user_skin = new_skin;
327 if(grabUserPrefs()) {
328 user_skin = G.user.prefs["opac.skin"];
329 skinCookie.put( COOKIE_SKIN, user_skin );
335 if(!user_skin) return;
337 if( cur_skin != user_skin ) {
338 var url = buildOPACLink();
339 goTo(url.replace(cur_skin, user_skin));
343 function updateUserSetting(setting, value, user) {
344 if(user == null) user = G.user;
347 var req = new Request( UPDATE_USER_PREFS, user.session, a );
352 function commitUserPrefs() {
353 var req = new Request(
354 UPDATE_USER_PREFS, G.user.session, G.user.prefs );
359 function grabSkinFromURL() {
360 var path = findBasePath();
361 path = path.replace("/xml/", "");
363 for( var i = path.length - 1; i >= 0; i-- ) {
364 var ch = path.charAt(i);
370 for( i = skin.length - 1; i >= 0; i--)
371 skin2 += skin.charAt(i);
377 /* returns a fleshed G.user on success, false on failure */
380 var uname = G.ui.login.username.value;
381 var passwd = G.ui.login.password.value;
383 var init_request = new Request( LOGIN_INIT, uname );
384 init_request.send(true);
385 var seed = init_request.result();
387 if( ! seed || seed == '0') {
388 alert( "Error Communicating with Authentication Server" );
392 var auth_request = new Request( LOGIN_COMPLETE,
393 uname, hex_md5(seed + hex_md5(passwd)), "opac", getOrigLocation());
396 auth_request.send(true);
397 var auth_result = auth_request.result();
399 var code = checkILSEvent(auth_result);
400 if(code) { alertILSEvent(code); return null; }
402 var u = grabUser(auth_result.payload.authtoken, true);
403 AUTHTIME = parseInt(auth_result.payload.authtime);
404 if(u) runEvt( "common", "locationChanged", u.home_ou(), findOrgDepth(u.home_ou()) );
411 function doLogout() {
413 /* be nice and delete the session from the server */
414 if(G.user && G.user.session) {
415 var req = new Request(LOGIN_DELETE, G.user.session);
417 try { req.result(); } catch(E){}
421 cookie.remove(COOKIE_SES);
422 skinCookie.remove(COOKIE_SKIN);
423 checkUserSkin("default");
427 args[PARAM_TERM] = "";
428 args[PARAM_LOCATION] = globalOrgTree.id();
429 args[PARAM_DEPTH] = findOrgDepth(globalOrgTree);
432 goTo(buildOPACLink(args));
436 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); }
437 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
440 /* ----------------------------------------------------------------------- */
441 /* build the org tree */
442 /* ----------------------------------------------------------------------- */
443 function drawOrgTree() {
444 //G.ui.common.org_tree.innerHTML = buildOrgSelector().toString();
445 //buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);
446 setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 10 );
450 function buildOrgSelector(node) {
451 //var tree = new dTree("orgTreeSelector");
452 var tree = new SlimTree(node,'orgTreeSelector');
453 orgTreeSelector = tree;
454 for( var i in orgArraySearcher ) {
455 var node = orgArraySearcher[i];
456 if( node == null ) continue;
457 if(node.parent_ou() == null)
458 tree.addNode(node.id(), -1, node.name(),
459 "javascript:orgSelect(" + node.id() + ");", node.name());
461 tree.addNode(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(),
462 "javascript:orgSelect(" + node.id() + ");", node.name());
465 hideMe($('org_loading_div'));
466 unHideMe($('org_selector_tip'));
470 function orgSelect(id) {
472 runEvt("common", "locationChanged", id, findOrgDepth(id) );
474 removeChildren(G.ui.common.now_searching);
475 G.ui.common.now_searching.appendChild(text(findOrgUnit(id).name()));
478 var fontCookie = new cookieObject("fonts", 1, "/", COOKIE_FONT);
479 function setFontSize(size) {
481 fontCookie.put(COOKIE_FONT, size);
486 var resourceFormats = [
489 "sound recording", "software, multimedia",
494 "three dimensional object" ];
497 function modsFormatToMARC(format) {
503 case "sound recording":
505 case "sound recording-nonmusical":
507 case "sound recording-musical":
509 case "software, multimedia":
515 case "mixed material":
517 case "notated music":
519 case "three dimensional object":
526 function MARCFormatToMods(format) {
532 return "moving image";
534 return "sound recording-nonmusical";
536 return "sound recording-musical";
538 return "software, multimedia";
540 return "still images";
543 return "cartographic";
546 return "mixed material";
549 return "notated music";
551 return "three dimensional object";
556 function setResourcePic( img, resource ) {
557 img.setAttribute( "src", "../../../images/tor/" + resource + ".jpg");
558 img.title = resource;
563 function msg( text ) {
569 function findRecord(id,type) {
571 for( var i = 0; i != recordsCache.length; i++ ) {
572 var rec = recordsCache[i];
573 if( rec && rec.doc_id() == id ) return rec;
576 var meth = FETCH_RMODS
577 if(type == 'M') meth = FETCH_MRMODS;
578 var req = new Request(meth, id);
583 function Timer(name, node){
588 Timer.prototype.start =
589 function(){_timerRun(this.name);}
590 Timer.prototype.stop =
591 function(){this.done = true;}
592 function _timerRun(tname) {
597 if( (_t.count % 5) == 0 )
599 _t.node.appendChild(text(str));
600 setTimeout("_timerRun('"+tname+"');", 200);
606 function checkILSEvent(obj) {
607 if( obj.ilsevent != null && obj.ilsevent != 0 )
608 return parseInt(obj.ilsevent);
611 function alertILSEvent(code) {
613 alert( $('ilsevent.' + code).innerHTML );