Last active
December 18, 2015 08:29
-
-
Save bocha/5754636 to your computer and use it in GitHub Desktop.
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
(function($, undefined) { | |
var globalResults = [ | |
{ page: 'Speech API', url: '/api/speech' }, | |
{ page: 'Speech API Documentation',url: '/api/speech/docs'}, | |
{ page: 'Speech API Pricing', url: '/api/speech/pricing'}, | |
{ page: 'SMS API', url: '/api/sms' }, | |
{ page: 'SMS Documentation API', url: '/api/sms/docs'}, | |
{ page: 'SMS Pricing API', url: '/api/sms/pricing'}, | |
{ page: 'Payment API', url: '/api/payment' }, | |
{ page: 'Payment API Documentation', url: '/api/payment/docs'}, | |
{ page: 'Payment API Pricing', url: '/api/payment/pricing'}, | |
{ page: 'ARO', url: '/aro'}, | |
{ page: 'Hackathons', url: '/hackathons'}, | |
{ page: 'Community', url: '/community'}, | |
{ page: 'Events', url: '/community/events'} | |
]; | |
var $suggestedResults = $('#suggested-result ul'); | |
var highlight = function(str) { return '<b>'+str+'</b>'; }; | |
$('#search').keyup(function(e) { | |
var $this = $(this); | |
var query = $this.val().toLowerCase(), | |
regex = new RegExp(query, 'i'), | |
results = [], | |
i; | |
if (!query.match(/^\s*$/)) { | |
for (i = 0; i < globalResults.length; i++) { | |
if (globalResults[i].page.match(regex)) { | |
// Prevent copying by reference | |
results.push({ page: globalResults[i].page, url: globalResults[i].url }); | |
results[results.length - 1].page = results[results.length - 1].page.replace(regex, highlight); | |
} | |
} | |
} | |
if (results.length) { | |
$suggestedResults.empty(); | |
for (i = 0; i < results.length; i++) { | |
var $a = $('<a/>').html(results[i].page).attr('href', results[i].url); | |
var $li = $('<li/>').append($a); | |
$suggestedResults.append($li); | |
} | |
$suggestedResults.parent().show(); | |
} else { | |
$suggestedResults.parent().hide(); | |
} | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment