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.
25 // Ebook to perform actions on.
27 if (typeof ebook_action.title_id !== 'undefined') {
28 active_ebook = new Ebook(ebook_action.vendor, ebook_action.title_id);
31 dojo.addOnLoad(function() {
33 dojo.forEach(vendor_list, function(v) {
34 var rel = new Relation(v, patron_id);
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();
43 addTransactionsToPage();
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);
57 // Update current page with cross-vendor transaction totals.
58 function addTotalsToPage() {
59 console.log('updating page with transaction totals');
61 updateMyAccountSummary();
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);
70 if (dojo.byId('ebook_spinner')) dojo.addClass('ebook_spinner', "hidden");
72 console.log('updating page with cached transaction details, if applicable');
73 if (myopac_page === 'ebook_circs')
75 if (myopac_page === 'ebook_holds')
77 if (myopac_page === 'ebook_holds_ready')
79 if (myopac_page === 'ebook_checkout')
80 getReadyForCheckout();
81 if (myopac_page === 'ebook_place_hold')
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;
92 var eCheckout = document.getElementById('dash_e_checked');
93 var eHolds = document.getElementById('dash_e_holds');
94 var ePickup = document.getElementById('dash_e_pickup');
95 var eDash = document.getElementById('dashboard_e');
97 if(typeof(eCheckout) != 'undefined' && eCheckout != null)
99 dojo.byId('dash_e_checked').innerHTML = total_checkouts;
101 if(typeof(eHolds) != 'undefined' && eHolds != null)
103 dojo.byId('dash_e_holds').innerHTML = total_holds_pending;
105 if(typeof(ePickup) != 'undefined' && ePickup != null)
107 dojo.byId('dash_e_pickup').innerHTML = total_holds_ready;
109 if(typeof(eDash) != 'undefined' && eDash != null)
111 // unhide ebook dashboard
112 dojo.removeClass('dashboard_e', "hidden");
117 function updateMyAccountSummary() {
118 if (myopac_page === 'main') {
119 console.log('updating account summary');
120 var total_checkouts = (typeof xacts.checkouts === 'undefined') ? '-' : xacts.checkouts.length;
121 var total_holds_pending = (typeof xacts.holds_pending === 'undefined') ? '-' : xacts.holds_pending.length;
122 var total_holds_ready = (typeof xacts.holds_ready === 'undefined') ? '-' : xacts.holds_ready.length;
124 dojo.byId('acct_sum_ebook_circ_total').innerHTML = total_checkouts;
125 dojo.byId('acct_sum_ebook_hold_total').innerHTML = total_holds_pending;
126 dojo.byId('acct_sum_ebook_hold_ready_total').innerHTML = total_holds_ready;
127 // unhide display elements
128 dojo.removeClass('acct_sum_ebook_circs', "hidden");
129 dojo.removeClass('acct_sum_ebook_holds', "hidden");
130 dojo.removeClass('acct_sum_ebook_holds_ready', "hidden");
134 function updateCheckoutView() {
135 if (xacts.checkouts.length < 1) {
136 dojo.removeClass('no_ebook_circs', "hidden");
138 dojo.empty('ebook_circs_main_table_body');
139 dojo.forEach(xacts.checkouts, function(x) {
140 var ebook = new Ebook(x.vendor, x.title_id);
141 var tr = dojo.create("tr", null, dojo.byId('ebook_circs_main_table_body'));
142 dojo.create("td", { innerHTML: x.title }, tr);
143 dojo.create("td", { innerHTML: x.author }, tr);
144 dojo.create("td", { innerHTML: x.due_date }, tr);
145 var dl_td = dojo.create("td", null, tr);
146 if (x.download_url) {
147 dl_td.innerHTML = '<a href="' + x.download_url + '">' + l_strings.download + '</a>';
150 var select = dojo.create("select", { id: "download-format" }, dl_td);
151 for (f in x.formats) {
152 dojo.create("option", { value: x.formats[f], innerHTML: f }, select);
154 var button = dojo.create("input", { id: "download-button", type: "button", value: l_strings.download }, dl_td);
155 ebook.conns.download = dojo.connect(button, 'onclick', ebook, "download");
157 // TODO: more actions (renew, checkin)
160 dojo.addClass('no_ebook_circs', "hidden");
161 dojo.removeClass('ebook_circs_main', "hidden");
165 function updateHoldView() {
166 if (myopac_page === 'ebook_holds_ready') {
167 // only show holds that are ready for checkout
168 var holds = xacts.holds_ready;
170 var holds_pending = xacts.holds_pending;
171 var holds_ready = xacts.holds_ready;
173 // combine all holds into a single list, ready-for-checkout holds first
174 var holds = holds_ready.concat(holds_pending);
177 if (holds.length < 1) {
178 dojo.removeClass('no_ebook_holds', "hidden");
180 dojo.empty('ebook_holds_main_table_body');
181 dojo.forEach(holds, function(h) {
184 hold_status = l_strings.ready_for_checkout;
185 } else if (h.is_frozen) {
186 hold_status = l_strings.suspended;
188 hold_status = h.queue_position + ' / ' + h.queue_size;
190 h.doCancelHold = function() {
191 var vendor = this.vendor;
192 var title_id = this.title_id;
193 checkSession(vendor, function() {
194 var ebook = new Ebook(vendor, title_id);
195 ebook.cancelHold(authtoken, patron_id, function(resp) {
196 if (resp.error_msg) {
197 console.log('Cancel hold failed: ' + resp.error_msg);
198 dojo.removeClass('ebook_cancel_hold_failed', "hidden");
200 console.log('Cancel hold succeeded!');
201 dojo.destroy("hold-" + ebook.id);
202 dojo.removeClass('ebook_cancel_hold_succeeded', "hidden");
203 // Updating the transaction cache to remove the canceled hold
204 // is inconvenient, so we skip cleanupAfterAction() and merely
205 // clear transaction cache to force a refresh on next page load.
206 dojo.cookie('ebook_xact_cache', '', {path: '/', expires: '-1h'});
211 var tr = dojo.create("tr", { id: "hold-" + h.title_id }, dojo.byId('ebook_holds_main_table_body'));
212 dojo.create("td", { innerHTML: h.title }, tr);
213 dojo.create("td", { innerHTML: h.author }, tr);
214 dojo.create("td", { innerHTML: h.expire_date }, tr);
215 dojo.create("td", { innerHTML: hold_status }, tr);
216 var actions_td = dojo.create("td", null, tr);
217 var button = dojo.create("input", { id: "cancel-hold-" + h.title_id, type: "button", value: l_strings.cancel_hold }, actions_td);
218 dojo.connect(button, 'onclick', h, "doCancelHold");
220 dojo.addClass('no_ebook_holds', "hidden");
221 dojo.removeClass('ebook_holds_main', "hidden");
225 // set up page for user to perform a checkout
226 function getReadyForCheckout() {
227 if (typeof ebook_action.type === 'undefined')
229 if (typeof active_ebook === 'undefined') {
230 console.log('No active ebook specified, cannot prepare for checkout');
231 dojo.removeClass('ebook_checkout_failed', "hidden");
233 active_ebook.getDetails( function(ebook) {
234 dojo.empty('ebook_circs_main_table_body');
235 var tr = dojo.create("tr", null, dojo.byId('ebook_circs_main_table_body'));
236 dojo.create("td", { innerHTML: ebook.title }, tr);
237 dojo.create("td", { innerHTML: ebook.author }, tr);
238 dojo.create("td", null, tr);
239 dojo.create("td", { id: "checkout-button-td" }, tr);
240 if (typeof active_ebook.formats !== 'undefined') {
241 var select = dojo.create("select", { id: "checkout-format" }, dojo.byId('checkout-button-td'));
242 dojo.forEach(active_ebook.formats, function(f) {
243 dojo.create("option", { value: f.id, innerHTML: f.name }, select);
246 var button = dojo.create("input", { id: "checkout-button", type: "button", value: l_strings.checkout }, dojo.byId('checkout-button-td'));
247 ebook.conns.checkout = dojo.connect(button, 'onclick', "doCheckout");
248 dojo.removeClass('ebook_circs_main', "hidden");
253 // set up page for user to place a hold
254 function getReadyForHold() {
255 if (typeof ebook_action.type === 'undefined')
257 if (typeof active_ebook === 'undefined') {
258 console.log('No active ebook specified, cannot prepare for hold');
259 dojo.removeClass('ebook_hold_failed', "hidden");
261 active_ebook.getDetails( function(ebook) {
262 dojo.empty('ebook_holds_main_table_body');
263 var tr = dojo.create("tr", null, dojo.byId('ebook_holds_main_table_body'));
264 dojo.create("td", { innerHTML: ebook.title }, tr);
265 dojo.create("td", { innerHTML: ebook.author }, tr);
266 dojo.create("td", null, tr); // Expire Date
267 dojo.create("td", null, tr); // Status
268 dojo.create("td", { id: "hold-button-td" }, tr);
269 if (ebook_action.type == 'place_hold') {
270 var button = dojo.create("input", { id: "hold-button", type: "button", value: l_strings.place_hold }, dojo.byId('hold-button-td'));
271 ebook.conns.checkout = dojo.connect(button, 'onclick', "doPlaceHold");
273 dojo.removeClass('ebook_holds_main', "hidden");
278 function cleanupAfterAction() {
279 // unset variables related to the transaction we have performed,
280 // to avoid any weirdness on page reload
282 // update page to account for successful checkout
284 addTransactionsToPage();
285 // clear transaction cache to force a refresh on next page load
286 dojo.cookie('ebook_xact_cache', '', {path: '/', expires: '-1h'});
289 // check out our active ebook
290 function doCheckout() {
291 var ses = dojo.cookie(active_ebook.vendor); // required when inspecting checkouts for download_url
292 active_ebook.checkout(authtoken, patron_id, function(resp) {
293 if (resp.error_msg) {
294 console.log('Checkout failed: ' + resp.error_msg);
295 dojo.removeClass('ebook_checkout_failed', "hidden");
298 console.log('Checkout succeeded!');
299 dojo.destroy('checkout-button');
300 dojo.destroy('checkout-format'); // remove optional format selector
301 dojo.removeClass('ebook_checkout_succeeded', "hidden");
302 // add our successful checkout to top of transaction cache
304 title_id: active_ebook.id,
305 title: active_ebook.title,
306 author: active_ebook.author,
307 due_date: resp.due_date,
309 console.log('new_xact.finish()');
310 xacts.checkouts.unshift(this);
311 cleanupAfterAction();
312 // When we switch to jQuery, we can use .one() instead of .on(),
313 // obviating the need for an explicit disconnect here.
314 dojo.disconnect(active_ebook.conns.checkout);
317 if (resp.download_url) {
318 // Use download URL from checkout response, if available.
319 new_xact.download_url = resp.download_url;
320 dojo.create("a", { href: new_xact.download_url, innerHTML: l_strings.download }, dojo.byId('checkout-button-td'));
322 } else if (typeof resp.formats !== 'undefined') {
323 // User must select download format from list of options.
324 var select = dojo.create("select", { id: "download-format" }, dojo.byId('checkout-button-td'));
325 for (f in resp.formats) {
326 dojo.create("option", { value: resp.formats[f], innerHTML: f }, select);
328 var button = dojo.create("input", { id: "download-button", type: "button", value: l_strings.download }, dojo.byId('checkout-button-td'));
329 active_ebook.conns.download = dojo.connect(button, 'onclick', active_ebook, "download");
331 } else if (typeof resp.xact_id !== 'undefined') {
332 // No download URL provided by API checkout response. Grab fresh
333 // list of user checkouts from API, find the just-completed
334 // checkout by transaction ID, and get the download URL from that.
335 // We call the OpenSRF method directly because Relation.getCheckouts()
336 // results in scoping issues when retrieving the vendor session cookie.
337 new_xact.xact_id = resp.xact_id;
338 new OpenSRF.ClientSession('open-ils.ebook_api').request({
339 method: 'open-ils.ebook_api.patron.get_checkouts',
340 params: [ authtoken, ses, patron_id ],
342 oncomplete: function(r) {
345 dojo.forEach(resp.content(), function(x) {
346 if (x.xact_id === new_xact.xact_id) {
347 new_xact.download_url = x.download_url;
348 dojo.create("a", { href: new_xact.download_url, innerHTML: l_strings.download }, dojo.byId('checkout-button-td'));
360 // place hold on our active ebook
361 function doPlaceHold() {
362 active_ebook.placeHold(authtoken, patron_id, function(resp) {
363 if (resp.error_msg) {
364 console.log('Place hold failed: ' + resp.error_msg);
365 dojo.removeClass('ebook_place_hold_failed', "hidden");
367 console.log('Place hold succeeded!');
368 dojo.destroy('hold-button');
369 dojo.removeClass('ebook_place_hold_succeeded', "hidden");
371 title_id: active_ebook.id,
372 title: active_ebook.title,
373 author: active_ebook.author,
374 queue_position: resp.queue_position,
375 queue_size: resp.queue_size,
376 expire_date: resp.expire_date
378 if ( resp.is_ready || (resp.queue_position === 1 && resp.queue_size === 1) ) {
379 xacts.holds_ready.unshift(new_hold);
381 xacts.holds_pending.unshift(new_hold);
383 cleanupAfterAction();
388 // deserialize transactions from cache, returning them as a JS object
389 function getCachedTransactions() {
390 console.log('retrieving cached transaction details');
392 var current_cache = dojo.cookie('ebook_xact_cache');
394 cache_obj = JSON.parse(current_cache);
395 xacts.checkouts = cache_obj.checkouts;
396 xacts.holds_pending = cache_obj.holds_pending;
397 xacts.holds_ready = cache_obj.holds_ready;
402 // add a single vendor's transactions to transaction cache
403 function addTransactionsToCache(rel) {
404 console.log('updating transaction cache');
406 var updated_xacts = {
411 // preserve any transactions with other vendors
412 dojo.forEach(xacts.checkouts, function(xact) {
413 if (xact.vendor !== v)
414 updated_xacts.checkouts.push(xact);
416 dojo.forEach(xacts.holds_pending, function(xact) {
417 if (xact.vendor !== v)
418 updated_xacts.holds_pending.push(xact);
420 dojo.forEach(xacts.holds_ready, function(xact) {
421 if (xact.vendor !== v)
422 updated_xacts.holds_ready.push(xact);
424 // add transactions from current vendor
425 dojo.forEach(rel.checkouts, function(xact) {
426 updated_xacts.checkouts.push(xact);
428 dojo.forEach(rel.holds_pending, function(xact) {
429 updated_xacts.holds_pending.push(xact);
431 dojo.forEach(rel.holds_ready, function(xact) {
432 updated_xacts.holds_ready.push(xact);
434 // TODO sort transactions by date
435 // save transactions to cache
436 xacts = updated_xacts;
437 var new_cache = JSON.stringify(xacts);
438 dojo.cookie('ebook_xact_cache', new_cache, {path: '/'});
439 // update current page
441 addTransactionsToPage();