User Tools

Site Tools


sample_code:file_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
Next revision Both sides next revision
sample_code:file_picker [2014/11/22 14:38]
stevegarman update status
sample_code:file_picker [2015/01/25 13:26]
octazid [The code] Display as colored code
Line 5: Line 5:
  
 Thanks to Tony Jones for testing it on DroidScript v1.12 and confirming that it works. Thanks to Tony Jones for testing it on DroidScript v1.12 and confirming that it works.
 +
 +See also [[sample_code:folder_picker|Sample Folder Picker]]
 ===== The code ===== ===== The code =====
-<code js filepicker.js> +<code javascript filepicker.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( "FilePicker demo" ); +
-    lay.AddChild( btn ); +
-    btn.SetOnTouch(btn_OnTouch); +
-     +
-    //Add layout to app.     +
-    app.AddLayout( lay ); +
-}//function Onstart()+
  
 +         //Create a text label and add it to layout.
 +        
 +    btn  =  app.CreateButton( "FilePicker demo" );    
 +    lay.AddChild( btn );    
 +    btn.SetOnTouch(btn_OnTouch);    
 +    pick  =  new  FilePicker(mycallback);     // don't show hidden files
 +        
 +    pick.SetHideFiles(true);     //Add layout to app.    
 +        
 +    app.AddLayout( lay );
 +  } //function Onstart()
  
-function btn_OnTouch(){ +function  btn_OnTouch() 
-    pick = new FilePicker(mycallback); +      
-    pick.SetFolder("/sdcard"); +    pick.SetFolder("/sdcard");     
-    pick.Show();     +    pick.Show();     
-}//function btn_OnTouch()+  } //function btn_OnTouch()
  
-function mycallback(fullpath){ +function  mycallback(fullpath) 
-    app.Alert("user chose " + fullpath) +    
-}//function mycallback()+    app.Alert("user chose   fullpath) 
 +  } //function mycallback()
  
-function FilePicker(Callback,basePath){ +function  FilePicker(Callback, basePath) 
-  var self = this; +    
-  this.basePath basePath || "/sdcard"; +    var  self   this;   
-  this.callback Callback || function(){}; +    this.basePath   basePath  ||  "/sdcard";   
-  this.FolderPath = this.basePath; +    this.callback   Callback  ||   function() {};   
-  this.dlg = app.CreateDialog(this.basePath); +    this.FolderPath   this.basePath;   
-  this.lay = app.CreateLayout( "linear", "horizontal,fillxy,left" ); +    this.hideHiddenFiles   false;   
-  this.lstFolds = app.CreateList("blank" , 0.35, 0.); +    this.dlg  =  app.CreateDialog(this.basePath);   
-  this.lstFolds.parent = self; +    this.lay   app.CreateLayout( "linear",  "horizontal,fillxy,left" );   
-  this.lstFolds.SetOnTouch(FilePicker_NewFolder);   +    this.lstFolds   app.CreateList("blank" ,  0.35,  0.);   
-  this.lay.AddChild(this.lstFolds); +    this.lstFolds.parent   self;   
-  this.lstFiles = app.CreateList("blank" , 0.35, 0.); +    this.lstFolds.SetOnTouch(FilePicker_NewFolder);     
-  this.lstFiles.parent = self; +    this.lay.AddChild(this.lstFolds);   
-  this.lstFiles.SetOnTouch(FilePicker_NewFile);  +    this.lstFiles   app.CreateList("blank" ,  0.35,  0.);   
-  this.lay.AddChild(this.lstFiles); +    this.lstFiles.parent   self;   
-  this.dlg.AddLayout(this.lay);+    this.lstFiles.SetOnTouch(FilePicker_NewFile);    
 +    this.lay.AddChild(this.lstFiles);   
 +    this.dlg.AddLayout(this.lay);
  
-  this.Show = function(){ +       
-    self.dlg.Show(); +    this.Show    function() 
-  +    {     
-  this.Hide = function(){ +      self.dlg.Show();   
-    self.dlg.Hide(); +      
-  +    this.SetHideFiles = function(val) 
-  this.SetFilter = function(filter){ +        
-      self.fileFilter = filter; +      if (val  ==  undefined)  val  =  true;     
-  +      self.hideHiddenFiles  =  val;   
-  this.GetFolder = function(){ +    }   
-    return self.FolderPath; +    this.Hide  =   function() 
-  +    {     
-  this.SetFolder = function(folderPath){ +      self.dlg.Hide();   
-    self.FolderPath = folderPath; +      
-    self.dlg.SetTitle(folderPath); +    this.SetFilter    function(filter) 
-    app.ShowProgress( "Loading..." ); +          
-    var lst = app.ListFolder(folderPath); +      self.fileFilter   filter;   
-    lst.sort(function(x,y){return (x.toLowerCase() > y.toLowerCase())?1:-1}); +      
-    var dirlist=[]; +    this.GetFolder    function() 
-    if( self.FolderPath != self.basePath dirlist = [".."]+    {     
-    var fillist = []; +      return  self.FolderPath;   
-    var ths = lst.shift(); +      
-    while (undefined != ths) { +    this.SetFolder    function(folderPath) 
-      var pth folderPath + "/" + ths; +    {     
-      if (app.IsFolder(pth)) +      self.FolderPath   folderPath;     
-         dirlist.push(ths); +      self.dlg.SetTitle(folderPath);     
-      else +      app.ShowProgress( "Loading..." );     
-         fillist.push(ths); +      var  lst   app.ListFolder(folderPath);     
-      ths = lst.shift(); +      lst.sort(function(x, y) 
-    +      { 
-    self.lstFolds.SetList(dirlist); +        return  (x.toLowerCase()   y.toLowerCase()) ? 1 : -1 
-    self.lstFiles.SetList(fillist); +      });     //var dirlist=[]
-    app.HideProgress(); +           //var fillist = []; 
-  }   +           
-}//function FilePicker()+      var  ths  =  lst.shift();     
 +      self.lstFolds.SetList("");     
 +      if ( self.FolderPath  !=  self.basePath       self.lstFolds.AddItem("..", null, "folder")    
 +      self.lstFiles.SetList("");     
 +      while  (undefined  !=  ths)  
 +      {       
 +        if  ((! self.hideHiddenFiles)  ||  (ths.indexOf(".")  ! 0)) 
 +        {         
 +          var  pth  =  folderPath   "/"   ths;         
 +          if  (app.IsFolder(pth))            self.lstFolds.AddItem(ths, null, "folder"        
 +          else            self.lstFiles.AddItem(ths)       
 +        }       
 +        ths   lst.shift();     
 +          
 +      app.HideProgress();   
 +      
 +  } //function FilePicker()
  
-function FilePicker_NewFolder(fil){ +function  FilePicker_NewFolder(fil) 
-  var par = this.parent; +    
-  var pth = par.GetFolder(); +    var  par   this.parent;   
-  if (fil != "..") { +    var  pth   par.GetFolder();   
-      pth += "/"fil +    if  (fil  !=  ".." 
-  +          
-  else{ +      pth  +=  "/"   fil   
-     if( pth == par.basePath || pth == "/" ){ +      
-       par.Hide() +    else 
-       return; +         
-     +      if ( pth  ==  par.basePath  ||  pth  ==  "/" ) 
-     var tst = pth.split("/"); +             
-     tmp = tst.pop(); +        par.Hide()        return;      
-     pth = (tst.join("/")); +           
-  +      var  tst   pth.split("/");      
-   +      tmp   tst.pop();      
-  this.parent.SetFolder(pth); +      pth    (tst.join("/"));   
-}//function FilePicker_NewFolder()+        
 +    par.SetFolder(pth); 
 +  } //function FilePicker_NewFolder()
  
-function FilePicker_NewFile(fil){ +function  FilePicker_NewFile(fil) 
-  var par = this.parent; +  {   
-  var pth = par.GetFolder(); +    var  par   this.parent;   
-  par.Hide(); +    var  pth   par.GetFolder();   
-  par.callback(pth += "/" + fil); +    par.Hide();   
-}//function FilePicker_NewFile()+    par.callback(pth  +=  "/"   fil); 
 +  } //function FilePicker_NewFile()
  
 </code> </code>
sample_code/file_picker.txt · Last modified: 2015/03/27 04:03 (external edit)