-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilePathViewComponent.cs
More file actions
226 lines (196 loc) · 13.4 KB
/
FilePathViewComponent.cs
File metadata and controls
226 lines (196 loc) · 13.4 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
/*==============================================================================================================================
| Author Ignia, LLC
| Client Ignia, LLC
| Project Topics Library
\=============================================================================================================================*/
using OnTopic.AspNetCore.Mvc;
using OnTopic.Repositories;
namespace OnTopic.Editor.AspNetCore.Attributes.FilePathAttribute {
/*============================================================================================================================
| CLASS: FILE PATH (VIEW COMPONENT)
\---------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Delivers a view model for a file path attribute type.
/// </summary>
public class FilePathViewComponent: ViewComponent {
/*==========================================================================================================================
| PRIVATE VARIABLES
\-------------------------------------------------------------------------------------------------------------------------*/
private Topic? _currentTopic;
/*==========================================================================================================================
| CONSTRUCTOR
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Initializes a new instance of a <see cref="FilePathViewComponent"/> with necessary dependencies.
/// </summary>
/// <remarks>
/// As with other attribute view components, the <see cref="FilePathViewComponent"/> receives a <see cref="
/// EditingTopicViewModel"/> via the <see cref="Invoke(EditingTopicViewModel, FilePathAttributeDescriptorViewModel, String
/// )"/>. That view model, however, is not sufficient to handle the specialized inheritance logic required by the <see
/// cref="FilePathViewComponent"/>. As a result, it <i>also</i> requires an instance of a <see cref="ITopicRepository"/>
/// so that it can work directly off the current <see cref="Topic"/> and its parent tree. The <see cref="
/// EditingTopicViewModel"/> is still passed not only for consistency, but also to spare the overhead and redundant logic
/// of mapping it again, since this was already done in <see cref="Controllers.EditorController"/>.
/// </remarks>
public FilePathViewComponent(ITopicRepository topicRepository) : base() {
Contract.Requires(topicRepository, nameof(topicRepository));
TopicRepository = topicRepository;
}
/*==========================================================================================================================
| TOPIC REPOSITORY
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Provides a reference to the <see cref="ITopicRepository"/> in order to allow the current topic to be identified based
/// on the route data.
/// </summary>
/// <returns>
/// The <see cref="ITopicRepository"/> associated with the <see cref="FilePathViewComponent"/>.
/// </returns>
protected ITopicRepository TopicRepository { get; }
/*==========================================================================================================================
| CURRENT TOPIC
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Provides a reference to the current topic associated with the request.
/// </summary>
/// <returns>The Topic associated with the current request.</returns>
protected Topic? CurrentTopic {
get {
if (_currentTopic is null) {
_currentTopic = TopicRepository.Load(RouteData);
}
Contract.Assume(_currentTopic, nameof(_currentTopic));
return _currentTopic;
}
}
/*==========================================================================================================================
| METHOD: INVOKE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Assembles the view model for the <see cref="FilePathViewComponent"/>.
/// </summary>
public IViewComponentResult Invoke(
EditingTopicViewModel currentTopic,
FilePathAttributeDescriptorViewModel attribute,
string htmlFieldPrefix
) {
/*------------------------------------------------------------------------------------------------------------------------
| Validate parameters
\-----------------------------------------------------------------------------------------------------------------------*/
Contract.Requires(currentTopic, nameof(currentTopic));
Contract.Requires(attribute, nameof(attribute));
/*------------------------------------------------------------------------------------------------------------------------
| Set HTML prefix
\-----------------------------------------------------------------------------------------------------------------------*/
ViewData.TemplateInfo.HtmlFieldPrefix = htmlFieldPrefix;
/*------------------------------------------------------------------------------------------------------------------------
| Establish view model
\-----------------------------------------------------------------------------------------------------------------------*/
var model = new FilePathAttributeViewModel(currentTopic, attribute) {
InheritedValue = GetInheritedValue(attribute.Key!, attribute)
};
/*------------------------------------------------------------------------------------------------------------------------
| Set model values
\-----------------------------------------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------------------------------------------
| Return view with view model
\-----------------------------------------------------------------------------------------------------------------------*/
return View(model);
}
/*==========================================================================================================================
| METHOD: GET INHERITED VALUE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Crawls up the tree to identify the source of inheritance (if available) and sets the value based on the base path
/// defined by the parent (assuming <see cref="AttributeViewModel.InheritedValue"/> is enabled) and the relative path
/// between that topic and the current topic (assuming <see cref="FilePathAttributeDescriptorViewModel.RelativeToTopicPath
/// "/> is enabled).
/// </summary>
public string GetInheritedValue(string attributeKey, FilePathAttributeDescriptorViewModel attribute) {
var inheritedValue = "";
if (attribute is { InheritValue: true, RelativeToTopicPath: true }) {
inheritedValue = GetPath(attributeKey, attribute);
}
else if (attribute is { InheritValue: true }) {
inheritedValue = CurrentTopic?.Attributes.GetValue(attributeKey, true)?? "";
}
return inheritedValue;
}
/*==========================================================================================================================
| METHOD: GET PATH
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Static helper method that returns a constructed file path based on evaluation and processing of the parameter values/
/// settings passed to the method.
/// </summary>
/// <param name="attributeKey">The attribute key.</param>
/// <param name="options">
/// An <see cref="FilePathAttributeDescriptorViewModel"/> to assess configuration options from.
/// </param>
/// <returns>A constructed file path.</returns>
public string GetPath(string attributeKey, FilePathAttributeDescriptorViewModel options) {
/*------------------------------------------------------------------------------------------------------------------------
| Validate parameters
\-----------------------------------------------------------------------------------------------------------------------*/
Contract.Requires(options, nameof(options));
/*------------------------------------------------------------------------------------------------------------------------
| Build configured file path string base on values and settings parameters passed to the method
\-----------------------------------------------------------------------------------------------------------------------*/
var filePath = "";
var relativePath = (string?)null;
var startTopic = CurrentTopic;
var endTopic = (options.IncludeCurrentTopic is null)? CurrentTopic: CurrentTopic?.Parent?? CurrentTopic;
var truncatePathAtTopic = options.BaseTopicPath?.Split(',').ToArray()?? Array.Empty<string>();
/*------------------------------------------------------------------------------------------------------------------------
| Only process the path if both topic and attribtueKey are provided
\-----------------------------------------------------------------------------------------------------------------------*/
if (startTopic is null || attributeKey is null or { Length: 0 }) return "";
/*------------------------------------------------------------------------------------------------------------------------
| Crawl up the topics tree to find file path values set at a higher level
\-----------------------------------------------------------------------------------------------------------------------*/
while (String.IsNullOrEmpty(filePath) && startTopic is not null && startTopic.Parent is not null) {
startTopic = startTopic.Parent;
if (startTopic is not null && !String.IsNullOrEmpty(attributeKey)) {
filePath = startTopic.Attributes.GetValue(attributeKey);
}
}
/*------------------------------------------------------------------------------------------------------------------------
| Add topic keys (directory names) between the start topic and the end topic based on the topic's WebPath property
\-----------------------------------------------------------------------------------------------------------------------*/
if (startTopic is not null) {
if (startTopic.GetWebPath().Length > endTopic?.GetWebPath().Length) {
throw new InvalidOperationException(
$"The path of {startTopic.GetWebPath()} should be shorter than the length of {endTopic.GetWebPath()}."
);
}
var startTopicWebPath = startTopic.GetWebPath().Replace("/Root/", "/", StringComparison.OrdinalIgnoreCase);
relativePath = endTopic?.GetWebPath()[Math.Max(startTopicWebPath.Length-1, 0)..];
}
/*------------------------------------------------------------------------------------------------------------------------
| Perform path truncation based on topics included in TruncatePathAtTopic
\-----------------------------------------------------------------------------------------------------------------------*/
if (!String.IsNullOrWhiteSpace(options.BaseTopicPath)) {
foreach (var truncationTopic in truncatePathAtTopic) {
var truncateTopicLocation = relativePath?.IndexOf(truncationTopic, StringComparison.OrdinalIgnoreCase);
if (truncateTopicLocation >= 0) {
relativePath = relativePath?[..(truncateTopicLocation.Value + truncationTopic.Length + 1)];
}
}
}
/*------------------------------------------------------------------------------------------------------------------------
| Add resulting relative path to the original file path (based on starting topic)
\-----------------------------------------------------------------------------------------------------------------------*/
filePath += relativePath;
/*------------------------------------------------------------------------------------------------------------------------
| Replace path slashes with backslashes if the resulting file path value uses a UNC or basic file path format
\-----------------------------------------------------------------------------------------------------------------------*/
if (filePath.Contains('\\', StringComparison.Ordinal)) {
filePath = filePath.Replace("/", "\\", StringComparison.Ordinal);
}
/*------------------------------------------------------------------------------------------------------------------------
| Return resulting file path
\-----------------------------------------------------------------------------------------------------------------------*/
return filePath;
}
} // Class
} // Namespace