-
Notifications
You must be signed in to change notification settings - Fork 7
/
HtmlHelperExtensions.cs
194 lines (168 loc) · 7.78 KB
/
HtmlHelperExtensions.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
194
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using MvcFlash.Core.Models;
namespace MvcFlash.Core.Extensions
{
public static class HtmlHelperExtensions {
/// <summary>
/// Flashes all messages and calls Html.Partial on them with the template specified.
/// </summary>
/// <param name="helper">The helper.</param>
/// <param name="templateName">Name of the template.</param>
/// <returns></returns>
public static MvcHtmlString Flash(this HtmlHelper helper, string templateName = "Flash")
{
var popper = FlashConfiguration.Popper;
if (popper == null) return MvcHtmlString.Empty;
var messages = popper.Pop();
return Create(messages, helper, templateName);
}
/// <summary>
/// Flashes all messages and calls the template function passed.
/// </summary>
/// <param name="helper">The helper.</param>
/// <param name="templateFunc">The template function.</param>
/// <returns></returns>
public static MvcHtmlString Flash(this HtmlHelper helper, Func<FlashContext, MvcHtmlString> templateFunc)
{
var messageBus = FlashConfiguration.Popper;
if (messageBus == null) return MvcHtmlString.Empty;
var messages = messageBus.Pop();
var htmlBuilder = new StringBuilder(string.Empty);
var count = 0;
var total = messages.Count();
foreach (var message in messages)
{
try {
var current = templateFunc.Invoke(new FlashContext { Message = message, Index = count++, Total = total });
htmlBuilder.AppendLine(current.ToHtmlString());
}
catch (Exception){
}
}
return MvcHtmlString.Create(htmlBuilder.ToString());
}
/// <summary>
/// Flashes the selected messages and keeps the remaining elements in the messages stack.
/// </summary>
/// <param name="helper">The helper.</param>
/// <param name="filter">The filter.</param>
/// <param name="templateName">Name of the template.</param>
/// <returns></returns>
public static MvcHtmlString FlashSelect(this HtmlHelper helper, string filter, string templateName = "Flash")
{
var popper = FlashConfiguration.Popper;
if (popper == null) return MvcHtmlString.Empty;
var messages = popper.Select(x => x.Type == filter);
return Create(messages, helper, templateName);
}
/// <summary>
/// Flashes the selected messages and keeps the remaining elements in the messages stack.
/// </summary>
/// <param name="helper">The helper.</param>
/// <param name="filter">The filter.</param>
/// <param name="templateName">Name of the template.</param>
/// <returns></returns>
public static MvcHtmlString FlashSelect(this HtmlHelper helper, Func<dynamic, bool> filter, string templateName = "Flash")
{
var popper = FlashConfiguration.Popper;
if (popper == null) return MvcHtmlString.Empty;
var messages = popper.Select(filter);
return Create(messages, helper, templateName);
}
/// <summary>
/// Flashes all messages and calls the template function passed.
/// </summary>
/// <param name="helper">The helper.</param>
/// <param name="filter">The filter.</param>
/// <param name="templateFunc">The template function.</param>
/// <returns></returns>
public static MvcHtmlString FlashSelect(this HtmlHelper helper, Func<dynamic, bool> filter, Func<FlashContext, MvcHtmlString> templateFunc)
{
var messageBus = FlashConfiguration.Popper;
if (messageBus == null) return MvcHtmlString.Empty;
var messages = messageBus.Select(filter);
var htmlBuilder = new StringBuilder(string.Empty);
var count = 0;
var total = messages.Count();
foreach (var message in messages)
{
try
{
var current = templateFunc.Invoke(new FlashContext { Message = message, Index = count++, Total = total });
htmlBuilder.AppendLine(current.ToHtmlString());
}
catch (Exception)
{
}
}
return MvcHtmlString.Create(htmlBuilder.ToString());
}
/// <summary>
/// Flashes only the messages that meet the type criteria, and disregards the rest of the messages.
/// *** You will lose all messages that do not match the type ***
/// </summary>
/// <param name="helper">The helper.</param>
/// <param name="type">The type.</param>
/// <param name="templateName">Name of the template.</param>
/// <returns></returns>
public static MvcHtmlString FlashOnly(this HtmlHelper helper, string type = null, string templateName = "Flash")
{
if (string.IsNullOrWhiteSpace(type)) return Flash(helper, templateName);
var types = new[] {type};
return FlashOnly(helper, x => Enumerable.Contains(types, x.Type), templateName);
}
/// <summary>
/// Flashes only the messages that meet the type criteria, and disregards the rest of the messages.
/// *** You will lose all messages that do not match the type ***
/// </summary>
/// <param name="helper">The helper.</param>
/// <param name="types">The types.</param>
/// <param name="templateName">Name of the template.</param>
/// <returns></returns>
public static MvcHtmlString FlashOnly(this HtmlHelper helper, string[] types, string templateName = "Flash")
{
return FlashOnly(helper, x => Enumerable.Contains(types, x.Type), templateName);
}
/// <summary>
/// Flashes only the messages that meet the type criteria, and disregards the rest of the messages.
/// *** You will lose all messages that do not match the type ***
/// </summary>
/// <param name="helper">The helper.</param>
/// <param name="where">The where.</param>
/// <param name="templateName">Name of the template.</param>
/// <returns></returns>
public static MvcHtmlString FlashOnly(this HtmlHelper helper, Func<dynamic,bool> where = null, string templateName = "Flash" )
{
var popper = FlashConfiguration.Popper;
if (popper == null) return MvcHtmlString.Empty;
var messages = where == null ? popper.Pop() : popper.Pop().Where(x =>
{
try {
return where.Invoke(x);
}
catch (Exception) {
return false;
}
});
return Create(messages.ToList(), helper, templateName);
}
private static MvcHtmlString Create(ICollection<dynamic> messages, HtmlHelper helper, string templateName)
{
var templateFunc = new Func<FlashContext, MvcHtmlString>(ctx => helper.Partial(templateName, ctx, helper.ViewData));
var htmlBuilder = new StringBuilder(string.Empty);
var count = 0;
var total = messages.Count;
foreach (var message in messages)
{
var current = templateFunc.Invoke(new FlashContext { Message = message, Index = count++, Total = total });
htmlBuilder.AppendLine(current.ToHtmlString());
}
return MvcHtmlString.Create(htmlBuilder.ToString());
}
}
}