-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
103 lines (85 loc) · 3.16 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
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
$(document).ready(function() {
//Function to check if the input search box isn't empty
function checkInput() {
if ($('#before_search').val() == "") {
alert('Please enter a keyword to search');
return false;
} else {
return true;
}
}
//When the search button is clicked
$('#search').click(function() {
if (checkInput()) {
search();
}
});
//When the user starts typing in..
$(document).keyup(function(e) {
if (e.which == 13) {
if (checkInput()) {
if ($('#searched_results_display').hasClass('hidden')) {
search();
} else {
var keyword = $('#after_search').val();
getWikiData(keyword);
}
}
} else {
if ($('.wrapper').hasClass('hidden')) {
var keyword = $('#after_search').val();
getWikiData(keyword);
}
}
});
//Changing the view when search button is clicked
function search() {
var keyword = $('#before_search').val();
$('#after_search').val(keyword);
$('.wrapper').addClass('hidden');
$('#searched_results_display').removeClass('hidden');
getWikiData(keyword);
}
//Function which does the magic of getting the wiki data according to the keyword user searches for
function getWikiData(keyword) {
$.ajax({
url: "https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=" + keyword + "&prop=info&inprop=url&utf8=&format=json",
dataType: "jsonp",
success: function(wikiResponse) {
if (wikiResponse.query.searchinfo.totalhits == 0) {
$('.wiki_results').html("");
$('.wiki_results').append("<div class='error'><div>Your search - <b>" + keyword + "</b> - did not match any documents.</div>" + "<br>" + "<div>Suggestions:</div>" + "<br>" + "<ul><li>Make sure that all words are spelled correctly</li><li>Try different keywords</li><li>Try more general words</li><li>Try fewer keywords</li></ul></div>");
} else {
showResults(wikiResponse);
}
},
error: function() {
alert('Error retrieving the desired results');
}
});
}
//Showing the results we retrieved from the wiki api in our html page
function showResults(wikiResponse) {
console.log(wikiResponse);
$('.wiki_results').html("");
for (var i = 0; i < 9; i++) {
$('.wiki_results').append("<div class='wiki_title wiki_title_" + i + "'></div>" + "<div class='wiki_snippet wiki_snippet_" + i + "'></div>");
}
for (var j = 0; j < 9; j++) {
var title = wikiResponse.query.search[j].title;
var url = title.replace(/\s/g, "_");
var snippet = wikiResponse.query.search[j].snippet;
$(".wiki_title_" + j + "").html("<a href='https://en.wikipedia.org/wiki/" + url + "' target='_blank'>" + title + "</a>");
$(".wiki_snippet_" + j + "").html(snippet);
}
}
//When user clicks the search icon on the display section where the wiki results are appended
$('.after_search_container span').click(function() {
if ($('#after_search').val() == "") {
alert('Please enter a keyword to search');
} else {
var keyword = $('#after_search').val();
getWikiData(keyword);
}
});
});