Beispiel #1
0
        // Set Default settings
        public static MvcHtmlString ResourceSettings(this HtmlHelper html, HTMLResourceOptions options)
        {
            var resources = GetResources(html);

            if (options.Bundle != null)
            {
                resources.Bundle = (bool)options.Bundle;
            }
            if (options.Debug != null)
            {
                resources.Debug = (bool)options.Debug;
            }
            if (options.Minify != null)
            {
                resources.Minify = (bool)options.Minify;
            }
            if (options.Strict != null)
            {
                resources.Strict = (bool)options.Strict;
            }
            if (options.Inline != null)
            {
                resources.Inline = (int)options.Inline;
            }
            if (options.CacheFolder != null)
            {
                resources.CacheFolder = options.CacheFolder;
                resources.StyleSheetFolder = options.StyleSheetFolder;
                resources.ScriptFolder = options.ScriptFolder;
                resources.ImageFolder = options.ImageFolder;
            }
            if (options.StyleSheetFolder != null)
            {
                resources.StyleSheetFolder = options.StyleSheetFolder;
            }
            if (options.ScriptFolder != null)
            {
                resources.ScriptFolder = options.ScriptFolder;
            }
            if (options.ImageFolder != null)
            {
                resources.ImageFolder = options.ImageFolder;
            }
            if (options.BundleTimeout != null)
            {
                resources.BundleTimeout = (int)options.BundleTimeout;
            }
            if (options.MimifyTimeout != null)
            {
                resources.MimifyTimeout = (int)options.MimifyTimeout;
            }
            if (options.ContextCacheLifetime != null)
            {
                resources.ContextCacheLifetime = (int)options.ContextCacheLifetime;
            }

            // TODO: If debug print settings as commented out HTML
            return null;
        }
Beispiel #2
0
 // Glob recursive options: html.Resource("~/Content/*.css", true, new ResourceOptions() { Bundle = false });
 public static MvcHtmlString Resource(this HtmlHelper html, string value, bool recursive, HTMLResourceOptions options)
 {
     return Resource(html, value, null, recursive, options, false);
 }
Beispiel #3
0
        public static MvcHtmlString Resource(this HtmlHelper html, string value, string regex, bool recursive, HTMLResourceOptions options, bool force)
        {
            var resources = GetResources(html);

            // TODO: Return cached copy here so we don't need to do more code parsing

            // Find out how deep we are in the page structure so we add the resources in the right order
            int depth = GetDepth(html);

            // Get helper class to convert path's
            var url = new UrlHelper(html.ViewContext.RequestContext);
            var server = html.ViewContext.RequestContext.HttpContext.Server;

            // Make sure the paths exist
            Directory.CreateDirectory(server.MapPath(resources.ScriptFolder));
            Directory.CreateDirectory(server.MapPath(resources.StyleSheetFolder));
            Directory.CreateDirectory(server.MapPath(resources.ImageFolder));

            FileInfo[] files = null;

            if (regex != null)
            {
                // TODO: Implement
            }
            // Try to glob for files if it's not a regular expression
            else
            {
                var filename = value.Substring(value.LastIndexOf('/') + 1);
                var asp_path = value.Substring(0, value.LastIndexOf('/') + 1);

                var di = new DirectoryInfo(Path.GetDirectoryName(server.MapPath(asp_path)));
                files = di.GetFiles(Path.GetFileName(filename), recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
            }

            // Throw exception if we are in strict mode and nothing was found
            if (files.Length == 0 && ((resources.Strict && resources.Strict != false) || resources.Strict == true))
            {
                throw new FileNotFoundException(String.Format("Could not find file {0}", value), value);
            }

            foreach (var info in files)
            {
                // Find the Relative URL path
                var relative_filename = MapPathReverse(html, info.FullName);

                // Skip cache folders
                if (InCacheFolder(resources, relative_filename) && !force)
                {
                    continue;
                }

                // Find the path diffrence so we can fix up included resources in fx css
                resources.PathOffset[relative_filename] = GetPathOffset(url.Content(relative_filename.Substring(0, relative_filename.Length - info.Name.Length)), url.Content(resources.ScriptFolder));

                // Save options for later use
                resources.Options[relative_filename] = options;

                // Make group as used if we included a resource from it
                if (resources.GroupsLookup.ContainsKey(relative_filename))
                {
                    resources.GroupsUsed.Add(resources.GroupsLookup[relative_filename]);
                }

                if (relative_filename.EndsWith(".js"))
                {
                    // Ensure that list exists.
                    if (!resources.Scripts.Keys.Contains(depth))
                    {
                        resources.Scripts.Add(depth, new List<string>());
                    }

                    if (!resources.Scripts[depth].Contains(relative_filename))
                    {
                        // Note the latest date a file was changed.
                        if (DateTime.Compare(resources.LatestScriptFile, info.LastWriteTime) < 0)
                        {
                            resources.LatestScriptFile = info.LastWriteTime;
                        }
                        resources.Scripts[depth].Add(relative_filename);
                    }
                }
                else if (relative_filename.EndsWith(".css"))
                {
                    // Ensure that list exists.
                    if (!resources.Stylesheets.Keys.Contains(depth))
                    {
                        resources.Stylesheets.Add(depth, new List<string>());
                    }
                    if (!resources.Stylesheets[depth].Contains(relative_filename))
                    {
                        // Note the latest date a file was changed.
                        if (DateTime.Compare(resources.LatestCSSFile, info.LastWriteTime) < 0)
                        {
                            resources.LatestCSSFile = info.LastWriteTime;
                        }

                        resources.Stylesheets[depth].Add(relative_filename);
                    }
                }
                else if (relative_filename.EndsWith(".png") || relative_filename.EndsWith(".gif"))
                {
                    // Ensure that list exists.
                    if (!resources.Images.Keys.Contains(depth))
                    {
                        resources.Images.Add(depth, new List<string>());
                    }
                    if (!resources.Images[depth].Contains(relative_filename))
                    {
                        // Note the latest date a file was changed.
                        if (DateTime.Compare(resources.LatestImageFile, info.LastWriteTime) < 0)
                        {
                            resources.LatestImageFile = info.LastWriteTime;
                        }

                        resources.Images[depth].Add(relative_filename);
                    }
                }
            }

            return null;
        }
Beispiel #4
0
 public static MvcHtmlString Resource(this HtmlHelper html, string value, HTMLResourceOptions options, bool force)
 {
     return Resource(html, value, null, false, options, force);
 }