From e0a71830fa9b0a9e2bf5092b3c2dccfe57b50dbf Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Tue, 4 Dec 2012 05:42:15 +0500 Subject: [PATCH] Updated macro editor to support Partial view macros. Improves the JS in the editor (no more c# based js) which auto clears other fields and drop downs when other files are selected. --- .../developer/Macros/EditMacro.aspx.cs | 46 ++++++++ .../umbraco/developer/Macros/editMacro.aspx | 11 +- .../developer/Macros/editMacro.aspx.cs | 107 ++++++++++-------- src/umbraco.cms/businesslogic/macro/Macro.cs | 16 ++- 4 files changed, 127 insertions(+), 53 deletions(-) diff --git a/src/Umbraco.Web.UI/umbraco/developer/Macros/EditMacro.aspx.cs b/src/Umbraco.Web.UI/umbraco/developer/Macros/EditMacro.aspx.cs index 5ffcc42354..3f2a0ec4ef 100644 --- a/src/Umbraco.Web.UI/umbraco/developer/Macros/EditMacro.aspx.cs +++ b/src/Umbraco.Web.UI/umbraco/developer/Macros/EditMacro.aspx.cs @@ -6,6 +6,8 @@ using System.Linq; using System.Web; using System.Web.UI.WebControls; using Umbraco.Core.IO; +using Umbraco.Core; +using umbraco.cms.businesslogic.macro; namespace Umbraco.Web.UI.Umbraco.Developer.Macros { @@ -19,6 +21,44 @@ namespace Umbraco.Web.UI.Umbraco.Developer.Macros PopulatePartialViewFiles(); } + /// + /// This ensures that the SelectedPartialView txt box value is set correctly when the m_macro object's + /// ScriptingFile property contains a full virtual path beginning with the MacroPartials path + /// + /// + /// + /// + protected override void PopulateFieldsOnLoad(Macro macro, string macroAssemblyValue, string macroTypeValue) + { + base.PopulateFieldsOnLoad(macro, macroAssemblyValue, macroTypeValue); + //check if the ScriptingFile property contains the MacroPartials path + if (macro.ScriptingFile.StartsWith(SystemDirectories.MvcViews + "/MacroPartials/")) + { + macroPython.Text = ""; + SelectedPartialView.Text = Path.GetFileName(macro.ScriptingFile); + } + } + + /// + /// This changes the macro type to a PartialViewMacro if the SelectedPartialView txt box has a value. + /// This then also updates the file path saved for the partial view to be the full virtual path, not just the file name. + /// + /// + /// + /// + /// + protected override void SetMacroValuesFromPostBack(Macro macro, int macroCachePeriod, string macroAssemblyValue, string macroTypeValue) + { + base.SetMacroValuesFromPostBack(macro, macroCachePeriod, macroAssemblyValue, macroTypeValue); + if (!SelectedPartialView.Text.IsNullOrWhiteSpace()) + { + macro.ScriptingFile = SystemDirectories.MvcViews + "/MacroPartials/" + SelectedPartialView.Text; + } + } + + /// + /// Populate the drop down list for partial view files + /// private void PopulatePartialViewFiles() { var partialsDir = IOHelper.MapPath(SystemDirectories.MvcViews + "/MacroPartials"); @@ -28,6 +68,12 @@ namespace Umbraco.Web.UI.Umbraco.Developer.Macros PartialViewList.Items.Insert(0, new ListItem("Browse partial view files on server...", string.Empty)); } + /// + /// Get the list of partial view files in the ~/Views/MacroPartials folder + /// + /// + /// + /// private IEnumerable GetPartialViewFiles(string orgPath, string path) { var files = new List(); diff --git a/src/Umbraco.Web.UI/umbraco/developer/Macros/editMacro.aspx b/src/Umbraco.Web.UI/umbraco/developer/Macros/editMacro.aspx index a958e1e62c..01a664569c 100644 --- a/src/Umbraco.Web.UI/umbraco/developer/Macros/editMacro.aspx +++ b/src/Umbraco.Web.UI/umbraco/developer/Macros/editMacro.aspx @@ -11,9 +11,16 @@ //handles the change selection of the drop downs to populate the text box (function($) { $(document).ready(function () { + //on drop down change, update the text box and clear other text boxes $("#Table2 td.propertyContent select").change(function() { - $(this).prev("input[type='text']").val($(this).val()); - }); + //update the txt box + var txt = $(this).prev("input[type='text']"); + txt.val($(this).val()); + //clear other text boxes + $("#Table2 td.propertyContent input[type='text']").not(txt).val(""); + //reset other drop downs + $("#Table2 td.propertyContent select").not($(this)).val(""); + }); }); })(jQuery); diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/developer/Macros/editMacro.aspx.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/developer/Macros/editMacro.aspx.cs index 510264bec3..fe8864594e 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/developer/Macros/editMacro.aspx.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/developer/Macros/editMacro.aspx.cs @@ -6,14 +6,13 @@ using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.IO; - +using Umbraco.Core.IO; using umbraco.BasePages; using umbraco.presentation.cache; using umbraco.uicontrols; using umbraco.DataLayer; using umbraco.cms.presentation.Trees; using umbraco.cms.businesslogic.macro; -using umbraco.IO; namespace umbraco.cms.presentation.developer { @@ -45,39 +44,20 @@ namespace umbraco.cms.presentation.developer .SetActiveTreeType(TreeDefinitionCollection.Instance.FindTree().Tree.Alias) .SyncTree("-1,init," + m_macro.Id.ToString(), false); - macroName.Text = m_macro.Name; - macroAlias.Text = m_macro.Alias; - string tempMacroAssembly = m_macro.Assembly == null ? "" : m_macro.Assembly; - string tempMacroType = m_macro.Type == null ? "" : m_macro.Type; - macroXslt.Text = m_macro.Xslt; - macroPython.Text = m_macro.ScriptingFile; - cachePeriod.Text = m_macro.RefreshRate.ToString(); + string tempMacroAssembly = m_macro.Assembly ?? ""; + string tempMacroType = m_macro.Type ?? ""; - macroRenderContent.Checked = m_macro.RenderContent; - macroEditor.Checked = m_macro.UseInEditor; - cacheByPage.Checked = m_macro.CacheByPage; - cachePersonalized.Checked = m_macro.CachePersonalized; - - // Populate either user control or custom control - if (tempMacroType != string.Empty && tempMacroAssembly != string.Empty) - { - macroAssembly.Text = tempMacroAssembly; - macroType.Text = tempMacroType; - } - else - { - macroUserControl.Text = tempMacroType; - } + PopulateFieldsOnLoad(m_macro, tempMacroAssembly, tempMacroType); // Check for assemblyBrowser if (tempMacroType.IndexOf(".ascx") > 0) assemblyBrowserUserControl.Controls.Add( - new LiteralControl("
")); else if (tempMacroType != string.Empty && tempMacroAssembly != string.Empty) assemblyBrowser.Controls.Add( - new LiteralControl("
")); @@ -91,37 +71,26 @@ namespace umbraco.cms.presentation.developer PopulatePythonFiles(); // Load usercontrols - PopulateUserControls(IOHelper.MapPath(SystemDirectories.Usercontrols)); + PopulateUserControls(IOHelper.MapPath(SystemDirectories.UserControls)); userControlList.Items.Insert(0, new ListItem("Browse usercontrols on server...", string.Empty)); } else { int macroID = Convert.ToInt32(Request.QueryString["macroID"]); + string tempMacroAssembly = macroAssembly.Text; string tempMacroType = macroType.Text; string tempCachePeriod = cachePeriod.Text; - if (tempCachePeriod == string.Empty) tempCachePeriod = "0"; - if (tempMacroAssembly == string.Empty && macroUserControl.Text != string.Empty) tempMacroType = macroUserControl.Text; - // Save macro - m_macro.UseInEditor = macroEditor.Checked; - m_macro.RenderContent = macroRenderContent.Checked; - m_macro.CacheByPage = cacheByPage.Checked; - m_macro.CachePersonalized = cachePersonalized.Checked; - m_macro.RefreshRate = Convert.ToInt32(tempCachePeriod); - m_macro.Alias = macroAlias.Text; - m_macro.Name = macroName.Text; - m_macro.Assembly = tempMacroAssembly; - m_macro.Type = tempMacroType; - m_macro.Xslt = macroXslt.Text; - m_macro.ScriptingFile = macroPython.Text; - m_macro.Save(); - + SetMacroValuesFromPostBack(m_macro, Convert.ToInt32(tempCachePeriod), tempMacroAssembly, tempMacroType); + + m_macro.Save(); + // Save elements foreach (RepeaterItem item in macroProperties.Items) { @@ -163,6 +132,54 @@ namespace umbraco.cms.presentation.developer } } + /// + /// Populates the control (textbox) values on page load + /// + /// + /// + /// + protected virtual void PopulateFieldsOnLoad(Macro macro, string macroAssemblyValue, string macroTypeValue) + { + macroName.Text = macro.Name; + macroAlias.Text = macro.Alias; + macroXslt.Text = macro.Xslt; + macroPython.Text = macro.ScriptingFile; + cachePeriod.Text = macro.RefreshRate.ToString(); + macroRenderContent.Checked = macro.RenderContent; + macroEditor.Checked = macro.UseInEditor; + cacheByPage.Checked = macro.CacheByPage; + cachePersonalized.Checked = macro.CachePersonalized; + + // Populate either user control or custom control + if (macroTypeValue != string.Empty && macroAssemblyValue != string.Empty) + { + macroAssembly.Text = macroAssemblyValue; + macroType.Text = macroTypeValue; + } + else + { + macroUserControl.Text = macroTypeValue; + } + } + + /// + /// Sets the values on the Macro object from the values posted back before saving the macro + /// + protected virtual void SetMacroValuesFromPostBack(Macro macro, int macroCachePeriod, string macroAssemblyValue, string macroTypeValue) + { + macro.UseInEditor = macroEditor.Checked; + macro.RenderContent = macroRenderContent.Checked; + macro.CacheByPage = cacheByPage.Checked; + macro.CachePersonalized = cachePersonalized.Checked; + macro.RefreshRate = macroCachePeriod; + macro.Alias = macroAlias.Text; + macro.Name = macroName.Text; + macro.Assembly = macroAssemblyValue; + macro.Type = macroTypeValue; + macro.Xslt = macroXslt.Text; + macro.ScriptingFile = macroPython.Text; + } + private void GetXsltFilesFromDir(string orgPath, string path, ArrayList files) { DirectoryInfo dirInfo = new DirectoryInfo(path); @@ -300,12 +317,12 @@ namespace umbraco.cms.presentation.developer { DirectoryInfo di = new DirectoryInfo(path); - string rootDir = IOHelper.MapPath(SystemDirectories.Usercontrols); + string rootDir = IOHelper.MapPath(SystemDirectories.UserControls); foreach (FileInfo uc in di.GetFiles("*.ascx")) { userControlList.Items.Add( - new ListItem(SystemDirectories.Usercontrols + + new ListItem(SystemDirectories.UserControls + uc.FullName.Substring(rootDir.Length).Replace(IOHelper.DirSepChar, '/'))); /* uc.FullName.IndexOf(usercontrolsDir), diff --git a/src/umbraco.cms/businesslogic/macro/Macro.cs b/src/umbraco.cms/businesslogic/macro/Macro.cs index 8000045b6e..029c77e728 100644 --- a/src/umbraco.cms/businesslogic/macro/Macro.cs +++ b/src/umbraco.cms/businesslogic/macro/Macro.cs @@ -131,10 +131,11 @@ namespace umbraco.cms.businesslogic.macro } /// - /// The relative path to the usercontrol - /// - /// Specified like: /usercontrols/myusercontrol.ascx (with the .ascx postfix) + /// The relative path to the usercontrol or the assembly type of the macro when using .Net custom controls /// + /// + /// When using a user control the value is specified like: /usercontrols/myusercontrol.ascx (with the .ascx postfix) + /// public string Type { get {return _type;} @@ -161,10 +162,13 @@ namespace umbraco.cms.businesslogic.macro } /// - /// The razor macro file to be executed - /// - /// Umbraco assumes that the python file is present in the "/python" folder + /// This field is used to store the file value for any scripting macro such as python, ruby, razor macros or Partial View Macros /// + /// + /// Depending on how the file is stored depends on what type of macro it is. For example if the file path is a full virtual path + /// starting with the ~/Views/MacroPartials then it is deemed to be a Partial View Macro, otherwise the file extension of the file + /// saved will determine which macro engine will be used to execute the file. + /// public string ScriptingFile { get { return _scriptingFile; } set {