Cherry-pick the "integrate Google Book Preview into record detail" feature based...
authordbs <dbs@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Sun, 8 Mar 2009 05:48:08 +0000 (05:48 +0000)
committerdbs <dbs@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Sun, 8 Mar 2009 05:48:08 +0000 (05:48 +0000)
I integrated the code directly into rdetail.js and edited it to remove unnecessary helper functions.

Tested with Firefox 3 (Linux), Internet Explorer 7, Google Chrome, and Safari 3.2.1 (Windows).

git-svn-id: svn://svn.open-ils.org/ILS/trunk@12465 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/web/opac/skin/default/js/rdetail.js
Open-ILS/web/opac/skin/default/xml/page_rdetail.xml

index f379b07..61f67e5 100644 (file)
@@ -199,6 +199,8 @@ function _rdetailDraw(r) {
                 var href = $('rdetail.jacket_attrib_link').getAttribute('href') +cleanISBN(record.isbn());
                 $('rdetail.jacket_attrib_link').setAttribute('href', href);
             }
+            rdetailCheckForPreview();
+               
         } else {
             hideMe($("rdetail.jacket_attrib_div"));
             hideMe($("rdetail_img_link"));
@@ -377,6 +379,7 @@ function rdetailShowExtra(type, args) {
                case "excerpt": 
                        addCSSClass($('rdetail_excerpt_link'), 'rdetail_extras_selected');
                        unHideMe($('rdetail_excerpt_div'));
+                       rdetailDisplayPreview();
                        break;
 
                case "anotes": 
@@ -703,11 +706,12 @@ function rdetailBuildBrowseInfo(row, cn, local, orgNode, cl) {
 
        $n(row, 'rdetail_callnumber_cell').appendChild(text(cn));
 
-    if (rdetailShowCopyLocation) {
-           var cl_cell = $n(row, 'rdetail_copylocation_cell');
-           cl_cell.appendChild(text(cl));
-           unHideMe(cl_cell);
-    }
+       if (rdetailShowCopyLocation) {
+               var cl_cell = $n(row, 'rdetail_copylocation_cell');
+               cl_cell.appendChild(text(cl));
+
+               unHideMe(cl_cell);
+       }
 
        _debug('setting action clicks for cn ' + cn);
 
@@ -805,3 +809,97 @@ function _rdetailSortStatuses(a, b) {
        return parseInt(a.id()) - parseInt(b.id());
 }
 
+function rdetailCheckForPreview() {
+  
+  var p = document.createElement('p');
+  p.appendChild( document.createTextNode('Loading... ' ) );
+  p.id = 'loading';
+  $('rdetail_excerpt_div').appendChild(p);
+  searchForPreview( cleanISBN(record.isbn()) );
+
+}
+/**
+ *
+ * @param {DOM object} query The form element containing the
+ *                     input parameters "isbns"
+ */
+function searchForPreview( isbn ) {
+
+  // Delete any previous Google Booksearch JSON queries.
+  var jsonScript = document.getElementById("jsonScript");
+  if (jsonScript) {
+    jsonScript.parentNode.removeChild(jsonScript);
+  }
+  // Add a script element with the src as the user's Google Booksearch query. 
+  // JSON output is specified by including the alt=json-in-script argument
+  // and the callback function is also specified as a URI argument.
+  var scriptElement = document.createElement("script");
+
+  scriptElement.setAttribute("id", "jsonScript");
+  scriptElement.setAttribute("src",
+      "http://books.google.com/books?bibkeys=" + 
+      isbn + "&jscmd=viewapi&callback=previewCallback");
+  scriptElement.setAttribute("type", "text/javascript");
+  // make the request to Google booksearch
+  document.documentElement.firstChild.appendChild(scriptElement);
+}
+
+/**
+ * This function is the call-back function for the JSON scripts which 
+ * executes a Google book search response.
+ *
+ * @param {JSON} booksInfo is the JSON object pulled from the Google books service.
+ */
+function previewCallback(bookInfo) {
+  // Clear any old data to prepare to display the Loading... message.
+  var div = document.getElementById("rdetail_excerpt_div");
+  var book;
+  
+  for ( i in bookInfo ) {
+    book = bookInfo[i];
+  }
+
+  if ( !book ) {
+    return;
+  }
+
+  if ( book.preview != "noview" ) {
+    if ( book.preview == 'full' ) {
+      setText( $('rdetail_excerpt_link_a'), 'Full Text' );
+      $('rdetail_excerpt_link_a').title = 'See the full text of this book.';      
+    }
+
+    // Add a button below the book cover image to load the preview.
+    badge = document.createElement( 'img' );
+    badge.src = 'http://books.google.com/intl/en/googlebooks/images/gbs_preview_button1.gif';
+    badge.title = 'Show a preview of this book from Google Book Search';
+    badge.style.border = 0;
+    badgelink = document.createElement( 'a' );
+    badgelink.href = 'javascript:rdetailShowExtra("excerpt");';
+    badgelink.appendChild( badge );
+    $('rdetail_image_cell').appendChild( badgelink );
+
+    unHideMe( $('rdetail_excerpt_link' ) );
+    $('rdetail_excerpt_div').style.height = 600;
+  }
+}
+
+/**
+ *  This is called when the user clicks on the 'Excerpt' link.  We assume
+ *  a preview is available from Google if this link was made visible.
+ */
+function rdetailDisplayPreview() {
+  previewPane = $('rdetail_excerpt_div');
+  if ( $('rdetail_excerpt_div').getAttribute('loaded') == null ||  $('rdetail_excerpt_div').getAttribute('loaded') == "false" ) {
+    google.load("books", "0", {"callback" : rdetailViewerLoadCallback, "language": "hy"} );
+    $('rdetail_excerpt_div').setAttribute('loaded', 'true');
+  }
+}
+
+function rdetailViewerLoadCallback() {
+
+  var viewer = new google.books.DefaultViewer(document.getElementById('rdetail_excerpt_div'));
+  viewer.load('ISBN:' + cleanISBN(record.isbn()) );
+
+}
index 3a36274..9d259ce 100644 (file)
@@ -7,13 +7,14 @@
        <script language='javascript' type='text/javascript' src='<!--#echo var="OILS_OPAC_JS_HOST"-->/skin/default/js/holds.js'></script>
        <script language='javascript' type='text/javascript' src='<!--#echo var="OILS_OPAC_JS_HOST"-->/skin/default/js/cn_browse.js'></script>
        <script language='javascript' type='text/javascript' src='<!--#echo var="OILS_OPAC_JS_HOST"-->/skin/default/js/container.js'></script>
+       <script src='http://www.google.com/jsapi' type='text/javascript' language='javascript'></script>
+       <script type='text/javascript' src='http://books.google.com/books/api.js?key=notsupplied&amp;v=0&amp;callback=google.loader.callbacks.books'></script>
 
        <script language='javascript' type='text/javascript'>
                config.ids.rdetail = {};
                config.ids.rdetail.view_marc = "rdetail_view_marc";
        </script>
 
-
        <table width='100%' id='np_table' class='hide_me' style='margin-top: 3px;'>
                <tbody>
                        <tr class='color_4' style='height: 1em;'>