]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/templates/opac/parts/acjs.tt2
33ab4b75c85572b26385579b4da9cadfc606f1bc
[working/Evergreen.git] / Open-ILS / src / templates / opac / parts / acjs.tt2
1
2 <script type="text/javascript">
3
4     /* Checks to see if a given type of added content has data to show.
5      * The first arg to callback() is boolean indicating the presence of data */
6     function acIsAvailable(ident, type, callback) {
7         var url = '/opac/extras/ac/' + type + '/html/' + ident;
8         dojo.xhr('HEAD', {
9             url : url, 
10             failOk : true, // http://bugs.dojotoolkit.org/ticket/11568
11             error : function(err) { callback(false, ident, type); },
12             load : function(result) { callback(true, ident, type); }
13         });
14     }
15
16     [%-  # XXX revisit when ident=ctx.bre_id
17         ident = ctx.record_attrs.isbn_clean || ctx.record_attrs.upc; 
18         IF ident; 
19             FOR type IN ctx.added_content.keys;
20                 IF ctx.added_content.$type.status == '3' # status unknown %]
21
22                     dojo.addOnLoad(function() {
23                         var ident = '[% ident %]';
24                         var type = '[% type %]';
25
26                         acIsAvailable(ident, type, function(avail, ident, type) { 
27                             if (avail) {
28
29                                 [% IF CGI.param('expand') == 'addedcontent' %]
30
31                                     // if the content is available, un-hide the tab
32                                     dojo.removeClass(dojo.byId('ac:' + type), 'hidden');
33
34                                 [% ELSE %]
35                                     // if no default type is selected on the main tab link
36                                     // set one here, since we have available content
37
38                                     var link = dojo.query('[name=addedcontent]')[0];
39                                     var href = link.getAttribute('href');
40                                     if (!href.match('[\&;]ac=')) {
41                                         href = href.replace('#addedcontent', ';ac=' + type + '#addedcontent');
42                                         dojo.attr(link, 'href', href);
43                                         dojo.attr(dojo.query('[name=addedcontent_lbl]')[0], 'href', href);
44                                     }
45
46                                 [% END %]
47                             }
48                         });
49                     });
50                 [% END; # IF status unknown
51             END; 
52         END; # IF ident
53     %]
54 </script>
55 [%- IF ctx.google_books_preview -%]
56 <script type="text/javascript">
57 var GBisbns = Array();
58
59 /**
60  *
61  * @param {DOM object} isbn The form element containing the input parameters "isbns"
62  */
63 function searchForGBPreview( isbn ) {
64   dojo.require("dojo.io.script");
65   dojo.io.script.get({"url": "https://www.google.com/jsapi"});
66   dojo.io.script.get({"url": "https://www.googleapis.com/books/v1/volumes", "content": { "q": "isbn:" + isbn, "callback": "GBPreviewCallback"}});
67 }
68
69 /**
70  * This function is the call-back function for the JSON scripts which 
71  * executes a Google book search response.
72  *
73  * @param {JSON} GBPBookInfo is the JSON object pulled from the Google books service.
74  */
75 function GBPreviewCallback(GBPBookInfo) {
76   if (GBPBookInfo.totalItems < 1) return;
77
78   var accessInfo = GBPBookInfo.items[0].accessInfo;
79   if ( !accessInfo ) {
80     return;
81   }
82
83   if ( accessInfo.embeddable ) {
84     /* Add a button below the book cover image to load the preview. */
85     var GBPBadge = document.createElement( 'img' );
86     GBPBadge.id = 'gbpbadge';
87     GBPBadge.src = 'https://www.google.com/intl/[% ctx.locale.substr(0,2) %]/googlebooks/images/gbs_preview_button1.gif';
88     GBPBadge.title = dojo.byId('rdetail_title').innerHTML;
89     GBPBadge.style.border = 0;
90     GBPBadge.style.margin = '0.5em 0 0 0';
91     GBPBadgelink = document.createElement('a');
92     GBPBadgelink.href = 'javascript:GBDisplayPreview();';
93     GBPBadgelink.appendChild( GBPBadge );
94     dojo.byId('rdetail_title_div').appendChild( GBPBadgelink );
95   }
96 }
97
98 /**
99  *  This is called when the user clicks on the 'Preview' link.  We assume
100  *  a preview is available from Google if this link was made visible.
101  */
102 function GBDisplayPreview() {
103   var GBPreviewPane = document.createElement('div');
104   GBPreviewPane.id = 'rdetail_preview_div';
105   GBPreviewPane.style.height = '800px';
106   GBPreviewPane.style.width = '600px';
107   GBPreviewPane.style.display = 'block';
108   var GBClear = document.createElement('div');
109   GBClear.style.padding = '1em';
110   dojo.byId('canvas_main').insertBefore(GBPreviewPane, dojo.byId('rdetail_record_details'));
111   dojo.byId('canvas_main').insertBefore(GBClear, dojo.byId('rdetail_record_details'));
112   if (GBPreviewPane.getAttribute('loaded') == null || GBPreviewPane.getAttribute('loaded') == "false" ) {
113      google.load("books", "0", {"callback" : GBPViewerLoadCallback, "language": "[% ctx.locale.substr(0,2) %]"} );
114      GBPreviewPane.setAttribute('loaded', 'true');
115   }
116 }
117
118 function GBPViewerLoadCallback() {
119   var GBPViewer = new google.books.DefaultViewer(dojo.byId('rdetail_preview_div'));
120   GBPViewer.load('ISBN:' + GBisbns[0]);
121   GBPViewer.resize();
122   dojo.byId('gbpbadge').style.display = 'none';
123 }
124
125 dojo.addOnLoad(function() {
126   var spans = dojo.query('li.rdetail_isbns span.rdetail_value');
127   for (var i = 0; i < spans.length; i++) {
128     var prop = spans[i].getAttribute('itemprop');
129     if (!prop) {
130       continue;
131     }
132     var isbn = spans[i].textContent || spans[i].innerText;
133     if (!isbn) {
134       continue;
135     }
136     isbn = isbn.toString().replace(/^\s+/,"");
137     var idx = isbn.indexOf(" ");
138     if (idx > -1) {
139       isbn = isbn.substring(0, idx);
140     }
141     isbn = isbn.toString().replace(/-/g,"");
142     if (!isbn) {
143       continue;
144     }
145     GBisbns.push(isbn);
146   }
147
148   searchForGBPreview(GBisbns[0]);
149 });
150 </script>
151 [%- END %]