This repository was archived by the owner on Feb 26, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathTextstringArrayControl.cs
More file actions
298 lines (257 loc) · 9.69 KB
/
Copy pathTextstringArrayControl.cs
File metadata and controls
298 lines (257 loc) · 9.69 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using uComponents.Core;
using umbraco;
using umbraco.cms.businesslogic.datatype;
using umbraco.editorControls;
[assembly: WebResource("uComponents.DataTypes.TextstringArray.TextstringArray.css", Constants.MediaTypeNames.Text.Css)]
[assembly: WebResource("uComponents.DataTypes.TextstringArray.TextstringArray.js", Constants.MediaTypeNames.Application.JavaScript)]
namespace uComponents.DataTypes.TextstringArray
{
/// <summary>
/// The <see cref="TextstringArrayControl"/> handles the presentation of the string arrays.
/// </summary>
[ValidationProperty("IsValid")]
public class TextstringArrayControl : PlaceHolder
{
/// <summary>
/// Field for the list of values.
/// </summary>
private List<string[]> values;
/// <summary>
/// The HiddenField to store the selected values.
/// </summary>
private HiddenField SelectedValues = new HiddenField();
/// <summary>
/// Gets or sets the options.
/// </summary>
/// <value>The options.</value>
public TextstringArrayOptions Options { get; set; }
/// <summary>
/// Gets the value of IsValid.
/// </summary>
/// <value>Returns 'Valid' if valid, otherwise an empty string.</value>
public string IsValid
{
get
{
if (!string.IsNullOrEmpty(this.Values))
{
return "Valid";
}
return string.Empty;
}
}
/// <summary>
/// Gets or sets the values.
/// </summary>
/// <value>The values.</value>
public string Values
{
get
{
return this.SelectedValues.Value;
}
set
{
this.SelectedValues.Value = value;
}
}
/// <summary>
/// Initialize the control, make sure children are created
/// </summary>
/// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.EnsureChildControls();
}
/// <summary>
/// Add the resources (sytle/scripts)
/// </summary>
/// <param name="e">The <see cref="T:System.EventArgs"/> object that contains the event data.</param>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// Adds the client dependencies.
this.RegisterEmbeddedClientResource("uComponents.DataTypes.Shared.Resources.Scripts.json2.js", ClientDependencyType.Javascript);
this.RegisterEmbeddedClientResource("uComponents.DataTypes.TextstringArray.TextstringArray.css", ClientDependencyType.Css);
this.RegisterEmbeddedClientResource("uComponents.DataTypes.TextstringArray.TextstringArray.js", ClientDependencyType.Javascript);
}
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.PreRender"/> event.
/// </summary>
/// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (!string.IsNullOrEmpty(this.Values))
{
// if the value is coming from a translation task, it will always be XML
if (Helper.Xml.CouldItBeXml(this.Values))
{
this.values = DeserializeFromXml(this.Values);
}
else
{
// load the values into a string array/list.
var deserializer = new JavaScriptSerializer();
this.values = deserializer.Deserialize<List<string[]>>(this.Values);
}
// check the number of items per row
for (var i = 0; i < this.values.Count; i++)
{
var row = this.values[i];
if (row.Length < this.Options.ItemsPerRow)
{
// if cell count is less, then add more cells
var diff = this.Options.ItemsPerRow - row.Length;
var newCells = new string(Constants.Common.COMMA, diff - 1).Split(new[] { Constants.Common.COMMA }, StringSplitOptions.None);
this.values[i] = (row ?? Enumerable.Empty<string>()).Concat(newCells).ToArray();
}
else if (row.Length > this.Options.ItemsPerRow)
{
// if cell count is greater, then remove extra cells
var diff = row.Length - this.Options.ItemsPerRow;
var tmp = new List<string>(row);
tmp.RemoveRange(this.Options.ItemsPerRow, diff);
this.values[i] = tmp.ToArray();
}
}
}
else
{
// initalise the string array/list.
this.values = new List<string[]>();
}
var emptyRow = new string(Constants.Common.COMMA, this.Options.ItemsPerRow - 1).Split(new[] { Constants.Common.COMMA }, StringSplitOptions.None);
// check the minimum number allowed, add extra fields.
if (this.values.Count < this.Options.MinimumRows && this.Options.MinimumRows > 1)
{
for (int i = this.values.Count; i < this.Options.MinimumRows; i++)
{
this.values.Add(emptyRow);
}
}
// check the maxmimum number allowed, remove the excess.
if (this.values.Count > this.Options.MaximumRows && this.Options.MaximumRows > 0)
{
this.values.RemoveRange(this.Options.MaximumRows, this.values.Count - this.Options.MaximumRows);
}
// if there are no selected values...
if (this.values.Count == 0)
{
// ... then add an empty string to display a single textstring row.
this.values.Add(emptyRow);
}
}
/// <summary>
/// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
/// </summary>
protected override void CreateChildControls()
{
base.CreateChildControls();
this.EnsureChildControls();
// populate the control's attributes.
this.SelectedValues.ID = this.SelectedValues.ClientID;
// add the controls.
this.Controls.Add(this.SelectedValues);
}
/// <summary>
/// Sends server control content to a provided <see cref="T:System.Web.UI.HtmlTextWriter"/> object, which writes the content to be rendered on the client.
/// </summary>
/// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> object that receives the server control content.</param>
protected override void Render(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, "TextstringArray");
writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
writer.RenderBeginTag(HtmlTextWriterTag.Div);
// render the header row
if (this.Options.ShowColumnLabels)
{
var labels = this.Options.ColumnLabels.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
if (labels != null && labels.Length > 0)
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, "textstring-header-row");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
foreach (string label in labels)
{
var displayLabel = label;
if (displayLabel.Length > 3 && displayLabel.StartsWith("[#") && displayLabel.EndsWith("]"))
{
var key = displayLabel.Substring(2, label.Length - 3);
displayLabel = uQuery.GetDictionaryItem(key, key);
}
writer.AddAttribute(HtmlTextWriterAttribute.Class, "textstring-header-row-col");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.WriteLine(displayLabel);
writer.RenderEndTag(); // </div> .textstring-header-row-col
}
writer.RenderEndTag();
}
}
// loop through each value
foreach (string[] row in this.values)
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, "textstring-row");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
foreach (string value in row)
{
// input tag
writer.AddAttribute(HtmlTextWriterAttribute.Class, "textstring-row-field");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.WriteLine("<input type='text' value='{0}' />", value.Replace("'", "'"));
writer.RenderEndTag(); // </div> .textstring-row-field
}
// append the add/remove buttons
writer.WriteLine("<div class='textstring-row-edit'>");
writer.WriteLine("<a href='#add' class='textstring-row-add' title='Add a new row'><img src='{0}/images/small_plus.png' /></a>", GlobalSettings.Path);
writer.WriteLine("<a href='#remove' class='textstring-row-remove' title='Remove this row'><img src='{0}/images/small_minus.png' /></a>", GlobalSettings.Path);
writer.WriteLine("</div>");
if (!this.Options.DisableSorting)
writer.WriteLine("<div class='textstring-row-sort' title='Re-order this row' style='background: url({0}/images/sort.png) no-repeat 0 2px;'></div>", GlobalSettings.Path);
writer.RenderEndTag(); // </div> .textstring-row
}
this.SelectedValues.RenderControl(writer);
writer.RenderEndTag(); // </div> .TextstringArray
// add jquery window load event
var javascriptMethod = string.Concat("new jQuery.textstringArray($('#", this.ClientID, "'), { hiddenId: '#", this.SelectedValues.ClientID, "', minimum: ", this.Options.MinimumRows, ", maximum: ", this.Options.MaximumRows, "});");
var javascript = string.Concat("<script type='text/javascript'>jQuery(window).load(function(){", javascriptMethod, "});</script>");
writer.WriteLine(javascript);
}
/// <summary>
/// Deserializes the XML data into the string array.
/// </summary>
/// <param name="data">The data as an XML string.</param>
/// <returns>Returns a string array.</returns>
private static List<string[]> DeserializeFromXml(string data)
{
var list = new List<string[]>();
var xml = new XmlDocument();
xml.LoadXml(data);
var xmlNodeList = xml.SelectNodes("/TextstringArray/values");
if (xmlNodeList != null)
{
foreach (XmlNode node in xmlNodeList)
{
var value = new List<string>();
var selectNodes = node.SelectNodes("value");
if (selectNodes != null)
{
foreach (XmlNode child in selectNodes)
{
value.Add(child.InnerText);
}
}
list.Add(value.ToArray());
}
}
return list;
}
}
}