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/11/26 12:19] stevegarman link to folder picker |
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() | ||
+ | { // | ||
+ | |||
+ | lay = app.CreateLayout( " | ||
- | //Called when application is started | + | |
- | function OnStart(){ | + | |
- | //Create a layout with objects vertically centered. | + | btn = app.CreateButton( " |
- | | + | |
+ | btn.SetOnTouch(btn_OnTouch); | ||
+ | pick = new FilePicker(mycallback); | ||
+ | |||
+ | pick.SetHideFiles(true); | ||
+ | |||
+ | app.AddLayout( lay ); | ||
+ | } // | ||
- | //Create a text label and add it to layout. | + | function btn_OnTouch() |
- | btn = app.CreateButton( " | + | |
- | | + | |
- | | + | |
- | | + | } //function btn_OnTouch() |
- | //Add layout to app. | + | |
- | app.AddLayout( lay ); | + | |
- | }//function Onstart() | + | |
+ | function mycallback(fullpath) | ||
+ | { | ||
+ | app.Alert(" | ||
+ | } // | ||
- | function | + | function FilePicker(Callback, |
- | | + | { |
- | | + | var self = this; |
- | | + | this.basePath = basePath || "/ |
- | }//function btn_OnTouch() | + | this.callback = Callback || function() {}; |
+ | | ||
+ | this.hideHiddenFiles = false; | ||
+ | this.dlg = app.CreateDialog(this.basePath); | ||
+ | | ||
+ | | ||
+ | this.lstFolds.parent = self; | ||
+ | | ||
+ | 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); | ||
- | function | + | |
- | app.Alert("user chose " + fullpath) | + | this.Show = function() |
- | }//function mycallback() | + | |
+ | 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); | ||
+ | | ||
+ | var lst = app.ListFolder(folderPath); | ||
+ | lst.sort(function(x, | ||
+ | { | ||
+ | return (x.toLowerCase() > y.toLowerCase()) ? 1 : -1 | ||
+ | }); // | ||
+ | // | ||
+ | |||
+ | 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, | ||
+ | | ||
+ | } | ||
+ | ths = lst.shift(); | ||
+ | } | ||
+ | app.HideProgress(); | ||
+ | } | ||
+ | | ||
- | function FilePicker(Callback, | + | function FilePicker_NewFolder(fil) |
- | | + | |
- | | + | var par = this.parent; |
- | this.callback = Callback || function(){}; | + | var pth = par.GetFolder(); |
- | | + | if (fil != "..") |
- | this.dlg = app.CreateDialog(this.basePath); | + | |
- | | + | pth += "/" + fil |
- | | + | } |
- | | + | else |
- | this.lstFolds.SetOnTouch(FilePicker_NewFolder); | + | |
- | this.lay.AddChild(this.lstFolds); | + | if ( pth |
- | | + | |
- | | + | par.Hide() return; |
- | this.lstFiles.SetOnTouch(FilePicker_NewFile); | + | |
- | this.lay.AddChild(this.lstFiles); | + | var tst = pth.split("/" |
- | | + | |
+ | | ||
+ | | ||
+ | par.SetFolder(pth); | ||
+ | | ||
- | this.Show = function(){ | + | function FilePicker_NewFile(fil) |
- | self.dlg.Show(); | + | { |
- | | + | |
- | this.Hide = function(){ | + | |
- | | + | par.Hide(); |
- | } | + | par.callback(pth += "/" |
- | this.SetFilter | + | } //function FilePicker_NewFile() |
- | self.fileFilter = filter; | + | |
- | } | + | |
- | | + | |
- | return self.FolderPath; | + | |
- | } | + | |
- | this.SetFolder = function(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 == "/" | + | |
- | par.Hide() | + | |
- | | + | |
- | } | + | |
- | 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.Hide(); | + | |
- | | + | |
- | }//function FilePicker_NewFile() | + | |
</ | </ |