-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
123 lines (85 loc) · 3.28 KB
/
app.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
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
$(document).ready(function(){
/**********Data collection and display*************/
var myQuery = "";
//trigger event when form is submit
$( "form" ).on( "submit", function( event ) {
content = "";
//stop form submitting and page refresh
event.preventDefault();
myQuery = $("#search").val();
//gathers api data from wikipidia
$.ajax({
type: "GET",
url: "http://en.wikipedia.org/w/api.php?action=query&format=json&list=search&titles=&generator=search&srsearch="+myQuery+"&gsrsearch=" + myQuery + "&gsrlimit=10&callback=?",
contentType: "application/json; charset=utf-8",
async: false,
dataType: "json",
//callback function when data is succesfully fetch
success: function (data, textStatus, jqXHR) {
var searchResult = data.query.search;
var dataResult = data.query.pages;
//rearrange required data
for(i=0; i<searchResult.length; i++) {
for(prop in dataResult) {
if(dataResult[prop].title === searchResult[i].title) {
searchResult[i]["pageid"] = dataResult[prop].pageid;
}
}
}
//move search box
afterSearch("add");
//call a function to append result to index.html
displaySearch(searchResult);
},
error: function (errorMessage) {
}
});
});
function displaySearch(results) {
var content = "";
for(i=0; i<results.length; i++) {
//
content += "<a href='https://en.wikipedia.org/?curid=" + results[i].pageid + "' target='blank'><div class='search-item'><h1>" + results[i].title + "</h1><p>" + results[i].snippet + "</p></div></a>"
}
//
$("#show-results").html(content).effect("slide", {direction: "down"}, 1000);
};
/*************front end effects*************/
//make search box draggable
$(".search-area").draggable({
cancel: null
});
$("#search").click(function() {
$(this).focus();
});
//search box shifting
function afterSearch(status) {
var searchBoxOption = {
duration: 300,
easing: "linear"
}
if(status === "add") {
$(".search-area").addClass("after-search", searchBoxOption);
}else if(status === "remove") {
$(".search-area").removeClass("after-search", searchBoxOption);
}
}
//input focus animation
var boxInputOption = {
duration: 500,
easing: "swing"
}
$("#search").focus(function() {
$("#search").addClass("box-input", boxInputOption);
setTimeout(function() {
$("#cross").css("display", "block")
}, 500);
});
//close search box
$("#cross").click(function() {
$("#cross").css("display", "none");
$("#search").removeClass("box-input", boxInputOption).val("");
$("#show-results").hide("slide", {direction: "down"}, 1000);
afterSearch("remove");
});
});