]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/opac/common/js/opac_utils.js
98d599b08d2bb519ad464fbc279ce8197414f450
[working/Evergreen.git] / Open-ILS / web / opac / common / js / opac_utils.js
1
2
3 /* - Request ------------------------------------------------------------- */
4 function Request(type) {
5         var s = type.split(":");
6         if(s[2] == "1" && isXUL()) s[1] += ".staff";
7         this.request = new RemoteRequest(s[0], s[1]);
8         for( var x = 1; x!= arguments.length; x++ ) 
9                 this.request.addParam(arguments[x]);
10 }
11
12 Request.prototype.callback = function(cal) { this.request.setCompleteCallback(cal); }
13 Request.prototype.send          = function(block){this.request.send(block);}
14 Request.prototype.result        = function(){return this.request.getResultObject();}
15 /* ----------------------------------------------------------------------- */
16
17 function showCanvas() { setTimeout(_showCanvas, 200); }
18 function _showCanvas() {
19         for( var x in G.ui.altcanvas ) {
20                 hideMe(G.ui.altcanvas[x]);
21         }
22         hideMe(G.ui.common.loading);
23         unHideMe(G.ui.common.canvas_main);
24         G.ui.searchbar.text.focus(); 
25 }
26
27 var newCanvasNode;
28 function swapCanvas(newNode) { newCanvasNode = newNode; setTimeout(_swapCanvas, 200); }
29 function _swapCanvas() {
30         for( var x in G.ui.altcanvas ) 
31                 hideMe(G.ui.altcanvas[x]);
32
33         hideMe(G.ui.common.loading);
34         hideMe(G.ui.common.canvas_main);
35         unHideMe(newCanvasNode);
36 }
37
38 /* finds the name of the current page */
39 var currentPage = null;
40 function findCurrentPage() {
41         if(currentPage) return currentPage;
42
43         var pages = [];
44         for( var p in config.page ) pages.push(config.page[p]);
45         pages = pages.sort( function(a,b){ return - (a.length - b.length); } );
46
47         var path = location.pathname;
48         if(!path.match(/.*\.xml$/))
49                 path += "index.xml"; /* in case they go to  / */
50
51         var page = null;
52         for( var p in pages ) {
53                 if( path.indexOf(pages[p]) != -1)
54                         page = pages[p];
55         }
56
57         for( var p in config.page ) {
58                 if(config.page[p] == page) {
59                         currentPage = p;
60                         return p;
61                 }
62         }
63
64         return null;
65 }
66
67
68 /* sets all of the params values  ----------------------------- */
69 var TERM,  STYPE,  LOCATION,  DEPTH,  FORM, OFFSET,  COUNT,  
70          HITCOUNT,  RANKS, SEARCHBAR_EXTRAS, FONTSIZE;
71
72 function initParams() {
73         var cgi = new CGI();    
74
75         TERM    = cgi.param(PARAM_TERM);
76         STYPE   = cgi.param(PARAM_STYPE);
77         FORM    = cgi.param(PARAM_FORM);
78
79         LOCATION        = parseInt(cgi.param(PARAM_LOCATION));
80         DEPTH           = parseInt(cgi.param(PARAM_DEPTH));
81         OFFSET  = parseInt(cgi.param(PARAM_OFFSET));
82         COUNT           = parseInt(cgi.param(PARAM_COUNT));
83         HITCOUNT        = parseInt(cgi.param(PARAM_HITCOUNT));
84         MRID            = parseInt(cgi.param(PARAM_MRID));
85         RID             = parseInt(cgi.param(PARAM_RID));
86
87         /* set up some sane defaults */
88         if(isNaN(LOCATION))     LOCATION        = 1;
89         if(isNaN(DEPTH))                DEPTH           = 0;
90         if(isNaN(OFFSET))               OFFSET  = 0;
91         if(isNaN(COUNT))                COUNT           = 10;
92         if(isNaN(HITCOUNT))     HITCOUNT        = 0;
93         if(isNaN(SEARCHBAR_EXTRAS))     SEARCHBAR_EXTRAS        = 0;
94         if(isNaN(MRID))         MRID            = 0;
95         if(isNaN(RID))                  RID             = 0;
96 }
97
98 function initCookies() {
99         FONTSIZE = "medium";
100         var font = fontCookie.get(COOKIE_FONT);
101         if(font) FONTSIZE = font;
102 }
103
104 /* URL param accessors */
105 function getTerm(){return TERM;}
106 function getStype(){return STYPE;}
107 function getLocation(){return LOCATION;}
108 function getDepth(){return DEPTH;}
109 function getForm(){return FORM;}
110 function getOffset(){return OFFSET;}
111 function getDisplayCount(){return COUNT;}
112 function getHitCount(){return HITCOUNT;}
113 function getSearchBarExtras(){return SEARCHBAR_EXTRAS;}
114 function getMrid(){return MRID;};
115 function getRid(){return RID;};
116
117 function getFontSize(){return FONTSIZE;};
118
119
120
121 /* builds an opac URL.  If no page is defined, the current page is used
122         if slim, then only everything after the ? is returned (no host portion) */
123 function findBasePath() {
124         var path = location.pathname;
125         if(!path.match(/.*\.xml$/)) path += "index.xml"; 
126         var idx = path.indexOf(config.page[findCurrentPage()]);
127         return path.substring(0, idx);
128 }
129
130 function findBaseURL(ssl) {
131         var path = findBasePath();
132         var proto = location.protocol;
133         if(ssl) proto = "https";
134         return location.protocol + "//" + location.host + path;
135 }
136
137 function buildImageLink(name, ssl) {
138         return findBaseURL(ssl) + "../../../images/" + name;
139 }
140
141 function  buildOPACLink(args, slim, ssl) {
142
143         if(!args) args = {};
144         var string = "";
145
146         if(!slim) {
147                 string = findBaseURL(ssl);
148                 if(args.page) string += config.page[args.page];
149                 else string += config.page[findCurrentPage()];
150         }
151
152         string += "?";
153
154         for( var x in args ) {
155                 if(x == "page" || args[x] == null) continue;
156                 string += "&" + x + "=" + encodeURIComponent(args[x]);
157         }
158
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         return string.replace(/\&$/,'').replace(/\?\&/,"?");    
170 }
171
172 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
173         var ret = "";
174         if( fieldVar != null && overrideArgs[fieldName] == null ) 
175                 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
176         return ret;
177 }
178
179
180 /*
181 function EX(message) { this.init(message); }
182 EX.prototype.init = function(message) { this.message = message; }
183 EX.prototype.toString = function() { return "\n *** Exception Occured \n" + this.message; }  
184 EXCommunication.prototype              = new EX();
185 EXCommunication.prototype.constructor  = EXCommunication;
186 EXCommunication.baseClass              = EX.prototype.constructor;
187 function EXCommunication(message) { this.init("EXCommunication: " + message); }                          
188 */
189
190 /* ----------------------------------------------------------------------- */
191 function cleanISBN(isbn) {
192    if(isbn) {
193       isbn = isbn.toString().replace(/^\s+/,"");
194       var idx = isbn.indexOf(" ");
195       if(idx > -1) { isbn = isbn.substring(0, idx); }
196    } else isbn = "";
197    return isbn;
198 }       
199
200
201
202
203 /* builds a link that goes to the title listings for a metarecord */
204 function buildTitleLink(rec, link) {
205         if(!rec) return;
206         link.appendChild(text(normalize(truncate(rec.title(), 65))));
207         var args = {};
208         args.page = RRESULT;
209         args[PARAM_OFFSET] = 0;
210         args[PARAM_MRID] = rec.doc_id();
211         link.setAttribute("href", buildOPACLink(args));
212 }
213
214 function buildTitleDetailLink(rec, link) {
215         if(!rec) return;
216         link.appendChild(text(normalize(truncate(rec.title(), 65))));
217         var args = {};
218         args.page = RDETAIL;
219         args[PARAM_OFFSET] = 0;
220         args[PARAM_RID] = rec.doc_id();
221         link.setAttribute("href", buildOPACLink(args));
222 }
223
224 /* 'type' is one of STYPE_AUTHOR, STYPE_SUBJECT, ... found in config.js 
225         'trunc' is the number of characters to show in the string, defaults to 65 */
226 function buildSearchLink(type, string, linknode, trunc) {
227         if(!trunc) trunc = 65;
228         var args = {};
229         args.page = MRESULT;
230         args[PARAM_OFFSET] = 0;
231         args[PARAM_TERM] = string;
232         args[PARAM_STYPE] = type;
233         linknode.appendChild(text(normalize(truncate(string, trunc))));
234         linknode.setAttribute("href", buildOPACLink(args));
235 }
236
237
238 /* ----------------------------------------------------------------------- */
239 /* user session handling */
240 /* ----------------------------------------------------------------------- */
241
242 /* session is the login session.  If no session is provided, we attempt
243         to find one in the cookies.  If 'force' is true we retrieve the 
244         user from the server even if there is already a global user present.
245         if ses != G.user.session, we also force a grab */
246 var cookie = new cookieObject("ses", 1, "/", COOKIE_SES);
247 function grabUser(ses, force) {
248
249         if(!ses) ses = cookie.get(COOKIE_SES);
250         if(!ses) return false;
251
252         if(!force) 
253                 if(G.user && G.user.session == ses)
254                         return G.user;
255
256         /* first make sure the session is valid */
257         var request = new Request(FETCH_SESSION, ses );
258         request.send(true);
259         var user = request.result();
260         if( !(typeof user == 'object' && user._isfieldmapper) ) {
261                 return false;
262         }
263
264         G.user = user;
265         G.user.fleshed = false;
266         G.user.session = ses;
267         cookie.put(COOKIE_SES, ses);
268         cookie.write();
269
270         return G.user;
271
272 }
273
274 function grabFleshedUser() {
275
276         if(!G.user || !G.user.session) {
277                 grabUser();     
278                 if(!G.user || !G.user.session) return null;
279         }
280
281         if(G.user.fleshed) return G.user;
282
283    var req = new Request(FETCH_FLESHED_USER, G.user.session);
284         req.send(true);
285
286         G.user = req.result();
287
288         if(!G.user || G.user.length == 0) { 
289                 G.user = null; return false; 
290                 cookie.remove(COOKIE_SES);
291         }
292
293         G.user.session = ses;
294         G.user.fleshed = true;
295
296         cookie.put(COOKIE_SES, ses); /*  update the cookie */
297         cookie.write();
298
299         return G.user;
300 }
301
302
303 /* returns a fleshed G.user on success, false on failure */
304 function doLogin() {
305
306         var uname = G.ui.login.username.value;
307         var passwd = G.ui.login.password.value; 
308
309         var init_request = new Request( LOGIN_INIT, uname );
310    init_request.send(true);
311    var seed = init_request.result();
312
313    if( ! seed || seed == '0') {
314       alert( "Error Communicating with Authentication Server" );
315       return null;
316    }
317
318    var auth_request = new Request( LOGIN_COMPLETE, 
319                 uname, hex_md5(seed + hex_md5(passwd)), "opac");
320
321    auth_request.send(true);
322    var auth_result = auth_request.result();
323
324    if(auth_result == '0' || auth_result == null || auth_result.length == 0) { return false; }
325
326         var u = grabUser(auth_result, true);
327         if(u) updateLoc(u.home_ou(), findOrgDepth(u.home_ou()));
328
329         return u;
330 }
331
332 function doLogout() {
333
334         /* be nice and delete the session from the server */
335         if(G.user && G.user.session) { 
336                 var req = new Request(LOGIN_DELETE, G.user.session);
337       req.send(true);
338                 try { req.result(); } catch(E){}
339     }
340
341         G.user = null;
342         cookie.remove(COOKIE_SES);
343
344         hideMe(G.ui.sidebar.logoutbox);
345         unHideMe(G.ui.sidebar.loginbox);
346         hideMe(G.ui.sidebar.logged_in_as);
347
348 }
349
350
351 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); } 
352 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
353
354
355 /* ----------------------------------------------------------------------- */
356 /* build the org tree */
357 /* ----------------------------------------------------------------------- */
358
359
360 function drawOrgTree() {
361         G.ui.common.org_tree.innerHTML = buildOrgSelector().toString();
362 }
363         
364 var orgTreeSelector;
365 function buildOrgSelector() {
366         var tree = new dTree("orgTreeSelector"); 
367         for( var i in orgArraySearcher ) { 
368                 var node = orgArraySearcher[i];
369                 if( node == null ) continue;
370                 if(node.parent_ou() == null)
371                         tree.add(node.id(), -1, node.name(), 
372                                 "javascript:orgSelect(" + node.id() + ");", node.name());
373                 else {
374                         tree.add(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(), 
375                                 "javascript:orgSelect(" + node.id() + ");", node.name());
376                 }
377         }
378         orgTreeSelector = tree;
379         return tree;
380 }
381
382 function orgSelect(id) {
383         showCanvas();
384         updateLoc(id, findOrgDepth(id));
385 }
386
387 var fontCookie = new cookieObject("fonts", 1, "/", COOKIE_FONT);
388 function setFontSize(size) {
389         scaleFonts(size);
390         fontCookie.put(COOKIE_FONT, size);
391         fontCookie.write();
392 }
393
394
395
396
397
398
399
400