]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/javascript/lib/js/opac/opac_utils.js
lots of simple web enhancements, org tree loaded and working
[working/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         this.request = new RemoteRequest(s[0], s[1]);
9         for( var x = 1; x!= arguments.length; x++ ) 
10                 this.request.addParam(arguments[x]);
11 }
12
13 Request.prototype.callback = function(cal) { this.request.setCompleteCallback(cal); }
14 Request.prototype.send          = function(block){this.request.send(block);}
15 Request.prototype.result        = function(){return this.request.getResultObject();}
16 /* ----------------------------------------------------------------------- */
17
18
19 /* ----------------------------------------------------------------------- */
20 /* Functions for showing the canvas (and hiding any other shown stuff) */
21 /* ----------------------------------------------------------------------- */
22 function showCanvas() {
23         for( var x in G.ui.altcanvas ) {
24                 hideMe(G.ui.altcanvas[x]);
25         }
26         hideMe(G.ui.common.loading);
27         unHideMe(G.ui.common.canvas_main);
28         G.ui.searchbar.text.focus(); /* focus the searchbar */
29 }
30
31 function swapCanvas(newNode) {
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(newNode);
38 }
39 /* ----------------------------------------------------------------------- */
40
41
42 /* finds the name of the current page */
43 function findCurrentPage() {
44         for( var p in config.page ) {
45                 var path = location.pathname;
46
47                 if(!path.match(/.*\.xml$/))
48                         path += "index.xml"; /* in case they go to  / */
49
50                 if( config.page[p] == path)
51                         return p;
52         }
53         return null;
54 }
55
56
57 /* builds an opac URL.  If no page is defined, the current page is used
58         if slim, then only everything after the ? is returned (no host portion)
59  */
60 function  buildOPACLink(args, slim) {
61
62         if(!args) args = {};
63
64         if(!slim) {
65                 var string = location.protocol + "//" + location.host;
66                 if(args.page) string += config.page[args.page];
67                 else string += config.page[findCurrentPage()];
68         }
69
70         string += "?";
71
72         for( var x in args ) {
73                 if(x == "page" || args[x] == null) continue;
74                 string += "&" + x + "=" + encodeURIComponent(args[x]);
75         }
76
77         string += _appendParam(TERM,            PARAM_TERM, args, getTerm, string);
78         string += _appendParam(STYPE,           PARAM_STYPE, args, getStype, string);
79         string += _appendParam(LOCATION, PARAM_LOCATION, args, getLocation, string);
80         string += _appendParam(DEPTH,           PARAM_DEPTH, args, getDepth, string);
81         string += _appendParam(FORM,            PARAM_FORM, args, getForm, string);
82         string += _appendParam(OFFSET,  PARAM_OFFSET, args, getOffset, string);
83         string += _appendParam(COUNT,           PARAM_COUNT, args, getDisplayCount, string);
84         string += _appendParam(HITCOUNT, PARAM_HITCOUNT, args, getHitCount, string);
85         string += _appendParam(MRID,            PARAM_MRID, args, getMrid, string);
86         string += _appendParam(RID,             PARAM_RID, args, getRid, string);
87         return string.replace(/\&$/,'').replace(/\?\&/,"?");    
88 }
89
90 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
91         var ret = "";
92         if( fieldVar != null && overrideArgs[fieldName] == null ) 
93                 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
94         return ret;
95 }
96
97
98
99
100
101 /* ----------------------------------------------------------------------- */
102 /* some useful exceptions */
103 function EX(message) { this.init(message); }
104
105 EX.prototype.init = function(message) {
106    this.message = message;
107 }
108
109 EX.prototype.toString = function() {
110    return "\n *** Exception Occured \n" + this.message;
111 }  
112
113 EXCommunication.prototype              = new EX();
114 EXCommunication.prototype.constructor  = EXCommunication;
115 EXCommunication.baseClass              = EX.prototype.constructor;
116
117 function EXCommunication(message) {
118    this.init("EXCommunication: " + message);
119 }                          
120 /* ----------------------------------------------------------------------- */
121
122 function cleanISBN(isbn) {
123    if(isbn) {
124       isbn = isbn.toString().replace(/^\s+/,"");
125       var idx = isbn.indexOf(" ");
126       if(idx > -1) { isbn = isbn.substring(0, idx); }
127    } else isbn = "";
128    return isbn;
129 }       
130
131
132
133
134 /* ----------------------------------------------------------------------- */
135 /* builds a link that goes to the title listings for a metarecord */
136 function buildTitleLink(rec, link) {
137
138         var t = rec.title(); 
139         t = normalize(truncate(t, 65));
140         link.appendChild(text(t));
141
142         var args = {};
143         args.page = RRESULT;
144         args[PARAM_OFFSET] = 0;
145         args[PARAM_MRID] = rec.doc_id();
146         link.setAttribute("href", buildOPACLink(args));
147 }
148
149 /* builds an author search link */
150 function buildAuthorLink(rec, link) {
151
152         var a = rec.author(); 
153         a = normalize(truncate(a, 65));
154         link.appendChild(text(a));
155
156         var args = {};
157         args.page = MRESULT;
158         args[PARAM_OFFSET] = 0;
159         args[PARAM_STYPE] = STYPE_AUTHOR;
160         args[PARAM_TERM] = rec.author();
161         link.setAttribute("href", buildOPACLink(args));
162
163 }
164 /* ----------------------------------------------------------------------- */
165
166
167
168 /* ----------------------------------------------------------------------- */
169 /* user session handling */
170 /* ----------------------------------------------------------------------- */
171
172 /* session is the login session.  If no session is provided, we attempt
173         to find one in the cookies.  
174         If 'force' is true we retrieve the 
175         user from the server even if there is already a global user present.
176         if ses != G.user.session, we also force a grab */
177 var cookie = new cookieObject("ses", 1, "/", COOKIE_SES);
178 function grabUser(ses, force) {
179
180         if(!ses) ses = cookie.get(COOKIE_SES);
181         if(!ses) return false;
182
183         if(!force) 
184                 if(G.user && G.user.session == ses)
185                         return G.user;
186
187
188         /* first make sure the session is valid */
189         var request = new Request(FETCH_SESSION, ses );
190         request.send(true);
191         var user = request.result();
192         if( !(typeof user == 'object' && user._isfieldmapper) ) {
193                 return false;
194         }
195
196                 
197    var req = new Request(FETCH_FLESHED_USER, ses);
198         req.send(true);
199
200         G.user = req.result();
201
202         if(!G.user || G.user.length == 0) { 
203                 G.user = null; return false; 
204                 cookie.remove(COOKIE_SES);
205         }
206
207         G.user.session = ses;
208         cookie.put(COOKIE_SES, ses);
209         cookie.write();
210
211         return G.user;
212
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_request.length == 0) { return false; }
238
239         return grabUser(auth_result, true);
240 }
241
242 function doLogout() {
243
244         /* be nice and delete the session from the server */
245         if(G.user && G.user.session) { 
246                 var req = new Request(LOGIN_DELETE, G.user.session);
247       req.send(true);
248                 try { req.result(); } catch(E){}
249     }
250
251         G.user = null;
252         cookie.remove(COOKIE_SES);
253
254         hideMe(G.ui.sidebar.logoutbox);
255         unHideMe(G.ui.sidebar.loginbox);
256         hideMe(G.ui.sidebar.logged_in_as);
257
258 }
259
260
261 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); } 
262 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
263
264
265 /* ----------------------------------------------------------------------- */
266 /* build the org tree */
267 /* ----------------------------------------------------------------------- */
268         
269 var orgTreeSelector;
270 function buildOrgSelector() {
271         var tree = new dTree("orgTreeSelector"); 
272         for( var i in orgArraySearcher ) { 
273                 var node = orgArraySearcher[i];
274                 if( node == null ) continue;
275                 if(node.parent_ou() == null)
276                         tree.add(node.id(), -1, node.name(), 
277                                 "javascript:orgSelect(" + node.id() + ");", node.name());
278                 else {
279                         tree.add(node.id(), node.parent_ou().id(), node.name(), 
280                                 "javascript:orgSelect(" + node.id() + ");", node.name());
281                 }
282         }
283         orgTreeSelector = tree;
284         return tree;
285 }
286
287 function orgSelect(id) {
288         showCanvas();
289         newSearchLocation = id;
290 }
291
292
293
294 /* ----------------------------------------------------------------------- */
295
296
297
298
299
300