User Tools

Site Tools


sample_code:folder_picker

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

sample_code:folder_picker [2014/12/06 21:13]
stevegarman
sample_code:folder_picker [2015/02/19 23:17]
Line 1: Line 1:
-====== Sample folder picker ====== 
-This is a simple dialog to allow a user to choose a file folder. 
  
-See also [[sample_code:file_picker|Sample File Picker]] 
-===== The code ===== 
-<code javascript folderpicker.js> 
-//Called when application is started 
-function  OnStart() 
-  {     //Create a layout with objects vertically centered. 
-         
-    lay  =  app.CreateLayout( "linear",  "Vertical,FillXY" );     
- 
-         //Create a text label and add it to layout. 
-         
-    btn  =  app.CreateButton( "FolderPicker demo" );     
-    lay.AddChild( btn );     
-    btn.SetOnTouch(btn_OnTouch);         //Add layout to app.     
-         
-    app.AddLayout( lay );     
-    pick  =  new  FolderPicker(mycallback);     // don't show hidden files 
-         
-    pick.SetHideFiles(true); 
-  } //function Onstart() 
- 
-function  btn_OnTouch() 
-  {     
-    pick.SetFolder("/sdcard");     
-    pick.Show();     
-  } //function btn_OnTouch() 
- 
-function  mycallback(fullpath) 
-  {   
-    app.Alert("user chose "  +  fullpath) 
-  } //function mycallback() 
- 
-function  FolderPicker(Callback, basePath) 
-  {   
-    var  self  =  this;   
-    this.basePath  =  basePath  ||  "/sdcard";   
-    this.callback  =  Callback  ||   function() {};   
-    this.FolderPath  =  this.basePath;   
-    this.hideHiddenFiles  =  false;   
-    this.dlg  =  app.CreateDialog(this.basePath);   
-    this.lay  =  app.CreateLayout( "linear",  "horizontal,fillxy" );     
-    this.lstFolds  =  app.CreateList("blank" ,  0.7,  0.7 );   
-    this.lstFolds.parent  =  self;   
-    this.lstFolds.SetOnTouch(FolderPicker_NewFolder);     
-    this.lay.AddChild(this.lstFolds); 
- 
-       
-    var  vlay  =  app.CreateLayout( "linear",  "vertical,fillxy" );   
-    this.okBtn  =  app.CreateButton("Ok");   
-    this.okBtn.parent  =  self;   
-    this.okBtn.SetOnTouch(FolderPicker_Select);   
-    vlay.AddChild(this.okBtn);     
-    this.cancelBtn  =  app.CreateButton("Cancel");   
-    this.cancelBtn.parent  =  self;   
-    this.cancelBtn.SetOnTouch(FolderPicker_Select);   
-    vlay.AddChild(this.cancelBtn);   
- 
-       
-    this.lay.AddChild(vlay) 
- 
-       this.dlg.AddLayout(this.lay); 
- 
-       
-    this.Show  =   function() 
-    {     
-      self.dlg.Show();   
-    }   
-    this.SetHideFiles = function(val) 
-    {     
-      if (val  ==  undefined)  val  =  true;     
-      self.hideHiddenFiles  =  val;   
-    }   
-    this.Hide  =   function() 
-    {     
-      self.dlg.Hide();   
-    }   
-    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=[]; 
-           
-      self.lstFolds.SetList("");     
-      if ( self.FolderPath  !=  self.basePath )       self.lstFolds.AddItem("..", null, "folder");     
-      var  ths  =  lst.shift();     
-      while  (undefined  !=  ths)  
-      {       
-        if  ((! self.hideHiddenFiles)  ||  (ths.indexOf(".")  !=  0)) 
-        {         
-          var  pth  =  folderPath  +  "/"  +  ths;         //if (app.IsFolder(pth))dirlist.push(ths); 
-                   
-          if  (app.IsFolder(pth))            self.lstFolds.AddItem(ths, null, "folder")       
-        }       
-        ths  =  lst.shift();     
-      }        // self.lstFolds.SetList(dirlist.join(",")); 
-           
-      app.HideProgress();   
-    }   
-  } //function FolderPicker() 
- 
-function  FolderPicker_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("/"));   
-    }     
-    par.SetFolder(pth); 
-  } //function FolderPicker_NewFolder() 
- 
-function  FolderPicker_Select(fil) 
-  {   
-    var  par  =  this.parent;   
-    var  pth  =  par.GetFolder();   
-    par.Hide();   
-    if (this.GetText()  ==  "Ok")  par.callback(pth ); 
-  } //function FolderPicker_Select() 
-</code> 
sample_code/folder_picker.txt · Last modified: 2015/02/19 23:17 (external edit)