1212using OnTopic . Editor . Models ;
1313using OnTopic . Editor . Models . Metadata ;
1414using OnTopic . Editor . Models . Queryable ;
15+ using OnTopic . Querying ;
1516using OnTopic . Repositories ;
1617
1718namespace 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