|
//right now, the app runs one query for the initial search, and then runs a query for each of those items. It would be nice to combine these with the generater feature |
|
//Right now, it displays the article introduction. I would like to change this so I grab the whole article, extract just the introduction, and then have the ability to expand the introduction to the full article. However, I currently retrieve text with no links, so the full articles might be a bit odd. |
|
|
|
//It would be nice to refactor the searching, parsing, and adding to results. |
|
|
|
var baseURL = 'https://en.wikipedia.org'; |
|
var articleURL = '/wiki/' |
|
var apiURL = 'w/api.php'; |
|
|
|
|
|
var randomArticleAPICall = 'https://en.wikipedia.org/w/api.php?action=query&generator=random&grnnamespace=0&prop=extracts&exintro&format=json&callback=?'; |
|
|
|
var searchBarStatus = 'collapsed' ; |
|
//can be 'expanded' or 'collapsed'. Starts 'collapsed' |
|
var currentAnimation = 'none' |
|
//can be 'none', 'expanding', 'collapsing. |
|
|
|
|
|
var truncateString = function(str, num) { |
|
// Clear out that junk in your trunk |
|
str = str.trim(); |
|
//console.log("string is " + str); |
|
if (str.length > num) { |
|
if (str.length <= 3) { |
|
str = str.substring(0, num) + "..."; |
|
} |
|
else { |
|
//console.log("hello. num is " + num); |
|
str = str.substring(0, num - 3) + "..."; |
|
} |
|
} |
|
return str; |
|
}; |
|
|
|
|
|
var capitalizeFirstLetter = function (string) { |
|
//http://stackoverflow.com/questions/1026069/capitalize-the-first-letter-of-string-in-javascript |
|
if (string.indexOf(' ') < 0) { |
|
return string.charAt(0).toUpperCase() + string.slice(1); |
|
} |
|
else { |
|
return string.split(' ').map(function(element) { |
|
return element.charAt(0).toUpperCase() + element.slice(1) |
|
}).join(' '); |
|
} |
|
}; |
|
|
|
|
|
|
|
|
|
var animateSearchBar = function(t, action) { |
|
var time = t || .2; |
|
//action is either 'expand' or 'collapse' as a string |
|
var elements = document.getElementsByClassName('animated-search-bar'); |
|
//console.log("elements :", elements); |
|
for (var i = 0; i < elements.length ; i++ ) { |
|
var element = elements[i]; |
|
$("#" + element.id).css('animation', action + "-" + element.id + " " + time + "s linear 1"); |
|
}`` |
|
$(".animated-search-bar").css('animation-fill-mode', 'forwards'); |
|
$(".animated-search-bar").css('animation-play-state', 'running'); |
|
} |
|
|
|
|
|
var collapseSearchBar = function(t) { |
|
//t can be input in seconds. Default is one second. |
|
var time = t || .2; |
|
if (searchBarStatus !== 'expanded') { |
|
alert("SearchBarStatus not collapsed. Shouldn't have received this alert"); |
|
} |
|
else { |
|
currentAnimation = 'collapsing'; |
|
animateSearchBar(time, 'collapse'); |
|
} |
|
}; |
|
|
|
|
|
|
|
var expandSearchBar = function(t) { |
|
//t can be input in seconds. Default is one second. |
|
var time = t || .2; |
|
if (searchBarStatus !== 'collapsed') { |
|
alert("SearchBarStatus not collapsed. Shouldn't have received this alert"); |
|
} |
|
else { |
|
currentAnimation = 'expanding'; |
|
animateSearchBar(time, 'expand'); |
|
} |
|
|
|
}; |
|
|
|
|
|
var animationListener = function(e) { |
|
//console.log("hey", e.type); |
|
switch(e.type) { |
|
case "animationstart": |
|
//console.log("Started: elapsed time is " + e.elapsedTime, "currentAnimation: ", currentAnimation); |
|
if (currentAnimation === 'collapsing') { |
|
$('.show-when-expanded').hide(); |
|
} |
|
else { |
|
$('.show-when-collapsed').hide(); |
|
} |
|
break; |
|
case "animationend": |
|
//console.log("Ended: elapsed time is " + e.elapsedTime); |
|
if (currentAnimation === 'collapsing') { |
|
searchBarStatus = 'collapsed' ; |
|
$('.show-when-collapsed').show(); |
|
} |
|
else { |
|
searchBarStatus = 'expanded' ; |
|
$('.show-when-expanded').show(); |
|
} |
|
currentAnimation = 'none'; |
|
$(".animated-search-bar").css('animation-play-state', 'paused'); |
|
break; |
|
} |
|
}; |
|
|
|
|
|
var setupListeners = function() { |
|
var e = $("#left-circle")[0]; |
|
//console.log("element:", e); |
|
e.addEventListener("animationstart", animationListener, false); |
|
e.addEventListener("animationend", animationListener, false); |
|
}; |
|
|
|
var showProgressMessage = function(searchQuery) { |
|
var articleDiv = $("<div class='article-result message' id='in-progress'></div>"); |
|
if (searchQuery) { |
|
var itemHTML = |
|
"<p>Search in progres for query '" + searchQuery + "'.</p>"; |
|
} |
|
else { |
|
var itemHTML = |
|
"<p>Loading random article</p>"; |
|
} |
|
articleDiv.html(itemHTML); |
|
articleDiv.hidden = 'true'; |
|
$('#search-results-container').append(articleDiv); |
|
$('#in-progress').show('fold', 500); |
|
} |
|
|
|
var showNoResultsMessage = function(searchQuery) { |
|
var articleDiv = $("<div class='article-result message' id='no-results'></div>"); |
|
var itemHTML = |
|
"<p>No results were found for the query '" + searchQuery + "'.</p>"; |
|
articleDiv.html(itemHTML); |
|
articleDiv.hidden = 'true'; |
|
$('#search-results-container').append(articleDiv); |
|
$('#no-results').show('fold', 5000); |
|
}; |
|
|
|
|
|
|
|
var addArticleToResultsold = function(data) { |
|
var item = { |
|
'url' : baseURL + articleURL + data.title, |
|
'snippet' : data.snippet, |
|
'title' : data.title |
|
}; |
|
|
|
var articleDiv = $("<div class='article-result' id='" + item.title+ "'></div>"); |
|
var itemHTML = |
|
"<a href='" + item.url + "' target='_blank'><h3><b>" + item.title + "</b></h3><p>" + item.snippet + "</p></a>"; |
|
articleDiv.html(itemHTML); |
|
//appendTableItemToTable(tableItem); |
|
$('#search-results-container').append(articleDiv); |
|
}; |
|
|
|
|
|
|
|
|
|
var addArticleToResults = function(data, url) { |
|
var item = { |
|
'url' : baseURL + articleURL + data.title, |
|
'snippet' : data.snippet, |
|
'title' : data.title, |
|
'extract' : '' |
|
}; |
|
|
|
var queryForIntro = baseURL + '/' + apiURL + '?' + |
|
createQueryString({'action' :'query', |
|
'prop' :'extracts', |
|
'format':'json', |
|
'titles' :item.title |
|
}) + |
|
'&exintro&callback=?'; |
|
//console.log(queryForIntro); |
|
$.getJSON(queryForIntro, function(data) { |
|
if (data.query ) { |
|
//console.log(data.query.pages); |
|
for (var page in data.query.pages) { |
|
// data.query.search.forEach(function(element) { |
|
var element = data.query.pages[page]; |
|
if (item.extract !== '') { |
|
alert('was not expacting to have mutltiple objects here'); |
|
} |
|
else { |
|
//console.log(element.extract); |
|
item.extract = element.extract; |
|
var articleDiv = $("<div class='article-result' id='" + item.title+ "'></div>"); |
|
var itemHTML = |
|
"<a href='" + item.url + "' target='_blank'><h3><b>" + item.title + "</b></h3></a>" + item.extract; |
|
articleDiv.html(itemHTML); |
|
$('#search-results-container').append(articleDiv); |
|
} |
|
} |
|
//}); |
|
//should handle greater than 10 hits |
|
} |
|
else { |
|
showNoResultsMessage(searchQuery); |
|
} |
|
}); |
|
}; |
|
|
|
|
|
var createQueryString = function (options) { |
|
var query = []; |
|
for (var i in options) { |
|
if (options.hasOwnProperty(i)) { |
|
query.push(encodeURIComponent(i) + '=' + encodeURIComponent(options[i])); |
|
} |
|
} |
|
return query.join('&'); |
|
}; |
|
|
|
|
|
var runQuery = function(searchQuery) { |
|
var initialURL = baseURL + "/" + apiURL ; |
|
//var action = 'opensearch'; |
|
var action = 'query' |
|
var list = 'search' |
|
//var searchQuery = 'warrior princess'; |
|
//searchQuery = 'Xena'; |
|
var searchOptions = { |
|
'action' : action, |
|
'list' : list, |
|
'srsearch' : 'intitle:' + searchQuery, |
|
'format' : 'json' |
|
}; |
|
|
|
var queryString = createQueryString(searchOptions); |
|
var fullQueryString = initialURL + '?' + queryString + '&callback=?'; |
|
//console.log(fullQueryString); |
|
$.getJSON(fullQueryString, function(data) { |
|
if (data.query.search.length > 0 ) { |
|
$('#in-progress').slideUp(300, function() {this.remove();}); |
|
data.query.search.forEach(function(element) { |
|
addArticleToResults(element); |
|
}); |
|
//should handle greater than 10 hits |
|
} |
|
else { |
|
showNoResultsMessage(searchQuery); |
|
} |
|
}); |
|
}; |
|
|
|
|
|
|
|
|
|
var runQueryOld = function(searchQuery) { |
|
|
|
var initialURL = baseURL + "/" + apiURL ; |
|
//var action = 'opensearch'; |
|
var action = 'query' |
|
var list = 'search' |
|
//var searchQuery = 'warrior princess'; |
|
//searchQuery = 'Xena'; |
|
var searchOptions = { |
|
'action' : action, |
|
'list' : list, |
|
'srsearch' : searchQuery, |
|
'format' : 'json' |
|
}; |
|
|
|
var queryString = createQueryString(searchOptions); |
|
var fullQueryString = initialURL + '?' + queryString + '&callback=?'; |
|
|
|
$.getJSON( fullQueryString, function(data) { |
|
if (data.query.search.length > 0 ) { |
|
data.query.search.forEach(function(element) { |
|
addArticleToResults(element); |
|
}); |
|
//should handle greater than 10 hits |
|
} |
|
else { |
|
showNoResultsMessage(searchQuery); |
|
} |
|
}); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
var loadRandomArticle = function() { |
|
//alert("Loading random article"); |
|
$.getJSON(randomArticleAPICall, function(data) { |
|
if (data.query ) { |
|
//console.log(data.query.pages); |
|
var numItems = 0; |
|
for (var page in data.query.pages) { |
|
numItems++; |
|
if (numItems > 1) { |
|
alert('was not expacting to have mutltiple objects here'); |
|
} |
|
else { |
|
$('#in-progress').slideUp(300, function() {this.remove();}); |
|
// data.query.search.forEach(function(element) { |
|
var element = data.query.pages[page]; |
|
var item = { |
|
'url' : baseURL + articleURL + element.title, |
|
'title' : element.title, |
|
'extract' : element.extract |
|
}; |
|
var articleDiv = $("<div class='article-result' id='" + item.title+ "'></div>"); |
|
var itemHTML = |
|
"<a href='" + item.url + "' target='_blank'><h3><b>" + item.title + "</b></h3></a>" + item.extract; |
|
articleDiv.html(itemHTML); |
|
$('#search-results-container').append(articleDiv); |
|
} |
|
} |
|
} |
|
}); |
|
}; |
|
|
|
var prepForQuery = function() { |
|
$('#search-results-container').empty(300, function() {this.remove();}); |
|
$('#intro').slideUp(300, function() {this.remove();}); |
|
$('#instruction-message').slideUp(300, function() {this.remove();}); |
|
|
|
}; |
|
|
|
$(document).ready(function(){ |
|
setupListeners(); |
|
$('.show-when-expanded').hide(); |
|
|
|
$('#search-box-glyph-remove').on('click', function() { |
|
$("#searchQueryField").val(''); |
|
collapseSearchBar(); |
|
}); |
|
|
|
$('.click-to-expand').on('click', function() { |
|
//alert('hey'); |
|
if (searchBarStatus === 'collapsed') { |
|
$('#instruction-message').remove(); |
|
expandSearchBar(); |
|
} |
|
}); |
|
|
|
$('#random-article-image').on('click', function() { |
|
prepForQuery(); |
|
showProgressMessage(); |
|
loadRandomArticle(); |
|
}); |
|
|
|
$('#searchQueryField').keyup(function(e){ |
|
if (!e) e = window.event; |
|
var keyCode = e.keyCode || e.which; |
|
//console.log(keyCode, e.currentTarget.value); |
|
if (keyCode === 13){ |
|
//console.log("Enter pressed"); |
|
prepForQuery(); |
|
showProgressMessage(e.currentTarget.value); |
|
runQuery(e.currentTarget.value); |
|
} |
|
else { |
|
//possible use to provide autocomplete suggestions |
|
//console.log("hey"); |
|
} |
|
}); |
|
//runQuery('Xena'); |
|
}); |