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

Next revision
Previous revision
sample_code:file_picker [2014/11/22 12:38]
stevegarman created
sample_code:file_picker [2015/03/27 04:03] (current)
Line 1: Line 1:
 ====== Sample File Picker ====== ====== Sample File Picker ======
-This is a simple file choosing dialog which has not yet been proerly tested on DroidScript v1.12+This is a simple file choosing dialog which is offered for use on DroidScript v1.12
  
-When the next version of DroidScript is released, I will tidy it up a bit and add a filter for file ectensions.+When the next version of DroidScript is released, I will tidy it up a bit and add a filter for file extensions.
  
-In the meantime, I would be grateful if someone would test it on DroidScript v1.12 and comment on wherther it works. +Thanks to Tony Jones for testing it on DroidScript v1.12 and confirming that it works.
-<code js filepicker.js>+
  
-//Called when application is started +See also [[sample_code:folder_picker|Sample Folder Picker]] 
-function OnStart(){ +===== The code ===== 
-    //Create a layout with objects vertically centered. +<code javascript filepicker.js>
-    lay app.CreateLayout( "linear", "Vertical,FillXY" );    +
  
-    //Create a text label and add it to layout. +//Called when application is started 
-    btn = app.CreateButton"FilePicker demo" )+function OnStart(){ 
-    lay.AddChild( btn ); +    //Create a layout with objects vertically centered
-    btn.SetOnTouch(btn_OnTouch); +    lay = app.CreateLayout"linear", "Vertical,FillXY" );    
-     +
-    //Add layout to app    +
-    app.AddLayoutlay ); +
-}//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(){ 
-    pick = new FilePicker(mycallback); 
-    pick.SetFolder("/sdcard"); 
-    pick.Show();     
-}//function btn_OnTouch() 
  
-function mycallback(fullpath){ +function btn_OnTouch(){ 
-    app.Alert("user chose + fullpath+    pick.SetFolder("/sdcard")
-}//function mycallback()+    pick.Show();     
 +}//function btn_OnTouch()
  
-function FilePicker(Callback,basePath){ +function mycallback(fullpath){ 
-  var self = this; +  app.Alert("user chose + fullpath
-  this.basePath = basePath || "/sdcard"; +}//function mycallback()
-  this.callback = Callback || function(){}; +
-  this.FolderPath = this.basePath; +
-  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);+
  
-  this.Show = function(){ +function FilePicker(Callback,basePath){ 
-    self.dlg.Show()+  var self = this
-  } +  this.basePath basePath || "/sdcard"
-  this.Hide function(){ +  this.callback Callback || function(){}
-    self.dlg.Hide()+  this.FolderPath this.basePath
-  } +  this.hideHiddenFiles false; 
-  this.SetFilter = function(filter){ +  this.dlg app.CreateDialog(this.basePath); 
-      self.fileFilter = filter+  this.lay = app.CreateLayout( "linear", "horizontal,fillxy,left" ); 
-  } +  this.lstFolds = app.CreateList("blank" , 0.350.); 
-  this.GetFolder function(){ +  this.lstFolds.parent self
-    return self.FolderPath+  this.lstFolds.SetOnTouch(FilePicker_NewFolder);   
-  } +  this.lay.AddChild(this.lstFolds); 
-  this.SetFolder function(folderPath){ +  this.lstFiles = app.CreateList("blank" , 0.35, 0.7 ); 
-    self.FolderPath folderPath; +  this.lstFiles.parent = self
-    self.dlg.SetTitle(folderPath); +  this.lstFiles.SetOnTouch(FilePicker_NewFile);  
-    app.ShowProgress( "Loading..." ); +  this.lay.AddChild(this.lstFiles); 
-    var lst = app.ListFolder(folderPath); +  this.dlg.AddLayout(this.lay);
-    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){ +  this.Show = function(){ 
-  var par = this.parent+    self.dlg.Show()
-  var pth = par.GetFolder(); +  } 
-  if (fil !"..") { +  this.SetHideFiles=function(val){ 
-      pth += "/" + fil +    if(val == undefined) val = true; 
-  +    self.hideHiddenFiles = val; 
-  else+  } 
-     if( pth == par.basePath || pth == "/" ){ +  this.Hide = function(){ 
-       par.Hide() +    self.dlg.Hide(); 
-       return+  
-     +  this.SetFilter = function(filter)
-     var tst pth.split("/"); +      self.fileFilter filter; 
-     tmp tst.pop(); +  } 
-     pth = (tst.join("/")); +  this.GetFolder function(){ 
-  } +    return self.FolderPath; 
-   +  } 
-  this.parent.SetFolder(pth); +  this.SetFolder function(folderPath){ 
-}//function FilePicker_NewFolder()+    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 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_NewFile(fil){ +function FilePicker_NewFolder(fil){ 
-  var par = this.parent; +  var par = this.parent; 
-  var pth = par.GetFolder(); +  var pth = par.GetFolder(); 
-  par.callback(pth += "/" + fil); +  if (fil != "..") { 
-}//function FilePicker_NewFile()+      pth += "/" + fil 
 +  } 
 +  else{ 
 +     if( pth == par.basePath || pth == "/" ){ 
 +       par.Hide() 
 +       return; 
 +     } 
 +     var tst = pth.split("/"); 
 +     tmp = tst.pop(); 
 +     pth = (tst.join("/")); 
 +     if(pth=="") pth = "/"; 
 +  } 
 +   
 +  par.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>
 +
 +===== How to use it =====
 +You need three function from the code above
 +  
 +<code>function FilePicker</code>
 +
 +<code>function FilePicker_NewFolder</code>
 + 
 +<code>function FilePicker_NewFile</code>
 +    
 +    
 +In your main code, you need something like
 +
 +<code>    pick = new FilePicker(myCallBack);
 +    pick.SetFolder("/sdcard");
 +    pick.Show();</code>    
 +
 +where myCallBack is a function which is called when a file is chosen. It is defined like
 +
 +<code>function myCallBack(fullpath){
 +    app.ShowPopup("user chose " + fullpath)
 +}</code>
 +
 +and it passes you the full path of the file that is chosen.
sample_code/file_picker.1416659899.txt.gz · Last modified: 2014/11/22 20:38 (external edit)