Ejemplo n.º 1
0
        /// <summary>
        /// Setup RequireJS to be used in layouts
        /// </summary>
        /// <param name="html">
        /// Html helper.
        /// </param>
        /// <param name="config">
        /// Configuration object for various options.
        /// </param>
        /// <returns>
        /// The <see cref="MvcHtmlString"/>.
        /// </returns>
        public static MvcHtmlString RenderRequireJsSetup(
            this HtmlHelper html,
            RequireRendererConfiguration config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            var entryPointPath = html.RequireJsEntryPoint(config.BaseUrl, config.EntryPointRoot);

            if (entryPointPath == null)
            {
                return(new MvcHtmlString(string.Empty));
            }

            if (config.ConfigurationFiles == null || !config.ConfigurationFiles.Any())
            {
                throw new Exception("No config files to load.");
            }

            var processedConfigs = config.ConfigurationFiles.Select(r =>
            {
                var resultingPath = html.ViewContext.HttpContext.MapPath(r);
                PathHelpers.VerifyFileExists(resultingPath);
                return(resultingPath);
            }).ToList();

            var resultingConfig = GetCachedOverridenConfig(processedConfigs, config, entryPointPath.ToString());

            var locale = config.LocaleSelector(html);

            var outputConfig = createOutputConfigFrom(resultingConfig, config, locale);

            var options = createOptionsFrom(html.ViewContext.HttpContext, config, locale);

            var configBuilder = new JavaScriptBuilder();

            configBuilder.AddStatement(JavaScriptHelpers.SerializeAsVariable(options, "requireConfig"));
            configBuilder.AddStatement(JavaScriptHelpers.SerializeAsVariable(outputConfig, "require"));

            var requireRootBuilder = new JavaScriptBuilder();

            requireRootBuilder.AddAttributesToStatement("src", config.RequireJsUrl);

            var requireEntryPointBuilder = new JavaScriptBuilder();

            requireEntryPointBuilder.AddStatement(
                JavaScriptHelpers.MethodCall(
                    "require",
                    (object)new[] { entryPointPath.ToString() }));

            return(new MvcHtmlString(
                       configBuilder.Render()
                       + Environment.NewLine
                       + requireRootBuilder.Render()
                       + Environment.NewLine
                       + requireEntryPointBuilder.Render()));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Setup RequireJS to be used in layouts
        /// </summary>
        /// <param name="html">
        /// Html helper.
        /// </param>
        /// <param name="config">
        /// Configuration object for various options.
        /// </param>
        /// <returns>
        /// The <see cref="MvcHtmlString"/>.
        /// </returns>
        public static MvcHtmlString RenderRequireJsSetup(
            this HtmlHelper html,
            RequireRendererConfiguration config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            var entryPointPath = html.RequireJsEntryPoint(config.BaseUrl, config.EntryPointRoot);

            if (entryPointPath == null)
            {
                return(new MvcHtmlString(string.Empty));
            }

            if (config.ConfigurationFiles == null || !config.ConfigurationFiles.Any())
            {
                throw new Exception("No config files to load.");
            }

            var processedConfigs = config.ConfigurationFiles.Select(r =>
            {
                var resultingPath = html.ViewContext.HttpContext.MapPath(r);
                PathHelpers.VerifyFileExists(resultingPath);
                return(resultingPath);
            }).ToList();

            var resultingConfig = GetCachedOverridenConfig(processedConfigs, config, entryPointPath.ToString());

            var locale = config.LocaleSelector(html);

            var outputConfig = new JsonRequireOutput
            {
                BaseUrl     = config.BaseUrl,
                Locale      = locale,
                UrlArgs     = config.UrlArgs,
                WaitSeconds = config.WaitSeconds,
                Paths       = resultingConfig.Paths.PathList.ToDictionary(r => r.Key, r => r.Value),
                Shim        = resultingConfig.Shim.ShimEntries.ToDictionary(
                    r => r.For,
                    r => new JsonRequireDeps
                {
                    Dependencies = r.Dependencies.Select(x => x.Dependency).ToList(),
                    Exports      = r.Exports
                }),
                Map = resultingConfig.Map.MapElements.ToDictionary(
                    r => r.For,
                    r => r.Replacements.ToDictionary(x => x.OldKey, x => x.NewKey))
            };

            config.ProcessConfig(outputConfig);

            var options = new JsonRequireOptions
            {
                Locale         = locale,
                PageOptions    = RequireJsOptions.GetPageOptions(html.ViewContext.HttpContext),
                WebsiteOptions = RequireJsOptions.GetGlobalOptions(html.ViewContext.HttpContext)
            };

            config.ProcessOptions(options);

            var configBuilder = new JavaScriptBuilder();

            configBuilder.AddStatement(JavaScriptHelpers.SerializeAsVariable(options, "requireConfig"));
            configBuilder.AddStatement(JavaScriptHelpers.SerializeAsVariable(outputConfig, "require"));

            var requireRootBuilder = new JavaScriptBuilder();

            requireRootBuilder.AddAttributesToStatement("src", config.RequireJsUrl);

            var requireEntryPointBuilder = new JavaScriptBuilder();

            requireEntryPointBuilder.AddStatement(
                JavaScriptHelpers.MethodCall(
                    "require",
                    (object)new[] { entryPointPath.ToString() }));

            return(new MvcHtmlString(
                       configBuilder.Render()
                       + Environment.NewLine
                       + requireRootBuilder.Render()
                       + Environment.NewLine
                       + requireEntryPointBuilder.Render()));
        }