This shows you the differences between two versions of the page.
sample_code:folder_picker [2014/11/26 12:25] stevegarman created |
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: | ||
- | ===== The code ===== | ||
- | |||
- | <code javascript folderpicker.js> | ||
- | |||
- | // | ||
- | function OnStart(){ | ||
- | // | ||
- | lay = app.CreateLayout( " | ||
- | |||
- | // | ||
- | btn = app.CreateButton( " | ||
- | lay.AddChild( btn ); | ||
- | btn.SetOnTouch(btn_OnTouch); | ||
- | |||
- | // | ||
- | app.AddLayout( lay ); | ||
- | }// | ||
- | |||
- | |||
- | function btn_OnTouch(){ | ||
- | pick = new FolderPicker(mycallback); | ||
- | pick.SetFolder("/ | ||
- | pick.Show(); | ||
- | }// | ||
- | |||
- | function mycallback(fullpath){ | ||
- | app.Alert(" | ||
- | }// | ||
- | |||
- | function FolderPicker(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(FolderPicker_NewFolder); | ||
- | this.lay.AddChild(this.lstFolds); | ||
- | |||
- | var vlay = app.CreateLayout( " | ||
- | this.okBtn = app.CreateButton(" | ||
- | this.okBtn.parent = self; | ||
- | this.okBtn.SetOnTouch(FolderPicker_Select); | ||
- | vlay.AddChild(this.okBtn); | ||
- | this.cancelBtn = app.CreateButton(" | ||
- | 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.Hide = function(){ | ||
- | self.dlg.Hide(); | ||
- | } | ||
- | 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 ths = lst.shift(); | ||
- | while (undefined != ths) { | ||
- | var pth = folderPath + "/" | ||
- | if (app.IsFolder(pth))dirlist.push(ths); | ||
- | ths = lst.shift(); | ||
- | } | ||
- | |||
- | self.lstFolds.SetList(dirlist.join("," | ||
- | app.HideProgress(); | ||
- | } | ||
- | }// | ||
- | |||
- | function FolderPicker_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 FolderPicker_Select(fil){ | ||
- | var par = this.parent; | ||
- | var pth = par.GetFolder(); | ||
- | par.Hide(); | ||
- | if(this.GetText() == " | ||
- | }// | ||
- | |||
- | </ |