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