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 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 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");
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;
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");
116 function updateCheckoutView() {
117 if (xacts.checkouts.length < 1) {
118 dojo.removeClass('no_ebook_circs', "hidden");
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>';
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);
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");
139 // TODO: more actions (renew, checkin)
142 dojo.addClass('no_ebook_circs', "hidden");
143 dojo.removeClass('ebook_circs_main', "hidden");
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;
152 var holds_pending = xacts.holds_pending;
153 var holds_ready = xacts.holds_ready;
155 // combine all holds into a single list, ready-for-checkout holds first
156 var holds = holds_ready.concat(holds_pending);
159 if (holds.length < 1) {
160 dojo.removeClass('no_ebook_holds', "hidden");
162 dojo.empty('ebook_holds_main_table_body');
163 dojo.forEach(holds, function(h) {
166 hold_status = l_strings.ready_for_checkout;
167 } else if (h.is_frozen) {
168 hold_status = l_strings.suspended;
170 hold_status = h.queue_position + ' / ' + h.queue_size;
172 h.doCancelHold = function() {
173 var ebook = new Ebook(this.vendor, this.title_id);
174 ebook.cancelHold(authtoken, patron_id, function(resp) {
175 if (resp.error_msg) {
176 console.log('Cancel hold failed: ' . resp.error_msg);
177 dojo.removeClass('ebook_cancel_hold_failed', "hidden");
179 console.log('Cancel hold succeeded!');
180 dojo.destroy("hold-" + ebook.id);
181 dojo.removeClass('ebook_cancel_hold_succeeded', "hidden");
182 // Updating the transaction cache to remove the canceled hold
183 // is inconvenient, so we skip cleanupAfterAction() and merely
184 // clear transaction cache to force a refresh on next page load.
185 dojo.cookie('ebook_xact_cache', '', {path: '/', expires: '-1h'});
189 var tr = dojo.create("tr", { id: "hold-" + h.title_id }, dojo.byId('ebook_holds_main_table_body'));
190 dojo.create("td", { innerHTML: h.title }, tr);
191 dojo.create("td", { innerHTML: h.author }, tr);
192 dojo.create("td", { innerHTML: h.expire_date }, tr);
193 dojo.create("td", { innerHTML: hold_status }, tr);
194 var actions_td = dojo.create("td", null, tr);
195 var button = dojo.create("input", { id: "cancel-hold-" + h.title_id, type: "button", value: l_strings.cancel_hold }, actions_td);
196 dojo.connect(button, 'onclick', h, "doCancelHold");
198 dojo.addClass('no_ebook_holds', "hidden");
199 dojo.removeClass('ebook_holds_main', "hidden");
203 // set up page for user to perform a checkout
204 function getReadyForCheckout() {
205 if (typeof ebook_action.type === 'undefined')
207 if (typeof active_ebook === 'undefined') {
208 console.log('No active ebook specified, cannot prepare for checkout');
209 dojo.removeClass('ebook_checkout_failed', "hidden");
211 active_ebook.getDetails( function(ebook) {
212 dojo.empty('ebook_circs_main_table_body');
213 var tr = dojo.create("tr", null, dojo.byId('ebook_circs_main_table_body'));
214 dojo.create("td", { innerHTML: ebook.title }, tr);
215 dojo.create("td", { innerHTML: ebook.author }, tr);
216 dojo.create("td", null, tr);
217 dojo.create("td", { id: "checkout-button-td" }, tr);
218 if (typeof active_ebook.formats !== 'undefined') {
219 var select = dojo.create("select", { id: "checkout-format" }, dojo.byId('checkout-button-td'));
220 dojo.forEach(active_ebook.formats, function(f) {
221 dojo.create("option", { value: f.id, innerHTML: f.name }, select);
224 var button = dojo.create("input", { id: "checkout-button", type: "button", value: l_strings.checkout }, dojo.byId('checkout-button-td'));
225 ebook.conns.checkout = dojo.connect(button, 'onclick', "doCheckout");
226 dojo.removeClass('ebook_circs_main', "hidden");
231 // set up page for user to place a hold
232 function getReadyForHold() {
233 if (typeof ebook_action.type === 'undefined')
235 if (typeof active_ebook === 'undefined') {
236 console.log('No active ebook specified, cannot prepare for hold');
237 dojo.removeClass('ebook_hold_failed', "hidden");
239 active_ebook.getDetails( function(ebook) {
240 dojo.empty('ebook_holds_main_table_body');
241 var tr = dojo.create("tr", null, dojo.byId('ebook_holds_main_table_body'));
242 dojo.create("td", { innerHTML: ebook.title }, tr);
243 dojo.create("td", { innerHTML: ebook.author }, tr);
244 dojo.create("td", null, tr); // Expire Date
245 dojo.create("td", null, tr); // Status
246 dojo.create("td", { id: "hold-button-td" }, tr);
247 if (ebook_action.type == 'place_hold') {
248 var button = dojo.create("input", { id: "hold-button", type: "button", value: l_strings.place_hold }, dojo.byId('hold-button-td'));
249 ebook.conns.checkout = dojo.connect(button, 'onclick', "doPlaceHold");
251 dojo.removeClass('ebook_holds_main', "hidden");
256 function cleanupAfterAction() {
257 // unset variables related to the transaction we have performed,
258 // to avoid any weirdness on page reload
260 // update page to account for successful checkout
262 addTransactionsToPage();
263 // clear transaction cache to force a refresh on next page load
264 dojo.cookie('ebook_xact_cache', '', {path: '/', expires: '-1h'});
267 // check out our active ebook
268 function doCheckout() {
269 var ses = dojo.cookie(active_ebook.vendor); // required when inspecting checkouts for download_url
270 active_ebook.checkout(authtoken, patron_id, function(resp) {
271 if (resp.error_msg) {
272 console.log('Checkout failed: ' + resp.error_msg);
273 dojo.removeClass('ebook_checkout_failed', "hidden");
276 console.log('Checkout succeeded!');
277 dojo.destroy('checkout-button');
278 dojo.destroy('checkout-format'); // remove optional format selector
279 dojo.removeClass('ebook_checkout_succeeded', "hidden");
280 // add our successful checkout to top of transaction cache
282 title_id: active_ebook.id,
283 title: active_ebook.title,
284 author: active_ebook.author,
285 due_date: resp.due_date,
287 console.log('new_xact.finish()');
288 xacts.checkouts.unshift(this);
289 cleanupAfterAction();
290 // When we switch to jQuery, we can use .one() instead of .on(),
291 // obviating the need for an explicit disconnect here.
292 dojo.disconnect(active_ebook.conns.checkout);
295 if (resp.download_url) {
296 // Use download URL from checkout response, if available.
297 new_xact.download_url = resp.download_url;
298 dojo.create("a", { href: new_xact.download_url, innerHTML: l_strings.download }, dojo.byId('checkout-button-td'));
300 } else if (typeof resp.formats !== 'undefined') {
301 // User must select download format from list of options.
302 var select = dojo.create("select", { id: "download-format" }, dojo.byId('checkout-button-td'));
303 for (f in resp.formats) {
304 dojo.create("option", { value: resp.formats[f], innerHTML: f }, select);
306 var button = dojo.create("input", { id: "download-button", type: "button", value: l_strings.download }, dojo.byId('checkout-button-td'));
307 active_ebook.conns.download = dojo.connect(button, 'onclick', active_ebook, "download");
309 } else if (typeof resp.xact_id !== 'undefined') {
310 // No download URL provided by API checkout response. Grab fresh
311 // list of user checkouts from API, find the just-completed
312 // checkout by transaction ID, and get the download URL from that.
313 // We call the OpenSRF method directly because Relation.getCheckouts()
314 // results in scoping issues when retrieving the vendor session cookie.
315 new_xact.xact_id = resp.xact_id;
316 new OpenSRF.ClientSession('open-ils.ebook_api').request({
317 method: 'open-ils.ebook_api.patron.get_checkouts',
318 params: [ authtoken, ses, patron_id ],
320 oncomplete: function(r) {
323 dojo.forEach(resp.content(), function(x) {
324 if (x.xact_id === new_xact.xact_id) {
325 new_xact.download_url = x.download_url;
326 dojo.create("a", { href: new_xact.download_url, innerHTML: l_strings.download }, dojo.byId('checkout-button-td'));
338 // place hold on our active ebook
339 function doPlaceHold() {
340 active_ebook.placeHold(authtoken, patron_id, function(resp) {
341 if (resp.error_msg) {
342 console.log('Place hold failed: ' . resp.error_msg);
343 dojo.removeClass('ebook_place_hold_failed', "hidden");
345 console.log('Place hold succeeded!');
346 dojo.destroy('hold-button');
347 dojo.removeClass('ebook_place_hold_succeeded', "hidden");
349 title_id: active_ebook.id,
350 title: active_ebook.title,
351 author: active_ebook.author,
352 queue_position: resp.queue_position,
353 queue_size: resp.queue_size,
354 expire_date: resp.expire_date
356 if ( resp.is_ready || (resp.queue_position === 1 && resp.queue_size === 1) ) {
357 xacts.holds_ready.unshift(new_hold);
359 xacts.holds_pending.unshift(new_hold);
361 cleanupAfterAction();
366 // deserialize transactions from cache, returning them as a JS object
367 function getCachedTransactions() {
368 console.log('retrieving cached transaction details');
370 var current_cache = dojo.cookie('ebook_xact_cache');
372 cache_obj = JSON.parse(current_cache);
373 xacts.checkouts = cache_obj.checkouts;
374 xacts.holds_pending = cache_obj.holds_pending;
375 xacts.holds_ready = cache_obj.holds_ready;
380 // add a single vendor's transactions to transaction cache
381 function addTransactionsToCache(rel) {
382 console.log('updating transaction cache');
384 var updated_xacts = {
389 // preserve any transactions with other vendors
390 dojo.forEach(xacts.checkouts, function(xact) {
391 if (xact.vendor !== v)
392 updated_xacts.checkouts.push(xact);
394 dojo.forEach(xacts.holds_pending, function(xact) {
395 if (xact.vendor !== v)
396 updated_xacts.holds_pending.push(xact);
398 dojo.forEach(xacts.holds_ready, function(xact) {
399 if (xact.vendor !== v)
400 updated_xacts.holds_ready.push(xact);
402 // add transactions from current vendor
403 dojo.forEach(rel.checkouts, function(xact) {
404 updated_xacts.checkouts.push(xact);
406 dojo.forEach(rel.holds_pending, function(xact) {
407 updated_xacts.holds_pending.push(xact);
409 dojo.forEach(rel.holds_ready, function(xact) {
410 updated_xacts.holds_ready.push(xact);
412 // TODO sort transactions by date
413 // save transactions to cache
414 xacts = updated_xacts;
415 var new_cache = JSON.stringify(xacts);
416 dojo.cookie('ebook_xact_cache', new_cache, {path: '/'});
417 // update current page
419 addTransactionsToPage();