Repository navigation
Expand file tree
/
Copy pathHtmlExtensions.cs
More file actions
252 lines (232 loc) · 12.1 KB
/
Copy pathHtmlExtensions.cs
File metadata and controls
252 lines (232 loc) · 12.1 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
using Grand.Core;
using Grand.Core.Caching;
using Grand.Core.Infrastructure;
using Grand.Framework;
using Grand.Framework.UI.Paging;
using Grand.Services.Localization;
using Grand.Services.Seo;
using Grand.Services.Topics;
using Grand.Web.Infrastructure.Cache;
using Grand.Web.Models.Boards;
using Grand.Web.Models.Common;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Text;
using System.Threading.Tasks;
namespace Grand.Web.Extensions
{
public static class HtmlExtensions
{
/// <summary>
/// BBCode editor
/// </summary>
/// <typeparam name="TModel">Model</typeparam>
/// <param name="html">HTML Helper</param>
/// <param name="name">Name</param>
/// <returns>Editor</returns>
public static IHtmlContent BBCodeEditor<TModel>(this IHtmlHelper<TModel> html, IWebHelper webHelper, string name)
{
var sb = new StringBuilder();
var storeLocation = webHelper.GetStoreLocation();
string bbEditorWebRoot = String.Format("{0}content/", storeLocation);
sb.AppendFormat("<script src=\"{0}content/bbeditor/ed.js\" ></script>", storeLocation);
sb.Append(Environment.NewLine);
sb.Append("<script language=\"javascript\" type=\"text/javascript\">");
sb.Append(Environment.NewLine);
sb.AppendFormat("edToolbar('{0}','{1}');", name, bbEditorWebRoot);
sb.Append(Environment.NewLine);
sb.Append("</script>");
sb.Append(Environment.NewLine);
return new HtmlString(sb.ToString());
}
//we have two pagers:
//The first one can have custom routes
//The second one just adds query string parameter
public static IHtmlContent Pager<TModel>(this IHtmlHelper<TModel> html, ILocalizationService localizationService, PagerModel model)
{
if (model.TotalRecords == 0)
return null;
var links = new StringBuilder();
if (model.ShowTotalSummary && (model.TotalPages > 0))
{
links.Append("<li class=\"total-summary page-item\">");
links.Append(string.Format(model.CurrentPageText, model.PageIndex + 1, model.TotalPages, model.TotalRecords));
links.Append("</li>");
}
if (model.ShowPagerItems && (model.TotalPages > 1))
{
if (model.ShowFirst)
{
//first page
if ((model.PageIndex >= 3) && (model.TotalPages > model.IndividualPagesDisplayedCount))
{
model.RouteValues.pageNumber = 1;
links.Append("<li class=\"first-page page-item\">");
if (model.UseRouteLinks)
{
links.Append(html.RouteLink(model.FirstButtonText, model.RouteActionName, model.RouteValues, new { title = localizationService.GetResource("Pager.FirstPageTitle"), @class = "page-link" }).ToHtmlString());
}
else
{
links.Append(html.ActionLink(model.FirstButtonText, model.RouteActionName, model.RouteValues, new { title = localizationService.GetResource("Pager.FirstPageTitle"), @class = "page-link" }).ToHtmlString());
}
links.Append("</li>");
}
}
if (model.ShowPrevious)
{
//previous page
if (model.PageIndex > 0)
{
model.RouteValues.pageNumber = (model.PageIndex);
links.Append("<li class=\"previous-page page-item\">");
if (model.UseRouteLinks)
{
links.Append(html.RouteLink(model.PreviousButtonText, model.RouteActionName, model.RouteValues, new { title = localizationService.GetResource("Pager.PreviousPageTitle"), @class = "page-link" }).ToHtmlString());
}
else
{
links.Append(html.ActionLink(model.PreviousButtonText, model.RouteActionName, model.RouteValues, new { title = localizationService.GetResource("Pager.PreviousPageTitle"), @class = "page-link" }).ToHtmlString());
}
links.Append("</li>");
}
}
if (model.ShowIndividualPages)
{
//individual pages
int firstIndividualPageIndex = model.GetFirstIndividualPageIndex();
int lastIndividualPageIndex = model.GetLastIndividualPageIndex();
for (int i = firstIndividualPageIndex; i <= lastIndividualPageIndex; i++)
{
if (model.PageIndex == i)
{
links.AppendFormat("<li class=\"current-page page-item\"><a class=\"page-link\">{0}</a></li>", (i + 1));
}
else
{
model.RouteValues.pageNumber = (i + 1);
links.Append("<li class=\"individual-page page-item\">");
if (model.UseRouteLinks)
{
links.Append(html.RouteLink((i + 1).ToString(), model.RouteActionName, model.RouteValues, new { title = String.Format(localizationService.GetResource("Pager.PageLinkTitle"), (i + 1)), @class = "page-link" }).ToHtmlString());
}
else
{
links.Append(html.ActionLink((i + 1).ToString(), model.RouteActionName, model.RouteValues, new { title = String.Format(localizationService.GetResource("Pager.PageLinkTitle"), (i + 1)), @class = "page-link" }).ToHtmlString());
}
links.Append("</li>");
}
}
}
if (model.ShowNext)
{
//next page
if ((model.PageIndex + 1) < model.TotalPages)
{
model.RouteValues.pageNumber = (model.PageIndex + 2);
links.Append("<li class=\"next-page page-item\">");
if (model.UseRouteLinks)
{
links.Append(html.RouteLink(model.NextButtonText, model.RouteActionName, model.RouteValues, new { title = localizationService.GetResource("Pager.NextPageTitle"), @class = "page-link" }).ToHtmlString());
}
else
{
links.Append(html.ActionLink(model.NextButtonText, model.RouteActionName, model.RouteValues, new { title = localizationService.GetResource("Pager.NextPageTitle"), @class = "page-link" }).ToHtmlString());
}
links.Append("</li>");
}
}
if (model.ShowLast)
{
//last page
if (((model.PageIndex + 3) < model.TotalPages) && (model.TotalPages > model.IndividualPagesDisplayedCount))
{
model.RouteValues.pageNumber = model.TotalPages;
links.Append("<li class=\"last-page page-item\">");
if (model.UseRouteLinks)
{
links.Append(html.RouteLink(model.LastButtonText, model.RouteActionName, model.RouteValues, new { title = localizationService.GetResource("Pager.LastPageTitle"), @class = "page-link" }).ToHtmlString());
}
else
{
links.Append(html.ActionLink(model.LastButtonText, model.RouteActionName, model.RouteValues, new { title = localizationService.GetResource("Pager.LastPageTitle"), @class = "page-link" }).ToHtmlString());
}
links.Append("</li>");
}
}
}
var result = links.ToString();
if (!String.IsNullOrEmpty(result))
{
result = "<ul class=\"pagination\">" + result + "</ul>";
}
return new HtmlString(result);
}
public static IHtmlContent ForumTopicSmallPager<TModel>(this IHtmlHelper<TModel> html, ILocalizationService localizationService, ForumTopicRowModel model)
{
var forumTopicId = model.Id;
var forumTopicSlug = model.SeName;
var totalPages = model.TotalPostPages;
if (totalPages > 0)
{
var links = new StringBuilder();
if (totalPages <= 4)
{
for (int x = 1; x <= totalPages; x++)
{
links.Append(html.RouteLink(x.ToString(), "TopicSlugPaged", new { id = forumTopicId, pageNumber = (x), slug = forumTopicSlug }, new { title = String.Format(localizationService.GetResource("Pager.PageLinkTitle"), x.ToString()) }).ToHtmlString());
if (x < totalPages)
{
links.Append(", ");
}
}
}
else
{
links.Append(html.RouteLink("1", "TopicSlugPaged", new { id = forumTopicId, pageNumber = (1), slug = forumTopicSlug }, new { title = String.Format(localizationService.GetResource("Pager.PageLinkTitle"), 1) }).ToHtmlString());
links.Append(" ... ");
for (int x = (totalPages - 2); x <= totalPages; x++)
{
links.Append(html.RouteLink(x.ToString(), "TopicSlugPaged", new { id = forumTopicId, pageNumber = (x), slug = forumTopicSlug }, new { title = String.Format(localizationService.GetResource("Pager.PageLinkTitle"), x.ToString()) }));
if (x < totalPages)
{
links.Append(", ");
}
}
}
// Inserts the topic page links into the localized string ([Go to page: {0}])
return new HtmlString(String.Format(localizationService.GetResource("Forum.Topics.GotoPostPager"), links));
}
return new HtmlString(string.Empty);
}
public static Pager Pager(this IHtmlHelper helper, IPageableModel pagination)
{
return new Pager(pagination, helper.ViewContext);
}
///// <summary>
///// Get topic system name
///// </summary>
///// <typeparam name="T">T</typeparam>
///// <param name="html">HTML helper</param>
///// <param name="systemName">System name</param>
///// <returns>Topic SEO Name</returns>
public static async Task<string> GetTopicSeName<T>(this IHtmlHelper<T> html, string systemName)
{
var workContext = EngineContext.Current.Resolve<IWorkContext>();
var storeContext = EngineContext.Current.Resolve<IStoreContext>();
//static cache manager
var cacheManager = EngineContext.Current.Resolve<ICacheManager>();
var cacheKey = string.Format(ModelCacheEventConsumer.TOPIC_SENAME_BY_SYSTEMNAME, systemName, workContext.WorkingLanguage.Id, storeContext.CurrentStore.Id);
var cachedSeName = await cacheManager.Get(cacheKey, async () =>
{
var topicService = EngineContext.Current.Resolve<ITopicService>();
var topic = await topicService.GetTopicBySystemName(systemName, storeContext.CurrentStore.Id);
if (topic == null)
return "";
return topic.GetSeName(workContext.WorkingLanguage.Id);
});
return cachedSeName;
}
}
}