This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
sample_code:file_picker [2014/11/22 14:33] stevegarman |
sample_code:file_picker [2015/02/19 16:06] stevegarman [The code] |
||
---|---|---|---|
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 extensions. | 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 |
+ | |||
+ | See also [[sample_code: | ||
===== The code ===== | ===== The code ===== | ||
- | < | + | < |
- | //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( " |
- | | + | //Create a text label and add it to layout. |
- | | + | btn = app.CreateButton( " |
- | lay.AddChild( | + | lay.AddChild( btn ); |
- | btn.SetOnTouch(btn_OnTouch); | + | btn.SetOnTouch(btn_OnTouch); |
- | + | pick = new FilePicker(mycallback); | |
- | //Add layout to app. | + | // don' |
- | app.AddLayout( | + | pick.SetHideFiles(true); |
- | }//function Onstart() | + | // |
+ | app.AddLayout( lay ); | ||
+ | }//function Onstart() | ||
- | function btn_OnTouch(){ | + | function btn_OnTouch(){ |
- | pick = new FilePicker(mycallback); | + | pick.SetFolder("/ |
- | | + | pick.Show(); |
- | pick.Show(); | + | }//function btn_OnTouch() |
- | }//function btn_OnTouch() | + | |
- | function mycallback(fullpath){ | + | function mycallback(fullpath){ |
- | app.Alert(" | + | app.Alert(" |
- | }//function mycallback() | + | }//function mycallback() |
- | function FilePicker(Callback, | + | function FilePicker(Callback, |
- | var self = this; | + | var self = this; |
- | this.basePath | + | this.basePath = basePath || "/ |
- | this.callback | + | this.callback = Callback || function(){}; |
- | this.FolderPath | + | this.FolderPath = this.basePath; |
- | this.dlg = app.CreateDialog(this.basePath); | + | this.hideHiddenFiles = false; |
- | this.lay = app.CreateLayout( " | + | this.dlg = app.CreateDialog(this.basePath); |
- | this.lstFolds | + | this.lay = app.CreateLayout( " |
- | this.lstFolds.parent | + | this.lstFolds = app.CreateList(" |
- | this.lstFolds.SetOnTouch(FilePicker_NewFolder); | + | this.lstFolds.parent = self; |
- | this.lay.AddChild(this.lstFolds); | + | this.lstFolds.SetOnTouch(FilePicker_NewFolder); |
- | this.lstFiles | + | this.lay.AddChild(this.lstFolds); |
- | this.lstFiles.parent | + | this.lstFiles = app.CreateList(" |
- | 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(){ |
- | 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 | + | } |
- | self.fileFilter | + | this.Hide = function(){ |
- | } | + | self.dlg.Hide(); |
- | this.GetFolder | + | } |
- | | + | this.SetFilter = function(filter){ |
- | } | + | self.fileFilter = filter; |
- | this.SetFolder | + | } |
- | self.FolderPath | + | this.GetFolder = function(){ |
- | self.dlg.SetTitle(folderPath); | + | return self.FolderPath; |
- | app.ShowProgress( " | + | } |
- | var lst = app.ListFolder(folderPath); | + | this.SetFolder = function(folderPath){ |
- | lst.sort(function(x, | + | self.FolderPath = folderPath; |
- | var dirlist=[]; | + | self.dlg.SetTitle(folderPath); |
- | if( self.FolderPath | + | app.ShowProgress( " |
- | var fillist = []; | + | var lst = app.ListFolder(folderPath); |
- | var ths = lst.shift(); | + | lst.sort(function(x, |
- | | + | var ths = lst.shift(); |
- | var pth = folderPath | + | self.lstFolds.SetList("" |
- | | + | if( self.FolderPath != self.basePath ) |
- | dirlist.push(ths); | + | self.lstFolds.AddItem(" |
- | else | + | self.lstFiles.SetList("" |
- | fillist.push(ths); | + | while (undefined != ths) { |
- | | + | if ((! self.hideHiddenFiles) || (ths.indexOf(" |
- | } | + | var pth = folderPath + "/" |
- | | + | if (app.IsFolder(pth)) |
- | self.lstFiles.SetList(fillist); | + | self.lstFolds.AddItem(ths, |
- | | + | 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 != " |
- | | + | pth += "/" |
- | } | + | } |
- | else{ | + | else{ |
- | | + | if( pth == par.basePath || pth == "/" |
- | | + | par.Hide() |
- | | + | return; |
- | | + | } |
- | var tst = pth.split("/" | + | var tst = pth.split("/" |
- | tmp = tst.pop(); | + | tmp = tst.pop(); |
- | pth = (tst.join("/" | + | pth = (tst.join("/" |
- | } | + | } |
- | + | ||
- | | + | 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 += "/" | + | par.callback(pth += "/" |
- | }//function FilePicker_NewFile() | + | }//function FilePicker_NewFile() |
</ | </ | ||
- | ==== How to use it ==== | + | ===== How to use it ===== |
You need three function from the code above | You need three function from the code above | ||
| |