-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
66 lines (53 loc) · 1.49 KB
/
main.js
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
// ----- custom js ----- //
// hide initial
$("#searching").hide();
$("#results-table").hide();
$("#error").hide();
// global
var url = 'http://static.pyimagesearch.com.s3-us-west-2.amazonaws.com/vacation-photos/dataset/';
var data = [];
$(function() {
// sanity check
console.log( "ready!" );
// image click
$(".img").click(function() {
// empty/hide results
$("#results").empty();
$("#results-table").hide();
$("#error").hide();
// remove active class
$(".img").removeClass("active")
// add active class to clicked picture
$(this).addClass("active")
// grab image url
var image = $(this).attr("src")
console.log(image)
// show searching text
$("#searching").show();
console.log("searching...")
// ajax request
$.ajax({
type: "POST",
url: "/search",
data : { img : image },
// handle success
success: function(result) {
console.log(result.results);
var data = result.results
// show table
$("#results-table").show();
// loop through results, append to dom
for (i = 0; i < data.length; i++) {
$("#results").append('<tr><th><a href="'+url+data[i]["image"]+'"><img src="'+url+data[i]["image"]+
'" class="result-img"></a></th><th>'+data[i]['score']+'</th></tr>')
};
},
// handle error
error: function(error) {
console.log(error);
// append to dom
$("#error").append()
}
});
});
});