/** * Plugin functions file and typeahead * * Contains handlers for all the js functions that we will * be executing */ ( function( $ ) { // vars = als.ajaxurl als.nextNonce // url = ajaxurl&action=alsgetsuggestions&nextNonce=als.nextNonce //Typeahead if(als.load_suggestions == 'yes'){ var engine = new Bloodhound({ remote: { url: als.ajaxurl + '?s=%Q&action=alsgetsuggestions&nextNonce=' + als.nextNonce, wildcard:'%Q' }, queryTokenizer: Bloodhound.tokenizers.whitespace, datumTokenizer: Bloodhound.tokenizers.whitespace }); $('input[name="s"]') .typeahead( { minLength: 1, highlight: true }, { name: 'als-suggestions', source: engine, limit: 5, }) .bind( 'typeahead:render', function(e){ var input = $(e.target); var width = input.css("width"); input.siblings('.tt-menu').css({width: width}); } ); } /** * This function loads search results via ajax * @param string holds the value of the input box * @param div holds the div that displays the results */ function loadResults(str, div) { if(str.length==0) { return false; } div.html('
'); $('.results-count').hide(); $.get(als.ajaxurl, { s:str, action: 'alsgetresults', nextNonce: als.nextNonce }, function(data,status,xhr) { div.html(data); }); } /** * Loads results via ajax when the form is submited */ $('.als-mainform form').submit(function(e){ if(als.ajax_results != 'yes'){ return; } e.preventDefault(); var term = $('.als-mainform input[name="s"]').val(); var div = $('.als-results'); loadResults(term, div); }); /** * Excecutes live search functionality * Only when backspace or spacebar has been pressed to save resources */ $(document).on('keyup', '.als-mainform input[name="s"]', function(e){ if(als.live_results != 'yes'){ return; } if(e.keyCode != 8 && e.keyCode != 32){ return; } var term = $('.als-mainform input[name="s"]').val(); var div = $('.als-results'); loadResults(term, div); }); } )( jQuery );