This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
sample_code:file_picker [2014/12/06 21:10] stevegarman change curly brace style |
sample_code:file_picker [2015/03/27 04:03] (current) |
||
---|---|---|---|
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, basePath) | + | function FilePicker(Callback, |
- | | + | var self = this; |
- | var self | + | this.basePath = basePath || "/ |
- | this.basePath = basePath || "/ | + | this.callback = Callback || function(){}; |
- | this.callback = Callback || function() {}; | + | this.FolderPath = this.basePath; |
- | | + | this.hideHiddenFiles = false; |
- | this.hideHiddenFiles = false; | + | this.dlg = app.CreateDialog(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.lstFolds.SetOnTouch(FilePicker_NewFolder); | + | this.lay.AddChild(this.lstFolds); |
- | this.lay.AddChild(this.lstFolds); | + | this.lstFiles = app.CreateList(" |
- | this.lstFiles = app.CreateList(" | + | this.lstFiles.parent = self; |
- | | + | this.lstFiles.SetOnTouch(FilePicker_NewFile); |
- | this.lstFiles.SetOnTouch(FilePicker_NewFile); | + | this.lay.AddChild(this.lstFiles); |
- | this.lay.AddChild(this.lstFiles); | + | this.dlg.AddLayout(this.lay); |
- | this.dlg.AddLayout(this.lay); | + | |
- | + | this.Show = function(){ | |
- | | + | self.dlg.Show(); |
- | { | + | } |
- | | + | this.SetHideFiles=function(val){ |
- | } | + | if(val == undefined) val = true; |
- | this.SetHideFiles = function(val) | + | 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; |
- | this.GetFolder = function() | + | self.dlg.SetTitle(folderPath); |
- | { | + | app.ShowProgress( " |
- | return self.FolderPath; | + | var lst = app.ListFolder(folderPath); |
- | } | + | lst.sort(function(x, |
- | | + | var ths = lst.shift(); |
- | { | + | self.lstFolds.SetList("" |
- | | + | if( self.FolderPath != self.basePath ) |
- | | + | self.lstFolds.AddItem(" |
- | app.ShowProgress( " | + | self.lstFiles.SetList("" |
- | var lst = app.ListFolder(folderPath); | + | while (undefined != ths) { |
- | lst.sort(function(x, | + | if ((! self.hideHiddenFiles) || (ths.indexOf(" |
- | | + | var pth = folderPath + "/" |
- | | + | if (app.IsFolder(pth)) |
- | | + | self.lstFolds.AddItem(ths, |
- | | + | else |
- | + | self.lstFiles.AddItem(ths) | |
- | var ths = lst.shift(); | + | } |
- | self.lstFolds.SetList("" | + | ths = lst.shift(); |
- | if ( self.FolderPath != self.basePath ) | + | } |
- | self.lstFiles.SetList("" | + | app.HideProgress(); |
- | while (undefined != ths) | + | } |
- | { | + | }// |
- | | + | |
- | { | + | |
- | var pth = folderPath + "/" | + | |
- | | + | |
- | | + | |
- | } | + | |
- | ths = lst.shift(); | + | |
- | } | + | |
- | app.HideProgress(); | + | |
- | } | + | |
- | } // | + | |
- | 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{ |
- | } | + | if( pth == par.basePath || pth == "/" |
- | else | + | par.Hide() |
- | | + | return; |
- | if ( pth == par.basePath || pth == "/" | + | } |
- | { | + | var tst = pth.split("/" |
- | | + | tmp = tst.pop(); |
- | } | + | pth = (tst.join("/" |
- | var tst = pth.split("/" | + | if(pth=="" |
- | | + | } |
- | | + | |
- | } | + | par.SetFolder(pth); |
- | par.SetFolder(pth); | + | }// |
- | } // | + | |
- | 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.callback(pth += "/" |
- | par.callback(pth += "/" | + | }// |
- | } // | + | |
</ | </ |