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
sample_code:file_picker [2014/12/06 21:10]
stevegarman change curly brace style
sample_code:file_picker [2015/03/27 04:03] (current)
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. 
 +    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()
  
-         //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.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.hideHiddenFiles = false; 
-    this.hideHiddenFiles  =  false;   +  this.dlg = app.CreateDialog(this.basePath); 
-    this.dlg  =  app.CreateDialog(this.basePath);   +  this.lay = app.CreateLayout( "linear", "horizontal,fillxy,left" ); 
-    this.lay  =  app.CreateLayout( "linear",  "horizontal,fillxy,left" );   +  this.lstFolds = app.CreateList("blank" , 0.35, 0.7 ); 
-    this.lstFolds  =  app.CreateList("blank" ,  0.35,  0.7 );   +  this.lstFolds.parent = self; 
-    this.lstFolds.parent  =  self;   +  this.lstFolds.SetOnTouch(FilePicker_NewFolder);   
-    this.lstFolds.SetOnTouch(FilePicker_NewFolder);     +  this.lay.AddChild(this.lstFolds); 
-    this.lay.AddChild(this.lstFolds);   +  this.lstFiles = app.CreateList("blank" , 0.35, 0.7 ); 
-    this.lstFiles  =  app.CreateList("blank" ,  0.35,  0.7 );   +  this.lstFiles.parent = self; 
-    this.lstFiles.parent  =  self;   +  this.lstFiles.SetOnTouch(FilePicker_NewFile);  
-    this.lstFiles.SetOnTouch(FilePicker_NewFile);    +  this.lay.AddChild(this.lstFiles); 
-    this.lay.AddChild(this.lstFiles);   +  this.dlg.AddLayout(this.lay);
-    this.dlg.AddLayout(this.lay);+
  
-       +  this.Show = function(){ 
-    this.Show  =   function() +    self.dlg.Show(); 
-    {     +  } 
-      self.dlg.Show();   +  this.SetHideFiles=function(val){ 
-    }   +    if(val == undefined) val = true; 
-    this.SetHideFiles = function(val) +    self.hideHiddenFiles = val; 
-    {     +  } 
-      if (val  ==  undefined)  val  =  true;     +  this.Hide = function(){ 
-      self.hideHiddenFiles  =  val;   +    self.dlg.Hide(); 
-    }   +  } 
-    this.Hide  =   function() +  this.SetFilter = function(filter){ 
-    {     +      self.fileFilter = filter; 
-      self.dlg.Hide();   +  } 
-    }   +  this.GetFolder = function(){ 
-    this.SetFilter  =   function(filter) +    return self.FolderPath; 
-    {       +  } 
-      self.fileFilter  =  filter;   +  this.SetFolder = function(folderPath){ 
-    }   +    self.FolderPath = folderPath; 
-    this.GetFolder  =   function() +    self.dlg.SetTitle(folderPath); 
-    {     +    app.ShowProgress( "Loading..." ); 
-      return  self.FolderPath;   +    var lst = app.ListFolder(folderPath); 
-    }   +    lst.sort(function(x,y){return (x.toLowerCase() > y.toLowerCase())?1:-1}); 
-    this.SetFolder  =   function(folderPath) +    var ths = lst.shift(); 
-    {     +    self.lstFolds.SetList(""); 
-      self.FolderPath  =  folderPath;     +    if( self.FolderPath != self.basePath ) 
-      self.dlg.SetTitle(folderPath);     +      self.lstFolds.AddItem("..",null,"folder"); 
-      app.ShowProgress( "Loading..." );     +    self.lstFiles.SetList(""); 
-      var  lst   app.ListFolder(folderPath);     +    while (undefined != ths) { 
-      lst.sort(function(x, y) +      if ((! self.hideHiddenFiles) || (ths.indexOf(".") != 0)){ 
-      { +        var pth = folderPath + "/" + ths; 
-        return  (x.toLowerCase()   y.toLowerCase()) ? 1 : -1 +        if (app.IsFolder(pth)) 
-      });     //var dirlist=[]+           self.lstFolds.AddItem(ths,null,"folder") 
-           //var fillist []; +        else 
-           +           self.lstFiles.AddItem(ths) 
-      var  ths  =  lst.shift();     +      } 
-      self.lstFolds.SetList("");     +      ths = lst.shift(); 
-      if ( self.FolderPath  !=  self.basePath )       self.lstFolds.AddItem("..", null, "folder");     +    } 
-      self.lstFiles.SetList("");     +    app.HideProgress(); 
-      while  (undefined  !=  ths)  +  }   
-      {       +}//function FilePicker()
-        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  par   this.parent;   +  var pth = par.GetFolder(); 
-    var  pth   par.GetFolder();   +  if (fil != ".."{ 
-    if  (fil  !=  "..")  +      pth += "/" + fil 
-    {       +  } 
-      pth  +=  "/"   fil   +  else{ 
-    }   +     if( pth == par.basePath || pth == "/" ){ 
-    else +       par.Hide() 
-         +       return; 
-      if ( pth  ==  par.basePath  ||  pth  ==  "/" ) +     } 
-      {        +     var tst = pth.split("/"); 
-        par.Hide()        return;      +     tmp = tst.pop(); 
-      }      +     pth = (tst.join("/")); 
-      var  tst   pth.split("/");      +     if(pth=="") pth = "/"; 
-      tmp  =  tst.pop();      +  
-      pth  =   (tst.join("/"));   +   
-        +  par.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.1417900250.txt.gz · Last modified: 2014/12/07 05:10 (external edit)