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