]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/templates/opac/parts/ac_google_books.tt2
lp1422555: Shorter permalink on record summary page
[Evergreen.git] / Open-ILS / src / templates / opac / parts / ac_google_books.tt2
1 <script type="text/javascript">
2 var GBisbns = Array();
3 var GBPreviewLink = '';
4 var GBPreviewShowing = false;
5
6 /**
7  *
8  * @param {DOM object} isbn The form element containing the input parameters "isbns"
9  */
10 function searchForGBPreview( isbn ) {
11   dojo.require("dojo.io.script");
12   dojo.io.script.get({"url": "https://www.google.com/jsapi"});
13   dojo.io.script.get({"url": "https://www.googleapis.com/books/v1/volumes", "content": { "q": "isbn:" + isbn, "callback": "GBPreviewCallback"}});
14 }
15
16 /**
17  * This function is the call-back function for the JSON scripts which 
18  * executes a Google book search response.
19  *
20  * @param {JSON} GBPBookInfo is the JSON object pulled from the Google books service.
21  */
22 function GBPreviewCallback(GBPBookInfo) {
23   if (GBPBookInfo.totalItems < 1) return;
24
25   var accessInfo = GBPBookInfo.items[0].accessInfo;
26   if ( !accessInfo ) {
27     return;
28   }
29
30   if ( accessInfo.embeddable ) {
31     GPPreviewLink = GBPBookInfo.items[0].volumeInfo.previewLink;
32     if ( !GPPreviewLink) {
33         return;
34     }
35     /* Add a button below the book cover image to load the preview. */
36     var GBPBadge = document.createElement( 'img' );
37     GBPBadge.id = 'gbpbadge';
38     GBPBadge.src = 'https://www.google.com/intl/[% ctx.locale.substr(0,2) %]/googlebooks/images/gbs_preview_button1.gif';
39     GBPBadge.title = dojo.byId('rdetail_title').innerHTML;
40     GBPBadge.style.border = 0;
41     GBPBadge.style.margin = '0.5em 0 0 0';
42     GBPBadgelink = document.createElement('a');
43     GBPBadgelink.id = 'gbpbadge_link';
44     GBPBadgelink.href = 'javascript:GBDisplayPreview(true);';
45     GBPBadgelink.appendChild( GBPBadge );
46     dojo.byId('rdetail_title_div').appendChild( GBPBadgelink );
47     unHideMe(dojo.byId('gbp_extra'));
48   }
49 }
50
51 /**
52  *  This is called when the user clicks on the 'Preview' link.  We assume
53  *  a preview is available from Google if this link was made visible.
54  */
55 function GBDisplayPreview(scroll_to_div) {
56   var GBPreviewPane = document.createElement('div');
57   GBPreviewPane.id = 'rdetail_preview_div';
58   GBPreviewPane.style.height = '800px';
59   GBPreviewPane.style.width = '600px';
60   GBPreviewPane.style.display = 'block';
61   var GBClear = document.createElement('div');
62   GBClear.style.padding = '1em';
63   dojo.byId('gbp_extra_container').appendChild(GBPreviewPane);
64   dojo.byId('gbp_extra_container').appendChild(GBClear);
65   if (GBPreviewPane.getAttribute('loaded') == null || GBPreviewPane.getAttribute('loaded') == "false" ) {
66      google.load("books", "0", {"callback" : GBPViewerLoadCallback, "language": "[% ctx.locale.substr(0,2) %]"} );
67      GBPreviewPane.setAttribute('loaded', 'true');
68   }
69   if (scroll_to_div) document.location.hash = '#gbp_extra';
70 }
71
72 function GBPViewerLoadCallback() {
73   var GBPViewer = new google.books.DefaultViewer(dojo.byId('rdetail_preview_div'));
74   GBPViewer.load(GPPreviewLink);
75   GBPViewer.resize();
76   var GBPBadgelink = dojo.byId('gbpbadge_link');
77   GBPBadgelink.href = 'javascript:GBShowHidePreview(true);';
78   dojo.forEach(
79     dojo.byId('gbp_extra_links').getElementsByTagName('a'),
80     function(link) {
81       link.href = 'javascript:GBShowHidePreview();';
82     }
83   );
84   hideMe(dojo.byId('gbp_arrow_link'));
85   unHideMe(dojo.byId('gbp_arrow_down_link'));
86   GBPreviewShowing = true;
87 }
88
89 function GBShowHidePreview(from_button) {
90   if (!GBPreviewShowing) {
91     dojo.byId('gbp_extra_container').style.display = 'inherit';
92     hideMe(dojo.byId('gbp_arrow_link'));
93     unHideMe(dojo.byId('gbp_arrow_down_link'));
94     GBPreviewShowing = true;
95   } else if (!from_button) { // button can open, but shouldn't close
96     dojo.byId('gbp_extra_container').style.display = 'none';
97     unHideMe(dojo.byId('gbp_arrow_link'));
98     hideMe(dojo.byId('gbp_arrow_down_link'));
99     GBPreviewShowing = false;
100   }
101   // button should always scroll
102   if (from_button) document.location.hash = '#gbp_extra';
103 }
104
105 dojo.addOnLoad(function() {
106   var spans = dojo.query('li.rdetail_isbns span.rdetail_value');
107   for (var i = 0; i < spans.length; i++) {
108     var prop = spans[i].getAttribute('property');
109     if (!prop) {
110       continue;
111     }
112     var isbn = spans[i].textContent || spans[i].innerText;
113     if (!isbn) {
114       continue;
115     }
116     isbn = isbn.toString().replace(/^\s+/,"");
117     var idx = isbn.indexOf(" ");
118     if (idx > -1) {
119       isbn = isbn.substring(0, idx);
120     }
121     isbn = isbn.toString().replace(/-/g,"");
122     if (!isbn) {
123       continue;
124     }
125     GBisbns.push(isbn);
126   }
127
128   if (GBisbns.length) {
129       searchForGBPreview(GBisbns[0]);
130   }
131 });
132 </script>