示例#1
0
 public static IHtmlString Render(this IImageField field, bool editable, string cssClass)
 {
     return(Render(field, x =>
     {
         x.CssClass = cssClass;
         x.DisableWebEditing = !editable;
     }));
 }
        /// <summary>
        /// Renders the image to a given HtmlTextWriter using field renderer. Nothing is rendered if the image has no value and we aren't page editing. 
        /// </summary>
        /// <param name="imageField">The image field to render</param>
        /// <param name="parameters">Action to execute to configure parameters on the output control</param>
        /// <param name="writer">The HtmlTextWriter to write to</param>
        public static void RenderImageField(this HtmlTextWriter writer, IImageField imageField, Action<Image> parameters)
        {
            var image = new Image();

            image.AttachToImageField(imageField);

            parameters(image);

            image.RenderControl(writer);
        }
        /// <summary>
        /// Renders the image to a given HtmlTextWriter using field renderer. Nothing is rendered if the image has no value and we aren't page editing.
        /// </summary>
        /// <param name="imageField">The image field to render</param>
        /// <param name="parameters">Action to execute to configure parameters on the output control</param>
        /// <param name="writer">The HtmlTextWriter to write to</param>
        public static void RenderImageField(this HtmlTextWriter writer, IImageField imageField, Action <Image> parameters)
        {
            var image = new Image();

            image.AttachToImageField(imageField);

            parameters(image);

            image.RenderControl(writer);
        }
        /// <summary>
        /// Renders the image to a given HtmlTextWriter using field renderer. Uses Sitecore image scaling if max width or height is passed (pass null for only one dimension). Nothing is rendered if the image has no value and we aren't page editing. 
        /// </summary>
        /// <param name="imageField">The image field to render</param>
        /// <param name="maxWidth">Maximum width for the image. May be less if it has a portrait aspect ratio. Pass null to scale by height only.</param>
        /// <param name="maxHeight">Maximum height for the image. May be less if it has a landscape aspect ratio. Pass null to scale by width only.</param>
        /// <param name="writer">The HtmlTextWriter to write to</param>
        public static void RenderImageField(this HtmlTextWriter writer, IImageField imageField, int? maxWidth, int? maxHeight)
        {
            RenderImageField(writer, imageField, image =>
            {
                if (maxWidth.HasValue)
                    image.MaxWidth = maxWidth.Value;

                if (maxHeight.HasValue)
                    image.MaxHeight = maxHeight.Value;
            });
        }
        public static void AttachToImageField(this Sitecore.Web.UI.WebControls.Image image, IImageField field)
        {
            if (!field.HasValue && !Sitecore.Context.PageMode.IsPageEditor)
            {
                image.Visible = false;
                return;
            }

            var item = (FieldType)field;
            image.Item = item.InnerField.Item;
            image.Field = item.InnerField.ID.ToString();
        }
示例#6
0
        public static IHtmlString Render(this IImageField field, Action <Image> parameters)
        {
            if (field.HasValue || Sitecore.Context.PageMode.IsExperienceEditor)
            {
                var imageRenderer = new Image();
                imageRenderer.AttachToImageField(field);
                parameters(imageRenderer);

                return(new MvcHtmlString(imageRenderer.RenderAsText()));
            }

            return(new MvcHtmlString(string.Empty));
        }
        /// <summary>
        /// Renders the image to a given HtmlTextWriter using field renderer. Uses Sitecore image scaling if max width or height is passed (pass null for only one dimension). Nothing is rendered if the image has no value and we aren't page editing.
        /// </summary>
        /// <param name="imageField">The image field to render</param>
        /// <param name="maxWidth">Maximum width for the image. May be less if it has a portrait aspect ratio. Pass null to scale by height only.</param>
        /// <param name="maxHeight">Maximum height for the image. May be less if it has a landscape aspect ratio. Pass null to scale by width only.</param>
        /// <param name="writer">The HtmlTextWriter to write to</param>
        public static void RenderImageField(this HtmlTextWriter writer, IImageField imageField, int?maxWidth, int?maxHeight)
        {
            RenderImageField(writer, imageField, image =>
            {
                if (maxWidth.HasValue)
                {
                    image.MaxWidth = maxWidth.Value;
                }

                if (maxHeight.HasValue)
                {
                    image.MaxHeight = maxHeight.Value;
                }
            });
        }
示例#8
0
        public static IHtmlString RenderDpiAware(this IImageField field, int?max1XWidth = null, int?max1XHeight = null, string cssClass = null, int maxScale = 2, bool editable = true)
        {
            if (Sitecore.Context.PageMode.IsExperienceEditor || maxScale == 1)
            {
                return(Render(field, max1XWidth, max1XHeight, cssClass, editable));
            }

            if (field.HasValue)
            {
                string mediaUrl = MediaManager.GetMediaUrl(field.MediaItem);

                var html = new StringBuilder();
                html.AppendFormat("<img src=\"{0}\"", GenerateImageUrl(mediaUrl, max1XWidth, max1XHeight));

                var srcset = new List <string>();
                for (int scaleFactor = 2; scaleFactor <= maxScale; scaleFactor++)
                {
                    int scaledMaxWidth  = (max1XWidth ?? 0) * scaleFactor;
                    int scaledMaxHeight = (max1XHeight ?? 0) * scaleFactor;

                    // ReSharper disable once UseStringInterpolation
                    srcset.Add(string.Format("{0} {1}x", HttpUtility.UrlPathEncode(GenerateImageUrl(mediaUrl, scaledMaxWidth, scaledMaxHeight)), scaleFactor));
                }

                html.AppendFormat(" srcset=\"{0}\"", string.Join(", ", srcset));

                if (!string.IsNullOrEmpty(field.MediaItem.Alt))
                {
                    html.AppendFormat(" alt=\"{0}\"", field.MediaItem.Alt);
                }

                if (!string.IsNullOrEmpty(cssClass))
                {
                    html.AppendFormat(" class=\"{0}\"", cssClass);
                }

                html.Append(">");

                return(new MvcHtmlString(html.ToString()));
            }

            return(new MvcHtmlString(string.Empty));
        }
示例#9
0
        public static IHtmlString Render(this IImageField field, int?maxWidth = null, int?maxHeight = null, string cssClass = null, bool editable = true)
        {
            return(Render(field, x =>
            {
                if (maxWidth.HasValue)
                {
                    x.MaxWidth = maxWidth.Value;
                }

                if (maxHeight.HasValue)
                {
                    x.MaxHeight = maxHeight.Value;
                }

                if (!editable)
                {
                    x.DisableWebEditing = true;
                }

                x.CssClass = cssClass;
            }));
        }
 /// <summary>
 /// Renders the image using field renderer. Nothing is rendered if the image has no value and we aren't page editing.
 /// </summary>
 public static void RenderImageField(this HtmlTextWriter writer, IImageField imageField)
 {
     RenderImageField(writer, imageField, x => { });
 }
 /// <summary>
 /// Renders the image using field renderer. Nothing is rendered if the image has no value and we aren't page editing.
 /// </summary>
 public static void RenderImageField(this HtmlTextWriter writer, IImageField imageField)
 {
     RenderImageField(writer, imageField, x => { });
 }
示例#12
0
 public static IHtmlString Render(this IImageField field, bool editable)
 {
     return(Render(field, editable, ""));
 }
示例#13
0
 public static IHtmlString Render(this IImageField field, string cssClass)
 {
     return(Render(field, true, cssClass));
 }
示例#14
0
        public static void AttachToImageField(this Sitecore.Web.UI.WebControls.Image image, IImageField field)
        {
            if (!field.HasValue && !Sitecore.Context.PageMode.IsExperienceEditor)
            {
                image.Visible = false;
                return;
            }

            var item = (FieldType)field;

            image.Item  = item.InnerField.Item;
            image.Field = item.InnerField.ID.ToString();
        }