Skip to content

Commit 222b332

Browse files
committed
Merge branch 'bugfix/TopicListAttributeViewComponent-issues' into develop
Resolves a series of bugs and oversights regarding the implementation of the `TopicListAttributeViewComponent` to ensure all intended functionality is supported, and that it works without error. This includes: - Ensure `DefaultLabel` isn't persisted as the value if not topic is selected (1be3eb3) - Resolve runtime exception when using `RelativeTopicBase`'s `ContentTypeDescriptor` (339fa6d) - Gracefully fail if the `TopicList`'s scope cannot be resolved (9ecf880) - Ensure `RelativeTopicBase` works correctly when creating new topics (7f027c6) - Introduce missing support for the `RelativeTopicPath` attribute (b727cf1) - Hide the `TopicListViewComponent` if no values are returned (753d0a4)
2 parents 18f676b + 753d0a4 commit 222b332

2 files changed

Lines changed: 44 additions & 25 deletions

File tree

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
@model TopicListAttributeViewModel
22

33
@{
4+
if (Model.TopicList.Count > 1) {
45
Layout = "~/Areas/Editor/Views/Editor/Components/_Layout.cshtml";
6+
}
57
}
68

7-
<select
8-
asp-for ="Value"
9-
asp-items ="Model.TopicList"
10-
class ="@Model.AttributeDescriptor.CssClass form-control form-inline"
11-
disabled =@(!Model.AttributeDescriptor.IsEnabled)
12-
required =@Model.AttributeDescriptor.IsRequired
13-
>
14-
</select>
9+
@if (Model.TopicList.Count > 1) {
10+
<select
11+
asp-for ="Value"
12+
asp-items ="Model.TopicList"
13+
class ="@Model.AttributeDescriptor.CssClass form-control form-inline"
14+
disabled =@(!Model.AttributeDescriptor.IsEnabled)
15+
required =@Model.AttributeDescriptor.IsRequired
16+
>
17+
</select>
18+
}

OnTopic.Editor.AspNetCore/Components/TopicListViewComponent.cs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using OnTopic.Editor.Models;
1313
using OnTopic.Editor.Models.Metadata;
1414
using OnTopic.Editor.Models.Queryable;
15+
using OnTopic.Querying;
1516
using OnTopic.Repositories;
1617

1718
namespace OnTopic.Editor.AspNetCore.Components {
@@ -76,7 +77,7 @@ public IViewComponentResult Invoke(
7677
\-----------------------------------------------------------------------------------------------------------------------*/
7778
viewModel.TopicList.Add(
7879
new SelectListItem {
79-
Value = null,
80+
Value = "",
8081
Text = attribute.DefaultLabel
8182
}
8283
);
@@ -87,35 +88,42 @@ public IViewComponentResult Invoke(
8788
var defaultValue = currentTopic.Attributes.ContainsKey(attribute.Key)? currentTopic.Attributes[attribute.Key] : null;
8889

8990
/*------------------------------------------------------------------------------------------------------------------------
90-
| Get values
91+
| Get root topic
9192
\-----------------------------------------------------------------------------------------------------------------------*/
92-
var topics = (List<QueryResultTopicViewModel>)null;
93+
var rootTopic = (Topic)null;
9394

9495
if (attribute.RelativeTopicBase != null) {
9596
var baseTopic = _topicRepository.Load(currentTopic.UniqueKey);
96-
var rootTopic = attribute.RelativeTopicBase switch {
97+
if (String.IsNullOrEmpty(currentTopic.Key)) {
98+
baseTopic = TopicFactory.Create("NewTopic", currentTopic.ContentType, baseTopic);
99+
baseTopic.Parent.Children.Remove(baseTopic);
100+
}
101+
rootTopic = attribute.RelativeTopicBase switch {
97102
"CurrentTopic" => baseTopic,
98103
"ParentTopic" => baseTopic.Parent,
99104
"GrandparentTopic" => (Topic)baseTopic.Parent?.Parent,
100-
"ContentTypeDescriptor" => (Topic)_topicRepository.GetContentTypeDescriptors().Where(t => t.Key.Equals(baseTopic.ContentType)),
105+
"ContentTypeDescriptor" => (Topic)_topicRepository.GetContentTypeDescriptors().FirstOrDefault(t => t.Key.Equals(baseTopic.ContentType)),
101106
_ => baseTopic
102107
};
103-
topics = GetTopics(
104-
rootTopic,
105-
attribute.AttributeKey,
106-
attribute.AttributeValue,
107-
allowedKeys
108-
);
109108
}
110109
else {
111-
topics = GetTopics(
112-
_topicRepository.Load(attribute.RootTopic?.UniqueKey?? attribute.RootTopicKey),
113-
attribute.AttributeKey,
114-
attribute.AttributeValue,
115-
allowedKeys
116-
);
110+
rootTopic = _topicRepository.Load(attribute.RootTopic?.UniqueKey?? attribute.RootTopicKey);
111+
}
112+
113+
if (rootTopic != null && !String.IsNullOrEmpty(attribute.RelativeTopicPath)) {
114+
rootTopic = rootTopic.GetByUniqueKey(rootTopic.GetUniqueKey() + ":" + attribute.RelativeTopicPath);
117115
}
118116

117+
/*------------------------------------------------------------------------------------------------------------------------
118+
| Get values
119+
\-----------------------------------------------------------------------------------------------------------------------*/
120+
var topics = GetTopics(
121+
rootTopic,
122+
attribute.AttributeKey,
123+
attribute.AttributeValue,
124+
allowedKeys
125+
);
126+
119127
/*------------------------------------------------------------------------------------------------------------------------
120128
| Get values from repository
121129
\-----------------------------------------------------------------------------------------------------------------------*/
@@ -158,6 +166,13 @@ public static List<QueryResultTopicViewModel> GetTopics(
158166
string allowedKeys = ""
159167
) {
160168

169+
/*------------------------------------------------------------------------------------------------------------------------
170+
| Swallow missing topic
171+
\-----------------------------------------------------------------------------------------------------------------------*/
172+
if (topic == null) {
173+
return new List<QueryResultTopicViewModel>();
174+
}
175+
161176
/*------------------------------------------------------------------------------------------------------------------------
162177
| Establish query options
163178
\-----------------------------------------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)