-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.php
134 lines (120 loc) · 3.14 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<html>
<head>
<title> Lendit </title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="//www.google.com/jsapi"></script>
<style>
.viewerCanvas{
width: 24%;
height: 250px;
display: none;
float: left;
padding-left: 5px;
}
.separator{
float: left;
width: 100%;
}
</style>
</head>
<body>
<h1>LendIt</h1>
Search Book:
<input type="text" id="booksearch" maxlength="200" >
<div id="viewerStatus"
style="padding: 5px; background-color: #eee; display: block"></div>
<div id="previewContainer">
</div>
</body>
<script type="text/javascript">
$(document).ready(function(){
var parent;
for(var i = 0; i < 20; i++){
if(i % 4 == 0){
parent = $('<div>', {'class': 'separator'}).appendTo('#previewContainer');
}
$('<div>', {
'class': 'viewerCanvas',
'id': i + 'viewerCanvas'
}).appendTo(parent);
}
//Allow you to delay
//http://stackoverflow.com/questions/4347993/jquery-delay-between-keyup-functions
var isLoading = false;
var isDirty = false;
function reloadSearch(){
if(!isLoading){
var q = $('#booksearch').val();
if (q.length >= 5) {
isLoading = true;
// ajax fetch the data
$('#viewerStatus').html("fetching...");
searchBook(q);
// enforce the delay
setTimeout(function(){
isLoading=false;
if(isDirty){
isDirty = false;
reloadSearch();
}
}, 2000);
} else {
hideCanvas();
}
}
}
$('#booksearch').keyup(function(){
isDirty = true;
reloadSearch();
$('#viewerStatus').html("Done fetching...");
});
function searchBook(bookname){
hideCanvas();
$.ajax({
url: 'https://www.googleapis.com/books/v1/volumes?q=' + bookname,
type: 'get',
data: 'json',
success: function(data){
loadResults(data);
},
});
}
function loadResults(results){
var res = results.items || [];
var loaded = 0;
for(var i = 0; i < res.length; i++){
var entry = res[i];
var id = entry.id;
var embeddable = entry.accessInfo.embeddable;
if(embeddable){
loadBook(id, loaded++);
}
}
if(loaded == 0)
$('#viewerStatus').html("No books available...");
}
function loadBook(id, loaded){
var showbookcallback = function(){showBook(id, loaded);};
google.load("books", "0", {"callback": showbookcallback});
}
function showBook(id, loaded){
var canvas = document.getElementById(loaded + 'viewerCanvas');
viewer = new google.books.DefaultViewer(canvas);
viewer.load(id);
showCanvas(canvas);
}
function showCanvas(showing){
//var canvasDiv = $('#viewerCanvas').css('display', showing ? 'block' : 'none');
$(showing).show();
}
function hideCanvas(){
$('.separator').children().each(function(){
$(this).hide();
});
}
function clearStatus(){
$('#viewerStatus').empty();
}
});
</script>
</html>