2 * variables defined in base_js.tt2:
5 * vendor_list = [ 'ebook_test' ]
9 * progress_icon (probably not done right)
11 * base_js.tt2 also "imports" dojo.cookie and a bunch of ebook_api JS
14 // Array of objects representing this patron's relationship with a specific vendor.
24 // Ebook to perform actions on.
26 if (typeof ebook_action.title_id !== 'undefined') {
27 active_ebook = new Ebook(ebook_action.vendor, ebook_action.title_id);
30 dojo.addOnLoad(function() {
32 dojo.forEach(vendor_list, function(v) {
33 var rel = new Relation(v, patron_id);
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();
42 addTransactionsToPage();
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);
56 // Update current page with cross-vendor transaction totals.
57 function addTotalsToPage() {
58 console.log('updating page with transaction totals');
60 updateMyAccountSummary();
63 // Update current page with detailed transaction info, where appropriate.
64 function addTransactionsToPage() {
66 console.log('updating page with cached transaction details, if applicable');
67 if (myopac_page === 'ebook_circs')
69 if (myopac_page === 'ebook_holds')
71 if (myopac_page === 'ebook_holds_ready')
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;
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");
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;
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");
106 function updateCheckoutView() {
107 if (typeof ebook_action.type !== 'undefined') {
108 if (ebook_action.type == 'checkout') {
109 getReadyForCheckout();
111 } else if (xacts.checkouts.length < 1) {
112 dojo.removeClass('no_ebook_circs', "hidden");
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)
124 dojo.addClass('no_ebook_circs', "hidden");
125 dojo.removeClass('ebook_circs_main', "hidden");
129 function updateHoldView() {
130 // handle hold actions
131 if (typeof ebook_action.type !== 'undefined') {
136 if (myopac_page === 'ebook_holds_ready') {
137 // only show holds that are ready for checkout
138 var holds = xacts.holds_ready;
140 var holds_pending = xacts.holds_pending;
141 var holds_ready = xacts.holds_ready;
143 // combine all holds into a single list, ready-for-checkout holds first
144 var holds = holds_ready.concat(holds_pending);
147 if (holds.length < 1) {
148 dojo.removeClass('no_ebook_holds', "hidden");
150 dojo.forEach(holds, function(h) {
153 hold_status = l_strings.ready_for_checkout;
154 } else if (h.is_frozen) {
155 hold_status = l_strings.suspended;
157 hold_status = h.queue_position + ' / ' + h.queue_size;
159 h.doCancelHold = function() {
160 var ebook = new Ebook(this.vendor, this.title_id);
161 ebook.cancelHold(authtoken, patron_id, function(resp) {
162 if (resp.error_msg) {
163 console.log('Cancel hold failed: ' . resp.error_msg);
164 dojo.removeClass('ebook_cancel_hold_failed', "hidden");
166 console.log('Cancel hold succeeded!');
167 dojo.destroy("hold-" + ebook.id);
168 dojo.removeClass('ebook_cancel_hold_succeeded', "hidden");
169 // Updating the transaction cache to remove the canceled hold
170 // is inconvenient, so we skip cleanupAfterAction() and merely
171 // clear transaction cache to force a refresh on next page load.
172 dojo.cookie('ebook_xact_cache', '', {path: '/', expires: '-1h'});
176 dojo.empty('ebook_holds_main_table_body');
177 var tr = dojo.create("tr", { id: "hold-" + h.title_id }, dojo.byId('ebook_holds_main_table_body'));
178 dojo.create("td", { innerHTML: h.title }, tr);
179 dojo.create("td", { innerHTML: h.author }, tr);
180 dojo.create("td", { innerHTML: h.expire_date }, tr);
181 dojo.create("td", { innerHTML: hold_status }, tr);
182 var actions_td = dojo.create("td", null, tr);
183 var button = dojo.create("input", { id: "cancel-hold-" + h.title_id, type: "button", value: l_strings.cancel_hold }, actions_td);
184 dojo.connect(button, 'onclick', h, "doCancelHold");
186 dojo.addClass('no_ebook_holds', "hidden");
187 dojo.removeClass('ebook_holds_main', "hidden");
191 // set up page for user to perform a checkout
192 function getReadyForCheckout() {
193 if (typeof active_ebook === 'undefined') {
194 console.log('No active ebook specified, cannot prepare for checkout');
195 dojo.removeClass('ebook_checkout_failed', "hidden");
197 active_ebook.getDetails( function(ebook) {
198 dojo.empty('ebook_circs_main_table_body');
199 var tr = dojo.create("tr", null, dojo.byId('ebook_circs_main_table_body'));
200 dojo.create("td", { innerHTML: ebook.title }, tr);
201 dojo.create("td", { innerHTML: ebook.author }, tr);
202 dojo.create("td", null, tr);
203 dojo.create("td", { id: "checkout-button-td" }, tr);
204 var button = dojo.create("input", { id: "checkout-button", type: "button", value: l_strings.checkout }, dojo.byId('checkout-button-td'));
205 ebook.conns.checkout = dojo.connect(button, 'onclick', "doCheckout");
206 dojo.removeClass('ebook_circs_main', "hidden");
211 // set up page for user to place a hold
212 function getReadyForHold() {
213 if (typeof active_ebook === 'undefined') {
214 console.log('No active ebook specified, cannot prepare for hold');
215 dojo.removeClass('ebook_hold_failed', "hidden");
217 active_ebook.getDetails( function(ebook) {
218 dojo.empty('ebook_holds_main_table_body');
219 var tr = dojo.create("tr", null, dojo.byId('ebook_holds_main_table_body'));
220 dojo.create("td", { innerHTML: ebook.title }, tr);
221 dojo.create("td", { innerHTML: ebook.author }, tr);
222 dojo.create("td", null, tr); // Expire Date
223 dojo.create("td", null, tr); // Status
224 dojo.create("td", { id: "hold-button-td" }, tr);
225 if (ebook_action.type == 'place_hold') {
226 var button = dojo.create("input", { id: "hold-button", type: "button", value: l_strings.place_hold }, dojo.byId('hold-button-td'));
227 ebook.conns.checkout = dojo.connect(button, 'onclick', "doPlaceHold");
229 dojo.removeClass('ebook_holds_main', "hidden");
234 function cleanupAfterAction() {
235 // unset variables related to the transaction we have performed,
236 // to avoid any weirdness on page reload
238 // update page to account for successful checkout
240 addTransactionsToPage();
241 // clear transaction cache to force a refresh on next page load
242 dojo.cookie('ebook_xact_cache', '', {path: '/', expires: '-1h'});
245 // check out our active ebook
246 function doCheckout() {
247 active_ebook.checkout(authtoken, patron_id, function(resp) {
249 console.log('Checkout succeeded!');
250 dojo.destroy('checkout-button');
251 dojo.removeClass('ebook_checkout_succeeded', "hidden");
252 // add our successful checkout to top of transaction cache
254 title_id: active_ebook.id,
255 title: active_ebook.title,
256 author: active_ebook.author,
257 due_date: resp.due_date,
258 download_url: '' // TODO - for OverDrive, user must "lock in" a format first!
260 xacts.checkouts.unshift(new_xact);
261 cleanupAfterAction();
263 console.log('Checkout failed: ' + resp.error_msg);
264 dojo.removeClass('ebook_checkout_failed', "hidden");
266 // When we switch to jQuery, we can use .one() instead of .on(),
267 // obviating the need for an explicit disconnect here.
268 dojo.disconnect(active_ebook.conns.checkout);
272 // place hold on our active ebook
273 function doPlaceHold() {
274 active_ebook.placeHold(authtoken, patron_id, function(resp) {
275 if (resp.error_msg) {
276 console.log('Place hold failed: ' . resp.error_msg);
277 dojo.removeClass('ebook_place_hold_failed', "hidden");
279 console.log('Place hold succeeded!');
280 dojo.destroy('hold-button');
281 dojo.removeClass('ebook_place_hold_succeeded', "hidden");
283 title_id: active_ebook.id,
284 title: active_ebook.title,
285 author: active_ebook.author,
286 queue_position: resp.queue_position,
287 queue_size: resp.queue_size,
288 expire_date: resp.expire_date
290 if ( resp.is_ready || (resp.queue_position === 1 && resp.queue_size === 1) ) {
291 xacts.holds_ready.unshift(new_hold);
293 xacts.holds_pending.unshift(new_hold);
295 cleanupAfterAction();
300 // deserialize transactions from cache, returning them as a JS object
301 function getCachedTransactions() {
302 console.log('retrieving cached transaction details');
304 var current_cache = dojo.cookie('ebook_xact_cache');
306 cache_obj = JSON.parse(current_cache);
307 xacts.checkouts = cache_obj.checkouts;
308 xacts.holds_pending = cache_obj.holds_pending;
309 xacts.holds_ready = cache_obj.holds_ready;
314 // add a single vendor's transactions to transaction cache
315 function addTransactionsToCache(rel) {
316 console.log('updating transaction cache');
318 var updated_xacts = {
323 // preserve any transactions with other vendors
324 dojo.forEach(xacts.checkouts, function(xact) {
325 if (xact.vendor !== v)
326 updated_xacts.checkouts.push(xact);
328 dojo.forEach(xacts.holds_pending, function(xact) {
329 if (xact.vendor !== v)
330 updated_xacts.holds_pending.push(xact);
332 dojo.forEach(xacts.holds_ready, function(xact) {
333 if (xact.vendor !== v)
334 updated_xacts.holds_ready.push(xact);
336 // add transactions from current vendor
337 dojo.forEach(rel.checkouts, function(xact) {
338 updated_xacts.checkouts.push(xact);
340 dojo.forEach(rel.holds_pending, function(xact) {
341 updated_xacts.holds_pending.push(xact);
343 dojo.forEach(rel.holds_ready, function(xact) {
344 updated_xacts.holds_ready.push(xact);
346 // TODO sort transactions by date
347 // save transactions to cache
348 xacts = updated_xacts;
349 var new_cache = JSON.stringify(xacts);
350 dojo.cookie('ebook_xact_cache', new_cache, {path: '/'});
351 // update current page
353 addTransactionsToPage();