From d17b2cf884d47fde581dcd7624f40f71d5b28ab7 Mon Sep 17 00:00:00 2001 From: Sebastiaan Janssen Date: Mon, 26 Aug 2013 17:28:15 +0200 Subject: [PATCH] Added ability to have predefined templates for Partial View Macros --- src/Umbraco.Web.UI/Umbraco.Web.UI.csproj | 1 + .../Templates/EmptyTemplate.cshtml | 1 + .../PartialViews/Templates/Login.cshtml | 3 + .../umbraco/create/PartialViewMacro.ascx | 8 +- .../umbraco/create/PartialViewMacro.ascx.cs | 44 +++- .../create/PartialViewMacro.ascx.designer.cs | 9 + src/Umbraco.Web/Umbraco.Web.csproj | 16 +- .../umbraco/create/PartialViewTasks.cs | 191 +++++++++--------- 8 files changed, 161 insertions(+), 112 deletions(-) create mode 100644 src/Umbraco.Web.UI/Umbraco/PartialViews/Templates/EmptyTemplate.cshtml create mode 100644 src/Umbraco.Web.UI/Umbraco/PartialViews/Templates/Login.cshtml diff --git a/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj b/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj index 6799d76b45..23dad85df3 100644 --- a/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj +++ b/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj @@ -2132,6 +2132,7 @@ + diff --git a/src/Umbraco.Web.UI/Umbraco/PartialViews/Templates/EmptyTemplate.cshtml b/src/Umbraco.Web.UI/Umbraco/PartialViews/Templates/EmptyTemplate.cshtml new file mode 100644 index 0000000000..f8e40a5677 --- /dev/null +++ b/src/Umbraco.Web.UI/Umbraco/PartialViews/Templates/EmptyTemplate.cshtml @@ -0,0 +1 @@ +@using Umbraco.Web.Macros.PartialViewMacroPage \ No newline at end of file diff --git a/src/Umbraco.Web.UI/Umbraco/PartialViews/Templates/Login.cshtml b/src/Umbraco.Web.UI/Umbraco/PartialViews/Templates/Login.cshtml new file mode 100644 index 0000000000..11b2cbc0c7 --- /dev/null +++ b/src/Umbraco.Web.UI/Umbraco/PartialViews/Templates/Login.cshtml @@ -0,0 +1,3 @@ +@using Umbraco.Web.Macros.PartialViewMacroPage + +@* Simple login template *@ \ No newline at end of file diff --git a/src/Umbraco.Web.UI/umbraco/create/PartialViewMacro.ascx b/src/Umbraco.Web.UI/umbraco/create/PartialViewMacro.ascx index 8b607fb8ad..b779f6854f 100644 --- a/src/Umbraco.Web.UI/umbraco/create/PartialViewMacro.ascx +++ b/src/Umbraco.Web.UI/umbraco/create/PartialViewMacro.ascx @@ -11,12 +11,10 @@ Filename (without .cshtml):
-<%--
+
Choose a template:
- - Clean - -
--%> + +
diff --git a/src/Umbraco.Web.UI/umbraco/create/PartialViewMacro.ascx.cs b/src/Umbraco.Web.UI/umbraco/create/PartialViewMacro.ascx.cs index 37090c7e6b..120a51273c 100644 --- a/src/Umbraco.Web.UI/umbraco/create/PartialViewMacro.ascx.cs +++ b/src/Umbraco.Web.UI/umbraco/create/PartialViewMacro.ascx.cs @@ -1,25 +1,47 @@ using System; -using System.Collections.Generic; -using System.IO; using System.Linq; -using System.Web; using System.Web.UI; using System.Web.UI.WebControls; -using umbraco; using umbraco.BasePages; +using Umbraco.Core; +using Umbraco.Core.IO; using umbraco.presentation.create; -using UmbracoSettings = Umbraco.Core.Configuration.UmbracoSettings; namespace Umbraco.Web.UI.Umbraco.Create { - public partial class PartialViewMacro : System.Web.UI.UserControl + public partial class PartialViewMacro : UserControl { protected override void OnLoad(EventArgs e) { base.OnLoad(e); DataBind(); + + LoadTemplates(PartialViewTemplate); } + private static void LoadTemplates(ListControl list) + { + var path = IOHelper.MapPath(SystemDirectories.Umbraco + "/partialviews/templates/"); + list.Items.Clear(); + + // always add the option of an empty one + list.Items.Add(new ListItem("Empty template", "EmptyTemplate.cshtml")); + + if (System.IO.Directory.Exists(path)) + { + const string extension = ".cshtml"; + + //Already adding Empty Template as the first item, so don't add it again + foreach (var fileInfo in new System.IO.DirectoryInfo(path).GetFiles("*" + extension).Where(f => f.Name != "EmptyTemplate.cshtml")) + { + var filename = System.IO.Path.GetFileName(fileInfo.FullName); + + var liText = filename.Replace(extension, "").SplitPascalCasing().ToFirstUpperInvariant(); + list.Items.Add(new ListItem(liText, filename)); + } + } + } + protected void SubmitButton_Click(object sender, System.EventArgs e) { if (Page.IsValid) @@ -30,11 +52,11 @@ namespace Umbraco.Web.UI.Umbraco.Create var createMacroVal = 0; if (CreateMacroCheckBox.Checked) createMacroVal = 1; - - var returnUrl = dialogHandler_temp.Create( - Request.GetItemAsString("nodeType"), - createMacroVal, //apparently we need to pass this value to 'ParentID'... of course! :P then we'll extract it in PartialViewTasks to create it. - FileName.Text); + + + string returnUrl = dialogHandler_temp.Create(Request.GetItemAsString("nodeType"), + createMacroVal, //apparently we need to pass this value to 'ParentID'... of course! :P then we'll extract it in PartialViewTasks to create it. + PartialViewTemplate.SelectedValue + "|||" + FileName.Text); BasePage.Current.ClientTools .ChangeContentFrameUrl(returnUrl) diff --git a/src/Umbraco.Web.UI/umbraco/create/PartialViewMacro.ascx.designer.cs b/src/Umbraco.Web.UI/umbraco/create/PartialViewMacro.ascx.designer.cs index 063f61ae35..6d2bd58554 100644 --- a/src/Umbraco.Web.UI/umbraco/create/PartialViewMacro.ascx.designer.cs +++ b/src/Umbraco.Web.UI/umbraco/create/PartialViewMacro.ascx.designer.cs @@ -30,6 +30,15 @@ namespace Umbraco.Web.UI.Umbraco.Create { /// protected global::System.Web.UI.WebControls.TextBox FileName; + /// + /// PartialViewTemplate control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.ListBox PartialViewTemplate; + /// /// CreateMacroCheckBox control. /// diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 8c8271800b..ef5cd23446 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -1872,9 +1872,13 @@ - + + ASPXCodeBehind + - + + ASPXCodeBehind + @@ -1943,11 +1947,15 @@ - + + ASPXCodeBehind + Form - + + ASPXCodeBehind + Designer diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/create/PartialViewTasks.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/create/PartialViewTasks.cs index 34552fa879..c1c9f27a8a 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/create/PartialViewTasks.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/create/PartialViewTasks.cs @@ -9,116 +9,123 @@ using Umbraco.Core; namespace umbraco { - /// - /// The UI 'tasks' for the create dialog and delete processes - /// - [UmbracoWillObsolete("http://issues.umbraco.org/issue/U4-1373", "This will one day be removed when we overhaul the create process")] - public class PartialViewTasks : interfaces.ITaskReturnUrl - { - private string _alias; - private int _parentId; - private int _typeId; - private int _userId; + /// + /// The UI 'tasks' for the create dialog and delete processes + /// + [UmbracoWillObsolete("http://issues.umbraco.org/issue/U4-1373", "This will one day be removed when we overhaul the create process")] + public class PartialViewTasks : interfaces.ITaskReturnUrl + { + private string _alias; + private int _parentId; + private int _typeId; + private int _userId; - protected virtual string EditViewFile - { - get { return "Settings/Views/EditView.aspx"; } - } + protected virtual string EditViewFile + { + get { return "Settings/Views/EditView.aspx"; } + } - protected string BasePath - { - get { return SystemDirectories.MvcViews + "/" + ParentFolderName.EnsureEndsWith('/'); } - } + protected string BasePath + { + get { return SystemDirectories.MvcViews + "/" + ParentFolderName.EnsureEndsWith('/'); } + } - protected virtual string ParentFolderName - { - get { return "Partials"; } - } + protected virtual string ParentFolderName + { + get { return "Partials"; } + } - public int UserId - { - set { _userId = value; } - } - public int TypeID - { - set { _typeId = value; } - get { return _typeId; } - } + public int UserId + { + set { _userId = value; } + } + public int TypeID + { + set { _typeId = value; } + get { return _typeId; } + } - public string Alias - { - set { _alias = value; } - get { return _alias; } - } + public string Alias + { + set { _alias = value; } + get { return _alias; } + } - public int ParentID - { - set { _parentId = value; } - get { return _parentId; } - } + public int ParentID + { + set { _parentId = value; } + get { return _parentId; } + } - public bool Save() - { - var fileName = _alias + ".cshtml"; - var fullFilePath = IOHelper.MapPath(BasePath + fileName); - - //return the link to edit the file if it already exists - if (File.Exists(fullFilePath)) - { - _returnUrl = string.Format(EditViewFile + "?file={0}", HttpUtility.UrlEncode(ParentFolderName.EnsureEndsWith('/') + fileName)); - return true; - } + public bool Save() + { + var pipesIndex = _alias.IndexOf("|||", System.StringComparison.Ordinal); + var template = _alias.Substring(0, pipesIndex).Trim(); + var fileName = _alias.Substring(pipesIndex + 3, _alias.Length - pipesIndex - 3) + ".cshtml"; - //create the file - using (var sw = File.CreateText(fullFilePath)) - { - WriteTemplateHeader(sw); - } + var fullFilePath = IOHelper.MapPath(BasePath + fileName); - // Create macro? - if (ParentID == 1) - { + //return the link to edit the file if it already exists + if (File.Exists(fullFilePath)) + { + _returnUrl = string.Format(EditViewFile + "?file={0}", HttpUtility.UrlEncode(ParentFolderName.EnsureEndsWith('/') + fileName)); + return true; + } + + //create the file + using (var sw = File.CreateText(fullFilePath)) + { + using (var templateFile = File.OpenText(IOHelper.MapPath(SystemDirectories.Umbraco + "/partialviews/templates/" + template))) + { + var templateContent = templateFile.ReadToEnd(); + sw.Write(templateContent); + } + } + + // Create macro? + if (ParentID == 1) + { var name = fileName .Substring(0, (fileName.LastIndexOf('.') + 1)).Trim('.') .SplitPascalCasing().ToFirstUpperInvariant(); - var m = cms.businesslogic.macro.Macro.MakeNew(name); - m.ScriptingFile = BasePath + fileName; - } - - _returnUrl = string.Format(EditViewFile + "?file={0}", HttpUtility.UrlEncode(ParentFolderName.EnsureEndsWith('/') + fileName)); - return true; - } + var m = cms.businesslogic.macro.Macro.MakeNew(name); + m.ScriptingFile = BasePath + fileName; + } - protected virtual void WriteTemplateHeader(StreamWriter sw) - { - //write out the template header - sw.Write("@inherits "); - sw.Write(typeof(UmbracoViewPage<>).FullName.TrimEnd("`1")); - sw.Write(""); - } + _returnUrl = string.Format(EditViewFile + "?file={0}", HttpUtility.UrlEncode(ParentFolderName.EnsureEndsWith('/') + fileName)); + return true; + } - public bool Delete() - { - var path = IOHelper.MapPath(BasePath + _alias.TrimStart('/')); + protected virtual void WriteTemplateHeader(StreamWriter sw) + { + //write out the template header + sw.Write("@inherits "); + sw.Write(typeof(UmbracoViewPage<>).FullName.TrimEnd("`1")); + sw.Write(""); + } - if (File.Exists(path)) - File.Delete(path); - else if (Directory.Exists(path)) - Directory.Delete(path, true); + public bool Delete() + { + var path = IOHelper.MapPath(BasePath + _alias.TrimStart('/')); - LogHelper.Info(string.Format("{0} Deleted by user {1}", _alias, UmbracoEnsuredPage.CurrentUser.Id)); + if (File.Exists(path)) + File.Delete(path); + else if (Directory.Exists(path)) + Directory.Delete(path, true); - return true; - } + LogHelper.Info(string.Format("{0} Deleted by user {1}", _alias, UmbracoEnsuredPage.CurrentUser.Id)); - #region ITaskReturnUrl Members - private string _returnUrl = ""; - public string ReturnUrl - { - get { return _returnUrl; } - } + return true; + } - #endregion - } + #region ITaskReturnUrl Members + private string _returnUrl = ""; + public string ReturnUrl + { + get { return _returnUrl; } + } + + #endregion + } } \ No newline at end of file