This shows you the differences between two versions of the page.
sample_code:file_picker [2015/03/26 20:03] stevegarman [The code] fix small bug |
sample_code:file_picker [2015/03/27 04:03] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Sample File Picker ====== | ||
- | 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. | ||
- | |||
- | Thanks to Tony Jones for testing it on DroidScript v1.12 and confirming that it works. | ||
- | |||
- | See also [[sample_code: | ||
- | ===== The code ===== | ||
- | <code javascript filepicker.js> | ||
- | |||
- | // | ||
- | function OnStart(){ | ||
- | // | ||
- | lay = app.CreateLayout( " | ||
- | |||
- | // | ||
- | btn = app.CreateButton( " | ||
- | lay.AddChild( btn ); | ||
- | btn.SetOnTouch(btn_OnTouch); | ||
- | pick = new FilePicker(mycallback); | ||
- | // | ||
- | pick.SetHideFiles(true); | ||
- | // | ||
- | app.AddLayout( lay ); | ||
- | }// | ||
- | |||
- | |||
- | function btn_OnTouch(){ | ||
- | pick.SetFolder("/ | ||
- | pick.Show(); | ||
- | }// | ||
- | |||
- | function mycallback(fullpath){ | ||
- | app.Alert(" | ||
- | }// | ||
- | |||
- | function FilePicker(Callback, | ||
- | var self = this; | ||
- | this.basePath = basePath || "/ | ||
- | this.callback = Callback || function(){}; | ||
- | this.FolderPath = this.basePath; | ||
- | this.hideHiddenFiles = false; | ||
- | 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.SetHideFiles=function(val){ | ||
- | if(val == undefined) val = true; | ||
- | self.hideHiddenFiles = val; | ||
- | } | ||
- | 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 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(" | ||
- | var pth = folderPath + "/" | ||
- | if (app.IsFolder(pth)) | ||
- | self.lstFolds.AddItem(ths, | ||
- | else | ||
- | self.lstFiles.AddItem(ths) | ||
- | } | ||
- | ths = lst.shift(); | ||
- | } | ||
- | app.HideProgress(); | ||
- | } | ||
- | }// | ||
- | |||
- | function FilePicker_NewFolder(fil){ | ||
- | var par = this.parent; | ||
- | var pth = par.GetFolder(); | ||
- | if (fil != " | ||
- | 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 += "/" | ||
- | }// | ||
- | |||
- | </ | ||
- | |||
- | ===== 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. |