This shows you the differences between two versions of the page.
sample_code:new_folder [2015/02/05 11:28] octazid add Text |
sample_code:new_folder [2015/02/05 19:29] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ======New Folder Dialog====== | ||
- | Sometimes you have to create new folder in a path.\\ | ||
- | Its easy with if you use a dialog.\\ \\ | ||
- | (look for the example to [[sample_code: | ||
- | {{: | ||
- | Save the **DlgNewFolder.js** in the Droidscriptfolder where you need the file.\\ | ||
- | You can call the dialog after you load the Script into your app at the OnStart() Event. | ||
- | |||
- | <code Javascript DlgNewFolder.js> | ||
- | / | ||
- | * CreateNewFolder Dialog | ||
- | ===================================================================== | ||
- | * Creation date: 23-01-2015 by octazid | ||
- | * Last update: 23-01-2015 by octazid | ||
- | ===================================================================== | ||
- | * Simple dialog to create a new folder | ||
- | ********************************************************************/ | ||
- | |||
- | |||
- | //*** CreateNewFolder Dialog***// | ||
- | function NewFolderDialog(savepath) | ||
- | { | ||
- | var self = this; | ||
- | this.Fdlg = app.CreateDialog(" | ||
- | this.Flay = app.CreateLayout( " | ||
- | this.Ftxtpath = app.CreateText(savepath, | ||
- | this.Ftxtpath.SetMargins(0, | ||
- | this.Ftxtpath.parent = self; | ||
- | this.Flay.AddChild(this.Ftxtpath); | ||
- | this.Fedt = app.CreateTextEdit("", | ||
- | this.Fedt.SetTextSize(18); | ||
- | this.Fedt.SetHint(" | ||
- | this.Fedt.parent = self; | ||
- | this.Flay.AddChild(this.Fedt); | ||
- | this.Fbtnlay = app.CreateLayout( " | ||
- | this.Fdlgbtn = app.CreateButton(" | ||
- | this.Fdlgbtn.parent = self; | ||
- | this.Fdlgbtn.SetOnTouch(Fdlgbtn_OnTouch); | ||
- | this.Fbtnlay.AddChild(this.Fdlgbtn); | ||
- | this.FdlgbtnCancel = app.CreateButton(" | ||
- | this.FdlgbtnCancel.parent = self; | ||
- | this.FdlgbtnCancel.SetOnTouch(Fdlgbtn_OnTouch); | ||
- | this.Fbtnlay.AddChild(this.FdlgbtnCancel); | ||
- | this.Flay.AddChild(this.Fbtnlay) | ||
- | this.Fdlg.AddLayout(this.Flay); | ||
- | this.Show = function(){self.Fdlg.Show(); | ||
- | this.Hide = function(){self.Fdlg.Hide(); | ||
- | }//function NewFolderDialog() | ||
- | |||
- | |||
- | // Called if a button is touched | ||
- | function Fdlgbtn_OnTouch() | ||
- | { | ||
- | var par = this.parent; | ||
- | folder = par.Fedt.GetText(); | ||
- | path = par.Ftxtpath.GetText(); | ||
- | if(this.GetText() | ||
- | par.Hide(); | ||
- | }//function Fdlgbtn_OnTouch | ||
- | |||
- | |||
- | // Called if Button Ok is touched | ||
- | function CreateFolder(foldername, | ||
- | { | ||
- | if (foldername != "" | ||
- | { | ||
- | //Replace illegal letters | ||
- | foldername = foldername.replace(/ | ||
- | |||
- | //Replace double Whitespaces | ||
- | foldername = foldername.replace(/ | ||
- | |||
- | //Replace Whitespaces at the start and at the end | ||
- | foldername = foldername.trim(); | ||
- | |||
- | //Check if Folder exists. If yes, show a message | ||
- | if (app.FolderExists(path + foldername)) | ||
- | { | ||
- | app.Alert(" | ||
- | "Error - Folder already exists!" | ||
- | } | ||
- | else | ||
- | { | ||
- | //Create the folder | ||
- | app.MakeFolder(path + foldername); | ||
- | //show a message if creation was ok | ||
- | if (app.FolderExists(path + foldername)) | ||
- | app.ShowPopup(" | ||
- | } | ||
- | } | ||
- | }//function CreateFolder | ||
- | </ | ||
- | |||
- | In your app you have to do just three things,\\ \\ | ||
- | 1. Load the Script with | ||
- | app.LoadScript(" | ||
- | 2. Create a Button to call the Dialog and add it to the layout | ||
- | btn = app.CreateButton( " | ||
- | btn.SetOnTouch(CreateNewFolder); | ||
- | lay.AddChild( btn ); | ||
- | 3. Write a function to call the Dialog | ||
- | //Create a new Dialog | ||
- | function CreateNewFolder() | ||
- | { | ||
- | // Change the path if you need | ||
- | DlgNewFolder = new NewFolderDialog("/ | ||
- | DlgNewFolder.Show(); | ||
- | } | ||
- | Or you can use this Example. | ||
- | <code Javascript exampleapp.js> | ||
- | //Called when application is started. | ||
- | function OnStart() | ||
- | { | ||
- | app.LoadScript(" | ||
- | lay = app.CreateLayout( " | ||
- | btn = app.CreateButton( " | ||
- | btn.SetOnTouch(CreateNewFolder); | ||
- | lay.AddChild( btn ); | ||
- | app.AddLayout( lay ); | ||
- | } | ||
- | |||
- | |||
- | //Create a new Dialog | ||
- | function CreateNewFolder() | ||
- | { | ||
- | // Change the path if you need | ||
- | DlgNewFolder = new NewFolderDialog("/ | ||
- | DlgNewFolder.Show(); | ||
- | } | ||
- | </ |