Skip to content

Commit f825b9d

Browse files
committed
Introduced LayoutController and affiliated views
In the **ASP.NET MVC Framework**, we use an embedded `LayoutController` instead of the newer view component functionality. It's effectively the same technique, just not quite as optimized for individual components. That said, the views are identical—outside of the fact that we can use an actual HTML `@Helper` in this version.
1 parent 32313d8 commit f825b9d

7 files changed

Lines changed: 138 additions & 6 deletions

File tree

Ignia.Topics.AspNetCore.Mvc.Host/Views/Shared/Components/PageLevelNavigation/Default.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99

1010
<!--
1111
Content Type: NavigationViewModel<NavigationTopicViewModel>
12-
View Location: ~/Views/Shared/Components/Menu/Default.cshtml
12+
View Location: ~/Views/Shared/Components/PageLevelNavigation/Default.cshtml
1313
-->
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*==============================================================================================================================
2+
| Author Ignia, LLC
3+
| Client Ignia, LLC
4+
| Project OnTopicSample OnTopic Site
5+
\=============================================================================================================================*/
6+
using System.Threading.Tasks;
7+
using System.Web.Mvc;
8+
using Ignia.Topics.Repositories;
9+
using Ignia.Topics.Web.Mvc.Controllers;
10+
using Ignia.Topics.Web.Mvc.Models;
11+
using Ignia.Topics.ViewModels;
12+
using Ignia.Topics.Mapping.Hierarchical;
13+
14+
namespace Ignia.Topics.Web.Mvc.Host.Controllers {
15+
16+
/*============================================================================================================================
17+
| CLASS: LAYOUT CONTROLLER
18+
\---------------------------------------------------------------------------------------------------------------------------*/
19+
/// <summary>
20+
/// Provides access to the default homepage for the site.
21+
/// </summary>
22+
public class LayoutController : LayoutControllerBase<NavigationTopicViewModel> {
23+
24+
/*==========================================================================================================================
25+
| PRIVATE FIELDS
26+
\-------------------------------------------------------------------------------------------------------------------------*/
27+
private readonly ITopicRepository _topicRepository = null;
28+
29+
/*==========================================================================================================================
30+
| CONSTRUCTOR
31+
\-------------------------------------------------------------------------------------------------------------------------*/
32+
/// <summary>
33+
/// Initializes a new instance of a Topic Controller with necessary dependencies.
34+
/// </summary>
35+
/// <returns>A topic controller for loading OnTopic views.</returns>
36+
public LayoutController(
37+
ITopicRoutingService topicRoutingService,
38+
IHierarchicalTopicMappingService<NavigationTopicViewModel> hierarchicalTopicMappingService,
39+
ITopicRepository topicRepository
40+
) : base(
41+
topicRoutingService,
42+
hierarchicalTopicMappingService
43+
) {
44+
_topicRepository = topicRepository;
45+
}
46+
47+
/*==========================================================================================================================
48+
| PAGE LEVEL NAVIGATION
49+
\-------------------------------------------------------------------------------------------------------------------------*/
50+
/// <summary>
51+
/// Provides page-level navigation for the current page.
52+
/// </summary>
53+
public async Task<PartialViewResult> PageLevelNavigation() {
54+
55+
/*------------------------------------------------------------------------------------------------------------------------
56+
| Establish variables
57+
\-----------------------------------------------------------------------------------------------------------------------*/
58+
var currentTopic = CurrentTopic;
59+
var navigationRootTopic = currentTopic;
60+
61+
/*------------------------------------------------------------------------------------------------------------------------
62+
| Identify navigation root
63+
>-------------------------------------------------------------------------------------------------------------------------
64+
| The navigation root in the case of the page-level navigation any parent of content type "PageGroup".
65+
\-----------------------------------------------------------------------------------------------------------------------*/
66+
if (navigationRootTopic != null) {
67+
while (navigationRootTopic.Parent != null && !navigationRootTopic.ContentType.Equals("PageGroup")) {
68+
navigationRootTopic = navigationRootTopic.Parent;
69+
}
70+
}
71+
72+
if (navigationRootTopic?.Parent == null) navigationRootTopic = null;
73+
74+
/*------------------------------------------------------------------------------------------------------------------------
75+
| Construct view model
76+
\-----------------------------------------------------------------------------------------------------------------------*/
77+
var navigationViewModel = new NavigationViewModel<NavigationTopicViewModel>() {
78+
NavigationRoot = await HierarchicalTopicMappingService.GetRootViewModelAsync(navigationRootTopic),
79+
CurrentKey = CurrentTopic?.GetUniqueKey()
80+
};
81+
82+
/*------------------------------------------------------------------------------------------------------------------------
83+
| Return the corresponding view
84+
\-----------------------------------------------------------------------------------------------------------------------*/
85+
return PartialView(navigationViewModel);
86+
87+
}
88+
89+
} // Class
90+
} // Namespace

Ignia.Topics.Web.Mvc.Host/Ignia.Topics.Web.Mvc.Host.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
</ItemGroup>
106106
<ItemGroup>
107107
<Compile Include="App_Start\RouteConfig.cs" />
108+
<Compile Include="Controllers\LayoutController.cs" />
108109
<Compile Include="Global.asax.cs">
109110
<DependentUpon>Global.asax</DependentUpon>
110111
</Compile>
@@ -126,6 +127,8 @@
126127
<Content Include="Views\Shared\_PageAttributes.cshtml" />
127128
<Content Include="Views\Shared\_TopicAttributes.cshtml" />
128129
<Content Include="Views\_ViewStart.cshtml" />
130+
<Content Include="Views\Layout\Menu.cshtml" />
131+
<Content Include="Views\Layout\PageLevelNavigation.cshtml" />
129132
<None Include="Web.Debug.config">
130133
<DependentUpon>Web.config</DependentUpon>
131134
</None>
@@ -135,7 +138,6 @@
135138
</ItemGroup>
136139
<ItemGroup>
137140
<Folder Include="App_Data\" />
138-
<Folder Include="Controllers\" />
139141
<Folder Include="Models\" />
140142
</ItemGroup>
141143
<ItemGroup>

Ignia.Topics.Web.Mvc.Host/SampleControllerFactory.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Ignia.Topics.Repositories;
1515
using Ignia.Topics.ViewModels;
1616
using Ignia.Topics.Web.Mvc.Controllers;
17+
using Ignia.Topics.Web.Mvc.Host.Controllers;
1718

1819
namespace Ignia.Topics.Web.Mvc.Host {
1920

@@ -102,13 +103,11 @@ protected override IController GetControllerInstance(RequestContext requestConte
102103
case nameof(SitemapController):
103104
return new SitemapController(_topicRepository);
104105

105-
/*
106106
case nameof(ErrorController):
107107
return new ErrorController();
108108

109109
case nameof(LayoutController):
110110
return new LayoutController(mvcTopicRoutingService, _hierarchicalTopicMappingService, _topicRepository);
111-
*/
112111

113112
case nameof(TopicController):
114113
return new TopicController(_topicRepository, mvcTopicRoutingService, _topicMappingService);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@model NavigationViewModel<NavigationTopicViewModel>
2+
3+
<h2>Menu</h2>
4+
<nav id="PrimaryNavigationSmallScreen" role="navigation" vocab="http://schema.org" typeof="SiteNavigationElement">
5+
<ul>
6+
@foreach (var topic in Model.NavigationRoot.Children) {
7+
@WriteMenu(topic);
8+
}
9+
</ul>
10+
</nav>
11+
12+
@helper WriteMenu(NavigationTopicViewModel topic, int indentLevel = 1) {
13+
14+
<li>
15+
<a href="@topic.WebPath">@(topic.ShortTitle?? topic.Title?? topic.Key)</a>
16+
<ul>
17+
@foreach (var childTopic in topic.Children) {
18+
@WriteMenu(childTopic, indentLevel+1);
19+
}
20+
</ul>
21+
</li>
22+
23+
}
24+
25+
<!--
26+
Content Type: NavigationViewModel<NavigationTopicViewModel
27+
View Location: ~/Views/Layout/Menu.cshtml
28+
-->
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@model NavigationViewModel<NavigationTopicViewModel>
2+
3+
<h2>PageLevelNavigation</h2>
4+
<ul>
5+
@foreach (var topic in Model.NavigationRoot.Children) {
6+
<li><a href="@topic.WebPath">@(topic.ShortTitle?? topic.Title)</a></li>
7+
}
8+
</ul>
9+
10+
<!--
11+
Content Type: NavigationViewModel<NavigationTopicViewModel>
12+
View Location: ~/Views/Layout/PageLevelNavigation.cshtml
13+
-->

Ignia.Topics.Web.Mvc.Host/Views/Layout/_Layout.cshtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<!-- Site Navigation Area -->
1414
<navigation id="SiteNavigation">
15-
<vc:menu />
15+
@Html.Action("Menu", "Layout")
1616
</navigation>
1717
<!-- /Site Navigation Area -->
1818

@@ -21,7 +21,7 @@
2121

2222
<!-- Page Header Area -->
2323
<section id="PageHeaderSection">
24-
<vc:page-level-navigation />
24+
@Html.Action("PageLevelNavigation", "Layout")
2525
</section>
2626
<!-- /Page Header Area -->
2727

0 commit comments

Comments
 (0)