improper added content info is now ignored / not displayed
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / myopac.js
1
2 attachEvt("common", "run", myOPACInit );
3 attachEvt("common", "loggedIn", myOPACInit );
4
5 var fleshedUser = null;
6
7
8 function clearNodes( node, keepArray ) {
9         if(!node) return;
10         for( var n in keepArray ) node.removeChild(keepArray[n]);
11         removeChildren(node);
12         for( var n in keepArray ) node.appendChild(keepArray[n]);
13 }
14
15 function myOPACInit() {
16         if(!(G.user && G.user.session)) initLogin();
17         else myOPACChangePage( "summary" );
18 }
19
20
21 function myOPACChangePage( page ) {
22         
23         var s = $("myopac_summary_td");
24         var c = $("myopac_checked_td");
25         var f = $("myopac_fines_td");
26         var h = $("myopac_holds_td");
27         var p = $("myopac_prefs_td");
28
29         var ss = $("myopac_summary_div");
30         var cc = $("myopac_checked_div");
31         var ff = $("myopac_fines_div");
32         var hh = $("myopac_holds_div");
33         var pp = $("myopac_prefs_div");
34
35         var cls = "myopac_link";
36         var acls = "myopac_link_active";
37
38         hideMe(ss);
39         hideMe(cc); hideMe(ff);
40         hideMe(hh); hideMe(pp);
41
42         removeCSSClass(s, acls );
43         removeCSSClass(c, acls );
44         removeCSSClass(f, acls );
45         removeCSSClass(h, acls );
46         removeCSSClass(p, acls );
47
48         switch( page ) {
49
50                 case "summary": 
51                         unHideMe(ss);
52                         addCSSClass(s, acls );
53                         myOPACShowSummary();
54                         break;
55
56                 case "checked": 
57                         unHideMe(cc);
58                         addCSSClass(c, acls );
59                         myOPACShowChecked();
60                         break;
61
62                 case "holds": 
63                         unHideMe(hh);
64                         addCSSClass(h, acls );
65                         myOPACShowHolds();
66                         break;
67
68                 case "fines": 
69                         unHideMe(ff);
70                         addCSSClass(f, acls );
71                         myOPACShowFines();
72                         break;
73
74                 case "prefs": 
75                         unHideMe(pp);
76                         addCSSClass(p, acls );
77                         myOPACShowPrefs();
78                         break;
79         }
80
81         /*
82         alert('Classes:\n' + s.className + '\n' + c.className + 
83                 '\n' + h.className + '\n' + f.className + '\n' + p.className);
84                 */
85 }
86
87 function myOPACShowChecked() {
88         var req = new Request(FETCH_CHECKED_OUT_SLIM, G.user.session);  
89         req.callback(myOPACDrawCheckedOutSlim);
90         req.send();
91 }
92
93
94 var checkedRowTemplate;
95 var circsCache = new Array();
96 var checkedDrawn = false;
97
98 function myOPACDrawCheckedOutSlim(r) {
99
100         var checked                     = r.getResultObject();
101         var tbody                       = $("myopac_checked_tbody");
102         var loading                     = $("myopac_checked_loading");
103         var none                                = $("myopac_checked_none");
104
105         if(checkedDrawn) return;
106         checkedDrawn = true;
107         if(!checkedRowTemplate) 
108                 checkedRowTemplate = tbody.removeChild($("myopac_checked_row"));
109
110         clearNodes( tbody, [ loading, none ] );
111
112         hideMe(loading); /* remove all children and start over */
113         if(!(checked && checked[0])) unHideMe(none);
114
115         for( var idx in checked ) {
116
117                 var circ    = checked[idx]
118                 var row = checkedRowTemplate.cloneNode(true);
119                 row.id = 'myopac_checked_row_ ' + circ.id();
120
121                 var due = circ.due_date();
122       due = due.replace(/[0-9][0-9]:.*$/,"");
123
124                 var dlink = $n( row, "myopac_checked_due" );
125                 var rlink = $n( row, "myopac_checked_renewals" );
126                 var rnlink = $n( row, "myopac_checked_renew_link" );
127
128                 dlink.appendChild(text(due));
129                 rlink.appendChild(text(circ.renewal_remaining()));
130                 unHideMe(row);
131                 rnlink.setAttribute('href', 'javascript:myOPACRenewCirc("'+circ.id()+'");');
132                 circsCache.push(circ);
133
134                 tbody.appendChild(row);
135
136                 var req = new Request(FETCH_MODS_FROM_COPY, circ.target_copy() );
137                 req.request.circ = circ.id();
138                 req.callback(myOPACDrawCheckedTitle);
139                 req.send();
140         }
141 }
142
143 function myOPACDrawCheckedTitle(r) {
144         var circid = r.circ;
145         var row = $('myopac_checked_row_ ' + circid);
146         var record = r.getResultObject();
147         var tlink = $n( row, "myopac_checked_title_link" );
148         var alink = $n( row, "myopac_checked_author_link" );
149         buildTitleDetailLink(record, tlink);
150         buildSearchLink(STYPE_AUTHOR, record.author(), alink);
151 }
152
153
154 function myOPACRenewCirc(circid) {
155         var circ;
156         for( var i = 0; i != circsCache.length; i++ ) 
157                 if(circsCache[i].id() == circid)
158                         circ = circsCache[i];
159
160         var req = new Request(RENEW_CIRC, G.user.session, circ );
161         req.send(true);
162         var res = req.result();
163         if(res.status) {
164                 alert(res.text);
165                 return;
166         }
167         alert($('myopac_renew_success').innerHTML);     
168         checkedDrawn = false;
169         myOPACShowChecked();
170 }
171
172
173
174 function myOPACShowHolds() {
175         var req = new Request(FETCH_HOLDS, G.user.session, G.user.id());        
176         req.callback(myOPACDrawHolds);
177         req.send();
178 }
179
180 var holdsTemplateRowOrig;
181 var holdsTemplateRow;
182 function myOPACDrawHolds(r) {
183
184         var tbody = $("myopac_holds_tbody");
185         if(holdsTemplateRow) return;
186         if(holdsTemplateRowOrig) {
187                 holdsTemplateRow = holdsTemplateRowOrig;
188                 removeChildren(tbody);
189         } else {
190                 holdsTemplateRow = tbody.removeChild($("myopac_holds_row"));
191                 holdsTemplateRowOrig = holdsTemplateRow;
192         }
193
194         hideMe($('myopac_holds_loading'));
195
196         var holds = r.getResultObject();
197
198         if(!holds || holds.length < 1) unHideMe($('myopac_holds_none'));
199         for( var i = 0; i != holds.length; i++ ) {
200
201                 var h = holds[i];
202                 var row = holdsTemplateRow.cloneNode(true);
203                 row.id = "myopac_holds_row_" + h.id() + '_' + h.target();
204
205                 var formats = (h.holdable_formats()) ? h.holdable_formats() : null;
206                 var form = $n(row, "myopac_holds_formats");
207                 form.id = "myopac_holds_form_" + h.id() + '_' + h.target();
208                 if(formats) form.appendChild(text(formats));
209
210                 $n(row, "myopac_holds_location").
211                         appendChild(text(findOrgUnit(h.pickup_lib()).name()));
212                 $n(row, "myopac_holds_email_link").
213                         appendChild(text(h.email_notify()));
214                 $n(row, "myopac_holds_phone_link").
215                         appendChild(text(h.phone_notify()));
216                 tbody.appendChild(row);
217
218                 $n(row,'myopac_holds_cancel_link').setAttribute(
219                         'href','javascript:myOPACCancelHold("'+ h.id()+'");'); 
220                 unHideMe(row);
221
222                 myOPACDrawHoldTitle(h);
223         }
224 }
225
226 function myOPACCancelHold(holdid) {
227         if( confirm($('myopac_holds_cancel_verify').innerHTML) ) {
228                 holdsCancel(holdid);
229                 holdsTemplateRow = null
230                 myOPACShowHolds();
231         }
232 }
233
234
235
236 function myOPACDrawHoldTitle(hold) {
237         var method;
238         if(hold.hold_type() == "M") method = FETCH_MRMODS;
239         if(hold.hold_type() == "T") method = FETCH_RMODS;
240         var req = new Request(method, hold.target());
241         req.callback(myOPACFleshHoldTitle);
242         req.request.hold = hold.id();
243         req.send();
244 }
245
246 function myOPACFleshHoldTitle(r) {
247
248         var record = r.getResultObject();
249         var row = $("myopac_holds_row_" + r.hold + '_' + record.doc_id());
250         var title_link = $n(row, "myopac_holds_title_link");
251         var author_link = $n(row, "myopac_holds_author_link");
252
253         buildTitleDetailLink(record, title_link);
254         buildSearchLink(STYPE_AUTHOR, record.author(), author_link);
255
256         var form = $("myopac_holds_form_" + r.hold + '_' + record.doc_id());
257
258         if(form) {
259                 var img = elem("img");
260                 img.setAttribute("src", 
261                         buildImageLink('tor/' + record.types_of_resource()[0] + ".jpg"));
262                 addCSSClass(img, "myopac_form_pic");
263                 form.appendChild(img);
264         }
265 }
266
267 var finesShown = false;
268 function myOPACShowFines() {
269         if(finesShown) return; finesShown = true;
270         var req = new Request(FETCH_FINES_SUMMARY, G.user.session, G.user.id() );
271         req.callback(_myOPACShowFines);
272         req.send();
273 }
274
275 function _myOPACShowFines(r) {
276         hideMe($('myopac_fines_summary_loading'));
277         unHideMe($('myopac_fines_summary_row'));
278
279         var summary = r.getResultObject();
280         var total       = "0.00"; /* localization? */
281         var paid                = "0.00";
282         var balance = "0.00";
283         if( instanceOf(summary,mous) ) {
284
285                 total           = _finesFormatNumber(summary.total_owed());
286                 paid            = _finesFormatNumber(summary.total_paid());
287                 balance = _finesFormatNumber(summary.balance_owed());
288
289                 var req = new Request(FETCH_TRANSACTIONS, G.user.session, G.user.id() );
290                 req.callback(myOPACShowTransactions);
291                 req.send();
292         }
293
294         $('myopac_fines_summary_total').appendChild(text(total));
295         $('myopac_fines_summary_paid').appendChild(text(paid));
296         $('myopac_fines_summary_balance').appendChild(text(balance));
297 }
298
299 function _finesFormatNumber(num) {
300         if(isNull(num)) num = 0;
301         num = num + "";
302         if(num.length < 2 || !num.match(/\./)) num += ".00";
303         if(num.match(/\./) && num.charAt(num.length-2) == '.') num += "0";
304         return num;
305 }          
306
307 function _trimSeconds(time) { if(!time) return ""; return time.replace(/\..*/,""); }
308
309
310 var transTemplate;
311 function myOPACShowTransactions(r) {
312
313         if(transTemplate) return;
314         var tbody = $('myopac_fines_tbody');
315         unHideMe($('myopac_trans_table'));
316         transTemplate = tbody.removeChild($('myopac_trans_row'));
317
318         var transactions = r.getResultObject();
319
320         //alert(js2JSON(transactions));
321
322         for( var idx in transactions ) {
323
324                 var trans = transactions[idx].transaction;
325                 var record = transactions[idx].record;
326                 var row = transTemplate.cloneNode(transTemplate);
327
328                 $n(row,'myopac_trans_start').
329                         appendChild(text(_trimSeconds(trans.xact_start())));
330                 $n(row,'myopac_trans_last_bill').
331                         appendChild(text(_trimSeconds(trans.last_billing_ts())));
332                 $n(row,'myopac_trans_last_payment').
333                         appendChild(text(_trimSeconds(trans.last_payment_ts())));
334                 $n(row,'myopac_trans_init_amount').
335                         appendChild(text(_finesFormatNumber(trans.total_owed())));
336                 $n(row,'myopac_trans_total_paid').
337                         appendChild(text(_finesFormatNumber(trans.total_paid())));
338                 $n(row,'myopac_trans_balance').
339                         appendChild(text(_finesFormatNumber(trans.balance_owed())));
340
341                 var extra = "";
342                 var type = trans.xact_type();
343                 $n(row,'myopac_trans_type').appendChild(text(type));
344                 if( type == "circulation" ) extra = record.title();
345                 $n(row, 'myopac_trans_extra').appendChild(text(extra));
346
347                 tbody.appendChild(row);
348         }
349 }
350
351 function myOPACSavePrefs() {
352         G.user.prefs['opac.hits_per_page'] = getSelectorVal($('prefs_hits_per'));
353         if(commitUserPrefs())
354                 alert($('prefs_update_success').innerHTML);
355         else alert($('prefs_update_failure').innerHTML);
356 }
357
358
359 function myOPACShowPrefs() {
360         grabUserPrefs();
361         myOPACShowHitsPer();
362         hideMe($('myopac_prefs_loading'));
363         unHideMe($('myopac_prefs_row'));
364 }
365
366 function myOPACShowHitsPer() {
367         var hits = 10;
368         if(G.user.prefs['opac.hits_per_page'])
369                 hits = G.user.prefs['opac.hits_per_page'];
370         var hitsSel = $('prefs_hits_per');
371         setSelector(hitsSel, hits);
372 }
373
374 var userShown = false;
375 function myOPACShowSummary() {
376         if(userShown) return; userShown = true;
377         var req = new Request(FETCH_FLESHED_USER,G.user.session, G.user.id());
378         req.callback(_myOPACSummaryShowUer);
379         req.send();
380 }
381
382 var addrRowTemplate;
383 function _myOPACSummaryShowUer(r) {
384
385         var user = r.getResultObject();
386         fleshedUser = user;
387
388         appendClear($('myopac_summary_first'),text(user.first_given_name()));
389         appendClear($('myopac_summary_middle'),text(user.second_given_name()));
390         appendClear($('myopac_summary_dayphone'),text(user.day_phone()));
391         appendClear($('myopac_summary_eveningphone'),text(user.evening_phone()));
392         appendClear($('myopac_summary_otherphone'),text(user.other_phone()));
393         appendClear($('myopac_summary_last'),text(user.family_name()));
394         appendClear($('myopac_summary_username'),text(user.usrname()));
395         appendClear($('myopac_summary_email'),text(user.email()));
396         appendClear($('myopac_summary_barcode'),text(user.card().barcode()));
397         appendClear($('myopac_summary_ident1'),text(user.ident_value()));
398         appendClear($('myopac_summary_ident2'),text(user.ident_value2()));
399         appendClear($('myopac_summary_homelib'),text(findOrgUnit(user.home_ou()).name()));
400         appendClear($('myopac_summary_create_date'),text(user.create_date()));
401
402         var tbody = $('myopac_addr_tbody');
403         var template;
404
405         if(addrRowTemplate) { 
406                 template = addrRowTemplate;
407         } else {
408                 template = tbody.removeChild($('myopac_addr_row'));
409                 addrRowTemplate = template;
410         }
411         removeChildren(tbody);
412
413         for( var a in user.addresses() ) {
414                 var row = template.cloneNode(true);
415                 myOPACDrawAddr(row, user.addresses()[a]);
416                 tbody.appendChild(row);
417         }
418 }
419
420 function myOPACDrawAddr(row, addr) {
421
422         appendClear($n(row, 'myopac_addr_type'),text(addr.address_type()));
423         var street = (addr.street2()) ? addr.street1() + ", " + addr.street2() : addr.street1();
424         appendClear($n(row, 'myopac_addr_street'),text(street));
425         appendClear($n(row, 'myopac_addr_city'),text(addr.city()));
426         appendClear($n(row, 'myopac_addr_county'),text(addr.county()));
427         appendClear($n(row, 'myopac_addr_state'),text(addr.state()));
428         appendClear($n(row, 'myopac_addr_zip'),text(addr.post_code()));
429 }
430
431
432 function myOPACUpdateUsername() {
433         var username = $('myopac_new_username').value;
434         if(username == null || username == "") {
435                 alert($('myopac_username_error').innerHTML);
436                 return;
437         }
438         var req = new Request(UPDATE_USERNAME, G.user.session, username );
439         req.send(true);
440         if(req.result()) {
441
442                 var evt;
443                 if(evt = checkILSEvent(req.result())) {
444                         alertILSEvent(evt);
445                         return;
446                 }
447
448                 G.user.usrname(username);
449                 hideMe($('myopac_update_username_row'));
450                 userShown = false;
451                 myOPACShowSummary();
452                 return;
453         }
454
455         alert($('myopac_username_failure').innerHTML);
456 }
457
458 function myOPACUpdateEmail() {
459         var email = $('myopac_new_email').value;
460         if(email == null || email == "") {
461                 alert($('myopac_email_error').innerHTML);
462                 return;
463         }
464
465         var req = new Request(UPDATE_EMAIL, G.user.session, email );
466         req.send(true);
467         if(req.result()) {
468                 G.user.usrname(email);
469                 hideMe($('myopac_update_email_row'));
470                 userShown = false;
471                 myOPACShowSummary();
472                 return;
473         }
474
475         alert($('myopac_email_failure').innerHTML);
476 }
477
478
479 function myOPACUpdatePassword() {
480         var curpassword = $('myopac_current_password').value;
481         var password = $('myopac_new_password').value;
482         var password2 = $('myopac_new_password2').value;
483
484         if(     curpassword == null || curpassword == "" || 
485                         password == null || password == "" || 
486                         password2 == null || password2 == "" || password != password2 ) {
487                 alert($('myopac_password_error').innerHTML);
488                 return;
489         }
490
491         var req = new Request(UPDATE_PASSWORD, G.user.session, password, curpassword );
492         req.send(true);
493         if(req.result()) {
494                 G.user.usrname(password);
495                 hideMe($('myopac_update_password_row'));
496                 userShown = false;
497                 myOPACShowSummary();
498                 return;
499         }
500
501         alert($('myopac_password_failure').innerHTML);
502 }
503
504
505
506