Esempio n. 1
0
        /// <summary>
        /// Renders all stylesheets.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="sectionName">The section name.</param>
        /// <returns></returns>
        internal static string RenderAllStylesheets(HttpContextBase context, string sectionName)
        {
            var stylesheetRegister = new ResourceRegister(ResourceHelper.CssRegisterName, context);
            var stylesheetMarkup   = ResourceHelper.BuildHtmlResourcesMarkup(stylesheetRegister, sectionName, ResourceType.Css);

            return(stylesheetMarkup);
        }
Esempio n. 2
0
        private static MvcHtmlString RegisterResource(HttpContextBase httpContext, string resourcePath, ResourceType resourceType, string sectionName, bool throwException)
        {
            throwException = throwException && httpContext.CurrentHandler != null;

            var registerName = string.Empty;

            if (resourceType == ResourceType.Js)
            {
                registerName = ResourceHelper.JsRegisterName;
            }
            else if (resourceType == ResourceType.Css)
            {
                registerName = ResourceHelper.CssRegisterName;
            }

            var register = new ResourceRegister(registerName, httpContext);

            MvcHtmlString result = MvcHtmlString.Empty;

            // No section name renders the script inline if it hasn't been rendered
            if (string.IsNullOrEmpty(sectionName) || !SectionRenderer.IsAvailable(httpContext.Handler.GetPageHandler(), sectionName))
            {
                if (!register.IsRegistered(resourcePath, sectionName: null))
                {
                    result = MvcHtmlString.Create(ResourceHelper.BuildSingleResourceMarkup(resourcePath, resourceType));
                }
            }

            // Register the resource even if it had to be rendered inline (avoid repetitions).
            register.Register(resourcePath, sectionName, throwException);

            return(result);
        }
        protected RequireSettings IncludeResource(string resourceType, string resourcePath, Context context)
        {
            var workContext     = context.GetWorkContext();
            var resourceManager = workContext.Resolve <IResourceManager>();
            var pathResolver    = workContext.Resolve <ITemplateItemProvidedPathResolver>();

            var renderingContext = context.GetTemplateRenderingContext();

            // If a template file is being rendered then resources paths can be used as usual from themes; if a
            // template item is rendered then relative virtual paths should be handled the same way (those reference
            // flat files).
            if (renderingContext.TemplateType == Models.TemplateType.TemplateFile ||
                (resourcePath.StartsWith("~") && pathResolver.IsRealVirtualPath(resourcePath)))
            {
                var resourceRegister = new ResourceRegister(
                    new DummyWebPage {
                    VirtualPath = renderingContext.TemplatePath
                },
                    resourceManager,
                    resourceType);
                return(resourceRegister.Include(resourcePath));
            }
            else
            {
                if (!resourcePath.StartsWith("//") &&
                    !resourcePath.StartsWith("http://", StringComparison.OrdinalIgnoreCase) &&
                    !resourcePath.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
                {
                    resourcePath = pathResolver.GenerateUrlFromPath(resourcePath);
                }

                return(resourceManager.Include(resourceType, resourcePath, resourcePath));
            }
        }
Esempio n. 4
0
        private static MvcHtmlString RegisterResource(HttpContextBase httpContext, string resourcePath, ResourceType resourceType, string sectionName, bool throwException, List <KeyValuePair <string, string> > attributes = null)
        {
            throwException = throwException && httpContext.CurrentHandler != null;

            var registerName = string.Empty;

            if (resourceType == ResourceType.Js)
            {
                registerName = ResourceHelper.JsRegisterName;
            }
            else if (resourceType == ResourceType.Css)
            {
                registerName = ResourceHelper.CssRegisterName;
            }

            var register = new ResourceRegister(registerName, httpContext);

            MvcHtmlString result = MvcHtmlString.Empty;

            if (!string.IsNullOrWhiteSpace(sectionName))
            {
                var pageHandler = httpContext.Handler.GetPageHandler();
                if (pageHandler != null && pageHandler.Master is MvcMasterPage)
                {
                    if (!throwException && !SectionRenderer.IsAvailable(pageHandler, sectionName))
                    {
                        sectionName = null;
                    }
                }
                else
                {
                    if (!SectionRenderer.IsAvailable(pageHandler, sectionName))
                    {
                        sectionName = null;
                    }
                }
            }
            else
            {
                sectionName = null;
            }

            // No section name renders the script inline if it hasn't been rendered
            if (sectionName == null ||
                ResourceHelper.RenderScriptSection)
            {
                if (!register.IsRegistered(resourcePath))
                {
                    result = MvcHtmlString.Create(ResourceHelper.BuildSingleResourceMarkup(resourcePath, resourceType, sectionName, attributes));
                }
            }

            // Register the resource even if it had to be rendered inline (avoid repetitions).
            register.Register(resourcePath, sectionName, throwException, attributes);

            return(result);
        }
Esempio n. 5
0
        /// <summary>
        /// Registers the resource.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="scriptKey">The script key.</param>
        /// <param name="scriptPath">The script path.</param>
        /// <param name="throwException">if set to <c>true</c> throws exception.</param>
        /// <returns></returns>
        private static System.Web.Mvc.MvcHtmlString RegisterResource(HttpContextBase context, string scriptKey, string scriptPath, bool throwException)
        {
            var attributes = new KeyValuePair <string, string> [2];

            attributes[0] = new KeyValuePair <string, string>("src", scriptPath);
            attributes[1] = new KeyValuePair <string, string>("type", "text/javascript");

            var register = new ResourceRegister(ResourceHelper.JsRegisterName, context);

            return(ResourceHelper.RegisterResource(register, scriptKey, throwException, tagName: "script", attribbutes: attributes));
        }
Esempio n. 6
0
        private static string BuildHtmlResourcesMarkup(ResourceRegister resourceRegister, string sectionName, ResourceType resourceType)
        {
            StringBuilder output = new StringBuilder();

            foreach (var resource in resourceRegister.GetResourcesForSection(sectionName))
            {
                if (!resourceRegister.IsRendered(resource))
                {
                    output.Append(ResourceHelper.BuildSingleResourceMarkup(resource, resourceType, sectionName));
                    resourceRegister.MarkAsRendered(resource);
                }
            }

            return(output.ToString());
        }
Esempio n. 7
0
        public void RegisterResource_NewResource_ResourceIsRegistered()
        {
            // Arrange
            string          registerName = "TestRegister";
            HttpContextBase context      = this.CreateHttpContext();
            var             register     = new ResourceRegister(registerName, context);

            string fakeResourceKey = "test-resource";

            Assert.IsTrue(register.GetInlineResources().Count(i => i == fakeResourceKey) == 0);

            // Act
            register.Register(fakeResourceKey);

            // Assert
            Assert.IsTrue(register.GetInlineResources().Count(i => i == fakeResourceKey) == 1);
        }
Esempio n. 8
0
        public void RegisterResource_AlreadyRegisteredResource_ExceptionIsThrown()
        {
            // Arrange
            string          registerName = "TestRegister";
            HttpContextBase context      = this.CreateHttpContext();
            var             register     = new ResourceRegister(registerName, context);

            string fakeResourceKey = "test-resource";

            register.Register(fakeResourceKey);
            Assert.IsTrue(register.GetInlineResources().Count(i => i == fakeResourceKey) == 1);

            // Act
            register.Register(fakeResourceKey, throwException: true);

            // Assert
            Assert.IsTrue(register.GetInlineResources().Count(i => i == fakeResourceKey) == 1);
        }
Esempio n. 9
0
        public void TryRegisterResource_NewResource_ResourceIsRegistered()
        {
            // Arrange
            string          registerName = "TestRegister";
            HttpContextBase context      = this.CreateHttpContext();
            var             register     = new ResourceRegister(registerName, context);

            string fakeResourceKey = "test-resource";

            Assert.IsTrue(register.Container.Count(i => i == fakeResourceKey) == 0);

            // Act
            bool result = register.TryRegisterResource(fakeResourceKey);

            // Assert
            Assert.IsTrue(result);
            Assert.IsTrue(register.Container.Count(i => i == fakeResourceKey) == 1);
        }
Esempio n. 10
0
        public void TryRegisterResource_AlreadyRegisteredResource_ResourceIsNotRegisteredTwice()
        {
            // Arrange
            string          registerName = "TestRegister";
            HttpContextBase context      = this.CreateHttpContext();
            var             register     = new ResourceRegister(registerName, context);

            string fakeResourceKey = "test-resource";

            register.Register(fakeResourceKey);
            Assert.IsTrue(register.GetInlineResources().Count(i => i == fakeResourceKey) == 1);

            // Act
            bool result = register.Register(fakeResourceKey);

            // Assert
            Assert.IsFalse(result);
            Assert.IsTrue(register.GetInlineResources().Count(i => i == fakeResourceKey) == 1);
        }
Esempio n. 11
0
        /// <summary>
        /// Registers resource reference.
        /// </summary>
        /// <param name="register">The register.</param>
        /// <param name="resourceKey">The resource key.</param>
        /// <param name="throwException">if set to <c>true</c> [throw exception].</param>
        /// <param name="tagName">Name of the tag.</param>
        /// <param name="attribbutes">The attribbutes.</param>
        /// <returns></returns>
        private static MvcHtmlString RegisterResource(ResourceRegister register, string resourceKey, bool throwException, string tagName, KeyValuePair <string, string>[] attribbutes)
        {
            string        output;
            MvcHtmlString result;

            if (throwException)
            {
                register.RegisterResource(resourceKey);
                output = ResourceHelper.GenerateTag(tagName, attribbutes);
                result = new MvcHtmlString(output);
            }
            else if (register.TryRegisterResource(resourceKey))
            {
                output = ResourceHelper.GenerateTag(tagName, attribbutes);
                result = new MvcHtmlString(output);
            }
            else
            {
                result = MvcHtmlString.Empty;
            }

            return(result);
        }
Esempio n. 12
0
        public void IncludeSkin(ResourceRegister Style, ResourceRegister Script, string settingsName)
        {
            var skinPart         = GetConfigurationPart(settingsName);
            var manifest         = GetSkinsManifest();
            var allowedSkinNames = GetSkinNames();

            if (manifest != null && skinPart != null)
            {
                var selectedSkin = manifest.Skins
                                   .FirstOrDefault(tsd => allowedSkinNames.Contains(tsd.Name) &&
                                                   tsd.Name.Equals(skinPart.SkinName));
                // there may be a Default skin configured in the manifest, to be used
                // when there is nothing selected in the skinPart
                if (selectedSkin == null && string.IsNullOrWhiteSpace(skinPart.SkinName))
                {
                    selectedSkin = manifest.Skins.FirstOrDefault(tsd => tsd.Name.Equals("Default", StringComparison.OrdinalIgnoreCase));
                }
                if (selectedSkin != null)
                {
                    // add css files to head of page
                    if (selectedSkin.StyleSheets != null)
                    {
                        foreach (var cssName in selectedSkin.StyleSheets)
                        {
                            var debugPath    = GetStyleSheet(cssName);
                            var resourcePath = GetStyleSheet(cssName, true);
                            if (string.IsNullOrWhiteSpace(resourcePath))
                            {
                                resourcePath = debugPath;
                            }
                            if (!string.IsNullOrWhiteSpace(resourcePath))
                            {
                                Style.Include(debugPath, resourcePath).AtHead();
                            }
                        }
                    }
                    // add scripts to head of page
                    if (selectedSkin.HeadScripts != null)
                    {
                        foreach (var scriptName in selectedSkin.HeadScripts)
                        {
                            var debugPath    = GetScript(scriptName);
                            var resourcePath = GetScript(scriptName, true);
                            if (string.IsNullOrWhiteSpace(resourcePath))
                            {
                                resourcePath = debugPath;
                            }
                            if (!string.IsNullOrWhiteSpace(resourcePath))
                            {
                                Script.Include(debugPath, resourcePath).AtHead();
                            }
                        }
                    }
                    // add scripts to foot of page
                    if (selectedSkin.FootScripts != null)
                    {
                        foreach (var scriptName in selectedSkin.FootScripts)
                        {
                            var debugPath    = GetScript(scriptName);
                            var resourcePath = GetScript(scriptName, true);
                            if (string.IsNullOrWhiteSpace(resourcePath))
                            {
                                resourcePath = debugPath;
                            }
                            if (!string.IsNullOrWhiteSpace(resourcePath))
                            {
                                Script.Include(debugPath, resourcePath).AtFoot();
                            }
                        }
                    }
                }
                // add variables that are configured in the part
                var configuredVariables = skinPart.Variables.Where(v => !string.IsNullOrWhiteSpace(v.Value));
                if (configuredVariables.Any())
                {
                    // create the style to add to the head of the page
                    var sb = new StringBuilder();
                    sb.AppendLine("<style>");
                    sb.AppendLine(":root {");
                    foreach (var variable in configuredVariables)
                    {
                        sb.AppendLine(string.Format("{0}: {1};", variable.Name, variable.Value));
                    }
                    sb.AppendLine("}");
                    sb.AppendLine("</style>");
                    _resourceManager.RegisterHeadScript(sb.ToString());
                }
            }
        }