/// <summary>
        /// Registers JavaScript reference and ensures that it loads maximum once for a page.
        /// </summary>
        /// <param name="helper">The helper.</param>
        /// <param name="scriptPath">The path to the JavaScript file.</param>
        /// <param name="sectionName">The name of the section that will render this script. If null it will render on the same place of the page</param>
        /// <param name="throwException">Indicates whether to throw an exception if the specified section does not exist.</param>
        /// <param name="tryUseScriptManager">Indicates whether to use script manager(if exists) when register JavaScript reference.
        /// If it is used the script will always be loaded on the top section of the page.</param>
        /// <returns>
        /// MvcHtmlString
        /// </returns>
        /// <remarks>
        /// This method uses directly the resource from the <see cref="scriptPath" />.
        /// In case you want to use embedded scripts from Sitefinity check <see cref="ResourceHelper.Script(this HtmlHelper helper, ScriptRef scriptReference, bool throwException = false)" />.
        /// </remarks>
        public static MvcHtmlString Script(this HtmlHelper helper, string scriptPath, string sectionName, bool throwException, bool tryUseScriptManager)
        {
            if (tryUseScriptManager && ResourceHelper.TryConfigureScriptManager(scriptPath, helper.ViewContext.HttpContext.CurrentHandler))
            {
                return(MvcHtmlString.Empty);
            }

            return(ResourceHelper.RegisterResource(helper.ViewContext.HttpContext, scriptPath, ResourceType.Js, sectionName, throwException));
        }
Beispiel #2
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));
        }
Beispiel #3
0
        public static System.Web.Mvc.MvcHtmlString Script(this HtmlHelper helper, string type, string embeddedScriptPath, bool throwException = false)
        {
            var context = helper.ViewContext.HttpContext;
            var page    = HttpContext.Current.Handler as Page ?? new PageProxy(null);

            var resourceUrl = page.ClientScript.GetWebResourceUrl(
                TypeResolutionService.ResolveType(type),
                embeddedScriptPath);

            return(ResourceHelper.RegisterResource(context, resourceUrl, resourceUrl, throwException));
        }
Beispiel #4
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 ClientResourceRegister("JsRegister", "script", "src");

            return(ResourceHelper.RegisterResource(register, attributes, throwException));
        }
Beispiel #5
0
        public static System.Web.Mvc.MvcHtmlString Script(this HtmlHelper helper, ScriptRef scriptReference, bool throwException = false)
        {
            if (ResourceHelper.TryConfigureScriptManager(scriptReference))
            {
                return(System.Web.Mvc.MvcHtmlString.Empty);
            }

            var resourceKey = scriptReference.ToString();
            var context     = helper.ViewContext.HttpContext;
            var resourceUrl = ResourceHelper.GetWebResourceUrl(scriptReference);

            if (string.IsNullOrEmpty(resourceUrl))
            {
                return(System.Web.Mvc.MvcHtmlString.Empty);
            }

            return(ResourceHelper.RegisterResource(context, resourceKey, resourceUrl, throwException));
        }
        public static MvcHtmlString Script(this HtmlHelper helper, ScriptRef scriptReference, bool throwException = false)
        {
            if (ResourceHelper.TryConfigureScriptManager(scriptReference))
            {
                return(MvcHtmlString.Empty);
            }

            var resourceKey = scriptReference.ToString();
            var context     = helper.ViewContext.HttpContext;
            var resourceUrl = ResourceHelper.GetWebResourceUrl(scriptReference);

            if (string.IsNullOrEmpty(resourceUrl))
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Script reference for {0} is not found.", resourceKey));
            }

            return(ResourceHelper.RegisterResource(context, resourceKey, resourceUrl, throwException));
        }
        /// <summary>
        /// Registers JavaScript reference and ensures that it loads maximum once for a page.
        /// </summary>
        /// <remarks>
        /// This helper references the same resource existing in Sitefinity.
        /// </remarks>
        /// <param name="helper">The helper.</param>
        /// <param name="scriptReference">The script reference.</param>
        /// <param name="sectionName">The name of the section that will render this script. If null it will render on the same place of the page</param>
        /// <param name="throwException">Indicates whether to throw an exception if the specified section does not exist.</param>
        /// <param name="tryUseScriptManager">Indicates whether to use script manager (if exists) when register JavaScript reference.
        /// If it is used the script will always be loaded on the top section of the page.</param>
        /// <returns>
        /// MvcHtmlString
        /// </returns>
        public static MvcHtmlString Script(this HtmlHelper helper, ScriptRef scriptReference, string sectionName, bool throwException, bool tryUseScriptManager)
        {
            if (tryUseScriptManager && ResourceHelper.TryConfigureScriptManager(scriptReference, helper.ViewContext.HttpContext.CurrentHandler))
            {
                return(System.Web.Mvc.MvcHtmlString.Empty);
            }

            var references = PageManager.GetScriptReferences(scriptReference).Select(r => new MvcScriptReference(r));

            StringBuilder outputMarkup = new StringBuilder();

            foreach (var script in references)
            {
                var resourceUrl = script.GetResourceUrl();
                outputMarkup.Append(ResourceHelper.RegisterResource(helper.ViewContext.HttpContext, resourceUrl, ResourceType.Js, sectionName, throwException));
            }

            return(MvcHtmlString.Create(outputMarkup.ToString()));
        }
 /// <summary>
 /// Registers style sheet reference and ensures that it loads maximum once for a page.
 /// </summary>
 /// <param name="helper">The helper.</param>
 /// <param name="resourcePath">The path to the CSS file.</param>
 /// <param name="sectionName">The name of the section that will render this script. If null it will render on the same place of the page</param>
 /// <param name="throwException">Indicates whether to throw an exception if the specified section does not exist.</param>
 /// <returns>
 /// MvcHtmlString
 /// </returns>
 public static MvcHtmlString StyleSheet(this HtmlHelper helper, string resourcePath, string sectionName, bool throwException)
 {
     return(ResourceHelper.RegisterResource(helper.ViewContext.HttpContext, resourcePath, ResourceType.Css, sectionName, throwException));
 }
Beispiel #9
0
        /// <summary>
        /// Registers JavaScript reference and ensures that it loads maximum once for a page.
        /// </summary>
        /// <remarks>
        /// This method uses directly the resource from the <see cref="scriptPath"/>.
        /// In case you want to use embedded scripts from Sitefinity check <see cref="ResourceHelper.Script(this HtmlHelper helper, ScriptRef scriptReference, bool throwException = false)"/>.
        /// </remarks>
        /// <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 System.Web.Mvc.MvcHtmlString Script(this HtmlHelper helper, string scriptPath, bool throwException = false)
        {
            var context = helper.ViewContext.HttpContext;

            return(ResourceHelper.RegisterResource(context, scriptPath, scriptPath, throwException));
        }
Beispiel #10
0
 /// <summary>
 /// Registers style sheet reference and ensures that it loads maximum once for a page.
 /// </summary>
 /// <param name="helper">The helper.</param>
 /// <param name="resourcePath">The path to the CSS file.</param>
 /// <param name="sectionName">The name of the section that will render this script. If null it will render on the same place of the page</param>
 /// <param name="throwException">Indicates whether to throw an exception if the specified section does not exist.</param>
 /// <param name="attributes">A list of attribute key value pairs to be added to the stylesheet</param>
 /// <returns>
 /// MvcHtmlString
 /// </returns>
 public static MvcHtmlString StyleSheet(this HtmlHelper helper, string resourcePath, string sectionName, bool throwException, List <KeyValuePair <string, string> > attributes)
 {
     return(ResourceHelper.RegisterResource(helper.ViewContext.HttpContext, resourcePath, ResourceType.Css, sectionName, throwException, attributes));
 }