// Delete canvasdesigner style
        internal static void DeleteStyle(int pageId)
        {
            // Get inherited tuned pageId and path
            var      contentService       = ApplicationContext.Current.Services.ContentService;
            IContent content              = contentService.GetById(pageId);
            int      inheritedTunedPageId = CanvasDesignerUtility.GetParentOrSelfTunedPageId(content.Path.Split(','), true);

            // Path to the less and css files
            string newResultLessPath = HttpContext.Current.Server.MapPath(string.Format(resultLessPath, inheritedTunedPageId));
            string newResultCssPath  = HttpContext.Current.Server.MapPath(string.Format(resultCssPath, inheritedTunedPageId));

            // Delete all style file for this page
            System.IO.File.Delete(newResultLessPath);
            System.IO.File.Delete(newResultCssPath);
        }
示例#2
0
        public static IHtmlString EnableCanvasDesigner(this HtmlHelper html,
                                                       UrlHelper url,
                                                       UmbracoContext umbCtx, string canvasdesignerConfigPath, string canvasdesignerPalettesPath)
        {
            var umbracoPath = url.Content(SystemDirectories.Umbraco);

            string previewLink = @"<script src=""{0}/lib/jquery/jquery.min.js"" type=""text/javascript""></script>" +
                                 @"<script src=""{1}"" type=""text/javascript""></script>" +
                                 @"<script src=""{2}"" type=""text/javascript""></script>" +
                                 @"<script type=""text/javascript"">var pageId = '{3}'</script>" +
                                 @"<script src=""{0}/js/canvasdesigner.front.js"" type=""text/javascript""></script>";

            string noPreviewLinks = @"<link href=""{1}"" type=""text/css"" rel=""stylesheet"" data-title=""canvasdesignerCss"" />";

            // 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));
        }
        // Save and publish less style
        internal static void SaveAndPublishStyle(string parameters, int pageId, bool inherited)
        {
            // Get inherited tuned pageId and path
            var      contentService       = ApplicationContext.Current.Services.ContentService;
            IContent content              = contentService.GetById(pageId);
            int      inheritedTunedPageId = CanvasDesignerUtility.GetParentOrSelfTunedPageId(content.Path.Split(','), true);

            // Load inherited Less content
            string inheritedLessContent = string.Empty;

            using (System.IO.StreamReader sr = new System.IO.StreamReader(HttpContext.Current.Server.MapPath(string.Format(resultLessPath, inheritedTunedPageId))))
            {
                inheritedLessContent = sr.ReadToEnd();
            }

            // Update pageId if parameters have changed
            if (inherited)
            {
                pageId = inheritedTunedPageId;
            }

            // Create front directory if necesary
            if (!Directory.Exists(frontBasePath))
            {
                Directory.CreateDirectory(frontBasePath);
            }

            // Prepare parameters and gf block and replace with the new value
            string newParamBlock = string.Empty;
            string newGfBlock    = string.Empty;

            foreach (string parameter in parameters.Trim().Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
            {
                if (parameter.IndexOf("@import") < 0)
                {
                    string name  = parameter.Substring(0, parameter.IndexOf(":"));
                    string value = parameter.Substring(parameter.IndexOf(":") + 1);
                    if (string.IsNullOrEmpty(value))
                    {
                        value = "''";
                    }
                    inheritedLessContent = Regex.Replace(inheritedLessContent, string.Format("{0}:([^;\n]*);", name), string.Format("{0}:{1};", name, value));
                }
                else
                {
                    newGfBlock += parameter + ";" + Environment.NewLine;
                }
            }

            // Save less file
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(HttpContext.Current.Server.MapPath(string.Format(resultLessPath, pageId))))
            {
                file.Write(inheritedLessContent);
            }

            // Compile the Less file
            string compiledStyle = GetCssFromLessString(newGfBlock + inheritedLessContent, false, true, true).Replace("@import", "@IMPORT");

            // Save compiled file
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(HttpContext.Current.Server.MapPath(string.Format(resultCssPath, pageId))))
            {
                file.Write(compiledStyle);
            }
        }