Exemplo 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()));
        }
        /// <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)
        {
            var result = RequireJsNet.RequireJsHtmlHelpers.RenderRequireJsSetup(html, config);
            var doc = new HtmlDocument();
            doc.LoadHtml(result.ToString());
            //doc.DocumentNode.SelectNodes()

            //return MvcHtmlString.Create(result);
            return result;
        }
Exemplo n.º 3
0
        private static ConfigurationCollection GetCachedOverridenConfig(
            List <string> processedConfigs,
            RequireRendererConfiguration config,
            string entryPointPath)
        {
            if (config.CacheConfigObject)
            {
                return(configObjectHash.GetOrSet(
                           ComputeConfigObjectHash(processedConfigs, entryPointPath),
                           () => GetOverridenConfig(processedConfigs, config, entryPointPath)));
            }

            return(GetOverridenConfig(processedConfigs, config, entryPointPath));
        }
Exemplo n.º 4
0
        private static ConfigurationCollection GetOverridenConfig(
            List <string> processedConfigs,
            RequireRendererConfiguration config,
            string entryPointPath)
        {
            var loader = new ConfigLoader(
                processedConfigs,
                config.Logger,
                new ConfigLoaderOptions
            {
                LoadOverrides = config.LoadOverrides,
                CachingPolicy = config.ConfigCachingPolicy
            });
            var resultingConfig = loader.Get();

            var overrider = new ConfigOverrider();

            overrider.Override(resultingConfig, entryPointPath.ToModuleName());

            return(resultingConfig);
        }
Exemplo n.º 5
0
        internal static JsonRequireOptions createOptionsFrom(System.Web.HttpContextBase httpContext, RequireRendererConfiguration config, string locale)
        {
            var options = new JsonRequireOptions
            {
                Locale         = locale,
                PageOptions    = RequireJsOptions.GetPageOptions(httpContext),
                WebsiteOptions = RequireJsOptions.GetGlobalOptions(httpContext)
            };

            config.ProcessOptions(options);
            return(options);
        }
Exemplo n.º 6
0
        internal static JsonRequireOutput createOutputConfigFrom(ConfigurationCollection resultingConfig, RequireRendererConfiguration config, string locale)
        {
            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),
                Packages    = resultingConfig.Packages.PackageList,
                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)),
                NodeIdCompat = resultingConfig.NodeIdCompat
            };

            config.ProcessConfig(outputConfig);

            return(outputConfig);
        }
        /// <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()));
        }