-
Notifications
You must be signed in to change notification settings - Fork 0
/
javascript.js
127 lines (102 loc) · 3.34 KB
/
javascript.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
124
125
126
127
/*jslint browser: true*/
/*global $, jQuery, alert*/
var wikipediaViewer = (function () {
'use strict';
document.getElementById("wikiSearchInput").focus();
function clearResults() {
$("#resultSpot").html("");
}
function clearSearchBar() {
$('input').val(null);
}
function buildResultButton(resultObj) {
var resultWell = document.createElement("div");
resultWell.classList.add("well");
resultWell.classList.add("resultWell");
var newDivTitle = document.createElement("h2"),
resultTitle = document.createTextNode(resultObj.title);
newDivTitle.appendChild(resultTitle);
var newDivSnip = document.createElement("p"),
resultSnip = document.createTextNode(resultObj.snip);
newDivSnip.appendChild(resultSnip);
var newButton = document.createElement("a"),
resultClick = document.createTextNode("click");
newButton.appendChild(resultClick);
newDivTitle.classList.add("resultTitle");
newDivSnip.classList.add("resultSnip");
newButton.setAttribute("target", "_blank");
newButton.setAttribute("href", resultObj.URL);
newButton.innerHTML = "Go To Page";
newButton.classList.add("goToWiki");
resultWell.appendChild(newDivTitle);
resultWell.appendChild(newDivSnip);
resultWell.appendChild(newButton);
document.getElementById("resultSpot").appendChild(resultWell);
return;
}
function displaySearchResults(resultArray) {
var btn = document.createElement('a'),
sniptxt = '',
URL = '',
j = 0;
for (j = 0; j < resultArray.length; j = j + 1) {
buildResultButton(resultArray[j]);
}
}
function arrayOfPages(data) {
var numberOfPages = data[1].length,
searchTerm = data[0],
allWikiResults = [],
i = 0;
if (numberOfPages === 0) {
var noResults = document.createTextNode("Sorry, your search for '" + searchTerm + "' returned no results. Ensure that it is spelled correctly or try using more general keywords.");
document.getElementById("resultSpot").appendChild(noResults);
} else {
for (i = 0; i < numberOfPages; i = i + 1) {
var wikiPage = {};
wikiPage.title = data[1][i];
wikiPage.snip = data[2][i];
wikiPage.URL = data[3][i];
allWikiResults.push(wikiPage);
}
}
displaySearchResults(allWikiResults);
}
var handleData = function (data, textStatus, jqXHR) {
console.log("I made it here");
console.log(data[0], data[1].length);
console.log(data);
arrayOfPages(data);
};
function searchWikipedia(searchTerm) {
var wikiSearchURL = "https://en.wikipedia.org/w/api.php?action=opensearch&search=" + searchTerm + "&limit=10&namespace=0&format=json&origin=*";
$.ajax({
url: wikiSearchURL,
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "GET",
async: "false",
success: handleData,
error: function (errorMessage) {
alert("Unable to retrieve results. Please refresh page.");
}
});
}
function getSearchTerm(searchTerm) {
var cleanSearchTerm = searchTerm.split(' '),
reallyCleanSearchTerm = cleanSearchTerm.join('_');
searchWikipedia(reallyCleanSearchTerm);
clearSearchBar();
clearResults();
}
$('#wikiSearchForm').on('submit', function (event) {
event.preventDefault();
var searchVal = $('#wikiSearchInput').val();
getSearchTerm(searchVal);
});
$(".wikiSearchBtn").click(function (event) {
event.preventDefault();
var searchVal = $("#wikiSearchInput").val();
getSearchTerm(searchVal);
});
}());