Slide Menu

You can swipe left to close this menu.

swipeMenu.js
var layMenu,txe;
//Called when application is started.
function OnStart()
{
    app.SetOrientation("Portrait");
    app.EnableBackKey(false);
 
    //container layout
    var layApp=app.CreateLayout("Absolute");
    layApp.SetBackColor("#ff224422");
    layApp.SetPosition(0,0,1,1);
    app.AddLayout(layApp);
 
    //home layout
    var layHome=app.CreateLayout("Linear","Left,FillXY");
    var btnMnu;
    btnMnu=app.CreateButton("[fa-bars]", -1, -1, "fontawesome,custom");
    btnMnu.SetStyle( "#77bb77", "#559955", 15, "#449944",2,0 );
    btnMnu.SetOnTouch(mnuAnimate);
    layHome.AddChild(btnMnu);
    txe =app.CreateTextEdit("aaaaa\nbbbbb",0.9,0.8);
    txe.SetMargins(0.05,0.02,0,0)
    txe.SetBackColor("#ff888888");
    layHome.AddChild(txe);
    layApp.AddChild(layHome);
 
   //Create menu layout
    layMenu = getSlideMenu(menu_OnTouch, menu_SwipeTest);    
    layMenu.SetPosition(0,0.08);
    //Populate menu list
    layMenu.AddItem("Hello","World","/Sys/Img/Add.png");
    layMenu.AddItem("Another","Item","/Sys/Img/Bulb.png");
    layMenu.AddItem("Three","","video");
    layMenu.AddItem("Four","","audio");
 
    //Add layout to app.    
    layApp.AddChild( layMenu );
}
 
function mnuAnimate()
{
    if(layMenu.GetVisibility()==="Hide")
       layMenu.Show();
    else
       layMenu.Hide();
}
 
function OnBack()
{
   if(layMenu.GetVisibility()==="Show")
      layMenu.Animate("SlideToLeft");
   else app.Exit();
}
 
//catch hardware menu button
function OnMenu( item )
{    
    if(item==null) mnuAnimate();
}
 
function getSlideMenu(onTouch, swipeTest)
{
//Create menu layout
    var lst
    var layMenu = app.CreateLayout( "Absolute", "" );    
    layMenu.SetPosition(0,0.08);
    layMenu.SetBackColor("#ff66aa66");
    layMenu.SetVisibility("Hide")
    //Create menu list
    lst = app.CreateList( "" ,0.8,0.8);
    if(onTouch) lst.SetOnTouch(onTouch);
    lst.SetList(null);
    layMenu.AddChild( lst );
    //create swiper image
    var img=app.CreateImage(null,0.8,0.8);
    img.menu=lst;
    img.downX=0;
    if(swipeTest) img.SetOnTouch(swipeTest);
    layMenu.AddChild(img);
 
    //public methods
    img.Hide=function()
    {
         layMenu.Hide();
    }
    layMenu.Show=function()
    {
         this.Animate("SlideFromLeft");
    }
    layMenu.Hide=function()
    {
         this.Animate("SlidetoLeft");
    }
    layMenu.AddItem=function( title,body,image )
    {
        lst.AddItem( title,body,image );
    }
    layMenu.Setlist=function( list,delim )
    {
        lst.SetList( list,delim )
    }
    return layMenu
}
 
// callback function for menu selection
function menu_OnTouch( title, body, image, index )
{
  var s = txe.GetText()+"\n"+title;
  txe.SetText(s);
  app.ShowPopup(title+" added")
}
 
// callback function to swipe close menu
function menu_SwipeTest(ev)
{
    if(ev.action==="Down")
    {
        this.downX=ev.X;
    }
    if(ev.action==="Up")
    {
        if(ev.X-this.downX < -0.1)
           this.Hide();
        else 
           app.SimulateTouch(this.menu,ev.X,ev.Y);
    }
}