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