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/02/19 16:06]
stevegarman [The 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 +//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+    //Create a text label and add it to layout
-    btn = app.CreateButton( "FilePicker demo" ); +    btn = app.CreateButton( "FilePicker demo" ); 
-    lay.AddChild( btn ); +    lay.AddChild( btn ); 
-    btn.SetOnTouch(btn_OnTouch); +    btn.SetOnTouch(btn_OnTouch); 
-     +    pick = new FilePicker(mycallback); 
-    //Add layout to app    +    // don't show hidden files 
-    app.AddLayout( lay ); +    pick.SetHideFiles(true); 
-}//function Onstart()+    //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) +  app.Alert("user chose " + fullpath) 
-}//function mycallback()+}//function mycallback()
  
-function FilePicker(Callback,basePath){ +function FilePicker(Callback,basePath){ 
-  var self = this; +  var self = this; 
-  this.basePath basePath || "/sdcard"; +  this.basePath basePath || "/sdcard"; 
-  this.callback Callback || function(){}; +  this.callback Callback || function(){}; 
-  this.FolderPath = this.basePath; +  this.FolderPath = this.basePath; 
-  this.dlg = app.CreateDialog(this.basePath); +  this.hideHiddenFiles = false; 
-  this.lay = app.CreateLayout( "linear", "horizontal,fillxy,left" ); +  this.dlg = app.CreateDialog(this.basePath); 
-  this.lstFolds = app.CreateList("blank" , 0.35, 0.); +  this.lay = app.CreateLayout( "linear", "horizontal,fillxy,left" ); 
-  this.lstFolds.parent = self; +  this.lstFolds = app.CreateList("blank" , 0.35, 0.); 
-  this.lstFolds.SetOnTouch(FilePicker_NewFolder);   +  this.lstFolds.parent = self; 
-  this.lay.AddChild(this.lstFolds); +  this.lstFolds.SetOnTouch(FilePicker_NewFolder);   
-  this.lstFiles = app.CreateList("blank" , 0.35, 0.); +  this.lay.AddChild(this.lstFolds); 
-  this.lstFiles.parent = self; +  this.lstFiles = app.CreateList("blank" , 0.35, 0.); 
-  this.lstFiles.SetOnTouch(FilePicker_NewFile);  +  this.lstFiles.parent = self; 
-  this.lay.AddChild(this.lstFiles); +  this.lstFiles.SetOnTouch(FilePicker_NewFile);  
-  this.dlg.AddLayout(this.lay);+  this.lay.AddChild(this.lstFiles); 
 +  this.dlg.AddLayout(this.lay);
  
-  this.Show = function(){ +  this.Show = function(){ 
-    self.dlg.Show(); +    self.dlg.Show(); 
-  +  
-  this.Hide = function(){ +  this.SetHideFiles=function(val){ 
-    self.dlg.Hide(); +    if(val == undefined) val = true; 
-  +    self.hideHiddenFiles = val; 
-  this.SetFilter = function(filter){ +  } 
-      self.fileFilter = filter; +  this.Hide = function(){ 
-  +    self.dlg.Hide(); 
-  this.GetFolder = function(){ +  
-    return self.FolderPath; +  this.SetFilter = function(filter){ 
-  +      self.fileFilter = filter; 
-  this.SetFolder = function(folderPath){ +  
-    self.FolderPath = folderPath; +  this.GetFolder = function(){ 
-    self.dlg.SetTitle(folderPath); +    return self.FolderPath; 
-    app.ShowProgress( "Loading..." ); +  
-    var lst = app.ListFolder(folderPath); +  this.SetFolder = function(folderPath){ 
-    lst.sort(function(x,y){return (x.toLowerCase() > y.toLowerCase())?1:-1}); +    self.FolderPath = folderPath; 
-    var dirlist=[]+    self.dlg.SetTitle(folderPath); 
-    if( self.FolderPath != self.basePath dirlist = [".."]+    app.ShowProgress( "Loading..." ); 
-    var fillist = []; +    var lst = app.ListFolder(folderPath); 
-    var ths = lst.shift(); +    lst.sort(function(x,y){return (x.toLowerCase() > y.toLowerCase())?1:-1}); 
-    while (undefined != ths) { +    var ths lst.shift()
-      var pth folderPath + "/" + ths; +    self.lstFolds.SetList(""); 
-      if (app.IsFolder(pth)) +    if( self.FolderPath != self.basePath ) 
-         dirlist.push(ths); +      self.lstFolds.AddItem("..",null,"folder")
-      else +    self.lstFiles.SetList(""); 
-         fillist.push(ths); +    while (undefined != ths) { 
-      ths = lst.shift(); +      if ((! self.hideHiddenFiles) || (ths.indexOf(".") !0)){ 
-    +        var pth = folderPath + "/" + ths; 
-    self.lstFolds.SetList(dirlist); +        if (app.IsFolder(pth)) 
-    self.lstFiles.SetList(fillist); +           self.lstFolds.AddItem(ths,null,"folder"
-    app.HideProgress(); +        else 
-    +           self.lstFiles.AddItem(ths) 
-}//function FilePicker()+      } 
 +      ths = lst.shift(); 
 +    
 +    app.HideProgress(); 
 +    
 +}//function FilePicker()
  
-function FilePicker_NewFolder(fil){ +function FilePicker_NewFolder(fil){ 
-  var par = this.parent; +  var par = this.parent; 
-  var pth = par.GetFolder(); +  var pth = par.GetFolder(); 
-  if (fil != "..") { +  if (fil != "..") { 
-      pth += "/" + fil +      pth += "/" + fil 
-  +  
-  else{ +  else{ 
-     if( pth == par.basePath || pth == "/" ){ +     if( pth == par.basePath || pth == "/" ){ 
-       par.Hide() +       par.Hide() 
-       return; +       return; 
-     +     
-     var tst = pth.split("/"); +     var tst = pth.split("/"); 
-     tmp = tst.pop(); +     tmp = tst.pop(); 
-     pth = (tst.join("/")); +     pth = (tst.join("/")); 
-  +  
-   +   
-  this.parent.SetFolder(pth); +  par.SetFolder(pth); 
-}//function FilePicker_NewFolder()+}//function FilePicker_NewFolder()
  
-function FilePicker_NewFile(fil){ +function FilePicker_NewFile(fil){ 
-  var par = this.parent; +  var par = this.parent; 
-  var pth = par.GetFolder(); +  var pth = par.GetFolder(); 
-  par.Hide(); +  par.Hide(); 
-  par.callback(pth += "/" + fil); +  par.callback(pth += "/" + fil); 
-}//function FilePicker_NewFile()+}//function FilePicker_NewFile()
  
 </code> </code>
sample_code/file_picker.txt · Last modified: 2015/03/27 04:03 (external edit)