This shows you the differences between two versions of the page.
sample_code:file_picker [2014/11/22 12:49] stevegarman spelling |
sample_code:file_picker [2015/03/27 04:03] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Sample File Picker ====== | ||
- | This is a simple file choosing dialog which has not yet been proerly tested 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. | ||
- | |||
- | In the meantime, I would be grateful if someone would test it on DroidScript v1.12 and comment on wherther it works. | ||
- | ===== The code ===== | ||
- | <code js filepicker.js> | ||
- | |||
- | //Called when application is started | ||
- | function OnStart(){ | ||
- | //Create a layout with objects vertically centered. | ||
- | lay = app.CreateLayout( " | ||
- | |||
- | //Create a text label and add it to layout. | ||
- | btn = app.CreateButton( " | ||
- | lay.AddChild( btn ); | ||
- | btn.SetOnTouch(btn_OnTouch); | ||
- | | ||
- | //Add layout to app. | ||
- | app.AddLayout( lay ); | ||
- | }//function Onstart() | ||
- | |||
- | |||
- | function btn_OnTouch(){ | ||
- | pick = new FilePicker(mycallback); | ||
- | pick.SetFolder("/ | ||
- | pick.Show(); | ||
- | }//function btn_OnTouch() | ||
- | |||
- | function mycallback(fullpath){ | ||
- | app.Alert(" | ||
- | }//function mycallback() | ||
- | |||
- | function FilePicker(Callback, | ||
- | var self = this; | ||
- | this.basePath = basePath || "/ | ||
- | this.callback = Callback || function(){}; | ||
- | this.FolderPath = this.basePath; | ||
- | this.dlg = app.CreateDialog(this.basePath); | ||
- | this.lay = app.CreateLayout( " | ||
- | this.lstFolds = app.CreateList(" | ||
- | 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(){ | ||
- | self.dlg.Show(); | ||
- | } | ||
- | this.Hide = function(){ | ||
- | self.dlg.Hide(); | ||
- | } | ||
- | this.SetFilter = function(filter){ | ||
- | self.fileFilter = filter; | ||
- | } | ||
- | this.GetFolder = function(){ | ||
- | return self.FolderPath; | ||
- | } | ||
- | this.SetFolder = function(folderPath){ | ||
- | self.FolderPath = folderPath; | ||
- | self.dlg.SetTitle(folderPath); | ||
- | app.ShowProgress( " | ||
- | var lst = app.ListFolder(folderPath); | ||
- | lst.sort(function(x, | ||
- | var dirlist=[]; | ||
- | if( self.FolderPath != self.basePath ) dirlist = [" | ||
- | var fillist = []; | ||
- | var ths = lst.shift(); | ||
- | while (undefined != ths) { | ||
- | var pth = folderPath + "/" | ||
- | if (app.IsFolder(pth)) | ||
- | | ||
- | else | ||
- | | ||
- | ths = lst.shift(); | ||
- | } | ||
- | self.lstFolds.SetList(dirlist); | ||
- | self.lstFiles.SetList(fillist); | ||
- | app.HideProgress(); | ||
- | } | ||
- | }//function FilePicker() | ||
- | |||
- | function FilePicker_NewFolder(fil){ | ||
- | var par = this.parent; | ||
- | var pth = par.GetFolder(); | ||
- | if (fil != " | ||
- | pth += "/" | ||
- | } | ||
- | else{ | ||
- | if( pth == par.basePath || pth == "/" | ||
- | | ||
- | | ||
- | } | ||
- | var tst = pth.split("/" | ||
- | tmp = tst.pop(); | ||
- | pth = (tst.join("/" | ||
- | } | ||
- | | ||
- | this.parent.SetFolder(pth); | ||
- | }//function FilePicker_NewFolder() | ||
- | |||
- | function FilePicker_NewFile(fil){ | ||
- | var par = this.parent; | ||
- | var pth = par.GetFolder(); | ||
- | par.callback(pth += "/" | ||
- | }//function FilePicker_NewFile() | ||
- | |||
- | </ |