Beispiel #1
0
        /// <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.
        /// <param name="attributes">A list of attribute key value pairs to be added to the script</param>
        /// 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, List <KeyValuePair <string, string> > attributes)
        {
            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, attributes));
            }

            return(MvcHtmlString.Create(outputMarkup.ToString()));
        }