-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
100 lines (88 loc) · 2.82 KB
/
script.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
$(document).ready(function() {
//autocomplete search
$("#searchThis").autocomplete({
source: function(request, response) {
$.ajax({
url: "https://en.wikipedia.org/w/api.php",
dataType: "jsonp",
data: {
'action': "opensearch",
'format': "json",
'search': request.term
},
success: function(data) {
response(data[1]);
}
});
}
});
//on keyup of the Enter key, trigger the submit button
$('#searchThis').on('keyup', function(event) {
if (event.keyCode === 13) {
$('#submitButton').click();
}
});
//show searh results
function printResults(data) {
$('#searchResults').append('<li data-id = "' + data[2] + '"><a href = "' + data[2] + '" target = "_blank"><strong>' + data[0] + '</strong></a><br/><span style = "color: green">' + data[2] + '</span><br/><strong>' + data[1] + '</strong></li>');
}
//on click, display search results
$('#clickToSearch').on('click', function() {
$(this).hide();
$('#randomSearch').hide();
$('#searchInstructions').hide();
$('#searchRandomly').hide();
$('#searchHere').removeClass('hidden');
$('#newRandomSearch').removeClass('hidden');
});
//on click, display random result
$('#randomSearch').on('click', function() {
$(this).hide();
$('#clickToSearch').hide();
$('#searchInstructions').hide();
$('#searchRandomly').hide();
$('#searchHere').removeClass('hidden');
$('#newRandomSearch').removeClass('hidden');
});
//on click, get a random article
$('#randomSearch').on('click', getRandomArticle);
$('#newRandomSearch').on('click', getRandomArticle);
//get a random article
function getRandomArticle() {
var url = "https://en.wikipedia.org/wiki/Special:Random";
window.open(url);
}
//on click, get the search results
$('#submitButton').on('click', getSearchResults);
//get the top 10 search results
function getSearchResults() {
var webURL = "https://en.wikipedia.org/w/api.php?action=opensearch&search=";
var whatToSearch = $('#searchThis').val();
var limits = "&limit=10&format=json";
url = webURL + whatToSearch + limits;
$.ajax({
type: 'GET',
url: url,
dataType: 'jsonp',
success: sortResults,
error: showError
});
//sort the raw search results data
function sortResults(data) {
var newArr = [];
var arrItem = [];
for (var i = 0; i < data[1].length; i++) {
arrItem = [data[1][i], data[2][i], data[3][i]];
newArr.push(arrItem);
}
$('#searchResults').empty();
$.each(newArr, function(index, item) {
printResults(newArr[index]);
});
}
//show an error message if the search results cannot be displayed
function showError() {
alert("Cannot display search results");
}
}
});