This shows you the differences between two versions of the page.
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 | + | 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 | + | Thanks to Tony Jones for testing |
- | <code js filepicker.js> | + | |
- | //Called when application is started | + | See also [[sample_code: |
- | function OnStart(){ | + | ===== The code ===== |
- | //Create a layout with objects vertically centered. | + | <code javascript filepicker.js> |
- | lay = app.CreateLayout( " | + | |
- | | + | //Called when application is started |
- | btn = app.CreateButton( " | + | function OnStart(){ |
- | lay.AddChild( btn ); | + | //Create a layout with objects vertically centered. |
- | btn.SetOnTouch(btn_OnTouch); | + | lay = app.CreateLayout( " |
- | | + | |
- | //Add layout to app. | + | |
- | app.AddLayout( lay ); | + | |
- | }//function Onstart() | + | |
+ | // | ||
+ | btn = app.CreateButton( " | ||
+ | lay.AddChild( btn ); | ||
+ | btn.SetOnTouch(btn_OnTouch); | ||
+ | pick = new FilePicker(mycallback); | ||
+ | // | ||
+ | pick.SetHideFiles(true); | ||
+ | // | ||
+ | app.AddLayout( lay ); | ||
+ | }// | ||
- | function btn_OnTouch(){ | ||
- | pick = new FilePicker(mycallback); | ||
- | pick.SetFolder("/ | ||
- | pick.Show(); | ||
- | }//function btn_OnTouch() | ||
- | function mycallback(fullpath){ | + | function btn_OnTouch(){ |
- | app.Alert("user chose " | + | pick.SetFolder("/sdcard"); |
- | }//function mycallback() | + | pick.Show(); |
+ | }//function btn_OnTouch() | ||
- | function FilePicker(Callback, | + | function mycallback(fullpath){ |
- | var self = this; | + | app.Alert("user chose " + fullpath) |
- | this.basePath = basePath || "/ | + | }// |
- | this.callback = Callback || function(){}; | + | |
- | this.FolderPath = this.basePath; | + | |
- | this.dlg = app.CreateDialog(this.basePath); | + | |
- | this.lay = app.CreateLayout( "linear", | + | |
- | | + | |
- | this.lstFolds.parent = self; | + | |
- | this.lstFolds.SetOnTouch(FilePicker_NewFolder); | + | |
- | this.lay.AddChild(this.lstFolds); | + | |
- | this.lstFiles = app.CreateList(" | + | |
- | 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, |
- | | + | var self = this; |
- | } | + | this.basePath = basePath || "/ |
- | | + | this.callback = Callback || function(){}; |
- | self.dlg.Hide(); | + | this.FolderPath = this.basePath; |
- | } | + | this.hideHiddenFiles = false; |
- | | + | this.dlg = app.CreateDialog(this.basePath); |
- | self.fileFilter = filter; | + | this.lay = app.CreateLayout( "linear", |
- | } | + | this.lstFolds = app.CreateList(" |
- | | + | this.lstFolds.parent = self; |
- | return self.FolderPath; | + | this.lstFolds.SetOnTouch(FilePicker_NewFolder); |
- | } | + | this.lay.AddChild(this.lstFolds); |
- | | + | this.lstFiles = app.CreateList(" |
- | self.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())? | + | |
- | var dirlist=[]; | + | |
- | if( self.FolderPath != self.basePath | + | |
- | var fillist = []; | + | |
- | var ths = lst.shift(); | + | |
- | while (undefined != ths) { | + | |
- | var pth = folderPath + "/" | + | |
- | if (app.IsFolder(pth)) | + | |
- | | + | |
- | | + | |
- | | + | |
- | ths = lst.shift(); | + | |
- | } | + | |
- | self.lstFolds.SetList(dirlist); | + | |
- | self.lstFiles.SetList(fillist); | + | |
- | app.HideProgress(); | + | |
- | } | + | |
- | }//function FilePicker() | + | |
- | function | + | this.Show = function(){ |
- | var par = this.parent; | + | self.dlg.Show(); |
- | var pth = par.GetFolder(); | + | } |
- | if (fil != "..") { | + | this.SetHideFiles=function(val){ |
- | pth += "/" | + | if(val == undefined) val = true; |
- | } | + | self.hideHiddenFiles = val; |
- | else{ | + | } |
- | if( pth == par.basePath || pth == "/" ){ | + | this.Hide = function(){ |
- | par.Hide() | + | self.dlg.Hide(); |
- | | + | } |
- | | + | 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, | ||
+ | var ths = lst.shift(); | ||
+ | self.lstFolds.SetList("" | ||
+ | if( self.FolderPath != self.basePath ) | ||
+ | self.lstFolds.AddItem(" | ||
+ | self.lstFiles.SetList("" | ||
+ | while (undefined != ths) { | ||
+ | if ((! self.hideHiddenFiles) || (ths.indexOf(".") != 0)){ | ||
+ | var pth = folderPath + "/" | ||
+ | if (app.IsFolder(pth)) | ||
+ | self.lstFolds.AddItem(ths, | ||
+ | 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(); |
- | par.callback(pth += "/" | + | if (fil != " |
- | }//function FilePicker_NewFile() | + | pth += "/" |
+ | } | ||
+ | else{ | ||
+ | if( pth == par.basePath || pth == "/" | ||
+ | par.Hide() | ||
+ | return; | ||
+ | } | ||
+ | var tst = pth.split("/" | ||
+ | tmp = tst.pop(); | ||
+ | pth = (tst.join("/" | ||
+ | | ||
+ | } | ||
+ | |||
+ | par.SetFolder(pth); | ||
+ | }// | ||
+ | |||
+ | function FilePicker_NewFile(fil){ | ||
+ | var par = this.parent; | ||
+ | var pth = par.GetFolder(); | ||
+ | par.Hide(); | ||
+ | par.callback(pth += "/" | ||
+ | }//function FilePicker_NewFile() | ||
</ | </ | ||
+ | |||
+ | ===== How to use it ===== | ||
+ | You need three function from the code above | ||
+ | | ||
+ | < | ||
+ | |||
+ | < | ||
+ | |||
+ | < | ||
+ | | ||
+ | | ||
+ | In your main code, you need something like | ||
+ | |||
+ | < | ||
+ | pick.SetFolder("/ | ||
+ | pick.Show();</ | ||
+ | |||
+ | where myCallBack is a function which is called when a file is chosen. It is defined like | ||
+ | |||
+ | < | ||
+ | app.ShowPopup(" | ||
+ | }</ | ||
+ | |||
+ | and it passes you the full path of the file that is chosen. |