]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/templates/opac/parts/ac_google_books.tt2
TPAC: Make Google Books Preview depend on Dojo
[Evergreen.git] / Open-ILS / src / templates / opac / parts / ac_google_books.tt2
1 <script type="text/javascript">
2 var GBisbns = Array();
3
4 /**
5  *
6  * @param {DOM object} isbn The form element containing the input parameters "isbns"
7  */
8 function searchForGBPreview( isbn ) {
9   dojo.require("dojo.io.script");
10   dojo.io.script.get({"url": "https://www.google.com/jsapi"});
11   dojo.io.script.get({"url": "https://www.googleapis.com/books/v1/volumes", "content": { "q": "isbn:" + isbn, "callback": "GBPreviewCallback"}});
12 }
13
14 /**
15  * This function is the call-back function for the JSON scripts which 
16  * executes a Google book search response.
17  *
18  * @param {JSON} GBPBookInfo is the JSON object pulled from the Google books service.
19  */
20 function GBPreviewCallback(GBPBookInfo) {
21   if (GBPBookInfo.totalItems < 1) return;
22
23   var accessInfo = GBPBookInfo.items[0].accessInfo;
24   if ( !accessInfo ) {
25     return;
26   }
27
28   if ( accessInfo.embeddable ) {
29     /* Add a button below the book cover image to load the preview. */
30     var GBPBadge = document.createElement( 'img' );
31     GBPBadge.id = 'gbpbadge';
32     GBPBadge.src = 'https://www.google.com/intl/[% ctx.locale.substr(0,2) %]/googlebooks/images/gbs_preview_button1.gif';
33     GBPBadge.title = dojo.byId('rdetail_title').innerHTML;
34     GBPBadge.style.border = 0;
35     GBPBadge.style.margin = '0.5em 0 0 0';
36     GBPBadgelink = document.createElement('a');
37     GBPBadgelink.href = 'javascript:GBDisplayPreview();';
38     GBPBadgelink.appendChild( GBPBadge );
39     dojo.byId('rdetail_title_div').appendChild( GBPBadgelink );
40   }
41 }
42
43 /**
44  *  This is called when the user clicks on the 'Preview' link.  We assume
45  *  a preview is available from Google if this link was made visible.
46  */
47 function GBDisplayPreview() {
48   var GBPreviewPane = document.createElement('div');
49   GBPreviewPane.id = 'rdetail_preview_div';
50   GBPreviewPane.style.height = '800px';
51   GBPreviewPane.style.width = '600px';
52   GBPreviewPane.style.display = 'block';
53   var GBClear = document.createElement('div');
54   GBClear.style.padding = '1em';
55   dojo.byId('canvas_main').insertBefore(GBPreviewPane, dojo.byId('rdetail_record_details'));
56   dojo.byId('canvas_main').insertBefore(GBClear, dojo.byId('rdetail_record_details'));
57   if (GBPreviewPane.getAttribute('loaded') == null || GBPreviewPane.getAttribute('loaded') == "false" ) {
58      google.load("books", "0", {"callback" : GBPViewerLoadCallback, "language": "[% ctx.locale.substr(0,2) %]"} );
59      GBPreviewPane.setAttribute('loaded', 'true');
60   }
61 }
62
63 function GBPViewerLoadCallback() {
64   var GBPViewer = new google.books.DefaultViewer(dojo.byId('rdetail_preview_div'));
65   GBPViewer.load('ISBN:' + GBisbns[0]);
66   GBPViewer.resize();
67   dojo.byId('gbpbadge').style.display = 'none';
68 }
69
70 dojo.addOnLoad(function() {
71   var spans = dojo.query('li.rdetail_isbns span.rdetail_value');
72   for (var i = 0; i < spans.length; i++) {
73     var prop = spans[i].getAttribute('itemprop');
74     if (!prop) {
75       continue;
76     }
77     var isbn = spans[i].textContent || spans[i].innerText;
78     if (!isbn) {
79       continue;
80     }
81     isbn = isbn.toString().replace(/^\s+/,"");
82     var idx = isbn.indexOf(" ");
83     if (idx > -1) {
84       isbn = isbn.substring(0, idx);
85     }
86     isbn = isbn.toString().replace(/-/g,"");
87     if (!isbn) {
88       continue;
89     }
90     GBisbns.push(isbn);
91   }
92
93   if (GBisbns.length) {
94       searchForGBPreview(GBisbns[0]);
95   }
96 });
97 </script>