-
Notifications
You must be signed in to change notification settings - Fork 1
/
ResponsiveImage.cs
106 lines (91 loc) · 3.29 KB
/
ResponsiveImage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Telerik.Sitefinity.Libraries.Model;
using Telerik.Sitefinity.Modules.Libraries;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Web.UI;
using Telerik.Sitefinity.Web.UI.ControlDesign;
namespace ResponsiveImages
{
/// <summary>
/// Class used to create custom page widget
/// </summary>
[ControlDesigner(typeof(ResponsiveImages.Designer.ResponsiveImageDesigner))]
public class ResponsiveImage : SimpleView
{
#region Properties
/// <summary>
/// Gets or sets the message that will be displayed in the label.
/// </summary>
public Guid? ImageId { get; set; }
protected override string LayoutTemplateName
{
get
{
return ResponsiveImage.layoutTemplatePath;
}
}
/// <summary>
/// Gets Reference to the Label control that shows the Message.
/// </summary>
protected virtual Label MessageLabel
{
get
{
return this.Container.GetControl<Label>("MessageLabel", true);
}
}
protected virtual HtmlImage OriginalImage
{
get
{
return this.Container.GetControl<HtmlImage>("originalImage", true);
}
}
protected virtual HtmlImage ResponsiveImageControl
{
get
{
return this.Container.GetControl<HtmlImage>("responsiveImage", true);
}
}
#endregion
#region Methods
/// <summary>
/// Initializes the controls.
/// </summary>
/// <param name="container">the container</param>
/// <remarks>
/// Initialize your controls in this method. Do not override CreateChildControls method.
/// </remarks>
protected override void InitializeControls(GenericContainer container)
{
if (!this.ImageId.HasValue)
{
this.MessageLabel.Text = "No Image Selected";
}
else
{
this.MessageLabel.Text = "Selected an Image with id : '" + this.ImageId.Value + "'";
Context.Items.Add("Image", LibrariesManager.GetManager().GetMediaItem(this.ImageId.Value));
//sets the original image url
this.OriginalImage.Src = ((MediaContent)Context.Items["Image"]).ResolveMediaUrl();
//sets the reponsive image url
this.ResponsiveImageControl.Attributes.Add("srcset", String.Join(", ",
new List<string>() { ((MediaContent)Context.Items["Image"]).ResolveThumbnailUrl("thumb768", true) + " 768w",
((MediaContent)Context.Items["Image"]).ResolveThumbnailUrl("thumb600", true) + " 600w",
((MediaContent)Context.Items["Image"]).ResolveThumbnailUrl("thumb480", true) + " 480w"
}));
}
}
#endregion
#region Private members & constants
private static readonly string layoutTemplatePath = "ResponsiveImages.ResponsiveImage.ascx";
#endregion
}
}