User Tools

Site Tools


sample_code:folder_picker

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
sample_code:folder_picker [2014/12/06 21:13]
stevegarman
sample_code:folder_picker [2015/02/19 23:17] (current)
Line 5: Line 5:
 ===== The code ===== ===== The code =====
 <code javascript folderpicker.js> <code javascript folderpicker.js>
 +
 //Called when application is started //Called when application is started
-function  OnStart() +function OnStart(){ 
-      //Create a layout with objects vertically centered. +    //Create a layout with objects vertically centered. 
-         +    lay = app.CreateLayout( "linear", "Vertical,FillXY" );     
-    lay  =  app.CreateLayout( "linear",  "Vertical,FillXY" );    + 
 +    //Create a text label and add it to layout. 
 +    btn = app.CreateButton( "FolderPicker demo" ); 
 +    lay.AddChild( btn ); 
 +    btn.SetOnTouch(btn_OnTouch); 
 +     
 +    //Add layout to app.     
 +    app.AddLayout( lay ); 
 +    pick = new FolderPicker(mycallback); 
 +    // don't show hidden files 
 +    pick.SetHideFiles(true); 
 +}//function Onstart() 
  
-         //Create a text label and add it to layout. +function btn_OnTouch(){ 
-         +    pick.SetFolder("/sdcard"); 
-    btn  =  app.CreateButton"FolderPicker demo" );     +    pick.Show();     
-    lay.AddChild( btn );     +}//function btn_OnTouch()
-    btn.SetOnTouch(btn_OnTouch);         //Add layout to app.     +
-         +
-    app.AddLayout( lay );     +
-    pick  =  new  FolderPicker(mycallback);     // don't show hidden files +
-         +
-    pick.SetHideFiles(true); +
-  } //function Onstart()+
  
-function  btn_OnTouch() +function mycallback(fullpath){ 
-      +  app.Alert("user chose + fullpath
-    pick.SetFolder("/sdcard");     +}//function mycallback()
-    pick.Show();     +
-  } //function btn_OnTouch()+
  
-function  mycallback(fullpath+function FolderPicker(Callback,basePath){ 
-  {   +  var self = this; 
-    app.Alert("user chose   fullpath+  this.basePath = basePath || "/sdcard"; 
-  } //function mycallback()+  this.callback = Callback || function(){}; 
 +  this.FolderPath = this.basePath; 
 +  this.hideHiddenFiles = false; 
 +  this.dlg = app.CreateDialog(this.basePath); 
 +  this.lay = app.CreateLayout( "linear", "horizontal,fillxy" ); 
 +   
 +  this.lstFolds = app.CreateList("blank" , 0.7, 0.7 ); 
 +  this.lstFolds.parent = self; 
 +  this.lstFolds.SetOnTouch(FolderPicker_NewFolder);   
 +  this.lay.AddChild(this.lstFolds);
  
-function  FolderPicker(CallbackbasePath+  var vlay = app.CreateLayout"linear""vertical,fillxy" ); 
-  {   +  this.okBtn app.CreateButton("Ok")
-    var  self  =  this;   +  this.okBtn.parent self
-    this.basePath   basePath  ||  "/sdcard";   +  this.okBtn.SetOnTouch(FolderPicker_Select)
-    this.callback   Callback  ||   function() {}  +  vlay.AddChild(this.okBtn);   
-    this.FolderPath  =  this.basePath;   +  this.cancelBtn = app.CreateButton("Cancel"); 
-    this.hideHiddenFiles  =  false  +  this.cancelBtn.parent = self; 
-    this.dlg  =  app.CreateDialog(this.basePath);   +  this.cancelBtn.SetOnTouch(FolderPicker_Select); 
-    this.lay   app.CreateLayout( "linear",  "horizontal,fillxy" );     +  vlay.AddChild(this.cancelBtn);  
-    this.lstFolds  =  app.CreateList("blank" ,  0.7,  0.7 );   +
-    this.lstFolds.parent  =  self;   +
-    this.lstFolds.SetOnTouch(FolderPicker_NewFolder);     +
-    this.lay.AddChild(this.lstFolds);+
  
-       +  this.lay.AddChild(vlay)
-    var  vlay  =  app.CreateLayout( "linear",  "vertical,fillxy" );   +
-    this.okBtn  =  app.CreateButton("Ok");   +
-    this.okBtn.parent  =  self;   +
-    this.okBtn.SetOnTouch(FolderPicker_Select);   +
-    vlay.AddChild(this.okBtn);     +
-    this.cancelBtn  =  app.CreateButton("Cancel");   +
-    this.cancelBtn.parent  =  self;   +
-    this.cancelBtn.SetOnTouch(FolderPicker_Select);   +
-    vlay.AddChild(this.cancelBtn);  +
  
-       +  this.dlg.AddLayout(this.lay);
-    this.lay.AddChild(vlay)+
  
-       this.dlg.AddLayout(this.lay);+  this.Show = function(){ 
 +    self.dlg.Show(); 
 +  } 
 +  this.SetHideFiles=function(val)
 +    if(val == undefined) val = true; 
 +    self.hideHiddenFiles = val; 
 +  } 
 +  this.Hide = function(){ 
 +    self.dlg.Hide(); 
 +  } 
 +  this.GetFolder = function(){ 
 +    return self.FolderPath; 
 +  } 
 +  this.SetFolder = function(folderPath){ 
 +    self.FolderPath = folderPath; 
 +    self.dlg.SetTitle(folderPath); 
 +    app.ShowProgress( "Loading..." ); 
 +    var lst = app.ListFolder(folderPath); 
 +    lst.sort(function(x,y){return (x.toLowerCase() > y.toLowerCase())?1:-1}); 
 +    self.lstFolds.SetList(""); 
 +    if( self.FolderPath != self.basePath ) 
 +      self.lstFolds.AddItem("..",null,"folder"); 
 +    var ths = lst.shift(); 
 +    while (undefined != ths) { 
 +      if ((! self.hideHiddenFiles) || (ths.indexOf(".") != 0)){ 
 +        var pth = folderPath + "/" + ths; 
 +        if (app.IsFolder(pth)) 
 +           self.lstFolds.AddItem(ths,null,"folder"
 +      } 
 +      ths = lst.shift(); 
 +    } 
 +     
 +    app.HideProgress(); 
 +  }   
 +}//function FolderPicker()
  
-       +function FolderPicker_NewFolder(fil){ 
-    this.Show  =   function() +  var par = this.parent
-        +  var pth par.GetFolder(); 
-      self.dlg.Show();   +  if (fil != "..") { 
-    }   +      pth += "/+ fil 
-    this.SetHideFiles = function(val) +  } 
-    {     +  else{ 
-      if (val  ==  undefined)  val  =  true    +     ifpth == par.basePath || pth ="/" )
-      self.hideHiddenFiles  =  val;   +       par.Hide() 
-    }   +       return
-    this.Hide    function() +     } 
-    {     +     var tst = pth.split("/"); 
-      self.dlg.Hide();   +     tmp tst.pop(); 
-    }   +     pth = (tst.join("/")); 
-    this.GetFolder  =   function(+  }   
-    {     +  par.SetFolder(pth); 
-      return  self.FolderPath;   +}//function FolderPicker_NewFolder()
-    }   +
-    this.SetFolder    function(folderPath) +
-    {     +
-      self.FolderPath  =  folderPath;     +
-      self.dlg.SetTitle(folderPath);     +
-      app.ShowProgress( "Loading..." );     +
-      var  lst  =  app.ListFolder(folderPath);     +
-      lst.sort(function(x, y) +
-      +
-        return  (x.toLowerCase()  >  y.toLowerCase()) ? 1 : -1 +
-      });     //var dirlist=[]; +
-           +
-      self.lstFolds.SetList("");     +
-      if ( self.FolderPath  !=  self.basePath )       self.lstFolds.AddItem("..", null, "folder");     +
-      var  ths  =  lst.shift();     +
-      while  (undefined  ! ths)  +
-      {       +
-        if  ((! self.hideHiddenFiles)  ||  (ths.indexOf(".")  !=  0)) +
-        {         +
-          var  pth  =  folderPath  +  "/"  +  ths;         //if (app.IsFolder(pth))dirlist.push(ths); +
-                   +
-          if  (app.IsFolder(pth))            self.lstFolds.AddItem(ths, null, "folder")       +
-        }       +
-        ths   lst.shift();     +
-      }        // self.lstFolds.SetList(dirlist.join(",")); +
-           +
-      app.HideProgress();   +
-    }   +
-  } //function FolderPicker()+
  
-function  FolderPicker_NewFolder(fil) +function FolderPicker_Select(fil){ 
-  {   +  var par = this.parent; 
-    var  par   this.parent;   +  var pth = par.GetFolder(); 
-    var  pth   par.GetFolder();   +  par.Hide(); 
-    if  (fil  !=  "..")  +  if(this.GetText() ="Ok") par.callback(pth ); 
-    {       +}//function FolderPicker_Select()
-      pth  +=  "/"  +  fil   +
-    }   +
-    else +
-    {      +
-      if ( pth  ==  par.basePath  ||  pth  ==  "/" ) +
-      {        +
-        par.Hide()        return     +
-      }      +
-      var  tst  =  pth.split("/");      +
-      tmp  =  tst.pop();      +
-      pth    (tst.join("/"));   +
-    }     +
-    par.SetFolder(pth); +
-  } //function FolderPicker_NewFolder()+
  
-function  FolderPicker_Select(fil) 
-  {   
-    var  par  =  this.parent;   
-    var  pth  =  par.GetFolder();   
-    par.Hide();   
-    if (this.GetText()  ==  "Ok")  par.callback(pth ); 
-  } //function FolderPicker_Select() 
 </code> </code>
sample_code/folder_picker.1417900408.txt.gz · Last modified: 2014/12/07 05:13 (external edit)