forked from dd4t/dynamic-delivery-4-tridion
/
SiteMapMenuHelper.cs
166 lines (143 loc) · 5.59 KB
/
SiteMapMenuHelper.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Sample.Web.Mvc.Contracts;
using DD4T.ContentModel;
using DD4T.Mvc.Providers;
using System.Text;
namespace Sample.Web.Mvc.HtmlHelpers
{
public static class SiteMapMenuHelper
{
#region extension methods
public static HtmlString SiteMapMenu(this HtmlHelper helper, SiteMapNode rootNode, IPage model, MenuType menutype)
{
HtmlString htmlReturn = new HtmlString(String.Empty);
switch (menutype)
{
case MenuType.TopMenu:
htmlReturn = CreateTopMenu(rootNode, helper.ViewContext, model);
break;
case MenuType.LeftMenu:
htmlReturn = CreateLeftMenu(rootNode, helper.ViewContext, model);
break;
}
return htmlReturn;
}
#endregion
#region menu generation methods
private static HtmlString CreateTopMenu(SiteMapNode siteMapNode, ViewContext viewContext, IPage model)
{
// Only render when node has childnodes
if (!siteMapNode.HasChildNodes)
{
return null;
}
// write out the Home node separately (in the sitemap, it is on a level by its own, but in the html it is a sibling of the first-level nodes below it)
TridionSiteMapNode homeNode = (TridionSiteMapNode) siteMapNode.ChildNodes[0];
StringBuilder result = new StringBuilder();
AddMenuItem(result, homeNode, null);
AddChildMenuItems(result, homeNode, 0);
TagBuilder navUl = new TagBuilder("ul");
navUl.Attributes.Add("class", "top-navigation");
navUl.InnerHtml = result.ToString();
// Return unordered list
return new HtmlString(navUl.ToString());
}
private static HtmlString CreateLeftMenu(SiteMapNode siteMapNode, ViewContext viewContext, IPage model)
{
// Only render when node has childnodes
if (!siteMapNode.HasChildNodes)
{
return null;
}
SiteMapNode parentNode;
TridionSiteMapNode parentTridionNode;
TridionSiteMapNode startingNode = null;
string structureGroupId = model.StructureGroup.Id;
if (SiteMapHelper.TryFindActiveNode(siteMapNode, structureGroupId, out parentNode))
{
parentTridionNode = (TridionSiteMapNode)parentNode;
if (parentTridionNode.Level > 2)
{
startingNode = FindMainParentNode(parentTridionNode);
}
else if (parentTridionNode.Level == 2)
{
startingNode = parentTridionNode;
}
else
{
return new MvcHtmlString(string.Format("<!-- error: cannot generate left menu for pages on level {0} -->", parentTridionNode.Level));
}
}
else
{
return null;
}
StringBuilder result = new StringBuilder();
AddChildMenuItems(result, startingNode, 2);
TagBuilder navUl = new TagBuilder("ul");
navUl.Attributes.Add("class", "left-navigation");
navUl.InnerHtml = result.ToString();
// Return unordered list
return new HtmlString(navUl.ToString());
}
#endregion
#region private helpers
private static void AddChildMenuItems(StringBuilder sb, TridionSiteMapNode node, int deep)
{
// Iterate through childnodes
if (!node.HasChildNodes)
return;
foreach (TridionSiteMapNode childNode in node.ChildNodes)
{
StringBuilder liNodes = new StringBuilder();
if (deep > 0 && childNode.HasChildNodes)
{
TagBuilder ul = new TagBuilder("ul");
AddChildMenuItems(liNodes, childNode, deep - 1);
ul.InnerHtml = liNodes.ToString();
AddMenuItem(sb, childNode, ul.ToString());
}
else
{
AddMenuItem(sb, childNode, null);
}
}
}
private static void AddMenuItem(StringBuilder sb, TridionSiteMapNode node, string submenu)
{
TagBuilder li = new TagBuilder("li");
TagBuilder a = new TagBuilder("a");
a.Attributes.Add("href", node.ResolvedUrl);
a.InnerHtml = node.Title;
if (submenu == null)
{
li.InnerHtml = a.ToString();
}
else
{
li.InnerHtml = a.ToString() + submenu;
}
sb.Append(li.ToString());
}
private static TridionSiteMapNode FindMainParentNode(TridionSiteMapNode childnode)
{
if (childnode == null)
{
return null;
}
TridionSiteMapNode currentNode = childnode;
while (currentNode.ParentNode != null && currentNode.Level > 2)
{
currentNode = (TridionSiteMapNode)currentNode.ParentNode;
}
return currentNode;
}
#endregion
}
}