Skip to content

Instantly share code, notes, and snippets.

@e30chris
Created July 7, 2012 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e30chris/3067676 to your computer and use it in GitHub Desktop.
Save e30chris/3067676 to your computer and use it in GitHub Desktop.
fake typeahead
<!-- for the autocomplete on arrivals departues and search box -->
<script>
$(function() {
var guest = [
"Peter Venkman",
"Ray Stantz",
"Dana Barrett",
"Egön Spengler",
"Louis Tullu",
"Janine Milnitz",
"Walter Peck",
"Winston Zeddmore",
"Deán Yager",
"Indiana Jones",
"Marion Ravenwood",
"Rene Belloq",
"Arnold Toht",
"Marcus Brody",
"Arthur Dent",
"Ford Perfect",
"Zaphod Beelbebrox",
"Humma Kavula",
"Daniel Larusso",
"Kesuke Miyagi",
"Ali Mills",
"John Kreese",
"Lucille Larusso",
"Johnny Lawerence",
"Bobby Brown",
"Freddy Fernandez",
];
var accentMap = {
"á": "a",
"ö": "o"
};
var normalize = function( term ) {
var ret = "";
for ( var i = 0; i < term.length; i++ ) {
ret += accentMap[ term.charAt(i) ] || term.charAt(i);
}
return ret;
};
$( "#guest" ).autocomplete({
source: function( request, response ) {
var matcher = new RegExp( '\\b' + $.ui.autocomplete.escapeRegex( request.term ), "i" );
response( $.grep( guest, function( value ) {
value = value.label || value.value || value;
return matcher.test( value ) || matcher.test( normalize( value ) );
}) );
}
});
$('.arrivals ul.arrDepBox li').click(function(){
$('#checkinform_name').html( $(this).html() );
var avail = $(this).siblings().map(function(){return $(this).html()});
avail.splice( Math.floor(Math.random()*(avail.length-3)), 3 );
var duration = Math.ceil(Math.random()*4) || 1
, checkin = new Date()
, checkout = new Date(checkin.valueOf() + duration * (1000*60*60*24))
;
$('#checkinform_checkindate').html( checkin.easyFormat( true ) );
$('#checkinform_checkoutdate').html( checkout.easyFormat( true ) );
$('#checkinform_stayduration').html( duration );
$('#checkinform_payment span').html( ( duration * 4 * 28.2 ).toString().replace('.',',') );
$('#checkinform_guestzero span').html($(this).html());
$('#checkinform_guestone span').html(avail[0]);
$('#checkinform_guesttwo span').html(avail[1]);
$('#checkinform_guestthree span').html(avail[2]);
$('#checkinform').find('.guestname input').attr('checked','checked')
$('#checkinform').dialog({title:'Verify Checkin',width:400,modal:true}).removeClass('hidden');
})
});
Date.prototype.easyFormat = function(include_day_of_week){
var ret = [];
if( include_day_of_week )
ret.push( (['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'])[this.getDay()] );
ret.push( (['January','February','March','April','May','June','July','August','September','October','November','December'])[this.getMonth()]);
ret.push( this.getDate() +',');
ret.push( this.getFullYear() );
return ret.join(' ');
}
function checkin( the_form ){
removeNamesFromList(
$(the_form).find('.guestname input').filter(':checked').parent().children('span').map(function(){
return $(this).html()
}),
$('.arrivals ul.arrDepBox li')
);
}
function removeNamesFromList( names, list ){
for(var i=0; i<names.length; i++){
var toDelete = names[i];
$(list).each(function(){if($(this).html()==toDelete) $(this).remove() })
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment