holds are coming along
[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, G.user.session);       
89         req.callback(myOPACDrawCheckedOut);
90         req.send();
91 }
92
93
94 var checkedRowTemplate;
95 function myOPACDrawCheckedOut(r) {
96
97
98         var checked                     = r.getResultObject();
99         var tbody                       = $("myopac_checked_tbody");
100         var loading                     = $("myopac_checked_loading");
101         var none                                = $("myopac_checked_none");
102
103         if(checkedRowTemplate) return;
104         checkedRowTemplate = tbody.removeChild($("myopac_checked_row"));
105
106         clearNodes( tbody, [ loading, none ] );
107
108         hideMe(loading); /* remove all children and start over */
109         if(!(checked && checked[0])) unHideMe(none);
110
111         for( var idx in checked ) {
112
113                 var row = checkedRowTemplate.cloneNode(true);
114
115                 var circ    = checked[idx].circ;
116       var record  = checked[idx].record;
117       var copy    = checked[idx].copy;
118                 var due = circ.due_date();
119       due = due.replace(/[0-9][0-9]:.*$/,"");
120
121                 var tlink = findNodeByName( row, "myopac_checked_title_link" );
122                 var alink = findNodeByName( row, "myopac_checked_author_link" );
123                 var dlink = findNodeByName( row, "myopac_checked_due" );
124                 var rlink = findNodeByName( row, "myopac_checked_renewals" );
125                 var rnlink = findNodeByName( row, "myopac_checked_renew_link" );
126
127                 buildTitleDetailLink(record, tlink);
128                 buildSearchLink(STYPE_AUTHOR, record.author(), alink);
129                 dlink.appendChild(text(due));
130                 rlink.appendChild(text(circ.renewal_remaining()));
131                 unHideMe(row);
132                 //rnlink /* set the renew action */
133
134                 tbody.appendChild(row);
135         }
136 }
137
138
139
140 function myOPACShowHolds() {
141         var req = new Request(FETCH_HOLDS, G.user.session, G.user.id());        
142         req.callback(myOPACDrawHolds);
143         req.send();
144 }
145
146 var holdsTemplateRow;
147 function myOPACDrawHolds(r) {
148
149         var tbody = $("myopac_holds_tbody");
150         if(holdsTemplateRow) return;
151         holdsTemplateRow = tbody.removeChild($("myopac_holds_row"));
152
153         hideMe($('myopac_holds_loading'));
154
155         var holds = r.getResultObject();
156
157         if(!holds || holds.length < 1) unHideMe($('myopac_holds_none'));
158         for( var i = 0; i != holds.length; i++ ) {
159
160                 var h = holds[i];
161                 var row = holdsTemplateRow.cloneNode(true);
162                 row.id = "myopac_holds_row_" + h.target();
163
164                 var formats = (h.holdable_formats()) ? h.holdable_formats() : null;
165                 var form = findNodeByName(row, "myopac_holds_formats");
166                 form.id = "myopac_holds_form_" + h.target();
167                 if(formats) form.appendChild(text(formats));
168
169                 findNodeByName(row, "myopac_holds_location").
170                         appendChild(text(findOrgUnit(h.pickup_lib()).name()));
171                 findNodeByName(row, "myopac_holds_email_link").
172                         appendChild(text(h.email_notify()));
173                 findNodeByName(row, "myopac_holds_phone_link").
174                         appendChild(text(h.phone_notify()));
175                 tbody.appendChild(row);
176                 unHideMe(row);
177
178                 myOPACDrawHoldTitle(h);
179         }
180 }
181
182 function myOPACDrawHoldTitle(hold) {
183         var method;
184         if(hold.hold_type() == "M") method = FETCH_MRMODS;
185         if(hold.hold_type() == "T") method = FETCH_RMODS;
186         var req = new Request(method, hold.target());
187         req.callback(myOPACFleshHoldTitle);
188         req.send();
189 }
190
191 function myOPACFleshHoldTitle(r) {
192
193         var record = r.getResultObject();
194         var row = $("myopac_holds_row_" + record.doc_id());
195         var title_link = findNodeByName(row, "myopac_holds_title_link");
196         var author_link = findNodeByName(row, "myopac_holds_author_link");
197
198         buildTitleDetailLink(record, title_link);
199         buildSearchLink(STYPE_AUTHOR, record.author(), author_link);
200
201         var form = $("myopac_holds_form_" + record.doc_id());
202
203         if(form) {
204                 var img = elem("img");
205                 img.setAttribute("src", buildImageLink('tor/' + record.types_of_resource()[0] + ".jpg"));
206                 addCSSClass(img, "myopac_form_pic");
207                 form.appendChild(img);
208         }
209 }
210
211 var finesShown = false;
212 function myOPACShowFines() {
213         if(finesShown) return; finesShown = true;
214         var req = new Request(FETCH_FINES_SUMMARY, G.user.session, G.user.id() );
215         req.callback(_myOPACShowFines);
216         req.send();
217 }
218
219 function _myOPACShowFines(r) {
220         hideMe($('myopac_fines_summary_loading'));
221         unHideMe($('myopac_fines_summary_row'));
222
223         var summary = r.getResultObject();
224         var total       = "0.00"; /* localization? */
225         var paid                = "0.00";
226         var balance = "0.00";
227         if( instanceOf(summary,mus) ) {
228
229                 total           = _finesFormatNumber(summary.total_owed());
230                 paid            = _finesFormatNumber(summary.total_paid());
231                 balance = _finesFormatNumber(summary.balance_owed());
232
233                 var req = new Request(FETCH_TRANSACTIONS, G.user.session, G.user.id() );
234                 req.callback(myOPACShowTransactions);
235                 req.send();
236         }
237
238         $('myopac_fines_summary_total').appendChild(text(total));
239         $('myopac_fines_summary_paid').appendChild(text(paid));
240         $('myopac_fines_summary_balance').appendChild(text(balance));
241 }
242
243 function _finesFormatNumber(num) {
244         if(isNull(num)) num = 0;
245         num = num + "";
246         if(num.length < 2 || !num.match(/\./)) num += ".00";
247         if(num.match(/\./) && num.charAt(num.length-2) == '.') num += "0";
248         return num;
249 }          
250
251 function _trimSeconds(time) { if(!time) return ""; return time.replace(/\..*/,""); }
252
253
254 var transTemplate;
255 function myOPACShowTransactions(r) {
256
257         if(transTemplate) return;
258         var tbody = $('myopac_fines_tbody');
259         unHideMe($('myopac_trans_table'));
260         transTemplate = tbody.removeChild($('myopac_trans_row'));
261
262         var transactions = r.getResultObject();
263
264         //alert(js2JSON(transactions));
265
266         for( var idx in transactions ) {
267
268                 var trans = transactions[idx].transaction;
269                 var record = transactions[idx].record;
270                 var row = transTemplate.cloneNode(transTemplate);
271
272                 findNodeByName(row,'myopac_trans_start').
273                         appendChild(text(_trimSeconds(trans.xact_start())));
274                 findNodeByName(row,'myopac_trans_last_bill').
275                         appendChild(text(_trimSeconds(trans.last_billing_ts())));
276                 findNodeByName(row,'myopac_trans_last_payment').
277                         appendChild(text(_trimSeconds(trans.last_payment_ts())));
278                 findNodeByName(row,'myopac_trans_init_amount').
279                         appendChild(text(_finesFormatNumber(trans.total_owed())));
280                 findNodeByName(row,'myopac_trans_total_paid').
281                         appendChild(text(_finesFormatNumber(trans.total_paid())));
282                 findNodeByName(row,'myopac_trans_balance').
283                         appendChild(text(_finesFormatNumber(trans.balance_owed())));
284
285                 var extra = "";
286                 var type = trans.xact_type();
287                 findNodeByName(row,'myopac_trans_type').appendChild(text(type));
288                 if( type == "circulation" ) extra = record.title();
289                 findNodeByName(row, 'myopac_trans_extra').appendChild(text(extra));
290
291                 tbody.appendChild(row);
292         }
293 }
294
295
296 function myOPACShowPrefs() {
297 }
298
299 var userShown = false;
300 function myOPACShowSummary() {
301         if(userShown) return; userShown = true;
302         var req = new Request(FETCH_FLESHED_USER,G.user.session, G.user.id());
303         req.callback(_myOPACSummaryShowUer);
304         req.send();
305 }
306
307 function _myOPACSummaryShowUer(r) {
308
309         var user = r.getResultObject();
310         fleshedUser = user;
311
312         $('myopac_summary_first').appendChild(text(user.first_given_name()));
313         $('myopac_summary_middle').appendChild(text(user.second_given_name()));
314         $('myopac_summary_dayphone').appendChild(text(user.day_phone()));
315         $('myopac_summary_eveningphone').appendChild(text(user.evening_phone()));
316         $('myopac_summary_otherphone').appendChild(text(user.other_phone()));
317         $('myopac_summary_last').appendChild(text(user.family_name()));
318         $('myopac_summary_username').appendChild(text(user.usrname()));
319         $('myopac_summary_email').appendChild(text(user.email()));
320         $('myopac_summary_barcode').appendChild(text(user.card().barcode()));
321         $('myopac_summary_ident1').appendChild(text(user.ident_value()));
322         $('myopac_summary_ident2').appendChild(text(user.ident_value2()));
323         $('myopac_summary_homelib').appendChild(text(findOrgUnit(user.home_ou()).name()));
324         $('myopac_summary_create_date').appendChild(text(user.create_date()));
325
326         var tbody = $('myopac_addr_tbody');
327         var template = tbody.removeChild($('myopac_addr_row'));
328         for( var a in user.addresses() ) {
329                 var row = template.cloneNode(true);
330                 myOPACDrawAddr(row, user.addresses()[a]);
331                 tbody.appendChild(row);
332         }
333 }
334
335 function myOPACDrawAddr(row, addr) {
336         findNodeByName(row, 'myopac_addr_type').appendChild(text(addr.address_type()));
337         var street = (addr.street2()) ? addr.street1() + ", " + addr.street2() : addr.street1();
338         findNodeByName(row, 'myopac_addr_street').appendChild(text(street));
339         findNodeByName(row, 'myopac_addr_city').appendChild(text(addr.city()));
340         findNodeByName(row, 'myopac_addr_county').appendChild(text(addr.county()));
341         findNodeByName(row, 'myopac_addr_state').appendChild(text(addr.state()));
342         findNodeByName(row, 'myopac_addr_zip').appendChild(text(addr.post_code()));
343 }
344
345
346
347