This repository has been archived by the owner on Oct 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
gca_wikisearch.html
179 lines (156 loc) · 5.4 KB
/
gca_wikisearch.html
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!DOCTYPE html>
<html>
<!--
This is the Wikipedia search page assignment I presented for the course on Freecodecamp.
The aim is to show a proficient usage of bootstrap, jQuery and AJAX requests.
This project has been updated and the assets have been merged in a single file (besides the jQuery and bootstrap files that are loaded from external links).
The original project can be seen on https://codepen.io/gianc/full/oLGRQG
An updated copy can be found also on my personal repository https://github.com/GCa/Web-Projects
-->
<head>
<title>Wikipedia Quick Search</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css">
<style>
@import url(https://fonts.googleapis.com/css?family=Niramit:500);
html,
body {
font-family: 'Niramit', sans-serif;
background: #FFD46B;
color: #E9B125;
}
#header {
text-align: center;
margin-bottom:22px;
}
.buttonBar {
padding: 14px 0;
}
.btn:focus,
.btn:active {
outline: none !important;
}
.form-control:focus {
outline: none;
box-shadow:none !important;
border:1px solid #ccc !important;
}
#search {
font-size: 1.1em;
}
.message {
height: auto;
margin: auto;
width: 50%;
background-color: #E9B125;
color: #FFEBBA;
padding: 8px;
text-align: center;
border-radius: 5px;
opacity: 0.8;
}
.article a {
color: #E9B125;
}
.article {
background: #6373C9;
color: #FFEBBA;
margin-bottom:8px;
border-radius:5px;
opacity:0.8;
}
.btn-primary {
color: #FFEBBA;
background: #6373C9;
}
</style>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript">
var lang = "en";
$(document).ready(function() {
$('#searchtext').keypress(function(event) {
if (event.which == 13)
$("#go").click();
});
$("#go").click(getArticles);
$("#randB").click(function() {
window.open("https://en.wikipedia.org/wiki/Special:Random", "_blank");
});
$("#clearB").click(function() {
$(".article").remove();
document.getElementById("searchtext").value = "";
});
$(".dropdown-menu li a").click(function() {
$(this).parents(".dropdown").find(".btn").html($(this).text() + ' <span class="caret"></span>');
lang = $(this).data("value");
console.log(lang);
});
$('#langlist li').on('click', function() {
lang = $(this).text();
});
$(".btn").mouseup(function() {
$(this).blur();
});
});
function getArticles() {
$(".article").remove();
if ($("#searchtext").val() == "") {
$(".message").html("<i class='fa fa-warning'></i> Nothing to search").fadeIn(500).delay(2000).fadeOut(800);
} else {
queryUrl = "https://" + lang + ".wikipedia.org/w/api.php?action=opensearch&format=json&search=" + encodeURIComponent($("#searchtext").val()) + "&limit=10";
console.log(queryUrl);
ret_data = $.ajax({
url: queryUrl,
dataType: "jsonp",
success: function(datalist) {
if (datalist[1].length === 0 ) {
$(".message").html("<i class='fa fa-warning'></i> Nothing found").fadeIn(500).delay(2000).fadeOut(800);
}
else {
var result = [];
for (var i = 0; i < datalist[1].length; i++) {
result.push([datalist[1][i], datalist[2][i], datalist[3][i]]);
}
for (i = 0; i < result.length; i++) {
var insert = "<div id=article-'" + i + "' class='article col-sm-10 col-sm-offset-1'><a href='" + result[i][2] + "' target='_blank'><div class='entry'><h3>" + result[i][0] + "</h3></a><p>" + result[i][1] + "</p></div></div>";
$(".articles").append(insert);
}
}
}
});
}
};
</script>
</head>
<body>
<div id="external">
<div id="header">
<h1>Wikipedia Searcher</h1></div>
<div id="search" class="container">
<div class="input-group">
<span class="input-group-btn dropdown">
<button type="button" class="btn btn-default btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Lang (default: EN) <span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a href="#" data-value="EN">English</a></li>
<li><a href="#" data-value="IT">Italian</a></li>
<li><a href="#" data-value="DE">German</a></li>
<li><a href="#" data-value="FR">French</a></li>
<li><a href="#" data-value="ES">Spanish</a></li>
</ul>
</span>
<input id="searchtext" type="text" class="form-control">
<span class="input-group-btn">
<button id="go" class="btn btn-default btn-primary" type="button" ><i class="fa fa-search"></i></button>
</div>
<div class="clearfix"></div>
<div class="text-center">
<div class="btn-group buttonBar"><button id="randB" name="randomButton" type="button" class="randButton btn btn-primary">Random Article <i class="fa fa-random fa-lg"></i></button>
<button id="clearB" name="clearButton" type="button" class="randButton btn btn-primary">Clear Page <i class="fa fa-recycle fa-lg"></i></button>
</div></div>
<div class="message" style="display:none"></div>
<div class="articles"></div>
</div>
</body>
</html>