This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
sample_code:folder_picker [2014/12/06 21:13] stevegarman |
sample_code:folder_picker [2015/02/19 23:17] (current) |
||
---|---|---|---|
Line 5: | Line 5: | ||
===== The code ===== | ===== The code ===== | ||
<code javascript folderpicker.js> | <code javascript folderpicker.js> | ||
+ | |||
// | // | ||
- | function OnStart() | + | function OnStart(){ |
- | | + | // |
- | | + | lay = app.CreateLayout( " |
- | | + | |
+ | // | ||
+ | btn = app.CreateButton( " | ||
+ | lay.AddChild( btn ); | ||
+ | btn.SetOnTouch(btn_OnTouch); | ||
+ | |||
+ | // | ||
+ | app.AddLayout( lay ); | ||
+ | pick = new FolderPicker(mycallback); | ||
+ | // | ||
+ | pick.SetHideFiles(true); | ||
+ | }// | ||
- | // | + | function btn_OnTouch(){ |
- | + | pick.SetFolder("/ | |
- | btn = app.CreateButton( " | + | pick.Show(); |
- | | + | }//function btn_OnTouch() |
- | btn.SetOnTouch(btn_OnTouch); // | + | |
- | | + | |
- | app.AddLayout( lay ); | + | |
- | pick = new FolderPicker(mycallback); | + | |
- | + | ||
- | | + | |
- | } //function Onstart() | + | |
- | function btn_OnTouch() | + | function mycallback(fullpath){ |
- | | + | app.Alert("user chose " + fullpath) |
- | pick.SetFolder("/sdcard"); | + | }//function mycallback() |
- | pick.Show(); | + | |
- | } //function btn_OnTouch() | + | |
- | function mycallback(fullpath) | + | function FolderPicker(Callback, |
- | { | + | var self = this; |
- | app.Alert("user chose " | + | this.basePath = basePath || "/ |
- | } // | + | this.callback = Callback || function(){}; |
+ | this.FolderPath = this.basePath; | ||
+ | this.hideHiddenFiles = false; | ||
+ | this.dlg = app.CreateDialog(this.basePath); | ||
+ | this.lay = app.CreateLayout( "linear", | ||
+ | |||
+ | this.lstFolds = app.CreateList(" | ||
+ | this.lstFolds.parent = self; | ||
+ | this.lstFolds.SetOnTouch(FolderPicker_NewFolder); | ||
+ | this.lay.AddChild(this.lstFolds); | ||
- | function FolderPicker(Callback, basePath) | + | var vlay = app.CreateLayout( " |
- | | + | this.okBtn = app.CreateButton("Ok"); |
- | var self = this; | + | this.okBtn.parent = self; |
- | | + | this.okBtn.SetOnTouch(FolderPicker_Select); |
- | this.callback | + | vlay.AddChild(this.okBtn); |
- | | + | this.cancelBtn = app.CreateButton("Cancel"); |
- | this.hideHiddenFiles = false; | + | this.cancelBtn.parent = self; |
- | | + | this.cancelBtn.SetOnTouch(FolderPicker_Select); |
- | this.lay = app.CreateLayout( "linear", | + | vlay.AddChild(this.cancelBtn); |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | + | this.lay.AddChild(vlay) | |
- | var vlay = app.CreateLayout( " | + | |
- | | + | |
- | 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); | + | |
- | | + | |
- | + | 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; | ||
+ | self.hideHiddenFiles = val; | ||
+ | } | ||
+ | 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, | ||
+ | self.lstFolds.SetList("" | ||
+ | if( self.FolderPath != self.basePath ) | ||
+ | self.lstFolds.AddItem(" | ||
+ | var ths = lst.shift(); | ||
+ | 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 FolderPicker_NewFolder(fil){ | |
- | this.Show = function() | + | var par = this.parent; |
- | | + | var pth = par.GetFolder(); |
- | | + | if (fil != " |
- | } | + | pth += "/" + fil |
- | | + | } |
- | { | + | else{ |
- | if (val == undefined) val = true; | + | if( pth == par.basePath || pth == "/" |
- | | + | par.Hide() |
- | } | + | return; |
- | this.Hide | + | } |
- | { | + | var tst = pth.split("/"); |
- | self.dlg.Hide(); | + | tmp = tst.pop(); |
- | } | + | pth = (tst.join(" |
- | this.GetFolder = function() | + | } |
- | { | + | par.SetFolder(pth); |
- | return self.FolderPath; | + | }//function FolderPicker_NewFolder() |
- | } | + | |
- | this.SetFolder | + | |
- | { | + | |
- | self.FolderPath = folderPath; | + | |
- | self.dlg.SetTitle(folderPath); | + | |
- | app.ShowProgress( "Loading..." ); | + | |
- | var lst = app.ListFolder(folderPath); | + | |
- | lst.sort(function(x, | + | |
- | | + | |
- | | + | |
- | }); // | + | |
- | + | ||
- | self.lstFolds.SetList("" | + | |
- | if ( self.FolderPath != self.basePath ) self.lstFolds.AddItem(" | + | |
- | var ths = lst.shift(); | + | |
- | | + | |
- | { | + | |
- | if ((! self.hideHiddenFiles) | + | |
- | { | + | |
- | var pth = folderPath + "/" | + | |
- | | + | |
- | if (app.IsFolder(pth)) self.lstFolds.AddItem(ths, null, "folder") | + | |
- | | + | |
- | ths = lst.shift(); | + | |
- | | + | |
- | | + | |
- | app.HideProgress(); | + | |
- | } | + | |
- | } //function FolderPicker() | + | |
- | function FolderPicker_NewFolder(fil) | + | function FolderPicker_Select(fil){ |
- | { | + | var par = this.parent; |
- | var par = this.parent; | + | var pth = par.GetFolder(); |
- | var pth = par.GetFolder(); | + | par.Hide(); |
- | if (fil != " | + | if(this.GetText() == "Ok" |
- | { | + | }//function FolderPicker_Select() |
- | pth += "/" | + | |
- | } | + | |
- | else | + | |
- | { | + | |
- | if ( pth == par.basePath || pth == "/" | + | |
- | { | + | |
- | | + | |
- | | + | |
- | var tst = pth.split("/" | + | |
- | tmp = tst.pop(); | + | |
- | pth = (tst.join("/")); | + | |
- | } | + | |
- | | + | |
- | } //function FolderPicker_NewFolder() | + | |
- | function FolderPicker_Select(fil) | ||
- | { | ||
- | var par = this.parent; | ||
- | var pth = par.GetFolder(); | ||
- | par.Hide(); | ||
- | if (this.GetText() == " | ||
- | } // | ||
</ | </ |