Created
September 15, 2011 18:20
-
-
Save inadarei/1220042 to your computer and use it in GitHub Desktop.
dso.search
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
dso.search = function() { | |
var data = false; | |
var search = $('#search'); | |
var template = _.template('<li><a href="<%=url%>"><span class="title"><%=title%></span><small><%=subtitle%></small><small class="date"><%=date%></small></a></li>'); | |
var find = function(phrase) { | |
if (!data) return $.ajax({ | |
url: '/search.json', | |
dataType: 'json', | |
success: function(resp) { | |
data = _(resp).chain() | |
.compact() | |
.map(function(p) { | |
p.words = (p.title.toLowerCase() +' '+ p.subtitle.toLowerCase()).match(/(\w+)/g); | |
return p; | |
}) | |
.value(); | |
find(phrase); | |
} | |
}); | |
var matches = _(data).filter(function(p) { | |
return _(phrase).filter(function(a) { | |
return _(p.words).any(function(b) { | |
return a === b || b.indexOf(a) === 0; | |
}); | |
}).length === phrase.length; | |
}); | |
return matches; | |
}; | |
$('a', search).click(function() { | |
if ($('body').hasClass('searching')) { | |
$('body').removeClass('searching'); | |
$('.branding .wasActive').removeClass('wasActive').addClass('active'); | |
} else { | |
$('body').addClass('searching'); | |
$('.branding .active').removeClass('active').addClass('wasActive'); | |
$('input', search).focus(); | |
} | |
return false; | |
}); | |
$('input', search).keyup(_(function() { | |
$('#search-results ul').empty(); | |
$('#search-results > div').removeClass('active'); | |
var el = { | |
'empty': $('#search-results > div.empty'), | |
'about': $('#search-results > div.about'), | |
'projects': $('#search-results > div.projects'), | |
'team': $('#search-results > div.team'), | |
'blog': $('#search-results > div.blog') | |
}; | |
var done = {}; | |
var phrase = $('input', search).val(); | |
if (phrase.length >= 4) { | |
var matches = find(phrase.toLowerCase().match(/(\w+)/g)); | |
_(matches).each(function(p) { | |
if (!p.category) return; | |
if (!done[p.category]) { | |
el[p.category].addClass('active'); | |
done[p.category] = true; | |
} | |
$('ul', el[p.category]).append(template(p)); | |
}); | |
if (matches.length) return; | |
} | |
$('#search-results > div.empty').addClass('active'); | |
return false; | |
}).debounce(100)); | |
}; | |
$(dso.search); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment