Repository navigation
Expand file tree
/
Copy pathWidgetZone.cs
More file actions
240 lines (206 loc) · 8.41 KB
/
Copy pathWidgetZone.cs
File metadata and controls
240 lines (206 loc) · 8.41 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// --------------------------------------------------------------------------------------------------------------------
// <summary>
// The widget zone.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using BlogEngine.Core;
using BlogEngine.Core.DataStore;
using Resources;
namespace BlogEngineWA.CustomCode.Controls
{
/// <summary>
/// The widget zone.
/// </summary>
public class WidgetZone : PlaceHolder
{
#region Constants and Fields
/// <summary>
/// The xml document by zone.
/// </summary>
private static readonly Dictionary<string, XmlDocument> XmlDocumentByZone =
new Dictionary<string, XmlDocument>();
/// <summary>
/// The zone name.
/// </summary>
/// <remarks>
/// For backwards compatibility or if a ZoneName is omitted, provide a default ZoneName.
/// </remarks>
private string zoneName = "be_WIDGET_ZONE";
#endregion
#region Constructors and Destructors
/// <summary>
/// Initializes static members of the <see cref = "WidgetZone" /> class.
/// </summary>
static WidgetZone()
{
WidgetEditBase.Saved += (sender, args) => OnZonesUpdated();
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the name of the data-container used by this instance
/// </summary>
public string ZoneName
{
get
{
return this.zoneName;
}
set
{
this.zoneName = Utils.RemoveIllegalCharacters(value);
}
}
/// <summary>
/// Gets the XML document.
/// </summary>
/// <value>The XML document.</value>
private XmlDocument XmlDocument
{
get
{
// look up the document by zone name
return XmlDocumentByZone.ContainsKey(this.ZoneName) ? XmlDocumentByZone[this.ZoneName] : null;
}
}
#endregion
#region Methods
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Init"/> event.
/// </summary>
/// <param name="e">
/// An <see cref="T:System.EventArgs"/> object that contains the event data.
/// </param>
protected override void OnInit(EventArgs e)
{
if (this.XmlDocument == null)
{
// if there's no document for this zone name yet, load it
var doc = RetrieveXml(this.ZoneName);
if (doc != null)
{
XmlDocumentByZone[this.ZoneName] = doc;
}
}
base.OnInit(e);
}
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Load"></see> event.
/// </summary>
/// <param name="e">
/// The <see cref="T:System.EventArgs"></see> object that contains the event data.
/// </param>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
var zone = this.XmlDocument.SelectNodes("//widget");
if (zone == null)
{
return;
}
// This is for compatibility with older themes that do not have a WidgetContainer control.
var widgetContainerExists = WidgetContainer.DoesThemeWidgetContainerExist();
var widgetContainerVirtualPath = WidgetContainer.GetThemeWidgetContainerVirtualPath();
foreach (XmlNode widget in zone)
{
var fileName = string.Format("{0}widgets/{1}/widget.ascx", Utils.RelativeWebRoot, widget.InnerText);
try
{
var control = (WidgetBase)this.Page.LoadControl(fileName);
if (widget.Attributes != null)
{
control.WidgetId = new Guid(widget.Attributes["id"].InnerText);
control.Title = widget.Attributes["title"].InnerText;
control.ShowTitle = control.IsEditable
? bool.Parse(widget.Attributes["showTitle"].InnerText)
: control.DisplayHeader;
}
control.ID = control.WidgetId.ToString().Replace("-", string.Empty);
control.Zone = this.zoneName;
control.LoadWidget();
// This will return the WidgetContainer with the control in it.
var widgetContainer = WidgetContainer.GetWidgetContainer(control, widgetContainerExists, widgetContainerVirtualPath);
this.Controls.Add(widgetContainer);
}
catch (Exception ex)
{
var lit = new Literal
{
Text = string.Format("<p style=\"color:red\">Widget {0} not found.<p>", widget.InnerText)
};
lit.Text += ex.Message;
if (widget.Attributes != null)
{
lit.Text +=
string.Format(
"<a class=\"delete\" href=\"#\" onclick=\"BlogEngine.widgetAdmin.removeWidget('{0}');return false\" title=\"{1} widget\">X</a>",
widget.Attributes["id"].InnerText,
labels.delete);
}
this.Controls.Add(lit);
}
}
}
/// <summary>
/// Sends server control content to a provided <see cref="T:System.Web.UI.HtmlTextWriter"></see>
/// object, which writes the content to be rendered on the client.
/// </summary>
/// <param name="writer">
/// The <see cref="T:System.Web.UI.HtmlTextWriter"></see> object
/// that receives the server control content.
/// </param>
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<div id=\"widgetzone_{0}\" class=\"widgetzone\">", this.zoneName);
base.Render(writer);
writer.Write("</div>");
if (!Security.IsAuthorizedTo(Rights.ManageWidgets))
{
return;
}
var selectorId = string.Format("widgetselector_{0}", this.zoneName);
writer.Write("<select id=\"{0}\" class=\"widgetselector\">", selectorId);
var di = new DirectoryInfo(this.Page.Server.MapPath(string.Format("{0}widgets", Utils.RelativeWebRoot)));
foreach (var dir in di.GetDirectories().Where(dir => File.Exists(Path.Combine(dir.FullName, "widget.ascx"))))
{
writer.Write("<option value=\"{0}\">{1}</option>", dir.Name, dir.Name);
}
writer.Write("</select> ");
writer.Write(
"<input type=\"button\" value=\"Add\" onclick=\"BlogEngine.widgetAdmin.addWidget(BlogEngine.$('{0}').value, '{1}')\" />",
selectorId,
this.zoneName);
writer.Write("<div class=\"clear\" id=\"clear\"> </div>");
}
/// <summary>
/// Called when [zones updated].
/// </summary>
private static void OnZonesUpdated()
{
XmlDocumentByZone.Clear();
}
/// <summary>
/// Retrieves the XML.
/// </summary>
/// <param name="zoneName">
/// The zone Name.
/// </param>
/// <returns>
/// An Xml Document.
/// </returns>
private static XmlDocument RetrieveXml(string zoneName)
{
var ws = new WidgetSettings(zoneName) { SettingsBehavior = new XmlDocumentBehavior() };
var doc = (XmlDocument)ws.GetSettings();
return doc;
}
#endregion
}
}