-
Notifications
You must be signed in to change notification settings - Fork 1
/
VueModel.cs
193 lines (169 loc) · 6.91 KB
/
VueModel.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
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
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Localization;
using Microsoft.AspNetCore.Mvc.Routing;
using OrchardCore.ContentManagement;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace Lombiq.DataTables.Models;
public class VueModel
{
/// <summary>
/// Gets or sets the text to be displayed. Always fill this unless you have <see cref="Html"/> or plan to use the
/// <c>component</c> property client-side. Even then, you need to provide either this or <see cref="Sort"/> if the
/// column is meant to be <see cref="DataTableColumnDefinition.Orderable"/>.
/// </summary>
[JsonPropertyName("text")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string Text { get; set; }
/// <summary>
/// Gets or sets the HTML content to be rendered inside the cell. When used <see cref="Text"/> and <see
/// cref="Badge"/> are ignored.
/// </summary>
[JsonPropertyName("html")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string Html { get; set; }
/// <summary>
/// Gets or sets the value used for sorting. If <see langword="null"/> or empty, the value of <see cref="Text"/> is
/// used for sorting.
/// </summary>
[JsonPropertyName("sort")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public object Sort { get; set; }
/// <summary>
/// Gets or sets the URL to be used in the <c>href</c> attributes. When this is used <see cref="Html"/> is ignored.
/// </summary>
[JsonPropertyName("href")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string Href { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("multipleLinks")]
public IEnumerable<MultipleHrefValue> MultipleLinks { get; set; }
/// <summary>
/// Gets or sets the Bootstrap badge class of the cell. To be used along with <see cref="Text"/> and optionally <see
/// cref="Href"/>.
/// </summary>
[JsonPropertyName("badge")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public object Badge { get; set; }
/// <summary>
/// Gets or sets the data used as extra information to be consumed by <c>special</c> event so the contents can be
/// updated with JavaScript on client side before each render.
/// </summary>
[JsonPropertyName("special")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public object Special { get; set; }
/// <summary>
/// Gets or sets the value of an <c><input type="hidden"></c> element that's rendered after the table (so
/// unaffected by paging and sorting) which can contain data to be POSTed back to the server if the table is inside
/// a <c>form</c> element.
/// </summary>
[JsonIgnore]
public HiddenInputValue HiddenInput { get; set; }
/// <summary>
/// Gets or sets the <c>Array</c> value of <c>hiddenInput</c>. Same as <see cref="HiddenInput"/>, except it contains
/// multiple instances in case the same cell needs to render several hidden <c>input</c> elements.
/// </summary>
[JsonIgnore]
public IEnumerable<HiddenInputValue> HiddenInputs { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("hiddenInput")]
[JsonInclude]
[SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "It's used for JSON conversion.")]
private object HiddenInputSerialize
{
get => (object)HiddenInput ?? HiddenInputs;
set
{
switch (value)
{
case JsonArray hiddenInputs:
HiddenInputs = hiddenInputs.ToObject<IEnumerable<HiddenInputValue>>();
break;
case JsonObject hiddenInput:
HiddenInput = hiddenInput.ToObject<HiddenInputValue>();
break;
case null:
return;
default:
throw new InvalidCastException("The value of \"hiddenInput\" must be Object, Array or null.");
}
}
}
public VueModel(IContent content, bool isEditor, IUrlHelper urlHelper)
{
if (content == null) return;
Text = content.ContentItem.DisplayText;
Href = isEditor
? urlHelper.EditContentItem(content.ContentItem.ContentItemId)
: urlHelper.DisplayContentItem(content);
}
public VueModel(string text = null, string href = null, object sort = null)
{
Text = text;
Href = href;
Sort = sort;
}
public VueModel(int number, string href = null)
: this(number.ToString(CultureInfo.InvariantCulture), href, number)
{
}
public VueModel SetHiddenInput(string name, string value)
{
HiddenInput = new HiddenInputValue { Name = name, Value = value };
return this;
}
public static async Task<JsonArray> TableToJsonAsync<T>(
IEnumerable<T> collection,
Func<T, int, Task<Dictionary<string, VueModel>>> select)
{
var rows = (await collection.AwaitEachAsync(select))
.Select((row, rowIndex) =>
{
var castRow = row.ToDictionary(pair => pair.Key, pair => JsonSerializer.SerializeToNode(pair.Value));
castRow["$rowIndex"] = rowIndex;
return castRow;
});
return JArray.FromObject(rows);
}
public static IDictionary<string, string> CreateTextForIcbinDataTable(IHtmlLocalizer localizer) =>
new Dictionary<string, string>
{
["lengthPicker"] = localizer["Show {{ count }} Entries"].Value,
["displayCount"] = localizer["Showing {{ from }} to {{ to }} of {{ total }} entries"].Value,
["previous"] = localizer["Previous"].Value,
["next"] = localizer["Next"].Value,
["all"] = localizer["All"].Value,
};
public class HiddenInputValue
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("value")]
public string Value { get; set; }
}
public class MultipleHrefValue
{
[JsonPropertyName("url")]
public string Url { get; set; }
[JsonPropertyName("text")]
public string Text { get; set; }
}
public static class BadgeNames
{
public const string Primary = "primary";
public const string Secondary = "secondary";
public const string Success = "success";
public const string Danger = "danger";
public const string Warning = "warning";
public const string Info = "info";
public const string Light = "light";
public const string Dark = "dark";
}
}