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/12/05 22:42] stevegarman updated for v1.15 |
sample_code:file_picker [2015/01/25 13:26] octazid [The code] Display as colored code |
||
---|---|---|---|
Line 8: | Line 8: | ||
See also [[sample_code: | See also [[sample_code: | ||
===== The code ===== | ===== The code ===== | ||
- | < | + | < |
// | // | ||
- | function OnStart(){ | + | 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 ); | + | |
- | }// | + | |
+ | // | ||
+ | |||
+ | btn = app.CreateButton( " | ||
+ | lay.AddChild( btn ); | ||
+ | btn.SetOnTouch(btn_OnTouch); | ||
+ | pick = new FilePicker(mycallback); | ||
+ | |||
+ | pick.SetHideFiles(true); | ||
+ | |||
+ | app.AddLayout( lay ); | ||
+ | } // | ||
- | function btn_OnTouch(){ | + | function btn_OnTouch() |
- | pick.SetFolder("/ | + | { |
- | pick.Show(); | + | |
- | }// | + | |
+ | } // | ||
- | function mycallback(fullpath){ | + | function mycallback(fullpath) |
- | app.Alert(" | + | { |
- | }// | + | |
+ | } // | ||
- | function FilePicker(Callback, | + | function FilePicker(Callback, basePath) |
- | var self = this; | + | { |
- | this.basePath = basePath || "/ | + | var self |
- | 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.lay.AddChild(this.lstFolds); |
- | this.lstFiles.parent = self; | + | |
- | this.lstFiles.SetOnTouch(FilePicker_NewFile); | + | |
- | this.lay.AddChild(this.lstFiles); | + | |
- | this.dlg.AddLayout(this.lay); | + | this.lay.AddChild(this.lstFiles); |
+ | | ||
- | 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); | + | return self.FolderPath; |
- | lst.sort(function(x, | + | } |
- | // | + | |
- | // | + | { |
- | var ths = lst.shift(); | + | |
- | self.lstFolds.SetList("" | + | |
- | if( self.FolderPath != self.basePath ) | + | |
- | self.lstFolds.AddItem(" | + | var lst = app.ListFolder(folderPath); |
- | self.lstFiles.SetList("" | + | |
- | while (undefined != ths) { | + | |
- | if ((! self.hideHiddenFiles) || (ths.indexOf(" | + | |
- | var pth = folderPath + "/" | + | |
- | if (app.IsFolder(pth)) | + | |
- | self.lstFolds.AddItem(ths, | + | |
- | else | + | var ths = lst.shift(); |
- | self.lstFiles.AddItem(ths) | + | |
- | } | + | |
- | ths = lst.shift(); | + | |
- | } | + | |
- | app.HideProgress(); | + | { |
- | } | + | |
- | }// | + | { |
+ | var pth = folderPath + "/" | ||
+ | | ||
+ | | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | | ||
+ | | ||
+ | } // | ||
- | function FilePicker_NewFolder(fil){ | + | function FilePicker_NewFolder(fil) |
- | var par = this.parent; | + | { |
- | var pth = par.GetFolder(); | + | var par = this.parent; |
- | if (fil != " | + | var pth = par.GetFolder(); |
- | pth += "/" | + | |
- | } | + | { |
- | else{ | + | |
- | if( pth == par.basePath || pth == "/" | + | } |
- | par.Hide() | + | |
- | return; | + | { |
- | } | + | |
- | var tst = pth.split("/" | + | |
- | tmp = tst.pop(); | + | par.Hide() |
- | pth = (tst.join("/" | + | } |
- | } | + | var tst = pth.split("/" |
- | + | | |
- | par.SetFolder(pth); | + | |
- | }// | + | } |
+ | par.SetFolder(pth); | ||
+ | } // | ||
- | function FilePicker_NewFile(fil){ | + | function FilePicker_NewFile(fil) |
- | var par = this.parent; | + | { |
- | var pth = par.GetFolder(); | + | var par = this.parent; |
- | par.Hide(); | + | var pth = par.GetFolder(); |
- | par.callback(pth += "/" | + | |
- | }// | + | |
+ | } // | ||
</ | </ |