]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/javascript/lib/js/opac/opac_utils.js
c927e281e6d976193a43e754f3d63699b271099d
[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 /* finds the name of the current page */
20 function findCurrentPage() {
21         for( var p in config.page ) {
22                 var path = location.pathname;
23
24                 if(!path.match(/.*\.xml$/))
25                         path += "index.xml"; /* in case they go to  / */
26
27                 if( config.page[p] == path)
28                         return p;
29         }
30         return null;
31 }
32
33
34 /* builds an opac URL.  If no page is defined, the current page is used
35         if slim, then only everything after the ? is returned (no host portion)
36  */
37 function  buildOPACLink(args, slim) {
38
39         if(!args) args = {};
40
41         if(!slim) {
42                 var string = location.protocol + "//" + location.host;
43                 if(args.page) string += config.page[args.page];
44                 else string += config.page[findCurrentPage()];
45         }
46
47         string += "?";
48
49         for( var x in args ) {
50                 if(x == "page" || args[x] == null) continue;
51                 string += "&" + x + "=" + encodeURIComponent(args[x]);
52         }
53
54         string += _appendParam(TERM,            PARAM_TERM, args, getTerm, string);
55         string += _appendParam(STYPE,           PARAM_STYPE, args, getStype, string);
56         string += _appendParam(LOCATION, PARAM_LOCATION, args, getLocation, string);
57         string += _appendParam(DEPTH,           PARAM_DEPTH, args, getDepth, string);
58         string += _appendParam(FORM,            PARAM_FORM, args, getForm, string);
59         string += _appendParam(OFFSET,  PARAM_OFFSET, args, getOffset, string);
60         string += _appendParam(COUNT,           PARAM_COUNT, args, getDisplayCount, string);
61         string += _appendParam(HITCOUNT, PARAM_HITCOUNT, args, getHitCount, string);
62         string += _appendParam(MRID,            PARAM_MRID, args, getMrid, string);
63         string += _appendParam(RID,             PARAM_RID, args, getRid, string);
64         return string.replace(/\&$/,'').replace(/\?\&/,"?");    
65 }
66
67 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
68         var ret = "";
69         if( fieldVar != null && overrideArgs[fieldName] == null ) 
70                 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
71         return ret;
72 }
73
74
75
76
77
78 /* ----------------------------------------------------------------------- */
79 /* some useful exceptions */
80 function EX(message) { this.init(message); }
81
82 EX.prototype.init = function(message) {
83    this.message = message;
84 }
85
86 EX.prototype.toString = function() {
87    return "\n *** Exception Occured \n" + this.message;
88 }  
89
90 EXCommunication.prototype              = new EX();
91 EXCommunication.prototype.constructor  = EXCommunication;
92 EXCommunication.baseClass              = EX.prototype.constructor;
93
94 function EXCommunication(message) {
95    this.init("EXCommunication: " + message);
96 }                          
97 /* ----------------------------------------------------------------------- */
98
99 function cleanISBN(isbn) {
100    if(isbn) {
101       isbn = isbn.toString().replace(/^\s+/,"");
102       var idx = isbn.indexOf(" ");
103       if(idx > -1) { isbn = isbn.substring(0, idx); }
104    } else isbn = "";
105    return isbn;
106 }       
107
108
109
110
111 /* ----------------------------------------------------------------------- */
112 /* builds a link that goes to the title listings for a metarecord */
113 function buildTitleLink(rec, link) {
114
115         var t = rec.title(); 
116         t = normalize(truncate(t, 65));
117         link.appendChild(text(t));
118
119         var args = {};
120         args.page = RRESULT;
121         args[PARAM_OFFSET] = 0;
122         args[PARAM_MRID] = rec.doc_id();
123         link.setAttribute("href", buildOPACLink(args));
124 }
125
126 /* builds an author search link */
127 function buildAuthorLink(rec, link) {
128
129         var a = rec.author(); 
130         a = normalize(truncate(a, 65));
131         link.appendChild(text(a));
132
133         var args = {};
134         args.page = MRESULT;
135         args[PARAM_OFFSET] = 0;
136         args[PARAM_STYPE] = STYPE_AUTHOR;
137         args[PARAM_TERM] = rec.author();
138         link.setAttribute("href", buildOPACLink(args));
139
140 }
141 /* ----------------------------------------------------------------------- */
142
143
144
145 /* ----------------------------------------------------------------------- */
146 /* user session handling */
147 /* ----------------------------------------------------------------------- */
148
149 /* session is the login session.  If no session is provided, we attempt
150         to find one in the cookies.  
151         If 'force' is true we retrieve the 
152         user from the server even if there is already a global user present.
153         if ses != G.user.session, we also force a grab */
154 var cookie = new cookieObject("ses", 1, "/", COOKIE_SES);
155 function grabUser(ses, force) {
156
157         if(!ses) ses = cookie.get(COOKIE_SES);
158         if(!ses) return false;
159
160         if(!force) 
161                 if(G.user && G.user.session == ses)
162                         return G.user;
163
164
165         /* first make sure the session is valid */
166         var request = new Request(FETCH_SESSION, ses );
167         request.send(true);
168         var user = request.result();
169         if( !(typeof user == 'object' && user._isfieldmapper) ) {
170                 return false;
171         }
172
173                 
174    var req = new Request(FETCH_FLESHED_USER, ses);
175         req.send(true);
176
177         G.user = req.result();
178
179         if(!G.user || G.user.length == 0) { 
180                 G.user = null; return false; 
181                 cookie.remove(COOKIE_SES);
182         }
183
184         G.user.session = ses;
185         cookie.put(COOKIE_SES, ses);
186         cookie.write();
187
188         return G.user;
189
190 }
191
192
193 /* returns a fleshed G.user on success, false on failure */
194 function doLogin() {
195
196         var uname = G.ui.login.username.value;
197         var passwd = G.ui.login.password.value; 
198
199         var init_request = new Request( LOGIN_INIT, uname );
200    init_request.send(true);
201    var seed = init_request.result();
202
203    if( ! seed || seed == '0') {
204       alert( "Error Communicating with Authentication Server" );
205       return null;
206    }
207
208    var auth_request = new Request( LOGIN_COMPLETE, 
209                 uname, hex_md5(seed + hex_md5(passwd)), "opac");
210
211    auth_request.send(true);
212    var auth_result = auth_request.result();
213
214    if(auth_result == '0' || auth_result == null || auth_request.length == 0) { return false; }
215
216         return grabUser(auth_result, true);
217 }
218
219 function doLogout() {
220
221         /* be nice and delete the session from the server */
222         if(G.user && G.user.session) { 
223                 var req = new Request(LOGIN_DELETE, G.user.session);
224       req.send(true);
225                 try { req.result(); } catch(E){}
226     }
227
228         G.user = null;
229         cookie.remove(COOKIE_SES);
230
231         hideMe(G.ui.sidebar.logoutbox);
232         unHideMe(G.ui.sidebar.loginbox);
233         hideMe(G.ui.sidebar.logged_in_as);
234
235 }
236
237
238 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); } 
239 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
240
241
242
243
244
245
246
247
248