LP#1673870: move ebook availability JS to standard location
[working/Evergreen.git] / Open-ILS / web / js / ui / default / opac / ebook_api / avail.js
1 dojo.addOnLoad(function() {
2
3     // detect ebooks on current page for each vendor
4     dojo.forEach(vendor_list, function(v) {
5         var vendor = new Vendor(v);
6         var ebook_nodes = dojo.query("." + v + "_avail");
7         console.log('found ' + ebook_nodes.length + ' ebooks on this page');
8
9         // we have ebooks for this vendor, so let's get availability info etc.
10         if (ebook_nodes.length > 0) {
11             checkSession(v, function(v,ses) {
12                 ebook_nodes.forEach(function(node) {
13                     var ebook = new Ebook(v, node.getAttribute("id"));
14                     ebook.rec_id = node.parentNode.getAttribute("id");
15                     vendor.ebooks.push(ebook);
16
17                     ebook.getHoldings( function(holdings) {
18                         if (typeof holdings.available !== 'undefined') {
19                             var avail = holdings.available;
20                             if (avail == 1) {
21                                 node.innerHTML = 'This title is available online.';
22                             } else if (avail == 0) {
23                                 node.innerHTML = 'This title is not currently available.';
24                             } else {
25                                 console.log(ebook.id + ' has bad availability: ' + avail);
26                             }
27                         } else {
28                             if (holdings.formats.length > 0) {
29                                 var formats_ul = dojo.create("ul", null, ebook.rec_id + '_formats');
30                                 dojo.forEach(holdings.formats, function(f) {
31                                     dojo.create("li", { innerHTML: f.name }, formats_ul);
32                                 });
33                                 var status_node = dojo.byId(ebook.rec_id + '_status');
34                                 var status_str = holdings.copies_available + ' of ' + holdings.copies_owned + ' available';
35                                 status_node.innerHTML = status_str;
36                                 dojo.removeClass(ebook.rec_id + '_ebook_holdings', "hidden");
37                                 if (holdings.copies_available > 0) {
38                                     dojo.removeClass(ebook.rec_id + '_ebook_checkout', "hidden");
39                                 } else {
40                                     dojo.removeClass(ebook.rec_id + '_ebook_place_hold', "hidden");
41                                 }
42                             }
43                         }
44                         // unhide holdings/availability info now that it's populated
45                         removeClass(node.parentNode, "hidden");
46                     });
47                 });
48             });
49         }
50     });
51
52 });