Last active
April 17, 2017 03:34
-
-
Save the-vampiire/3f29c277e9b2eb792021bec308a0d5ab to your computer and use it in GitHub Desktop.
Wikipedia Viewer Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// jQuery Shortcuts | |
var search_bar = $('#search_bar'); | |
var search_button = $('#search_button'); | |
var search_query = $('[name=search_query]')[0]; | |
var links = $('#links'); | |
var random = $('#random_button'); | |
// Animations | |
// activate search on click | |
search_button.click(show_and_hide); | |
// reset screen on random click | |
random.click(hide_and_show); | |
// activate results window on keypress | |
search_bar.keypress(function(){ | |
random.slideUp(500); | |
links.slideDown(200); | |
}); | |
// if search bar is empty (from deleting) revert back to search icon | |
search_bar.keyup(function(){ | |
if(search_bar.val().length === 0){ | |
links.slideUp(200); | |
hide_and_show(); | |
} | |
}); | |
// function to show the random button and search bar while hiding search icon | |
function show_and_hide(){ | |
search_button.fadeOut(400); | |
random.slideDown(1000); | |
search_bar.slideDown(1000); | |
search_bar.select(); | |
} | |
// hide search bar and display search button | |
function hide_and_show(){ | |
search_bar.slideUp(600); | |
random.slideUp(600); | |
search_button.slideDown(1000); | |
} | |
// Link generator function for results window | |
function link_generator(data){ | |
var i = 0, | |
title = data[1], | |
description = data[2], | |
link = data[3]; | |
for(i; i < data[3].length; i++){ | |
var list_link = '<a href="'+link[i]+'">'+title[i]+'</a><br/>'; | |
list_description = list_link+description[i]+'<hr>'; | |
links.append(list_description); | |
} | |
} | |
// Wiki Auto-complete | |
search_bar.autocomplete({ | |
source: function(query, result) { | |
$.ajax({ | |
url: "https://en.wikipedia.org/w/api.php", | |
dataType: "jsonp", | |
data: { | |
'action': "opensearch", | |
'format': "json", | |
'search': query.term | |
}, | |
success: function (data) { | |
links.empty(); | |
link_generator(data); | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment