-
Notifications
You must be signed in to change notification settings - Fork 1
/
CallsToActionViewComponent.cs
78 lines (67 loc) · 4.72 KB
/
CallsToActionViewComponent.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
/*==============================================================================================================================
| Author Ignia, LLC
| Client GoldSim
| Project Website
\=============================================================================================================================*/
using OnTopic.AspNetCore.Mvc.Components;
using OnTopic.AspNetCore.Mvc.Models;
using OnTopic.Mapping.Hierarchical;
namespace GoldSim.Web.Components {
/*============================================================================================================================
| CLASS: CALLS TO ACTION VIEW COMPONENT
\---------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Defines a <see cref="ViewComponent"/> which provides access to a menu of <typeparamref name="NavigationTopicViewModel"/>
/// instances representing the nearest calls to action for a given page.
/// </summary>
public class CallsToActionViewComponent: NavigationTopicViewComponentBase<Models.NavigationTopicViewModel> {
/*==========================================================================================================================
| CONSTRUCTOR
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Initializes a new instance of a <see cref="MenuViewComponentBase{T}"/> with necessary dependencies.
/// </summary>
/// <returns>A topic controller for loading OnTopic views.</returns>
public CallsToActionViewComponent(
ITopicRepository topicRepository,
IHierarchicalTopicMappingService<Models.NavigationTopicViewModel> hierarchicalTopicMappingService
) : base(
topicRepository,
hierarchicalTopicMappingService
) {}
/*==========================================================================================================================
| METHOD: INVOKE (ASYNC)
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Provides the calls-to-action for the current page, which may change based on the current context.
/// </summary>
public async Task<IViewComponentResult> InvokeAsync() {
/*------------------------------------------------------------------------------------------------------------------------
| Establish variables
\-----------------------------------------------------------------------------------------------------------------------*/
var currentTopic = CurrentTopic;
/*------------------------------------------------------------------------------------------------------------------------
| Identify navigation root
>-------------------------------------------------------------------------------------------------------------------------
| The navigation root in the case of the main menu is the namespace; i.e., the first topic underneath the root.
\-----------------------------------------------------------------------------------------------------------------------*/
var navigationRootTopic = HierarchicalTopicMappingService.GetHierarchicalRoot(currentTopic, 3, "Web");
/*------------------------------------------------------------------------------------------------------------------------
| Validate conditions
\-----------------------------------------------------------------------------------------------------------------------*/
Contract.Assume(navigationRootTopic, $"The root topic could not be identified for the page-level navigation.");
Contract.Assume(CurrentTopic, $"The current topic could not be identified for the page-level navigation.");
/*------------------------------------------------------------------------------------------------------------------------
| Construct view model
\-----------------------------------------------------------------------------------------------------------------------*/
var navigationViewModel = new NavigationViewModel<Models.NavigationTopicViewModel>() {
NavigationRoot = await HierarchicalTopicMappingService.GetRootViewModelAsync(navigationRootTopic).ConfigureAwait(true),
CurrentWebPath = CurrentTopic?.GetWebPath()
};
/*------------------------------------------------------------------------------------------------------------------------
| Return the corresponding view
\-----------------------------------------------------------------------------------------------------------------------*/
return View(navigationViewModel);
}
} // Class
} // Namespace