]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/opac/ebook_api/loggedin.js
LP 2061136 follow-up: ng lint --fix
[Evergreen.git] / Open-ILS / web / js / ui / default / opac / ebook_api / loggedin.js
1 /*
2  * variables defined in base_js.tt2:
3  *
4  * ou
5  * vendor_list = [ 'ebook_test' ]
6  * authtoken
7  * patron_id (barcode)
8  * myopac_page
9  * progress_icon (probably not done right)
10  *
11  * base_js.tt2 also "imports" dojo.cookie and a bunch of ebook_api JS
12  */
13
14 // Array of objects representing this patron's relationship with a specific vendor.
15 var relations = [];
16
17 // Transaction cache.
18 var xacts = {
19     checkouts: [],
20     holds_pending: [],
21     holds_ready: []
22 };
23 var ebooks = [];
24
25 // Ebook to perform actions on.
26 var active_ebook;
27 if (typeof ebook_action.title_id !== 'undefined') {
28     active_ebook = new Ebook(ebook_action.vendor, ebook_action.title_id);
29 }
30
31 dojo.addOnLoad(function() {
32
33     dojo.forEach(vendor_list, function(v) {
34         var rel = new Relation(v, patron_id);
35         relations.push(rel);
36     });
37
38     // Pull patron transaction info from cache (cookie), if available.
39     // Otherwise, do a live lookup against all enabled vendors.
40     if (dojo.cookie('ebook_xact_cache')) {
41         getCachedTransactions();
42         addTotalsToPage();
43         addTransactionsToPage();
44     } else {
45         console.log('retrieving patron transaction info for all vendors');
46         dojo.forEach(relations, function(rel) {
47             checkSession(rel.vendor, function(ses) {
48                 rel.getTransactions( function(r) {
49                     addTransactionsToCache(r);
50                 });
51             });
52         });
53     }
54
55 });
56
57 // Update current page with cross-vendor transaction totals.
58 function addTotalsToPage() {
59     console.log('updating page with transaction totals');
60     updateDashboard();
61     updateMyAccountSummary();
62 }
63
64 // Update current page with detailed transaction info, where appropriate.
65 function addTransactionsToPage() {
66     // ensure active ebook has access to session ID to avoid scoping issues during transactions
67     if (active_ebook && typeof active_ebook.vendor !== 'undefined') {
68         active_ebook.ses = active_ebook.ses || dojo.cookie(active_ebook.vendor);
69     }
70     if (dojo.byId('ebook_spinner')) dojo.addClass('ebook_spinner', "hidden");
71     if (myopac_page) {
72         console.log('updating page with cached transaction details, if applicable');
73         if (myopac_page === 'ebook_circs')
74             updateCheckoutView();
75         if (myopac_page === 'ebook_holds')
76             updateHoldView();
77         if (myopac_page === 'ebook_holds_ready')
78             updateHoldView();
79         if (myopac_page === 'ebook_checkout')
80             getReadyForCheckout();
81         if (myopac_page === 'ebook_place_hold')
82             getReadyForHold();
83     }
84 }
85         
86 function updateDashboard() {
87     console.log('updating dashboard');
88     var total_checkouts = (typeof xacts.checkouts === 'undefined') ? '-' : xacts.checkouts.length;
89     var total_holds_pending = (typeof xacts.holds_pending === 'undefined') ? '-' : xacts.holds_pending.length;
90     var total_holds_ready = (typeof xacts.holds_ready === 'undefined') ? '-' : xacts.holds_ready.length;
91     // update totals
92     dojo.byId('dash_e_checked').innerHTML = total_checkouts;
93     dojo.byId('dash_e_holds').innerHTML = total_holds_pending;
94     dojo.byId('dash_e_pickup').innerHTML = total_holds_ready;
95     // unhide ebook dashboard
96     dojo.removeClass('dashboard_e', "hidden");
97 }
98
99 function updateMyAccountSummary() {
100     if (myopac_page === 'main') {
101         console.log('updating account summary');
102         var total_checkouts = (typeof xacts.checkouts === 'undefined') ? '-' : xacts.checkouts.length;
103         var total_holds_pending = (typeof xacts.holds_pending === 'undefined') ? '-' : xacts.holds_pending.length;
104         var total_holds_ready = (typeof xacts.holds_ready === 'undefined') ? '-' : xacts.holds_ready.length;
105         // update totals
106         dojo.byId('acct_sum_ebook_circ_total').innerHTML = total_checkouts;
107         dojo.byId('acct_sum_ebook_hold_total').innerHTML = total_holds_pending;
108         dojo.byId('acct_sum_ebook_hold_ready_total').innerHTML = total_holds_ready;
109         // unhide display elements
110         dojo.removeClass('acct_sum_ebook_circs', "hidden");
111         dojo.removeClass('acct_sum_ebook_holds', "hidden");
112         dojo.removeClass('acct_sum_ebook_holds_ready', "hidden");
113     }
114 }
115
116 function updateCheckoutView() {
117     if (xacts.checkouts.length < 1) {
118         dojo.removeClass('no_ebook_circs', "hidden");
119     } else {
120         dojo.empty('ebook_circs_main_table_body');
121         dojo.forEach(xacts.checkouts, function(x) {
122             var ebook = new Ebook(x.vendor, x.title_id);
123             var tr = dojo.create("tr", null, dojo.byId('ebook_circs_main_table_body'));
124             dojo.create("td", { innerHTML: x.title }, tr);
125             dojo.create("td", { innerHTML: x.author }, tr);
126             dojo.create("td", { innerHTML: x.due_date }, tr);
127             var dl_td = dojo.create("td", null, tr);
128             if (x.download_url) {
129                 dl_td.innerHTML = '<a href="' + x.download_url + '">' + l_strings.download + '</a>';
130             }
131             if (x.formats) {
132                 var select = dojo.create("select", { id: "download-format" }, dl_td);
133                 for (f in x.formats) {
134                     dojo.create("option", { value: x.formats[f], innerHTML: f }, select);
135                 }
136                 var button = dojo.create("input", { id: "download-button", type: "button", value: l_strings.download }, dl_td);
137                 ebook.conns.download = dojo.connect(button, 'onclick', ebook, "download");
138             }
139             // TODO: more actions (renew, checkin)
140             ebooks.push(ebook);
141         });
142         dojo.addClass('no_ebook_circs', "hidden");
143         dojo.removeClass('ebook_circs_main', "hidden");
144     }
145 }
146
147 function updateHoldView() {
148     if (myopac_page === 'ebook_holds_ready') {
149         // only show holds that are ready for checkout
150         var holds = xacts.holds_ready;
151     } else {
152         var holds_pending = xacts.holds_pending;
153         var holds_ready = xacts.holds_ready;
154
155         // combine all holds into a single list, ready-for-checkout holds first
156         var holds = holds_ready.concat(holds_pending);
157     }
158
159     if (holds.length < 1) {
160         dojo.removeClass('no_ebook_holds', "hidden");
161     } else {
162         dojo.empty('ebook_holds_main_table_body');
163         dojo.forEach(holds, function(h) {
164             var hold_status;
165             if (h.is_ready) {
166                 hold_status = l_strings.ready_for_checkout;
167             } else if (h.is_frozen) {
168                 hold_status = l_strings.suspended;
169             } else {
170                 hold_status = h.queue_position + ' / ' + h.queue_size;
171             }
172             h.doCancelHold = function() {
173                 var vendor = this.vendor;
174                 var title_id = this.title_id;
175                 checkSession(vendor, function() {
176                     var ebook = new Ebook(vendor, title_id);
177                     ebook.cancelHold(authtoken, patron_id, function(resp) {
178                         if (resp.error_msg) {
179                             console.log('Cancel hold failed: ' + resp.error_msg);
180                             dojo.removeClass('ebook_cancel_hold_failed', "hidden");
181                         } else {
182                             console.log('Cancel hold succeeded!');
183                             dojo.destroy("hold-" + ebook.id);
184                             dojo.removeClass('ebook_cancel_hold_succeeded', "hidden");
185                             // Updating the transaction cache to remove the canceled hold
186                             // is inconvenient, so we skip cleanupAfterAction() and merely
187                             // clear transaction cache to force a refresh on next page load.
188                             dojo.cookie('ebook_xact_cache', '', {path: '/', expires: '-1h'});
189                         }
190                     });
191                 });
192             };
193             var tr = dojo.create("tr", { id: "hold-" + h.title_id }, dojo.byId('ebook_holds_main_table_body'));
194             dojo.create("td", { innerHTML: h.title }, tr);
195             dojo.create("td", { innerHTML: h.author }, tr);
196             dojo.create("td", { innerHTML: h.expire_date }, tr);
197             dojo.create("td", { innerHTML: hold_status }, tr);
198             var actions_td = dojo.create("td", null, tr);
199             var button = dojo.create("input", { id: "cancel-hold-" + h.title_id, type: "button", value: l_strings.cancel_hold }, actions_td);
200             dojo.connect(button, 'onclick', h, "doCancelHold");
201         });
202         dojo.addClass('no_ebook_holds', "hidden");
203         dojo.removeClass('ebook_holds_main', "hidden");
204     }
205 }
206
207 // set up page for user to perform a checkout
208 function getReadyForCheckout() {
209     if (typeof ebook_action.type === 'undefined')
210         return;
211     if (typeof active_ebook === 'undefined') {
212         console.log('No active ebook specified, cannot prepare for checkout');
213         dojo.removeClass('ebook_checkout_failed', "hidden");
214     } else {
215         active_ebook.getDetails( function(ebook) {
216             dojo.empty('ebook_circs_main_table_body');
217             var tr = dojo.create("tr", null, dojo.byId('ebook_circs_main_table_body'));
218             dojo.create("td", { innerHTML: ebook.title }, tr);
219             dojo.create("td", { innerHTML: ebook.author }, tr);
220             dojo.create("td", null, tr);
221             dojo.create("td", { id: "checkout-button-td" }, tr);
222             if (typeof active_ebook.formats !== 'undefined') {
223                 var select = dojo.create("select", { id: "checkout-format" }, dojo.byId('checkout-button-td'));
224                 dojo.forEach(active_ebook.formats, function(f) {
225                     dojo.create("option", { value: f.id, innerHTML: f.name }, select);
226                 });
227             }
228             var button = dojo.create("input", { id: "checkout-button", type: "button", value: l_strings.checkout }, dojo.byId('checkout-button-td'));
229             ebook.conns.checkout = dojo.connect(button, 'onclick', "doCheckout");
230             dojo.removeClass('ebook_circs_main', "hidden");
231         });
232     }
233 }
234
235 // set up page for user to place a hold
236 function getReadyForHold() {
237     if (typeof ebook_action.type === 'undefined')
238         return;
239     if (typeof active_ebook === 'undefined') {
240         console.log('No active ebook specified, cannot prepare for hold');
241         dojo.removeClass('ebook_hold_failed', "hidden");
242     } else {
243         active_ebook.getDetails( function(ebook) {
244             dojo.empty('ebook_holds_main_table_body');
245             var tr = dojo.create("tr", null, dojo.byId('ebook_holds_main_table_body'));
246             dojo.create("td", { innerHTML: ebook.title }, tr);
247             dojo.create("td", { innerHTML: ebook.author }, tr);
248             dojo.create("td", null, tr); // Expire Date
249             dojo.create("td", null, tr); // Status
250             dojo.create("td", { id: "hold-button-td" }, tr);
251             if (ebook_action.type == 'place_hold') {
252                 var button = dojo.create("input", { id: "hold-button", type: "button", value: l_strings.place_hold }, dojo.byId('hold-button-td'));
253                 ebook.conns.checkout = dojo.connect(button, 'onclick', "doPlaceHold");
254             }
255             dojo.removeClass('ebook_holds_main', "hidden");
256         });
257     }
258 }
259
260 function cleanupAfterAction() {
261     // unset variables related to the transaction we have performed,
262     // to avoid any weirdness on page reload
263     ebook_action = {};
264     // update page to account for successful checkout
265     addTotalsToPage();
266     addTransactionsToPage();
267     // clear transaction cache to force a refresh on next page load
268     dojo.cookie('ebook_xact_cache', '', {path: '/', expires: '-1h'});
269 }
270
271 // check out our active ebook
272 function doCheckout() {
273     var ses = dojo.cookie(active_ebook.vendor); // required when inspecting checkouts for download_url
274     active_ebook.checkout(authtoken, patron_id, function(resp) {
275         if (resp.error_msg) {
276             console.log('Checkout failed: ' + resp.error_msg);
277             dojo.removeClass('ebook_checkout_failed', "hidden");
278             return;
279         }
280         console.log('Checkout succeeded!');
281         dojo.destroy('checkout-button');
282         dojo.destroy('checkout-format'); // remove optional format selector
283         dojo.removeClass('ebook_checkout_succeeded', "hidden");
284         // add our successful checkout to top of transaction cache
285         var new_xact = {
286             title_id: active_ebook.id,
287             title: active_ebook.title,
288             author: active_ebook.author,
289             due_date: resp.due_date,
290             finish: function() {
291                 console.log('new_xact.finish()');
292                 xacts.checkouts.unshift(this);
293                 cleanupAfterAction();
294                 // When we switch to jQuery, we can use .one() instead of .on(),
295                 // obviating the need for an explicit disconnect here.
296                 dojo.disconnect(active_ebook.conns.checkout);
297             }
298         };
299         if (resp.download_url) {
300             // Use download URL from checkout response, if available.
301             new_xact.download_url = resp.download_url;
302             dojo.create("a", { href: new_xact.download_url, innerHTML: l_strings.download }, dojo.byId('checkout-button-td'));
303             new_xact.finish();
304         } else if (typeof resp.formats !== 'undefined') {
305             // User must select download format from list of options.
306             var select = dojo.create("select", { id: "download-format" }, dojo.byId('checkout-button-td'));
307             for (f in resp.formats) {
308                 dojo.create("option", { value: resp.formats[f], innerHTML: f }, select);
309             }
310             var button = dojo.create("input", { id: "download-button", type: "button", value: l_strings.download }, dojo.byId('checkout-button-td'));
311             active_ebook.conns.download = dojo.connect(button, 'onclick', active_ebook, "download");
312             new_xact.finish();
313         } else if (typeof resp.xact_id !== 'undefined') {
314             // No download URL provided by API checkout response.  Grab fresh
315             // list of user checkouts from API, find the just-completed
316             // checkout by transaction ID, and get the download URL from that.
317             // We call the OpenSRF method directly because Relation.getCheckouts()
318             // results in scoping issues when retrieving the vendor session cookie.
319             new_xact.xact_id = resp.xact_id;
320             new OpenSRF.ClientSession('open-ils.ebook_api').request({
321                 method: 'open-ils.ebook_api.patron.get_checkouts',
322                 params: [ authtoken, ses, patron_id ],
323                 async: false,
324                 oncomplete: function(r) {
325                     var resp = r.recv();
326                     if (resp) {
327                         dojo.forEach(resp.content(), function(x) {
328                             if (x.xact_id === new_xact.xact_id) {
329                                 new_xact.download_url = x.download_url;
330                                 dojo.create("a", { href: new_xact.download_url, innerHTML: l_strings.download }, dojo.byId('checkout-button-td'));
331                                 return;
332                             }
333                         });
334                         new_xact.finish();
335                     }
336                 }
337             }).send();
338         }
339     });
340 }
341
342 // place hold on our active ebook
343 function doPlaceHold() {
344     active_ebook.placeHold(authtoken, patron_id, function(resp) {
345         if (resp.error_msg) {
346             console.log('Place hold failed: ' + resp.error_msg);
347             dojo.removeClass('ebook_place_hold_failed', "hidden");
348         } else {
349             console.log('Place hold succeeded!');
350             dojo.destroy('hold-button');
351             dojo.removeClass('ebook_place_hold_succeeded', "hidden");
352             var new_hold = {
353                 title_id: active_ebook.id,
354                 title: active_ebook.title,
355                 author: active_ebook.author,
356                 queue_position: resp.queue_position,
357                 queue_size: resp.queue_size,
358                 expire_date: resp.expire_date
359             };
360             if ( resp.is_ready || (resp.queue_position === 1 && resp.queue_size === 1) ) {
361                 xacts.holds_ready.unshift(new_hold);
362             } else {
363                 xacts.holds_pending.unshift(new_hold);
364             }
365             cleanupAfterAction();
366         }
367     });
368 }
369
370 // deserialize transactions from cache, returning them as a JS object
371 function getCachedTransactions() {
372     console.log('retrieving cached transaction details');
373     var cache_obj;
374     var current_cache = dojo.cookie('ebook_xact_cache');
375     if (current_cache) {
376         cache_obj = JSON.parse(current_cache);
377         xacts.checkouts = cache_obj.checkouts;
378         xacts.holds_pending = cache_obj.holds_pending;
379         xacts.holds_ready = cache_obj.holds_ready;
380     }
381     return cache_obj;
382 }
383
384 // add a single vendor's transactions to transaction cache
385 function addTransactionsToCache(rel) {
386     console.log('updating transaction cache');
387     var v = rel.vendor;
388     var updated_xacts = {
389         checkouts: [],
390         holds_pending: [],
391         holds_ready: []
392     };
393     // preserve any transactions with other vendors
394     dojo.forEach(xacts.checkouts, function(xact) {
395         if (xact.vendor !== v)
396             updated_xacts.checkouts.push(xact);
397     });
398     dojo.forEach(xacts.holds_pending, function(xact) {
399         if (xact.vendor !== v)
400             updated_xacts.holds_pending.push(xact);
401     });
402     dojo.forEach(xacts.holds_ready, function(xact) {
403         if (xact.vendor !== v)
404             updated_xacts.holds_ready.push(xact);
405     });
406     // add transactions from current vendor
407     dojo.forEach(rel.checkouts, function(xact) {
408         updated_xacts.checkouts.push(xact);
409     });
410     dojo.forEach(rel.holds_pending, function(xact) {
411         updated_xacts.holds_pending.push(xact);
412     });
413     dojo.forEach(rel.holds_ready, function(xact) {
414         updated_xacts.holds_ready.push(xact);
415     });
416     // TODO sort transactions by date
417     // save transactions to cache
418     xacts = updated_xacts;
419     var new_cache = JSON.stringify(xacts);
420     dojo.cookie('ebook_xact_cache', new_cache, {path: '/'});
421     // update current page
422     addTotalsToPage();
423     addTransactionsToPage();
424 }
425