-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPageHelper.cs
139 lines (121 loc) · 4.77 KB
/
PageHelper.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
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
using HomeSeerAPI;
using Hspi.Utils;
using NullGuard;
using Scheduler;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using static System.FormattableString;
namespace Hspi.Pages
{
internal class PageHelper : PageBuilderAndMenu.clsPageBuilder
{
public PageHelper(IHSApplication HS, PluginConfig pluginConfig, string pageName) : base(pageName)
{
this.HS = HS;
this.pluginConfig = pluginConfig;
}
public static string HtmlEncode<T>([AllowNull]T value)
{
if (value == null)
{
return string.Empty;
}
return HttpUtility.HtmlEncode(value);
}
protected static string HtmlTextBox(string name, string defaultText, int size = 25, string type = "text", bool @readonly = false)
{
return Invariant($@"<input type='{type}' id='{NameToIdWithPrefix(name)}' size='{size}' name='{name}' value='{HtmlEncode(defaultText)}' {(@readonly ? "readonly" : string.Empty)}>");
}
protected static string NameToId(string name)
{
return name.Replace(' ', '_');
}
protected static string NameToIdWithPrefix(string name)
{
return Invariant($"{IdPrefix}{NameToId(name)}");
}
protected string FormCheckBox(string name, string label, bool @checked, bool autoPostBack = false)
{
var cb = new clsJQuery.jqCheckBox(name, label, PageName, true, true)
{
id = NameToIdWithPrefix(name),
@checked = @checked,
autoPostBack = autoPostBack,
};
return cb.Build();
}
protected string FormPageButton(string name, string label)
{
var b = new clsJQuery.jqButton(name, label, PageName, true)
{
id = NameToIdWithPrefix(name),
};
return b.Build();
}
protected string PageTypeButton(string name, string label, string pageType, string deviceId = null)
{
var b = new clsJQuery.jqButton(name, label, PageName, false)
{
id = NameToIdWithPrefix(name),
url = Invariant($@"/{HttpUtility.UrlEncode(ConfigPage.Name)}?{PageTypeId}={HttpUtility.UrlEncode(pageType)}&{DeviceIdId}={HttpUtility.UrlEncode(deviceId ?? string.Empty)}"),
};
return b.Build();
}
protected string FormDropDown(string name, NameValueCollection options, string selected,
int width, string tooltip, bool autoPostBack = true)
{
return FormDropDown(name, options, selected,
width, tooltip, autoPostBack, PageName);
}
protected static string FormDropDown(string name, NameValueCollection options, string selected,
int width, string tooltip, bool autoPostBack, string pageName)
{
var dropdown = new clsJQuery.jqDropList(name, pageName, false)
{
selectedItemIndex = -1,
id = NameToIdWithPrefix(name),
autoPostBack = autoPostBack,
toolTip = tooltip,
style = Invariant($"width: {width}px;"),
enabled = true,
submitForm = autoPostBack,
};
if (options != null)
{
for (var i = 0; i < options.Count; i++)
{
var sel = options.GetKey(i) == selected;
dropdown.AddItem(options.Get(i), options.GetKey(i), sel);
}
}
return dropdown.Build();
}
protected static NameValueCollection CreateNameValueCreation<T>() where T : Enum
{
var collection = new NameValueCollection();
foreach (var value in EnumUtil.GetValues<T>())
{
collection.Add(value.ToString(), value.ToString());
}
return collection;
}
protected static string RedirectPageJS(string url)
{
StringBuilder stb = new StringBuilder();
stb.AppendLine("<script type='text/javascript'>");
stb.AppendLine(Invariant($"$(document).ready(function() {{ location.assign('{url}'); }});"));
stb.AppendLine("</script>");
return stb.ToString();
}
protected const string DeviceIdId = "DeviceIdId";
protected const string PageTypeId = "type";
protected readonly IHSApplication HS;
protected readonly PluginConfig pluginConfig;
private const string IdPrefix = "id_";
}
}