/*
* == Search ==
* index and algolia settings are defined in layout.jade
*/
$(document).ready(function() {
if (typeof algoliaIndex === 'undefined') return;
var searchInput = $('#cloud-search');
var tu = searchInput.typeahead({hint: true}, {
source: algoliaIndex.ttAdapter(),
displayKey: 'name',
limit: 10,
minLength: 0,
templates: {
suggestion: function(hit) {
var hitMedia = (hit.media ? ' · '+hit.media+'' : '');
var hitFree = (hit.is_free ? '
free
' : '');
var hitPicture;
if (hit.picture){
hitPicture = '
';
} else {
hitPicture = '';
hitPicture += (hit.media ? '' : '');
hitPicture += '
';
};
return '' +
'' +
'' +
'
' +
hitPicture +
hitFree +
'
' +
'
' +
hit.name + ' ' +
'
' +
'
' +
'' + hit.project.name + ' · ' +
'' + hit.node_type + '' +
hitMedia +
'
' +
'
'+
'';
}
}
});
$('.search-site-result.advanced, .search-icon').on('click', function(e){
e.stopPropagation();
e.preventDefault();
window.location.href = '/search#q='+ $("#cloud-search").val() + '&page=1';
});
searchInput.bind('typeahead:select', function(ev, hit) {
$('.search-icon').removeClass('pi-search').addClass('pi-spin spin');
window.location.href = '/nodes/'+ hit.objectID + '/redir';
});
searchInput.bind('typeahead:active', function() {
$('#search-overlay').addClass('active');
$('.page-body').addClass('blur');
});
searchInput.bind('typeahead:close', function() {
$('#search-overlay').removeClass('active');
$('.page-body').removeClass('blur');
});
searchInput.keyup(function(e) {
if ( $('.tt-dataset').is(':empty') ){
if(e.keyCode == 13){
window.location.href = '/search#q='+ $("#cloud-search").val() + '&page=1';
};
};
});
searchInput.bind('typeahead:render', function(event, suggestions, async, dataset) {
if( suggestions != undefined && $('.tt-all-results').length <= 0){
$('.tt-dataset').append(
'' +
'' +
'
' +
'
' +
'Use Advanced Search' +
'
' +
'
'+
'');
};
});
});