//Called when application is started.
function OnStart()
{
//Create a layout with objects aligned to the bottom of the screen.
lay = app.CreateLayout( "linear", "Bottom,FillXY" );
//create an empty list and set a function to trigger
//when an item is touched
lst = app.CreateList( "", 0.8 );
lst.SetOnTouch( lst_OnTouch );
lay.AddChild( lst );
//Create a text edit, set a function to trigger on
//change, and add it to layout.
txt = app.CreateTextEdit( "Country",0.5 );
txt.SetTextSize( 32 );
txt.SetOnChange( txt_OnChange );
lay.AddChild( txt );
//Add layout to app.
app.AddLayout( lay );
}
//Triggered everytime the text edit
function txt_OnChange(){
var text = txt.GetText().trim(); //get rid of trailing spaces with trim()
if (text.length > 0){ //only act after the first character
//here I set the list with the result of calling the
//matchCountries function with the text on txt
lst.SetList( matchCountries( txt.GetText() ), "," );
}
else{
//empty the list if I have less than one character
lst.SetList("");
}
}
//When an user touch an item on the list
function lst_OnTouch( item ){
//I write the touched item to the textedit
txt.SetText( item );
//I clear the list
lst.SetList("");
}
function matchCountries(input) {
var reg = new RegExp(input.split('').join('\\w*').replace(/\W/, ""), 'i');
return countries.filter(function(country) {
if (country.match(reg)) {
return country;
}
});
}
//This is just the list of countries that it will filter
var countries = ['Afghanistan',
'Albania',
'Algeria',
'Andorra',
//...
'Vietnam',
'Yemen',
'Zambia',
'Zimbabwe'];