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));
79 /* set up some sane defaults */
80 if(isNaN(LOCATION)) LOCATION = 1;
81 if(isNaN(DEPTH)) DEPTH = 0;
82 if(isNaN(OFFSET)) OFFSET = 0;
83 if(isNaN(COUNT)) COUNT = 10;
84 if(isNaN(HITCOUNT)) HITCOUNT = 0;
85 if(isNaN(MRID)) MRID = 0;
86 if(isNaN(RID)) RID = 0;
87 if(isNaN(ORIGLOC)) ORIGLOC = 0;
88 if(isNaN(TOPRANK)) TOPRANK = 1;
91 function initCookies() {
93 var font = fontCookie.get(COOKIE_FONT);
94 if(font) FONTSIZE = font;
95 SKIN = skinCookie.get(COOKIE_SKIN);
98 /* URL param accessors */
99 function getTerm(){return TERM;}
100 function getStype(){return STYPE;}
101 function getLocation(){return LOCATION;}
102 function getDepth(){return DEPTH;}
103 function getForm(){return FORM;}
104 function getOffset(){return OFFSET;}
105 function getDisplayCount(){return COUNT;}
106 function getHitCount(){return HITCOUNT;}
107 function getMrid(){return MRID;};
108 function getRid(){return RID;};
109 function getOrigLocation(){return ORIGLOC;}
110 function getTopRank(){return TOPRANK;}
112 function getSearchBarExtras(){return SBEXTRAS;}
113 function getFontSize(){return FONTSIZE;};
114 function getSkin(){return SKIN;};
117 function findBasePath() {
118 var path = location.pathname;
119 if(!path.match(/.*\.xml$/)) path += "index.xml";
120 var idx = path.indexOf(config.page[findCurrentPage()]);
121 return path.substring(0, idx);
124 function findBaseURL(ssl) {
125 var path = findBasePath();
126 var proto = (ssl) ? "https:" : "http:";
127 return proto + "//" + location.host + path;
128 dump( 'ssl: ' + ssl + 'proto ' + proto );
132 function buildISBNSrc(isbn) {
133 return "http://" + location.host + "/jackets/" + isbn;
137 function buildImageLink(name, ssl) {
138 return findBaseURL(ssl) + "../../../images/" + name;
141 function buildOPACLink(args, slim, ssl) {
147 string = findBaseURL(ssl);
148 if(args.page) string += config.page[args.page];
149 else string += config.page[findCurrentPage()];
154 for( var x in args ) {
155 if(x == "page" || args[x] == null) continue;
156 string += "&" + x + "=" + encodeURIComponent(args[x]);
159 string += _appendParam(TERM, PARAM_TERM, args, getTerm, string);
160 string += _appendParam(STYPE, PARAM_STYPE, args, getStype, string);
161 string += _appendParam(LOCATION, PARAM_LOCATION, args, getLocation, string);
162 string += _appendParam(DEPTH, PARAM_DEPTH, args, getDepth, string);
163 string += _appendParam(FORM, PARAM_FORM, args, getForm, string);
164 string += _appendParam(OFFSET, PARAM_OFFSET, args, getOffset, string);
165 string += _appendParam(COUNT, PARAM_COUNT, args, getDisplayCount, string);
166 string += _appendParam(HITCOUNT, PARAM_HITCOUNT, args, getHitCount, string);
167 string += _appendParam(MRID, PARAM_MRID, args, getMrid, string);
168 string += _appendParam(RID, PARAM_RID, args, getRid, string);
169 string += _appendParam(TOPRANK, PARAM_TOPRANK, args, getTopRank, string);
171 return string.replace(/\&$/,'').replace(/\?\&/,"?");
174 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
176 if( fieldVar != null && overrideArgs[fieldName] == null )
177 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
181 /* ----------------------------------------------------------------------- */
182 function cleanISBN(isbn) {
184 isbn = isbn.toString().replace(/^\s+/,"");
185 var idx = isbn.indexOf(" ");
186 if(idx > -1) { isbn = isbn.substring(0, idx); }
192 /* builds a link that goes to the title listings for a metarecord */
193 function buildTitleLink(rec, link) {
195 link.appendChild(text(normalize(truncate(rec.title(), 65))));
198 args[PARAM_OFFSET] = 0;
199 args[PARAM_MRID] = rec.doc_id();
200 link.setAttribute("href", buildOPACLink(args));
203 function buildTitleDetailLink(rec, link) {
205 link.appendChild(text(normalize(truncate(rec.title(), 65))));
208 args[PARAM_OFFSET] = 0;
209 args[PARAM_RID] = rec.doc_id();
210 link.setAttribute("href", buildOPACLink(args));
213 /* 'type' is one of STYPE_AUTHOR, STYPE_SUBJECT, ... found in config.js
214 'trunc' is the number of characters to show in the string, defaults to 65 */
215 function buildSearchLink(type, string, linknode, trunc) {
216 if(!trunc) trunc = 65;
219 args[PARAM_OFFSET] = 0;
220 args[PARAM_TERM] = string;
221 args[PARAM_STYPE] = type;
222 linknode.appendChild(text(normalize(truncate(string, trunc))));
223 linknode.setAttribute("href", buildOPACLink(args));
227 /* ----------------------------------------------------------------------- */
228 /* user session handling */
229 /* ----------------------------------------------------------------------- */
230 /* session is the login session. If no session is provided, we attempt
231 to find one in the cookies. If 'force' is true we retrieve the
232 user from the server even if there is already a global user present.
233 if ses != G.user.session, we also force a grab */
234 var cookie = new cookieObject("ses", 1, "/", COOKIE_SES);
235 function grabUser(ses, force) {
237 if(!ses) ses = cookie.get(COOKIE_SES);
238 try{if(!ses && isXUL()) ses = xulG['auth_ses'][0];}catch(e){}
239 if(!ses) return false;
244 if(G.user && G.user.session == ses)
247 /* first make sure the session is valid */
248 var request = new Request(FETCH_SESSION, ses );
250 var user = request.result();
251 if( !(typeof user == 'object' && user._isfieldmapper) ) {
256 G.user.fleshed = false;
257 G.user.session = ses;
258 cookie.put(COOKIE_SES, ses);
262 if(G.user.prefs['opac.hits_per_page'])
263 COUNT = parseInt(G.user.prefs['opac.hits_per_page']);
270 /* sets the 'prefs' field of the user object to their preferences
271 and returns the preferences */
272 function grabUserPrefs(user, force) {
273 if(user == null) user = G.user;
274 if(!force && user.prefs) return user.prefs;
275 var req = new Request(FETCH_USER_PREFS, user.session, user.id());
277 user.prefs = req.result();
281 function grabFleshedUser() {
283 if(!G.user || !G.user.session) {
285 if(!G.user || !G.user.session) return null;
288 if(G.user.fleshed) return G.user;
290 var req = new Request(FETCH_FLESHED_USER, G.user.session);
293 G.user = req.result();
295 if(!G.user || G.user.length == 0) {
296 G.user = null; return false;
297 cookie.remove(COOKIE_SES);
300 G.user.session = ses;
301 G.user.fleshed = true;
303 cookie.put(COOKIE_SES, ses); /* update the cookie */
309 var skinCookie = new cookieObject("skin", 1, "/", COOKIE_SKIN);
310 function checkUserSkin(new_skin) {
312 return; /* XXX do some debugging with this... */
314 var user_skin = getSkin();
315 var cur_skin = grabSkinFromURL();
317 if(new_skin) user_skin = new_skin;
322 if(grabUserPrefs()) {
323 user_skin = G.user.prefs["opac.skin"];
324 skinCookie.put( COOKIE_SKIN, user_skin );
330 if(!user_skin) return;
332 if( cur_skin != user_skin ) {
333 var url = buildOPACLink();
334 goTo(url.replace(cur_skin, user_skin));
338 function updateUserSetting(setting, value, user) {
339 if(user == null) user = G.user;
342 var req = new Request( UPDATE_USER_PREFS, user.session, a );
347 function commitUserPrefs() {
348 var req = new Request(
349 UPDATE_USER_PREFS, G.user.session, G.user.prefs );
354 function grabSkinFromURL() {
355 var path = findBasePath();
356 path = path.replace("/xml/", "");
358 for( var i = path.length - 1; i >= 0; i-- ) {
359 var ch = path.charAt(i);
365 for( i = skin.length - 1; i >= 0; i--)
366 skin2 += skin.charAt(i);
372 /* returns a fleshed G.user on success, false on failure */
375 var uname = G.ui.login.username.value;
376 var passwd = G.ui.login.password.value;
378 var init_request = new Request( LOGIN_INIT, uname );
379 init_request.send(true);
380 var seed = init_request.result();
382 if( ! seed || seed == '0') {
383 alert( "Error Communicating with Authentication Server" );
387 var auth_request = new Request( LOGIN_COMPLETE,
388 uname, hex_md5(seed + hex_md5(passwd)), "opac");
391 auth_request.send(true);
392 var auth_result = auth_request.result();
394 if(auth_result == '0' || auth_result == null || auth_result.length == 0) {
395 alert("Login failed");
399 var u = grabUser(auth_result, true);
400 if(u) runEvt( "common", "locationChanged", u.home_ou(), findOrgDepth(u.home_ou()) );
407 function doLogout() {
409 /* be nice and delete the session from the server */
410 if(G.user && G.user.session) {
411 var req = new Request(LOGIN_DELETE, G.user.session);
413 try { req.result(); } catch(E){}
417 cookie.remove(COOKIE_SES);
418 skinCookie.remove(COOKIE_SKIN);
419 checkUserSkin("default");
423 args[PARAM_TERM] = "";
424 args[PARAM_LOCATION] = globalOrgTree.id();
425 args[PARAM_DEPTH] = findOrgDepth(globalOrgTree);
428 goTo(buildOPACLink(args));
432 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); }
433 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
436 /* ----------------------------------------------------------------------- */
437 /* build the org tree */
438 /* ----------------------------------------------------------------------- */
439 function drawOrgTree() {
440 //G.ui.common.org_tree.innerHTML = buildOrgSelector().toString();
441 //buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);
442 setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 10 );
446 function buildOrgSelector(node) {
447 //var tree = new dTree("orgTreeSelector");
448 var tree = new SlimTree(node,'orgTreeSelector');
449 orgTreeSelector = tree;
450 for( var i in orgArraySearcher ) {
451 var node = orgArraySearcher[i];
452 if( node == null ) continue;
453 if(node.parent_ou() == null)
454 tree.addNode(node.id(), -1, node.name(),
455 "javascript:orgSelect(" + node.id() + ");", node.name());
457 tree.addNode(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(),
458 "javascript:orgSelect(" + node.id() + ");", node.name());
461 hideMe($('org_loading_div'));
462 unHideMe($('org_selector_tip'));
466 function orgSelect(id) {
468 runEvt("common", "locationChanged", id, findOrgDepth(id) );
470 removeChildren(G.ui.common.now_searching);
471 G.ui.common.now_searching.appendChild(text(findOrgUnit(id).name()));
474 var fontCookie = new cookieObject("fonts", 1, "/", COOKIE_FONT);
475 function setFontSize(size) {
477 fontCookie.put(COOKIE_FONT, size);
482 var resourceFormats = [
485 "sound recording", "software, multimedia",
490 "three dimensional object" ];
493 function modsFormatToMARC(format) {
499 case "sound recording":
501 case "sound recording-nonmusical":
503 case "sound recording-musical":
505 case "software, multimedia":
511 case "mixed material":
513 case "notated music":
515 case "three dimensional object":
522 function MARCFormatToMods(format) {
528 return "moving image";
530 return "sound recording-nonmusical";
532 return "sound recording-musical";
534 return "software, multimedia";
536 return "still images";
539 return "cartographic";
542 return "mixed material";
545 return "notated music";
547 return "three dimensional object";
552 function setResourcePic( img, resource ) {
553 img.setAttribute( "src", "../../../images/tor/" + resource + ".jpg");
554 img.title = resource;
559 function msg( text ) {
565 function findRecord(id,type) {
567 for( var i = 0; i != recordsCache.length; i++ ) {
568 var rec = recordsCache[i];
569 if( rec && rec.doc_id() == id ) return rec;
572 var meth = FETCH_RMODS
573 if(type == 'M') meth = FETCH_MRMODS;
574 var req = new Request(meth, id);
579 function Timer(name, node){
584 Timer.prototype.start =
585 function(){_timerRun(this.name);}
586 Timer.prototype.stop =
587 function(){this.done = true;}
588 function _timerRun(tname) {
593 if( (_t.count % 5) == 0 )
595 _t.node.appendChild(text(str));
596 setTimeout("_timerRun('"+tname+"');", 200);