예제 #1
0
        /// <summary>
        /// Creates the proper CSS link reference within the target CSHTML page's head section
        /// </summary>
        /// <param name="virtualPath">The relative path of the image to be displayed, or its directory</param>
        /// <returns>Link tag if the file is found.</returns>
        public static IHtmlString ImportStylesheet(string virtualPath)
        {
            ImageOptimizations.EnsureInitialized();

            if (Path.HasExtension(virtualPath))
            {
                virtualPath = Path.GetDirectoryName(virtualPath);
            }

            HttpContextBase httpContext = new HttpContextWrapper(HttpContext.Current);

            string cssFileName = ImageOptimizations.LinkCompatibleCssFile(httpContext.Request.Browser) ?? ImageOptimizations.LowCompatibilityCssFileName;

            virtualPath = Path.Combine(virtualPath, cssFileName);
            string physicalPath = HttpContext.Current.Server.MapPath(virtualPath);

            if (File.Exists(physicalPath))
            {
                TagBuilder htmlTag = new TagBuilder("link");
                htmlTag.MergeAttribute("href", ResolveUrl(virtualPath));
                htmlTag.MergeAttribute("rel", "stylesheet");
                htmlTag.MergeAttribute("type", "text/css");
                htmlTag.MergeAttribute("media", "all");
                return(new HtmlString(htmlTag.ToString(TagRenderMode.SelfClosing)));
            }

            return(null);
        }
예제 #2
0
        /// <summary>
        /// Prepares some field variables and check them if they are correct for sprite rendering.
        /// </summary>
        /// <returns>true if ready for sprite rendering</returns>
        private bool PrepareSpriteRendering()
        {
            ImageOptimizations.EnsureInitialized();

            if (!EnableSprites)
            {
                return(false);
            }

            _cssFileName = ImageOptimizations.LinkCompatibleCssFile(new HttpContextWrapper(Context).Request.Browser);
            if (String.IsNullOrEmpty(_cssFileName))
            {
                return(false);
            }

            string spriteDirectoryPath = Path.GetDirectoryName(ImageUrl);

            // Check that CSS file is accessible
            if (!File.Exists(Path.Combine(Context.Server.MapPath(spriteDirectoryPath), _cssFileName)))
            {
                return(false);
            }

            return(true);
        }
예제 #3
0
 protected override void Render(HtmlTextWriter writer)
 {
     if (_usingSprites)
     {
         // Need to output the src element but without using the ImageUrl (ViewState)
         string imageUrl = ImageOptimizations.GetBlankImageSource(new HttpContextWrapper(Context).Request.Browser);
         ImageHtmlTextWriter imageHtmlTextWriter = new ImageHtmlTextWriter(writer, ResolveClientUrl(imageUrl));
         base.Render(imageHtmlTextWriter);
     }
     else
     {
         base.Render(writer);
     }
 }
예제 #4
0
        public void Init(HttpApplication context)
        {
            lock (_lockObj) {
                if (_hasAlreadyRun)
                {
                    return;
                }
                else
                {
                    _hasAlreadyRun = true;
                }
            }

            var spriteDirectoryPhysicalPath = context.Context.Server.MapPath(ImageOptimizations.SpriteDirectoryRelativePath);

            if (Directory.Exists(spriteDirectoryPhysicalPath))
            {
                ImageOptimizations.SaveBlankFile(spriteDirectoryPhysicalPath);
                ImageOptimizations.AddCacheDependencies(spriteDirectoryPhysicalPath, rebuildImages: true);
                ImageOptimizations.Initialized = true;
            }
        }
예제 #5
0
        /// <summary>
        /// Creates a reference to the sprite / inlined version of the desired image including special attributes.
        /// </summary>
        /// <param name="virtualPath">The relative path of the image to be displayed</param>
        /// <param name="htmlAttributes">Html Attributes of IDictionary form</param>
        /// <returns>Image tag.</returns>
        public static IHtmlString Image(string virtualPath, IDictionary <string, object> htmlAttributes)
        {
            ImageOptimizations.EnsureInitialized();

            TagBuilder htmlTag = new TagBuilder("img");

            htmlTag.MergeAttributes(htmlAttributes);

            HttpContextBase httpContext = new HttpContextWrapper(HttpContext.Current);

            if (ImageOptimizations.LinkCompatibleCssFile(httpContext.Request.Browser) == null)
            {
                htmlTag.MergeAttribute("src", ResolveUrl(virtualPath));
                return(new HtmlString(htmlTag.ToString(TagRenderMode.SelfClosing)));
            }
            else
            {
                htmlTag.AddCssClass(ImageOptimizations.MakeCssClassName(virtualPath));
                htmlTag.MergeAttribute("src", ResolveUrl(ImageOptimizations.GetBlankImageSource(httpContext.Request.Browser)));
                return(new HtmlString(htmlTag.ToString(TagRenderMode.SelfClosing)));
            }
        }
예제 #6
0
        protected override void OnPreRender(EventArgs e)
        {
            ImageOptimizations.EnsureInitialized();

            if (Path.HasExtension(ImageUrl) || ImageUrl.EndsWith(Path.AltDirectorySeparatorChar.ToString(), StringComparison.OrdinalIgnoreCase) || ImageUrl.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.OrdinalIgnoreCase))
            {
                ImageUrl = Path.GetDirectoryName(ImageUrl);
            }

            string cssFileName = ImageOptimizations.LinkCompatibleCssFile(new HttpContextWrapper(Context).Request.Browser) ?? ImageOptimizations.LowCompatibilityCssFileName;

            // Set up fileName and path variables
            string localPath = Context.Server.MapPath(ImageUrl);

            // Check that CSS file is accessible
            if (!File.Exists(Path.Combine(localPath, cssFileName)))
            {
                return;
            }

            AddCssToPage(Page, Path.Combine(ImageUrl, cssFileName));
        }
예제 #7
0
        internal static void AddCssToPage(Page page, string href)
        {
            string keyDirectory = href;

            if (Path.HasExtension(keyDirectory))
            {
                keyDirectory = Path.GetDirectoryName(keyDirectory);
            }

            SpriteDirectoryPathKey key = new SpriteDirectoryPathKey(keyDirectory);

            if (!page.Items.Contains(key))
            {
                page.Items.Add(key, null);

                HtmlLink css = new HtmlLink();
                css.Href = ImageOptimizations.FixVirtualPathSlashes(href);
                css.Attributes["rel"]   = "stylesheet";
                css.Attributes["type"]  = "text/css";
                css.Attributes["media"] = "all";
                page.Header.Controls.Add(css);
            }
        }
예제 #8
0
        protected override void OnPreRender(EventArgs e)
        {
            if (PrepareSpriteRendering())
            {
                string spriteDirectoryPath = Path.GetDirectoryName(ImageUrl);

                string spriteCssClassName = ImageOptimizations.MakeCssClassName(ImageUrl);
                if (CssClass.Length != 0)
                {
                    CssClass = spriteCssClassName + " " + CssClass;
                }
                else
                {
                    CssClass = spriteCssClassName;
                }

                ImageSpriteCssLink.AddCssToPage(Page, Path.Combine(spriteDirectoryPath, _cssFileName));

                _usingSprites = true;
            }

            base.OnPreRender(e);
        }