]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/opac/ebook_api/loggedin.js
LP#1673870: Add ebook API cancel hold functionality to OPAC
[working/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
24 // Ebook to perform actions on.
25 var active_ebook;
26 if (typeof ebook_action.title_id !== 'undefined') {
27     active_ebook = new Ebook(ebook_action.vendor, ebook_action.title_id);
28 }
29
30 dojo.addOnLoad(function() {
31
32     dojo.forEach(vendor_list, function(v) {
33         var rel = new Relation(v, patron_id);
34         relations.push(rel);
35     });
36
37     // Pull patron transaction info from cache (cookie), if available.
38     // Otherwise, do a live lookup against all enabled vendors.
39     if (dojo.cookie('ebook_xact_cache')) {
40         getCachedTransactions();
41         addTotalsToPage();
42         addTransactionsToPage();
43     } else {
44         console.log('retrieving patron transaction info for all vendors');
45         dojo.forEach(relations, function(rel) {
46             checkSession(rel.vendor, function(ses) {
47                 rel.getTransactions( function(r) {
48                     addTransactionsToCache(r);
49                 });
50             });
51         });
52     }
53
54 });
55
56 // Update current page with cross-vendor transaction totals.
57 function addTotalsToPage() {
58     console.log('updating page with transaction totals');
59     updateDashboard();
60     updateMyAccountSummary();
61 }
62
63 // Update current page with detailed transaction info, where appropriate.
64 function addTransactionsToPage() {
65     if (myopac_page) {
66         console.log('updating page with cached transaction details, if applicable');
67         if (myopac_page === 'ebook_circs')
68             updateCheckoutView();
69         if (myopac_page === 'ebook_holds')
70             updateHoldView();
71         if (myopac_page === 'ebook_holds_ready')
72             updateHoldReadyView();
73     }
74 }
75         
76 function updateDashboard() {
77     console.log('updating dashboard');
78     var total_checkouts = (typeof xacts.checkouts === 'undefined') ? '-' : xacts.checkouts.length;
79     var total_holds_pending = (typeof xacts.holds_pending === 'undefined') ? '-' : xacts.holds_pending.length;
80     var total_holds_ready = (typeof xacts.holds_ready === 'undefined') ? '-' : xacts.holds_ready.length;
81     // update totals
82     dojo.byId('dash_e_checked').innerHTML = total_checkouts;
83     dojo.byId('dash_e_holds').innerHTML = total_holds_pending;
84     dojo.byId('dash_e_pickup').innerHTML = total_holds_ready;
85     // unhide ebook dashboard
86     dojo.removeClass('dashboard_e', "hidden");
87 }
88
89 function updateMyAccountSummary() {
90     if (myopac_page === 'main') {
91         console.log('updating account summary');
92         var total_checkouts = (typeof xacts.checkouts === 'undefined') ? '-' : xacts.checkouts.length;
93         var total_holds_pending = (typeof xacts.holds_pending === 'undefined') ? '-' : xacts.holds_pending.length;
94         var total_holds_ready = (typeof xacts.holds_ready === 'undefined') ? '-' : xacts.holds_ready.length;
95         // update totals
96         dojo.byId('acct_sum_ebook_circ_total').innerHTML = total_checkouts;
97         dojo.byId('acct_sum_ebook_hold_total').innerHTML = total_holds_pending;
98         dojo.byId('acct_sum_ebook_hold_ready_total').innerHTML = total_holds_ready;
99         // unhide display elements
100         dojo.removeClass('acct_sum_ebook_circs', "hidden");
101         dojo.removeClass('acct_sum_ebook_holds', "hidden");
102         dojo.removeClass('acct_sum_ebook_holds_ready', "hidden");
103     }
104 }
105
106 function updateCheckoutView() {
107     if (typeof ebook_action.type !== 'undefined') {
108         if (ebook_action.type == 'checkout') {
109             getReadyForCheckout();
110         }
111     } else if (xacts.checkouts.length < 1) {
112         dojo.removeClass('no_ebook_circs', "hidden");
113     } else {
114         dojo.forEach(xacts.checkouts, function(x) {
115             dojo.empty('ebook_circs_main_table_body');
116             var dl_link = '<a href="' + x.download_url + '">' + l_strings.download + '</a>';
117             var tr = dojo.create("tr", null, dojo.byId('ebook_circs_main_table_body'));
118             dojo.create("td", { innerHTML: x.title }, tr);
119             dojo.create("td", { innerHTML: x.author }, tr);
120             dojo.create("td", { innerHTML: x.due_date }, tr);
121             dojo.create("td", { innerHTML: dl_link}, tr);
122             // TODO: more actions (renew, checkin)
123         });
124         dojo.addClass('no_ebook_circs', "hidden");
125         dojo.removeClass('ebook_circs_main', "hidden");
126     }
127 }
128
129 function updateHoldView() {
130     // handle hold actions
131     if (typeof ebook_action.type !== 'undefined') {
132         getReadyForHold();
133         return;
134     }
135
136     // no hold action, just displaying holds
137     var holds_pending = xacts.holds_pending;
138     var holds_ready = xacts.holds_ready;
139
140     // combine all holds into a single list, ready-for-checkout holds first
141     var holds = holds_ready.concat(holds_pending);
142
143     if (holds.length < 1) {
144         dojo.removeClass('no_ebook_holds', "hidden");
145     } else {
146         dojo.forEach(holds, function(h) {
147             var hold_status;
148             if (h.is_ready) {
149                 hold_status = l_strings.ready_for_checkout;
150             } else if (h.is_frozen) {
151                 hold_status = l_strings.suspended;
152             } else {
153                 hold_status = h.queue_position + ' / ' + h.queue_size;
154             }
155             h.doCancelHold = function() {
156                 var ebook = new Ebook(this.vendor, this.title_id);
157                 ebook.cancelHold(authtoken, patron_id, function(resp) {
158                     if (resp.error_msg) {
159                         console.log('Cancel hold failed: ' . resp.error_msg);
160                         dojo.removeClass('ebook_cancel_hold_failed', "hidden");
161                     } else {
162                         console.log('Cancel hold succeeded!');
163                         dojo.destroy("hold-" + ebook.id);
164                         dojo.removeClass('ebook_cancel_hold_succeeded', "hidden");
165                         // Updating the transaction cache to remove the canceled hold
166                         // is inconvenient, so we skip cleanupAfterAction() and merely
167                         // clear transaction cache to force a refresh on next page load.
168                         dojo.cookie('ebook_xact_cache', '', {path: '/', expires: '-1h'});
169                     }
170                 });
171             };
172             dojo.empty('ebook_holds_main_table_body');
173             var tr = dojo.create("tr", { id: "hold-" + h.title_id }, dojo.byId('ebook_holds_main_table_body'));
174             dojo.create("td", { innerHTML: h.title }, tr);
175             dojo.create("td", { innerHTML: h.author }, tr);
176             dojo.create("td", { innerHTML: h.expire_date }, tr);
177             dojo.create("td", { innerHTML: hold_status }, tr);
178             var actions_td = dojo.create("td", null, tr);
179             var button = dojo.create("input", { id: "cancel-hold-" + h.title_id, type: "button", value: l_strings.cancel_hold }, actions_td);
180             dojo.connect(button, 'onclick', h, "doCancelHold");
181         });
182         dojo.addClass('no_ebook_holds', "hidden");
183         dojo.removeClass('ebook_holds_main', "hidden");
184     }
185 }
186
187 function updateHoldReadyView() {
188     var holds = xacts.holds_ready;
189     if (holds.length < 1) {
190         dojo.removeClass('no_ebook_holds', "hidden");
191     } else {
192         dojo.forEach(holds, function(h) {
193             dojo.empty('ebook_holds_main_table_body');
194             var tr = dojo.create("tr", null, dojo.byId('ebook_holds_main_table_body'));
195             dojo.create("td", { innerHTML: h.title }, tr);
196             dojo.create("td", { innerHTML: h.author }, tr);
197             dojo.create("td", { innerHTML: h.expire_date }, tr);
198             var actions_td = dojo.create("td", null, tr);
199             var actions_ul = dojo.create("ul", null, actions_td);
200             var cancel_hold_li = dojo.create("li", null, actions_ul);
201             dojo.create("a", { href: ebookActionUrl(h, "cancel_hold"), innerHTML: l_strings.cancel_hold }, cancel_hold_li);
202         });
203         dojo.addClass('no_ebook_holds', "hidden");
204         dojo.removeClass('ebook_holds_main', "hidden");
205     }
206 }
207
208 // construct a link to perform an ebook action
209 // TODO: preserve any existing GET params (aside from vendor, title, action)
210 function ebookActionUrl(xact, action) {
211     var base_uri;
212     if (action === 'checkout') {
213         base_uri = 'ebook_circs';
214     } else if (action === 'place_hold' || action === 'cancel_hold') {
215         base_uri = 'ebook_holds';
216     } else {
217         return;
218     }
219     return base_uri + "?vendor=" + xact.vendor + ";title=" + xact.title_id + ";action=" + action;
220 }
221
222 // set up page for user to perform a checkout
223 function getReadyForCheckout() {
224     if (typeof active_ebook === 'undefined') {
225         console.log('No active ebook specified, cannot prepare for checkout');
226         dojo.removeClass('ebook_checkout_failed', "hidden");
227     } else {
228         active_ebook.getDetails( function(ebook) {
229             dojo.empty('ebook_circs_main_table_body');
230             var tr = dojo.create("tr", null, dojo.byId('ebook_circs_main_table_body'));
231             dojo.create("td", { innerHTML: ebook.title }, tr);
232             dojo.create("td", { innerHTML: ebook.author }, tr);
233             dojo.create("td", null, tr);
234             dojo.create("td", { id: "checkout-button-td" }, tr);
235             var button = dojo.create("input", { id: "checkout-button", type: "button", value: l_strings.checkout }, dojo.byId('checkout-button-td'));
236             ebook.conns.checkout = dojo.connect(button, 'onclick', "doCheckout");
237             dojo.removeClass('ebook_circs_main', "hidden");
238         });
239     }
240 }
241
242 // set up page for user to place a hold
243 function getReadyForHold() {
244     if (typeof active_ebook === 'undefined') {
245         console.log('No active ebook specified, cannot prepare for hold');
246         dojo.removeClass('ebook_hold_failed', "hidden");
247     } else {
248         active_ebook.getDetails( function(ebook) {
249             dojo.empty('ebook_holds_main_table_body');
250             var tr = dojo.create("tr", null, dojo.byId('ebook_holds_main_table_body'));
251             dojo.create("td", { innerHTML: ebook.title }, tr);
252             dojo.create("td", { innerHTML: ebook.author }, tr);
253             dojo.create("td", null, tr); // Expire Date
254             dojo.create("td", null, tr); // Status
255             dojo.create("td", { id: "hold-button-td" }, tr);
256             if (ebook_action.type == 'place_hold') {
257                 var button = dojo.create("input", { id: "hold-button", type: "button", value: l_strings.place_hold }, dojo.byId('hold-button-td'));
258                 ebook.conns.checkout = dojo.connect(button, 'onclick', "doPlaceHold");
259             }
260             dojo.removeClass('ebook_holds_main', "hidden");
261         });
262     }
263 }
264
265 function cleanupAfterAction() {
266     // unset variables related to the transaction we have performed,
267     // to avoid any weirdness on page reload
268     ebook_action = {};
269     // update page to account for successful checkout
270     addTotalsToPage();
271     addTransactionsToPage();
272     // clear transaction cache to force a refresh on next page load
273     dojo.cookie('ebook_xact_cache', '', {path: '/', expires: '-1h'});
274 }
275
276 // check out our active ebook
277 function doCheckout() {
278     active_ebook.checkout(authtoken, patron_id, function(resp) {
279         if (resp.due_date) {
280             console.log('Checkout succeeded!');
281             dojo.destroy('checkout-button');
282             dojo.removeClass('ebook_checkout_succeeded', "hidden");
283             // add our successful checkout to top of transaction cache
284             var new_xact = {
285                 title_id: active_ebook.id,
286                 title: active_ebook.title,
287                 author: active_ebook.author,
288                 due_date: resp.due_date,
289                 download_url: '' // TODO - for OverDrive, user must "lock in" a format first!
290             };
291             xacts.checkouts.unshift(new_xact);
292             cleanupAfterAction();
293         } else {
294             console.log('Checkout failed: ' + resp.error_msg);
295             dojo.removeClass('ebook_checkout_failed', "hidden");
296         }
297         // When we switch to jQuery, we can use .one() instead of .on(),
298         // obviating the need for an explicit disconnect here.
299         dojo.disconnect(active_ebook.conns.checkout);
300     });
301 }
302
303 // place hold on our active ebook
304 function doPlaceHold() {
305     active_ebook.placeHold(authtoken, patron_id, function(resp) {
306         if (resp.error_msg) {
307             console.log('Place hold failed: ' . resp.error_msg);
308             dojo.removeClass('ebook_place_hold_failed', "hidden");
309         } else {
310             console.log('Place hold succeeded!');
311             dojo.destroy('hold-button');
312             dojo.removeClass('ebook_place_hold_succeeded', "hidden");
313             var new_hold = {
314                 title_id: active_ebook.id,
315                 title: active_ebook.title,
316                 author: active_ebook.author,
317                 queue_position: resp.queue_position,
318                 queue_size: resp.queue_size,
319                 expire_date: resp.expire_date
320             };
321             if ( resp.is_ready || (resp.queue_position === 1 && resp.queue_size === 1) ) {
322                 xacts.holds_ready.unshift(new_hold);
323             } else {
324                 xacts.holds_pending.unshift(new_hold);
325             }
326             cleanupAfterAction();
327         }
328     });
329 }
330
331 // deserialize transactions from cache, returning them as a JS object
332 function getCachedTransactions() {
333     console.log('retrieving cached transaction details');
334     var cache_obj;
335     var current_cache = dojo.cookie('ebook_xact_cache');
336     if (current_cache) {
337         cache_obj = JSON.parse(current_cache);
338         xacts.checkouts = cache_obj.checkouts;
339         xacts.holds_pending = cache_obj.holds_pending;
340         xacts.holds_ready = cache_obj.holds_ready;
341     }
342     return cache_obj;
343 }
344
345 // add a single vendor's transactions to transaction cache
346 function addTransactionsToCache(rel) {
347     console.log('updating transaction cache');
348     var v = rel.vendor;
349     var updated_xacts = {
350         checkouts: [],
351         holds_pending: [],
352         holds_ready: []
353     };
354     // preserve any transactions with other vendors
355     dojo.forEach(xacts.checkouts, function(xact) {
356         if (xact.vendor !== v)
357             updated_xacts.checkouts.push(xact);
358     });
359     dojo.forEach(xacts.holds_pending, function(xact) {
360         if (xact.vendor !== v)
361             updated_xacts.holds_pending.push(xact);
362     });
363     dojo.forEach(xacts.holds_ready, function(xact) {
364         if (xact.vendor !== v)
365             updated_xacts.holds_ready.push(xact);
366     });
367     // add transactions from current vendor
368     dojo.forEach(rel.checkouts, function(xact) {
369         updated_xacts.checkouts.push(xact);
370     });
371     dojo.forEach(rel.holds_pending, function(xact) {
372         updated_xacts.holds_pending.push(xact);
373     });
374     dojo.forEach(rel.holds_ready, function(xact) {
375         updated_xacts.holds_ready.push(xact);
376     });
377     // TODO sort transactions by date
378     // save transactions to cache
379     xacts = updated_xacts;
380     var new_cache = JSON.stringify(xacts);
381     dojo.cookie('ebook_xact_cache', new_cache, {path: '/'});
382     // update current page
383     addTotalsToPage();
384     addTransactionsToPage();
385 }
386