|
/* load the google books api */ |
|
google.load("books", "0"); |
|
|
|
jQuery(document).ready(function($) { |
|
/* on page load, fetch books about a default keyword (we picked math) */ |
|
fetchBooks("math"); |
|
|
|
/* if the volume can't be loaded, throw an error. */ |
|
function alertNotFound() { |
|
alert("Could not load the specified volume"); |
|
} |
|
|
|
/* make some CSS changes to the preview window */ |
|
function removePreviewFooter() { |
|
/* removes the footer to the preview */ |
|
$('#viewerCanvas > div > div:nth-child(2)').css('display','none'); |
|
/* calculate the window height, and place the search options 5% from the bottom */ |
|
var offset = ($(window).height() * .95) + "px"; |
|
$('#viewerCanvas > div > div > div:nth-child(2)').css('top', offset); |
|
} |
|
|
|
/* when someone selects a new book from the dropdown, load it */ |
|
$('#book').change(function(viewer) { |
|
/* get the ID of the book from the dropdown */ |
|
var book = $('#book').val(); |
|
/* if the value is empty, do nothing */ |
|
if(book === '') { |
|
return; |
|
} |
|
/* update the viewer */ |
|
var viewer = new google.books.DefaultViewer(document.getElementById('viewerCanvas')); |
|
viewer.load(book, alertNotFound, removePreviewFooter); |
|
}); |
|
|
|
/* when someone enters a new search term and clicks enter, run a new book search */ |
|
$('#keyword').change(function() { |
|
var keyword = $(this).val(); |
|
/* see fetchBooks function */ |
|
fetchBooks(keyword); |
|
}); |
|
|
|
/* font awesome icons are used to toggle things. when someone clicks on one, toggle |
|
* all the elements within the parent */ |
|
$('.fa').click(function() { |
|
var parent = $(this).parent(); |
|
$(parent).children().toggle(); |
|
$(parent).toggleClass('closed'); |
|
}); |
|
|
|
function fetchBooks(keyword) { |
|
/* the URL for the books REST api */ |
|
/* insert the keyword, wherever it's coming from - default or user entry */ |
|
var booksAPI = "https://www.googleapis.com/books/v1/volumes?q=" + keyword + "&sortBy=newest&filter=full&projection=lite&maxResults=20"; |
|
/* retrieve the json from the api */ |
|
$.getJSON( booksAPI, { |
|
}) |
|
/* when we're done retrieving the json, do something with it */ |
|
.done(function(data) { |
|
/* reset the internals of the select with a heading option */ |
|
/* use an empty string as the value for comparison purposes later */ |
|
$('#book').html("<option value=''>Browse some free books about " + keyword + "</option>"); |
|
/* iterate through the data. keep track with i */ |
|
$.each(data.items, function(i, item) { |
|
var rawTitle = item.volumeInfo.title; |
|
/* if the raw title is greater than 64 characters, |
|
* seperate it at the next word and append "..." |
|
* otherwise, do nothing */ |
|
if(rawTitle.length > 64) { |
|
var title = jQuery.trim(rawTitle).substring(0, 64).split(" ").slice(0, -1).join(" ") + "..."; |
|
} else { |
|
var title = rawTitle; |
|
} |
|
/* append each new book option to the select */ |
|
$('#book').append("<option value='" + item.id + "'>" + title + "</option>"); |
|
/* when it's reached 20 items, stop */ |
|
if ( i === 20 ) { |
|
return false; |
|
} |
|
}); |
|
}); |
|
}; |
|
}); |