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/26 12:19]
stevegarman link to folder picker
sample_code:file_picker [2015/01/25 13:26]
octazid [The code] Display as colored code
Line 8: Line 8:
 See also [[sample_code:folder_picker|Sample Folder Picker]] 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 
 +function  OnStart() 
 +  {     //Create a layout with objects vertically centered. 
 +         
 +    lay  =  app.CreateLayout( "linear",  "Vertical,FillXY" );    
  
-//Called when application is started +         //Create a text label and add it to layout. 
-function OnStart(){ +         
-    //Create a layout with objects vertically centered+    btn  =  app.CreateButton"FilePicker demo" );     
-    lay = app.CreateLayout"linear", "Vertical,FillXY" );    +    lay.AddChild( btn );     
 +    btn.SetOnTouch(btn_OnTouch);     
 +    pick  =  new  FilePicker(mycallback);     // don't show hidden files 
 +         
 +    pick.SetHideFiles(true);     //Add layout to app    
 +         
 +    app.AddLayoutlay ); 
 +  } //function Onstart()
  
-    //Create a text label and add it to layout. +function  btn_OnTouch() 
-    btn = app.CreateButton"FilePicker demo" ); +  {     
-    lay.AddChild( btn ); +    pick.SetFolder("/sdcard");     
-    btn.SetOnTouch(btn_OnTouch); +    pick.Show();     
-     +  } //function btn_OnTouch()
-    //Add layout to app.     +
-    app.AddLayoutlay ); +
-}//function Onstart()+
  
 +function  mycallback(fullpath)
 +  {  
 +    app.Alert("user chose "  +  fullpath)
 +  } //function mycallback()
  
-function btn_OnTouch(){ +function  FilePicker(Callback, basePath) 
-    pick new FilePicker(mycallback); +  {   
-    pick.SetFolder("/sdcard"); +    var  self  =  this;   
-    pick.Show();     +    this.basePath  =  basePath  ||  "/sdcard";   
-}//function btn_OnTouch()+    this.callback  =  Callback  ||   function() {};   
 +    this.FolderPath   this.basePath;   
 +    this.hideHiddenFiles  =  false;   
 +    this.dlg  =  app.CreateDialog(this.basePath);   
 +    this.lay  =  app.CreateLayout( "linear",  "horizontal,fillxy,left" );   
 +    this.lstFolds  =  app.CreateList("blank" ,  0.35,  0.7 );   
 +    this.lstFolds.parent  =  self;   
 +    this.lstFolds.SetOnTouch(FilePicker_NewFolder);     
 +    this.lay.AddChild(this.lstFolds);   
 +    this.lstFiles  =  app.CreateList("blank" ,  0.35,  0.7 );   
 +    this.lstFiles.parent  =  self;   
 +    this.lstFiles.SetOnTouch(FilePicker_NewFile);    
 +    this.lay.AddChild(this.lstFiles);   
 +    this.dlg.AddLayout(this.lay);
  
-function mycallback(fullpath){ +       
-    app.Alert("user chose " + fullpath+    this.Show  =   function() 
-}//function mycallback()+        
 +      self.dlg.Show();   
 +    }   
 +    this.SetHideFiles = function(val) 
 +    {     
 +      if (val  ==  undefined)  val  =  true;     
 +      self.hideHiddenFiles  =  val;   
 +    }   
 +    this.Hide  =   function() 
 +    {     
 +      self.dlg.Hide();   
 +    }   
 +    this.SetFilter  =   function(filter) 
 +    {       
 +      self.fileFilter  =  filter;   
 +    }   
 +    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 
 +      });     //var dirlist=[]; 
 +           //var fillist = []; 
 +           
 +      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(Callback,basePath){ +function  FilePicker_NewFolder(fil
-  var self = this; +  {   
-  this.basePath basePath || "/sdcard"; +    var  par   this.parent  
-  this.callback = Callback || function(){}+    var  pth   par.GetFolder();   
-  this.FolderPath this.basePath; +    if  (fil  ! "..")  
-  this.dlg = app.CreateDialog(this.basePath); +    {       
-  this.lay app.CreateLayout( "linear", "horizontal,fillxy,left" ); +      pth  + "/ +  fil   
-  this.lstFolds app.CreateList("blank, 0.35, 0.7 ); +    }   
-  this.lstFolds.parent = self; +    else 
-  this.lstFolds.SetOnTouch(FilePicker_NewFolder);   +    {      
-  this.lay.AddChild(this.lstFolds); +      if ( pth  ==  par.basePath  ||  pth  ==  "/" ) 
-  this.lstFiles app.CreateList("blank" , 0.35, 0.7 ); +      {        
-  this.lstFiles.parent self; +        par.Hide()        return     
-  this.lstFiles.SetOnTouch(FilePicker_NewFile);  +      }      
-  this.lay.AddChild(this.lstFiles); +      var  tst  =  pth.split("/");      
-  this.dlg.AddLayout(this.lay);+      tmp   tst.pop();      
 +      pth    (tst.join("/"));   
 +    }     
 +    par.SetFolder(pth); 
 +  } //function FilePicker_NewFolder()
  
-  this.Show = function()+function  FilePicker_NewFile(fil
-    self.dlg.Show(); +  {   
-  +    var  par   this.parent  
-  this.Hide = function()+    var  pth   par.GetFolder();   
-    self.dlg.Hide(); +    par.Hide();   
-  } +    par.callback(pth  +=  "/"   fil); 
-  this.SetFilter function(filter){ +  } //function FilePicker_NewFile()
-      self.fileFilter = filter; +
-  } +
-  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}); +
-    var dirlist=[]; +
-    if( self.FolderPath != self.basePath ) dirlist = [".."]; +
-    var fillist = []; +
-    var ths = lst.shift(); +
-    while (undefined != ths) { +
-      var pth = folderPath + "/" + ths; +
-      if (app.IsFolder(pth)) +
-         dirlist.push(ths); +
-      else +
-         fillist.push(ths); +
-      ths = lst.shift(); +
-    } +
-    self.lstFolds.SetList(dirlist); +
-    self.lstFiles.SetList(fillist); +
-    app.HideProgress(); +
-  }   +
-}//function FilePicker() +
- +
-function FilePicker_NewFolder(fil){ +
-  var par = this.parent; +
-  var pth = par.GetFolder(); +
-  if (fil != "..") { +
-      pth += "/" + fil +
-  } +
-  else{ +
-     if( pth == par.basePath || pth == "/" ){ +
-       par.Hide() +
-       return+
-     } +
-     var tst = pth.split("/"); +
-     tmp = tst.pop(); +
-     pth = (tst.join("/")); +
-  } +
-   +
-  this.parent.SetFolder(pth); +
-}//function FilePicker_NewFolder() +
- +
-function FilePicker_NewFile(fil){ +
-  var par = this.parent; +
-  var pth = par.GetFolder(); +
-  par.Hide(); +
-  par.callback(pth += "/" + fil); +
-}//function FilePicker_NewFile()+
  
 </code> </code>
sample_code/file_picker.txt · Last modified: 2015/03/27 04:03 (external edit)