2013-09-11 16:12:08 +00:00
|
|
|
/*global jQuery */
|
|
|
|
/*!
|
|
|
|
* Simple Jekyll Search
|
|
|
|
* Christian Fei - https://github.com/christian-fei/Simple-Jekyll-Search *
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function($) {
|
2014-08-22 17:50:42 +00:00
|
|
|
$.fn.jekyllSearch = function(options) {
|
2013-09-11 16:12:08 +00:00
|
|
|
var settings = $.extend({
|
2014-08-22 17:50:42 +00:00
|
|
|
jsonFile : '/search.json',
|
|
|
|
template : '<a href="{url}" title="{desc}">{title}</a>',
|
|
|
|
searchResults : '.results',
|
|
|
|
searchResultsTitle : '<h4>Search results</h4>',
|
|
|
|
limit : '10',
|
|
|
|
noResults : '<p>Oh shucks<br/><small>Nothing found :(</small></p>'
|
2013-09-11 16:12:08 +00:00
|
|
|
}, options);
|
|
|
|
|
|
|
|
var jsonData = [],
|
|
|
|
origThis = this,
|
|
|
|
searchResults = $(settings.searchResults);
|
|
|
|
|
2014-08-22 17:50:42 +00:00
|
|
|
var matches = [];
|
|
|
|
|
2013-09-11 16:12:08 +00:00
|
|
|
if(settings.jsonFile.length && searchResults.length){
|
|
|
|
$.ajax({
|
|
|
|
type: "GET",
|
|
|
|
url: settings.jsonFile,
|
|
|
|
dataType: 'json',
|
|
|
|
success: function(data, textStatus, jqXHR) {
|
|
|
|
jsonData = data;
|
|
|
|
registerEvent();
|
|
|
|
},
|
|
|
|
error: function(x,y,z) {
|
2014-08-22 17:50:42 +00:00
|
|
|
console.log("***ERROR in jekyllSearch.js***");
|
2013-09-11 16:12:08 +00:00
|
|
|
console.log(x);
|
|
|
|
console.log(y);
|
|
|
|
console.log(z);
|
|
|
|
// x.responseText should have what's wrong
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerEvent(){
|
|
|
|
origThis.keyup(function(e){
|
2014-08-22 17:50:42 +00:00
|
|
|
if(e.which === 13){
|
|
|
|
if(matches)
|
|
|
|
window.location = matches[0].url;
|
|
|
|
|
|
|
|
//follow the first link
|
|
|
|
// if(searchResults.children().length)
|
|
|
|
}
|
2013-09-11 16:12:08 +00:00
|
|
|
if($(this).val().length){
|
|
|
|
writeMatches( performSearch($(this).val()) );
|
|
|
|
}else{
|
|
|
|
clearSearchResults();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function performSearch(str){
|
2014-08-22 17:50:42 +00:00
|
|
|
matches = [];
|
2013-09-11 16:12:08 +00:00
|
|
|
|
2014-08-22 17:50:42 +00:00
|
|
|
for (var i = 0; i < jsonData.length; i++) {
|
|
|
|
var obj = jsonData[i];
|
|
|
|
for (key in obj) {
|
|
|
|
if(obj.hasOwnProperty(key)){
|
|
|
|
if (obj[key] instanceof Array){
|
|
|
|
var seen = false;
|
|
|
|
for (var j = 0; j < obj[key].length; j++){
|
|
|
|
if(obj[key][j].toLowerCase().indexOf(str.toLowerCase()) >= 0){
|
|
|
|
matches.push(obj);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else if (obj[key].toLowerCase().indexOf(str.toLowerCase()) >= 0){
|
|
|
|
matches.push(obj);
|
|
|
|
break;
|
|
|
|
}
|
2013-09-11 16:12:08 +00:00
|
|
|
}
|
2014-08-22 17:50:42 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-11 16:12:08 +00:00
|
|
|
return matches;
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeMatches(m){
|
|
|
|
clearSearchResults();
|
2014-08-22 17:50:42 +00:00
|
|
|
|
2013-09-11 16:12:08 +00:00
|
|
|
searchResults.append( $(settings.searchResultsTitle) );
|
|
|
|
|
2014-08-22 17:50:42 +00:00
|
|
|
if(m && m.length){
|
|
|
|
for (var i = 0; i < m.length && i < settings.limit; i++) {
|
|
|
|
var obj = m[i];
|
|
|
|
output = settings.template;
|
|
|
|
output = output.replace(/\{(.*?)\}/g, function(match, property) {
|
|
|
|
return obj[property];
|
|
|
|
});
|
|
|
|
searchResults.append($(output));
|
|
|
|
}
|
2013-09-11 16:12:08 +00:00
|
|
|
}else{
|
|
|
|
searchResults.append( settings.noResults );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function clearSearchResults(){
|
|
|
|
searchResults.children().remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(jQuery));
|