-
Notifications
You must be signed in to change notification settings - Fork 2
/
wikipedia.js
65 lines (56 loc) · 2.01 KB
/
wikipedia.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
$(document).ready(function () {
var baseArticleURL = "https://en.wikipedia.org/wiki/";
var baseSearchURL = "https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&srwhat=text&srprop=snippet&srsearch=";
function ResultBox(url, title, snippet) {
this.url = url;
this.title = title;
this.snippet = snippet;
this.html = "<div class='resultBox' onclick=window.open('" + this.url + "')>";
this.html += "<p class='title'>"+this.title+"</p>";
this.html += "<p class='snippet'>"+this.snippet+"</p>";
this.html += "</div>";
this.draw = function(drawArea) {
$(drawArea).append(this.html);
};
}
var drawResultBoxes = function(numberOfBoxes, drawArea, results) {
for (var i = 0; i < numberOfBoxes; i++) {
var resultBox = new ResultBox(articleURL, title, snippet);
resultBox.draw(drawArea);
}
}
$("form").submit(function(event) {
var searchTerm = $("#searchbox").val();
$.ajax({
dataType: "jsonp",
url: baseSearchURL + searchTerm,
success: function(data) {
results = data.query.search;
if (results.length > 0) {
$("#results").empty();
for (var i = 0; i < results.length; i++) {
$("#main").animate({"margin-top":"0"}, {queue:false, duration:500});
var title = results[i].title;
var parsedTitle = title.replace(/ /g, "_");
var articleURL = baseArticleURL + parsedTitle;
var snippet = results[i].snippet;
var resultBox = new ResultBox(articleURL, title, snippet);
resultBox.draw("#results");
}
}
else {
alert("No results found! Please try again.");
$("#searchbox").val("");
}
}
});
event.preventDefault();
});
$("#searchbox").keyup(function(e) {
if (e.keyCode === 27) {
$("#main").animate({"margin-top":"5%"}, {queue:false, duration:500});
$("#searchbox").val("");
$("#results").empty();
}
});
});