]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/common/js/opac_utils.js
moved isXUL to RemoteRequest
[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;
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 /* 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 getSearchBarExtras(){return SEARCHBAR_EXTRAS;}
108 function getMrid(){return MRID;};
109 function getRid(){return RID;};
110
111
112
113 /* builds an opac URL.  If no page is defined, the current page is used
114         if slim, then only everything after the ? is returned (no host portion) */
115 function findBasePath() {
116         var path = location.pathname;
117         if(!path.match(/.*\.xml$/)) path += "index.xml"; 
118         var idx = path.indexOf(config.page[findCurrentPage()]);
119         return path.substring(0, idx);
120 }
121
122 function findBaseURL(ssl) {
123         var path = findBasePath();
124         var proto = location.protocol;
125         if(ssl) proto = "https";
126         return location.protocol + "//" + location.host + path;
127 }
128
129 function buildImageLink(name, ssl) {
130         return findBaseURL(ssl) + "../../../images/" + name;
131 }
132
133 function  buildOPACLink(args, slim, ssl) {
134
135         if(!args) args = {};
136         var string = "";
137
138         if(!slim) {
139                 string = findBaseURL(ssl);
140                 if(args.page) string += config.page[args.page];
141                 else string += config.page[findCurrentPage()];
142         }
143
144         string += "?";
145
146         for( var x in args ) {
147                 if(x == "page" || args[x] == null) continue;
148                 string += "&" + x + "=" + encodeURIComponent(args[x]);
149         }
150
151         string += _appendParam(TERM,            PARAM_TERM, args, getTerm, string);
152         string += _appendParam(STYPE,           PARAM_STYPE, args, getStype, string);
153         string += _appendParam(LOCATION, PARAM_LOCATION, args, getLocation, string);
154         string += _appendParam(DEPTH,           PARAM_DEPTH, args, getDepth, string);
155         string += _appendParam(FORM,            PARAM_FORM, args, getForm, string);
156         string += _appendParam(OFFSET,  PARAM_OFFSET, args, getOffset, string);
157         string += _appendParam(COUNT,           PARAM_COUNT, args, getDisplayCount, string);
158         string += _appendParam(HITCOUNT, PARAM_HITCOUNT, args, getHitCount, string);
159         string += _appendParam(MRID,            PARAM_MRID, args, getMrid, string);
160         string += _appendParam(RID,             PARAM_RID, args, getRid, string);
161         return string.replace(/\&$/,'').replace(/\?\&/,"?");    
162 }
163
164 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
165         var ret = "";
166         if( fieldVar != null && overrideArgs[fieldName] == null ) 
167                 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
168         return ret;
169 }
170
171
172 function EX(message) { this.init(message); }
173 EX.prototype.init = function(message) { this.message = message; }
174 EX.prototype.toString = function() { return "\n *** Exception Occured \n" + this.message; }  
175 EXCommunication.prototype              = new EX();
176 EXCommunication.prototype.constructor  = EXCommunication;
177 EXCommunication.baseClass              = EX.prototype.constructor;
178 function EXCommunication(message) { this.init("EXCommunication: " + message); }                          
179
180 /* ----------------------------------------------------------------------- */
181 function cleanISBN(isbn) {
182    if(isbn) {
183       isbn = isbn.toString().replace(/^\s+/,"");
184       var idx = isbn.indexOf(" ");
185       if(idx > -1) { isbn = isbn.substring(0, idx); }
186    } else isbn = "";
187    return isbn;
188 }       
189
190
191
192
193 /* builds a link that goes to the title listings for a metarecord */
194 function buildTitleLink(rec, link) {
195         if(!rec) return;
196         link.appendChild(text(normalize(truncate(rec.title(), 65))));
197         var args = {};
198         args.page = RRESULT;
199         args[PARAM_OFFSET] = 0;
200         args[PARAM_MRID] = rec.doc_id();
201         link.setAttribute("href", buildOPACLink(args));
202 }
203
204 function buildTitleDetailLink(rec, link) {
205         if(!rec) return;
206         link.appendChild(text(normalize(truncate(rec.title(), 65))));
207         var args = {};
208         args.page = RDETAIL;
209         args[PARAM_OFFSET] = 0;
210         args[PARAM_RID] = rec.doc_id();
211         link.setAttribute("href", buildOPACLink(args));
212 }
213
214 /* 'type' is one of STYPE_AUTHOR, STYPE_SUBJECT, ... found in config.js 
215         'trunc' is the number of characters to show in the string, defaults to 65 */
216 function buildSearchLink(type, string, linknode, trunc) {
217         if(!trunc) trunc = 65;
218         var args = {};
219         args.page = MRESULT;
220         args[PARAM_OFFSET] = 0;
221         args[PARAM_TERM] = string;
222         args[PARAM_STYPE] = type;
223         linknode.appendChild(text(normalize(truncate(string, trunc))));
224         linknode.setAttribute("href", buildOPACLink(args));
225 }
226
227
228 /* ----------------------------------------------------------------------- */
229 /* user session handling */
230 /* ----------------------------------------------------------------------- */
231
232 /* session is the login session.  If no session is provided, we attempt
233         to find one in the cookies.  If 'force' is true we retrieve the 
234         user from the server even if there is already a global user present.
235         if ses != G.user.session, we also force a grab */
236 var cookie = new cookieObject("ses", 1, "/", COOKIE_SES);
237 function grabUser(ses, force) {
238
239         if(!ses) ses = cookie.get(COOKIE_SES);
240         if(!ses) return false;
241
242         if(!force) 
243                 if(G.user && G.user.session == ses)
244                         return G.user;
245
246         /* first make sure the session is valid */
247         var request = new Request(FETCH_SESSION, ses );
248         request.send(true);
249         var user = request.result();
250         if( !(typeof user == 'object' && user._isfieldmapper) ) {
251                 return false;
252         }
253
254         G.user = user;
255         G.user.fleshed = false;
256         G.user.session = ses;
257         cookie.put(COOKIE_SES, ses);
258         cookie.write();
259
260         return G.user;
261
262 }
263
264 function grabFleshedUser() {
265
266         if(!G.user || !G.user.session) {
267                 grabUser();     
268                 if(!G.user || !G.user.session) return null;
269         }
270
271         if(G.user.fleshed) return G.user;
272
273    var req = new Request(FETCH_FLESHED_USER, G.user.session);
274         req.send(true);
275
276         G.user = req.result();
277
278         if(!G.user || G.user.length == 0) { 
279                 G.user = null; return false; 
280                 cookie.remove(COOKIE_SES);
281         }
282
283         G.user.session = ses;
284         G.user.fleshed = true;
285
286         cookie.put(COOKIE_SES, ses); /*  update the cookie */
287         cookie.write();
288
289         return G.user;
290 }
291
292
293 /* returns a fleshed G.user on success, false on failure */
294 function doLogin() {
295
296         var uname = G.ui.login.username.value;
297         var passwd = G.ui.login.password.value; 
298
299         var init_request = new Request( LOGIN_INIT, uname );
300    init_request.send(true);
301    var seed = init_request.result();
302
303    if( ! seed || seed == '0') {
304       alert( "Error Communicating with Authentication Server" );
305       return null;
306    }
307
308    var auth_request = new Request( LOGIN_COMPLETE, 
309                 uname, hex_md5(seed + hex_md5(passwd)), "opac");
310
311    auth_request.send(true);
312    var auth_result = auth_request.result();
313
314    if(auth_result == '0' || auth_result == null || auth_result.length == 0) { return false; }
315
316         var u = grabUser(auth_result, true);
317         if(u) updateLoc(u.home_ou(), findOrgDepth(u.home_ou()));
318
319         return u;
320 }
321
322 function doLogout() {
323
324         /* be nice and delete the session from the server */
325         if(G.user && G.user.session) { 
326                 var req = new Request(LOGIN_DELETE, G.user.session);
327       req.send(true);
328                 try { req.result(); } catch(E){}
329     }
330
331         G.user = null;
332         cookie.remove(COOKIE_SES);
333
334         hideMe(G.ui.sidebar.logoutbox);
335         unHideMe(G.ui.sidebar.loginbox);
336         hideMe(G.ui.sidebar.logged_in_as);
337
338 }
339
340
341 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); } 
342 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
343
344
345 /* ----------------------------------------------------------------------- */
346 /* build the org tree */
347 /* ----------------------------------------------------------------------- */
348
349
350 function drawOrgTree() {
351         G.ui.common.org_tree.innerHTML = buildOrgSelector().toString();
352 }
353         
354 var orgTreeSelector;
355 function buildOrgSelector() {
356         var tree = new dTree("orgTreeSelector"); 
357         for( var i in orgArraySearcher ) { 
358                 var node = orgArraySearcher[i];
359                 if( node == null ) continue;
360                 if(node.parent_ou() == null)
361                         tree.add(node.id(), -1, node.name(), 
362                                 "javascript:orgSelect(" + node.id() + ");", node.name());
363                 else {
364                         tree.add(node.id(), node.parent_ou().id(), node.name(), 
365                                 "javascript:orgSelect(" + node.id() + ");", node.name());
366                 }
367         }
368         orgTreeSelector = tree;
369         return tree;
370 }
371
372 function orgSelect(id) {
373         showCanvas();
374         updateLoc(id, findOrgDepth(id));
375 }
376
377
378 /* ------------------------------------------------------------------------------------------------------ */
379 /* org tree utilities */
380 /* ------------------------------------------------------------------------------------------------------ */
381
382 /* takes an org unit or id and return the numeric depth */
383 function findOrgDepth(org_id_or_node) {
384         return findOrgType(findOrgUnit(org_id_or_node).ou_type()).depth();
385 }
386
387 /* takes the org type id from orgunit.ou_type() field and returns
388         the org type object */
389 function findOrgType(type_id) {
390         if(typeof type_id == 'object') return type_id;
391         for(var type in globalOrgTypes) {
392                 var t =globalOrgTypes[type]; 
393                 if( t.id() == type_id || t.id() == parseInt(type_id) ) 
394                         return t;
395         }
396         return null;
397 }
398
399
400 /* returns an org unit by id.  if an object is passed in as the id,
401         then the object is assumed to be an org unit and is returned */
402 function findOrgUnit(org_id) {
403         return (typeof org_id == 'object') ? org_id : orgArraySearcher[org_id];
404 }
405
406
407 /* builds a trail from the top of the org tree to the node provide.
408         basically fleshes out 'my orgs' 
409         Returns an array of [org0, org1, ..., myorg] */
410 function orgNodeTrail(node) {
411         var na = new Array();
412         while( node ) {
413                 na.push(node);
414                 node = findOrgUnit(node.parent_ou());
415         }
416         return na.reverse();
417 }
418
419
420 /* returns an array of sibling org units */
421 function findSiblingOrgs(node) { return findOrgUnit(node.parent_ou()).children(); }
422 /* ------------------------------------------------------------------------------------------------------ */
423
424
425
426
427
428
429