From 99e7f84f79da6abcd41a98273278d6448f88cc2e Mon Sep 17 00:00:00 2001 From: Shannon Date: Fri, 16 Oct 2015 13:36:10 +0200 Subject: [PATCH] Moves EnableCanvasDesigner to HtmlHelperRenderExtensions because it's for rendering html, obsoletes the old ones and fixes up the canvasdesigner to use the correct umbraco paths instead of hard coded absolute paths. --- src/Umbraco.Web/GridTemplateExtensions.cs | 10 +-- src/Umbraco.Web/HtmlHelperRenderExtensions.cs | 71 +++++++++++++++++-- src/Umbraco.Web/UmbracoHelper.cs | 59 +++++++-------- 3 files changed, 99 insertions(+), 41 deletions(-) diff --git a/src/Umbraco.Web/GridTemplateExtensions.cs b/src/Umbraco.Web/GridTemplateExtensions.cs index ca75552e51..cf267897d4 100644 --- a/src/Umbraco.Web/GridTemplateExtensions.cs +++ b/src/Umbraco.Web/GridTemplateExtensions.cs @@ -87,7 +87,7 @@ namespace Umbraco.Web } - //[Obsolete("This should not be used, GetGridHtml methods accepting HtmlHelper as a parameter or GetGridHtml extensions on HtmlHelper should be used instead")] + [Obsolete("This should not be used, GetGridHtml methods accepting HtmlHelper as a parameter or GetGridHtml extensions on HtmlHelper should be used instead")] public static MvcHtmlString GetGridHtml(this IPublishedProperty property, string framework = "bootstrap3") { var asString = property.Value as string; @@ -97,13 +97,13 @@ namespace Umbraco.Web return htmlHelper.GetGridHtml(property, framework); } - //[Obsolete("This should not be used, GetGridHtml methods accepting HtmlHelper as a parameter or GetGridHtml extensions on HtmlHelper should be used instead")] + [Obsolete("This should not be used, GetGridHtml methods accepting HtmlHelper as a parameter or GetGridHtml extensions on HtmlHelper should be used instead")] public static MvcHtmlString GetGridHtml(this IPublishedContent contentItem) { return GetGridHtml(contentItem, "bodyText", "bootstrap3"); } - //[Obsolete("This should not be used, GetGridHtml methods accepting HtmlHelper as a parameter or GetGridHtml extensions on HtmlHelper should be used instead")] + [Obsolete("This should not be used, GetGridHtml methods accepting HtmlHelper as a parameter or GetGridHtml extensions on HtmlHelper should be used instead")] public static MvcHtmlString GetGridHtml(this IPublishedContent contentItem, string propertyAlias) { Mandate.ParameterNotNullOrEmpty(propertyAlias, "propertyAlias"); @@ -111,7 +111,7 @@ namespace Umbraco.Web return GetGridHtml(contentItem, propertyAlias, "bootstrap3"); } - //[Obsolete("This should not be used, GetGridHtml methods accepting HtmlHelper as a parameter or GetGridHtml extensions on HtmlHelper should be used instead")] + [Obsolete("This should not be used, GetGridHtml methods accepting HtmlHelper as a parameter or GetGridHtml extensions on HtmlHelper should be used instead")] public static MvcHtmlString GetGridHtml(this IPublishedContent contentItem, string propertyAlias, string framework) { Mandate.ParameterNotNullOrEmpty(propertyAlias, "propertyAlias"); @@ -127,7 +127,7 @@ namespace Umbraco.Web return htmlHelper.GetGridHtml(contentItem, propertyAlias, framework); } - //[Obsolete("This shouldn't need to be used but because the obsolete extension methods above don't have access to the current HtmlHelper, we need to create a fake one, unfortunately however this will not pertain the current views viewdata, tempdata or model state so should not be used")] + [Obsolete("This shouldn't need to be used but because the obsolete extension methods above don't have access to the current HtmlHelper, we need to create a fake one, unfortunately however this will not pertain the current views viewdata, tempdata or model state so should not be used")] private static HtmlHelper CreateHtmlHelper(object model) { var cc = new ControllerContext diff --git a/src/Umbraco.Web/HtmlHelperRenderExtensions.cs b/src/Umbraco.Web/HtmlHelperRenderExtensions.cs index 7ae519a44b..918bf72844 100644 --- a/src/Umbraco.Web/HtmlHelperRenderExtensions.cs +++ b/src/Umbraco.Web/HtmlHelperRenderExtensions.cs @@ -756,8 +756,7 @@ namespace Umbraco.Web } #endregion - - + #region Wrap public static HtmlTagWrapper Wrap(this HtmlHelper html, string tag, string innerText, params IHtmlTagWrapper[] children) @@ -823,7 +822,71 @@ namespace Umbraco.Web return item; } - #endregion + #endregion - } + #region canvasdesigner + + public static IHtmlString EnableCanvasDesigner(this HtmlHelper html, + UrlHelper url, + UmbracoContext umbCtx) + { + return html.EnableCanvasDesigner(url, umbCtx, string.Empty, string.Empty); + } + + public static IHtmlString EnableCanvasDesigner(this HtmlHelper html, + UrlHelper url, + UmbracoContext umbCtx, string canvasdesignerConfigPath) + { + return html.EnableCanvasDesigner(url, umbCtx, canvasdesignerConfigPath, string.Empty); + } + + public static IHtmlString EnableCanvasDesigner(this HtmlHelper html, + UrlHelper url, + UmbracoContext umbCtx, string canvasdesignerConfigPath, string canvasdesignerPalettesPath) + { + + var umbracoPath = url.Content(SystemDirectories.Umbraco); + + string previewLink = @"" + + @"" + + @"" + + @"" + + @""; + + string noPreviewLinks = @""; + + // Get page value + int pageId = umbCtx.PublishedContentRequest.UmbracoPage.PageID; + string[] path = umbCtx.PublishedContentRequest.UmbracoPage.SplitPath; + string result = string.Empty; + string cssPath = CanvasDesignerUtility.GetStylesheetPath(path, false); + + if (umbCtx.InPreviewMode) + { + canvasdesignerConfigPath = string.IsNullOrEmpty(canvasdesignerConfigPath) == false + ? canvasdesignerConfigPath + : string.Format("{0}/js/canvasdesigner.config.js", umbracoPath); + canvasdesignerPalettesPath = string.IsNullOrEmpty(canvasdesignerPalettesPath) == false + ? canvasdesignerPalettesPath + : string.Format("{0}/js/canvasdesigner.palettes.js", umbracoPath); + + if (string.IsNullOrEmpty(cssPath) == false) + result = string.Format(noPreviewLinks, cssPath) + Environment.NewLine; + + result = result + string.Format(previewLink, umbracoPath, canvasdesignerConfigPath, canvasdesignerPalettesPath, pageId); + } + else + { + // Get css path for current page + if (string.IsNullOrEmpty(cssPath) == false) + result = string.Format(noPreviewLinks, cssPath); + } + + return new HtmlString(result); + + } + + #endregion + + } } diff --git a/src/Umbraco.Web/UmbracoHelper.cs b/src/Umbraco.Web/UmbracoHelper.cs index 35797a4432..6d5e33b239 100644 --- a/src/Umbraco.Web/UmbracoHelper.cs +++ b/src/Umbraco.Web/UmbracoHelper.cs @@ -14,6 +14,9 @@ using Umbraco.Core.Xml; using Umbraco.Web.Routing; using Umbraco.Web.Security; using System.Collections.Generic; +using System.IO; +using System.Web.Mvc; +using System.Web.Routing; using Umbraco.Core.Cache; namespace Umbraco.Web @@ -1170,52 +1173,44 @@ namespace Umbraco.Web #region canvasdesigner - public HtmlString EnableCanvasDesigner() + [Obsolete("Use EnableCanvasDesigner on the HtmlHelper extensions instead")] + public IHtmlString EnableCanvasDesigner() { return EnableCanvasDesigner(string.Empty, string.Empty); } - public HtmlString EnableCanvasDesigner(string canvasdesignerConfigPath) + [Obsolete("Use EnableCanvasDesigner on the HtmlHelper extensions instead")] + public IHtmlString EnableCanvasDesigner(string canvasdesignerConfigPath) { return EnableCanvasDesigner(canvasdesignerConfigPath, string.Empty); } - public HtmlString EnableCanvasDesigner(string canvasdesignerConfigPath, string canvasdesignerPalettesPath) + [Obsolete("Use EnableCanvasDesigner on the HtmlHelper extensions instead")] + public IHtmlString EnableCanvasDesigner(string canvasdesignerConfigPath, string canvasdesignerPalettesPath) { + var html = CreateHtmlHelper(""); + var urlHelper = new UrlHelper(UmbracoContext.HttpContext.Request.RequestContext); + return html.EnableCanvasDesigner(urlHelper, UmbracoContext, canvasdesignerConfigPath, canvasdesignerPalettesPath); + } - string previewLink = @"" + - @"" + - @"" + - @"" + - @""; - - string noPreviewLinks = @""; - - // Get page value - int pageId = UmbracoContext.PublishedContentRequest.UmbracoPage.PageID; - string[] path = UmbracoContext.PublishedContentRequest.UmbracoPage.SplitPath; - string result = string.Empty; - string cssPath = CanvasDesignerUtility.GetStylesheetPath(path, false); - - if (UmbracoContext.Current.InPreviewMode) + [Obsolete("This shouldn't need to be used but because the obsolete extension methods above don't have access to the current HtmlHelper, we need to create a fake one, unfortunately however this will not pertain the current views viewdata, tempdata or model state so should not be used")] + private HtmlHelper CreateHtmlHelper(object model) + { + var cc = new ControllerContext { - canvasdesignerConfigPath = !string.IsNullOrEmpty(canvasdesignerConfigPath) ? canvasdesignerConfigPath : "/umbraco/js/canvasdesigner.config.js"; - canvasdesignerPalettesPath = !string.IsNullOrEmpty(canvasdesignerPalettesPath) ? canvasdesignerPalettesPath : "/umbraco/js/canvasdesigner.palettes.js"; - - if (!string.IsNullOrEmpty(cssPath)) - result = string.Format(noPreviewLinks, cssPath) + Environment.NewLine; + RequestContext = UmbracoContext.HttpContext.Request.RequestContext + }; + var viewContext = new ViewContext(cc, new FakeView(), new ViewDataDictionary(model), new TempDataDictionary(), new StringWriter()); + var htmlHelper = new HtmlHelper(viewContext, new ViewPage()); + return htmlHelper; + } - result = result + string.Format(previewLink, canvasdesignerConfigPath, canvasdesignerPalettesPath, pageId); - } - else + [Obsolete("This shouldn't need to be used but because the obsolete extension methods above don't have access to the current HtmlHelper, we need to create a fake one, unfortunately however this will not pertain the current views viewdata, tempdata or model state so should not be used")] + private class FakeView : IView + { + public void Render(ViewContext viewContext, TextWriter writer) { - // Get css path for current page - if (!string.IsNullOrEmpty(cssPath)) - result = string.Format(noPreviewLinks, cssPath); } - - return new HtmlString(result); - } #endregion