-
Notifications
You must be signed in to change notification settings - Fork 745
/
BreadCrumb.ascx.cs
255 lines (218 loc) · 9.91 KB
/
BreadCrumb.ascx.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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information
namespace DotNetNuke.UI.Skins.Controls
{
using System;
using System.Text;
using System.Text.RegularExpressions;
using DotNetNuke.Abstractions;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Tabs;
using Microsoft.Extensions.DependencyInjection;
/// <summary></summary>
public partial class BreadCrumb : SkinObjectBase
{
private const string UrlRegex = "(href|src)=(\\\"|'|)(.[^\\\"']*)(\\\"|'|)";
private readonly StringBuilder breadcrumb = new StringBuilder("<span itemscope itemtype=\"http://schema.org/BreadcrumbList\">");
private readonly INavigationManager navigationManager;
private string separator = "<img alt=\"breadcrumb separator\" src=\"" + Globals.ApplicationPath + "/images/breadcrumb.gif\">";
private string cssClass = "SkinObject";
private int rootLevel = 0;
private bool showRoot = false;
private string homeUrl = string.Empty;
private string homeTabName = "Root";
public BreadCrumb()
{
this.navigationManager = Globals.DependencyProvider.GetRequiredService<INavigationManager>();
this.CleanerMarkup = false;
}
public int ProfileUserId
{
get
{
return string.IsNullOrEmpty(this.Request.Params["UserId"])
? Null.NullInteger
: int.Parse(this.Request.Params["UserId"]);
}
}
public int GroupId
{
get
{
return string.IsNullOrEmpty(this.Request.Params["GroupId"])
? Null.NullInteger
: int.Parse(this.Request.Params["GroupId"]);
}
}
// Separator between breadcrumb elements
public string Separator
{
get { return this.separator; }
set { this.separator = value; }
}
public string CssClass
{
get { return this.cssClass; }
set { this.cssClass = value; }
}
// Level to begin processing breadcrumb at.
// -1 means show root breadcrumb
public string RootLevel
{
get
{
return this.rootLevel.ToString();
}
set
{
this.rootLevel = int.Parse(value);
if (this.rootLevel < 0)
{
this.showRoot = true;
this.rootLevel = 0;
}
}
}
// Use the page title instead of page name
public bool UseTitle { get; set; }
// Do not show when there is no breadcrumb (only has current tab)
public bool HideWithNoBreadCrumb { get; set; }
/// <summary>Gets or sets a value indicating whether to take advantage of the enhanced markup (remove extra wrapping elements).</summary>
public bool CleanerMarkup { get; set; }
/// <inheritdoc/>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// Position in breadcrumb list
var position = 1;
// resolve image path in separator content
this.ResolveSeparatorPaths();
// If we have enabled hiding when there are no breadcrumbs, simply return
if (this.HideWithNoBreadCrumb && this.PortalSettings.ActiveTab.BreadCrumbs.Count == (this.rootLevel + 1))
{
return;
}
// Without checking if the current tab is the home tab, we would duplicate the root tab
if (this.showRoot && this.PortalSettings.ActiveTab.TabID != this.PortalSettings.HomeTabId)
{
// Add the current protocal to the current URL
this.homeUrl = Globals.AddHTTP(this.PortalSettings.PortalAlias.HTTPAlias);
// Make sure we have a home tab ID set
if (this.PortalSettings.HomeTabId != -1)
{
this.homeUrl = this.navigationManager.NavigateURL(this.PortalSettings.HomeTabId);
var tc = new TabController();
var homeTab = tc.GetTab(this.PortalSettings.HomeTabId, this.PortalSettings.PortalId, false);
this.homeTabName = homeTab.LocalizedTabName;
// Check if we should use the tab's title instead
if (this.UseTitle && !string.IsNullOrEmpty(homeTab.Title))
{
this.homeTabName = homeTab.Title;
}
}
// Append all of the HTML for the root breadcrumb
this.breadcrumb.Append("<span itemprop=\"itemListElement\" itemscope itemtype=\"http://schema.org/ListItem\">");
this.breadcrumb.Append("<a href=\"" + this.homeUrl + "\" class=\"" + this.cssClass + "\" itemprop=\"item\" ><span itemprop=\"name\">" + this.homeTabName + "</span></a>");
this.breadcrumb.Append("<meta itemprop=\"position\" content=\"" + position++ + "\" />"); // Notice we post-increment the position variable
this.breadcrumb.Append("</span>");
// Add a separator
this.breadcrumb.Append(this.separator);
}
// process bread crumbs
for (var i = this.rootLevel; i < this.PortalSettings.ActiveTab.BreadCrumbs.Count; ++i)
{
// Only add separators if we're past the root level
if (i > this.rootLevel)
{
this.breadcrumb.Append(this.separator);
}
// Grab the current tab
var tab = (TabInfo)this.PortalSettings.ActiveTab.BreadCrumbs[i];
var tabName = tab.LocalizedTabName;
// Determine if we should use the tab's title instead of tab name
if (this.UseTitle && !string.IsNullOrEmpty(tab.Title))
{
tabName = tab.Title;
}
// Get the absolute URL of the tab
var tabUrl = tab.FullUrl;
if (this.ProfileUserId > -1)
{
tabUrl = this.navigationManager.NavigateURL(tab.TabID, string.Empty, "UserId=" + this.ProfileUserId);
}
if (this.GroupId > -1)
{
tabUrl = this.navigationManager.NavigateURL(tab.TabID, string.Empty, "GroupId=" + this.GroupId);
}
// Is this tab disabled? If so, only render a span
if (tab.DisableLink)
{
if (this.CleanerMarkup)
{
this.breadcrumb.Append("<span class=\"" + this.cssClass + "\">" + tabName + "</span>");
}
else
{
this.breadcrumb.Append("<span><span class=\"" + this.cssClass + "\">" + tabName + "</span></span>");
}
}
else
{
// An enabled page, render the breadcrumb
this.breadcrumb.Append("<span itemprop=\"itemListElement\" itemscope itemtype=\"http://schema.org/ListItem\">");
this.breadcrumb.Append("<a href=\"" + tabUrl + "\" class=\"" + this.cssClass + "\" itemprop=\"item\"><span itemprop=\"name\">" + tabName + "</span></a>");
this.breadcrumb.Append("<meta itemprop=\"position\" content=\"" + position++ + "\" />"); // Notice we post-increment the position variable
this.breadcrumb.Append("</span>");
}
}
this.breadcrumb.Append("</span>"); // End of BreadcrumbList
this.lblBreadCrumb.Text = this.breadcrumb.ToString();
}
private void ResolveSeparatorPaths()
{
if (string.IsNullOrEmpty(this.separator))
{
return;
}
var urlMatches = Regex.Matches(this.separator, UrlRegex, RegexOptions.IgnoreCase);
if (urlMatches.Count > 0)
{
foreach (Match match in urlMatches)
{
var url = match.Groups[3].Value;
var changed = false;
if (url.StartsWith("/"))
{
if (!string.IsNullOrEmpty(Globals.ApplicationPath))
{
url = string.Format("{0}{1}", Globals.ApplicationPath, url);
changed = true;
}
}
else if (url.StartsWith("~/"))
{
url = Globals.ResolveUrl(url);
changed = true;
}
else
{
url = string.Format("{0}{1}", this.PortalSettings.ActiveTab.SkinPath, url);
changed = true;
}
if (changed)
{
var newMatch = string.Format(
"{0}={1}{2}{3}",
match.Groups[1].Value,
match.Groups[2].Value,
url,
match.Groups[4].Value);
this.separator = this.separator.Replace(match.Value, newMatch);
}
}
}
}
}
}