Exemple #1
0
        /// <summary>
        /// Registers javascript reference.
        /// </summary>
        /// <param name="scriptPath">The path to the javascript file.</param>
        /// <param name="throwException">OPTIONAL: Indicates whether to throw an exception if the javascript is already registered. By default the value is set to <value>false</value>.</param>
        /// <returns>MvcHtmlString</returns>
        public static MvcHtmlString Script(this HtmlHelper helper, string scriptPath, bool throwException = false)
        {
            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("JsRegister", helper.ViewContext.HttpContext);

            return ResourceHelper.RegisterResource(register, scriptPath, throwException, tagName: "script", attribbutes: attributes);
        }
        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);
        }
        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);
        }
        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);
        }
Exemple #5
0
        /// <summary>
        /// Registers resource reference.
        /// </summary>
        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;
        }
        /// <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 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);
        }
        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;
        }
        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));
                    resourceRegister.MarkAsRendered(resource);
                }
            }

            return output.ToString();
        }
        /// <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;
        }
Exemple #10
0
        /// <summary>
        /// Renders all scripts.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="sectionName">The section name.</param>
        /// <returns></returns>
        internal static string RenderAllScripts(HttpContextBase context, string sectionName)
        {
            var scriptRegister = new ResourceRegister(ResourceHelper.JsRegisterName, context);
            var scriptMarkup = ResourceHelper.BuildHtmlResourcesMarkup(scriptRegister, sectionName, ResourceType.Js);

            return scriptMarkup;
        }
        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);
        }