Created
November 20, 2018 21:37
-
-
Save ngalluzzo/777a97242cbb35b4246c7f34220085b5 to your computer and use it in GitHub Desktop.
Library Manager: https://nicknack.knack.com/library-manager#home
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function searchBook(isbn) { | |
// fetch the book information using Google API's ISBN search: | |
// https://developers.google.com/books/ | |
return $.get('https://www.googleapis.com/books/v1/volumes?q=isbn:' + isbn); | |
} | |
function initSearch($isbn) { | |
// Focus on the isbn input immediately | |
$isbn.focus(); | |
// When the view first loads, if there's an isbn already filled in, search for a book | |
// Happens when we open the view with URL variables filled in | |
populateFields($isbn); | |
// Every time the isbn value is updated, search for a book | |
$isbn.on('input', function(keyup_event){ | |
populateFields($isbn); | |
}); | |
} | |
function populateFields($isbn) { | |
// Only trigger the search if there's an isbn value with more than 9 characters | |
if ($isbn.val().length > 9) { | |
setTimeout(function() { | |
Knack.showSpinner(); | |
// search for the book | |
searchBook($isbn.val()).then(function(response){ | |
// If google gives us back a book... | |
if(response.items && response.items.length) { | |
let book = response.items[0].volumeInfo; | |
// Populate all the desired form inputs | |
$('#field_1').val(book.title); | |
$('#field_8').val(book.authors[0]); | |
$('#field_5').val(book.description); | |
// If google gives us back images (sometimes they don't), fill those in too | |
if(book.imageLinks && book.imageLinks.thumbnail) { | |
$('#field_9').val(book.imageLinks.thumbnail); | |
} | |
Knack.hideSpinner(); | |
} | |
}); | |
},500); | |
} | |
} | |
// Example Book: Steve Jobs isbn: 9781451648546 | |
// On the "Add Book Inventory" Form: https://nicknack.knack.com/library-manager#book-inventory/add-book-inventory/ | |
$(document).on('knack-view-render.view_7', function(){ | |
initSearch($('input#field_4')); | |
}); | |
// Example Book: Steve Jobs isbn: 9781451648546 | |
// On the "Add Book" Form: https://nicknack.knack.com/library-manager#home/add-book/ | |
$(document).on('knack-view-render.view_14', function(){ | |
initSearch($('input#field_4')); | |
}); | |
// Example Book: Fire and Blood: 9781524796280 | |
// On the "Search" Form: https://nicknack.knack.com/library-manager#home/ | |
// After a search is made... | |
$(document).on('knack-records-render.view_10', function(event, view, records){ | |
let $isbn = $('span.value input'); | |
setTimeout(function(){ | |
$isbn.focus(); | |
},300); | |
// If there were no records found... | |
if(!records.length) { | |
let url_var = JSON.stringify({"field_4":$isbn.val()}); | |
// Modify the "No Data" text with a link to the "Add Book" form with the search value filled in | |
$('#no_result_link').attr('href', $('#no_result_link').attr('href') + '?view_14_vars=' + encodeURIComponent(url_var)) | |
} | |
}); | |
// When adding book inventory: https://nicknack.knack.com/library-manager#book-inventory/add-book-inventory/ | |
// Automatically focus on the Barcode - UPC field for easy scanning | |
$(document).on('knack-view-render.view_16', function(event, view, records) { | |
setTimeout(function(){ | |
$('#field_34').focus(); | |
}, 500) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment