From 30067d92aed01615feaa6e4da7ec3c286402c218 Mon Sep 17 00:00:00 2001 From: Kenn Jacobsen Date: Fri, 31 May 2019 20:29:00 +0200 Subject: [PATCH] V8: Always show content and media search results first (#5309) --- src/Umbraco.Web/Editors/EntityController.cs | 8 +++----- .../Search/SearchableApplicationTree.cs | 8 +++++++- src/Umbraco.Web/Search/SearchableTreeAttribute.cs | 15 ++++++++++++++- .../Search/SearchableTreeCollection.cs | 10 +++++++++- src/Umbraco.Web/Trees/ContentTreeController.cs | 2 +- src/Umbraco.Web/Trees/MediaTreeController.cs | 2 +- 6 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/Umbraco.Web/Editors/EntityController.cs b/src/Umbraco.Web/Editors/EntityController.cs index 082469f690..3ef8a92cdc 100644 --- a/src/Umbraco.Web/Editors/EntityController.cs +++ b/src/Umbraco.Web/Editors/EntityController.cs @@ -153,22 +153,20 @@ namespace Umbraco.Web.Editors var allowedSections = Security.CurrentUser.AllowedSections.ToArray(); - foreach (var searchableTree in _searchableTreeCollection.SearchableApplicationTrees) + foreach (var searchableTree in _searchableTreeCollection.SearchableApplicationTrees.OrderBy(t => t.Value.SortOrder)) { if (allowedSections.Contains(searchableTree.Value.AppAlias)) { var tree = _treeService.GetByAlias(searchableTree.Key); if (tree == null) continue; //shouldn't occur - var searchableTreeAttribute = searchableTree.Value.SearchableTree.GetType().GetCustomAttribute(false); - result[Tree.GetRootNodeDisplayName(tree, Services.TextService)] = new TreeSearchResult { Results = searchableTree.Value.SearchableTree.Search(query, 200, 0, out var total), TreeAlias = searchableTree.Key, AppAlias = searchableTree.Value.AppAlias, - JsFormatterService = searchableTreeAttribute == null ? "" : searchableTreeAttribute.ServiceName, - JsFormatterMethod = searchableTreeAttribute == null ? "" : searchableTreeAttribute.MethodName + JsFormatterService = searchableTree.Value.FormatterService, + JsFormatterMethod = searchableTree.Value.FormatterMethod }; } } diff --git a/src/Umbraco.Web/Search/SearchableApplicationTree.cs b/src/Umbraco.Web/Search/SearchableApplicationTree.cs index 997d8ce8e8..346f106b84 100644 --- a/src/Umbraco.Web/Search/SearchableApplicationTree.cs +++ b/src/Umbraco.Web/Search/SearchableApplicationTree.cs @@ -4,15 +4,21 @@ namespace Umbraco.Web.Search { public class SearchableApplicationTree { - public SearchableApplicationTree(string appAlias, string treeAlias, ISearchableTree searchableTree) + public SearchableApplicationTree(string appAlias, string treeAlias, int sortOrder, string formatterService, string formatterMethod, ISearchableTree searchableTree) { AppAlias = appAlias; TreeAlias = treeAlias; + SortOrder = sortOrder; + FormatterService = formatterService; + FormatterMethod = formatterMethod; SearchableTree = searchableTree; } public string AppAlias { get; } public string TreeAlias { get; } + public int SortOrder { get; } + public string FormatterService { get; } + public string FormatterMethod { get; } public ISearchableTree SearchableTree { get; } } } diff --git a/src/Umbraco.Web/Search/SearchableTreeAttribute.cs b/src/Umbraco.Web/Search/SearchableTreeAttribute.cs index 6c44b8946d..a2311ae989 100644 --- a/src/Umbraco.Web/Search/SearchableTreeAttribute.cs +++ b/src/Umbraco.Web/Search/SearchableTreeAttribute.cs @@ -6,16 +6,27 @@ namespace Umbraco.Web.Search [AttributeUsage(AttributeTargets.Class)] public sealed class SearchableTreeAttribute : Attribute { + public const int DefaultSortOrder = 1000; + /// /// This constructor defines both the angular service and method name to use /// /// /// - public SearchableTreeAttribute(string serviceName, string methodName) + public SearchableTreeAttribute(string serviceName, string methodName) : this(serviceName, methodName, DefaultSortOrder) { } + + /// + /// This constructor defines both the angular service and method name to use and explicitly defines a sort order for the results + /// + /// + /// + /// + public SearchableTreeAttribute(string serviceName, string methodName, int sortOrder) { if (string.IsNullOrWhiteSpace(serviceName)) throw new ArgumentNullOrEmptyException(nameof(serviceName)); if (string.IsNullOrWhiteSpace(methodName)) throw new ArgumentNullOrEmptyException(nameof(methodName)); MethodName = methodName; + SortOrder = sortOrder; ServiceName = serviceName; } @@ -28,9 +39,11 @@ namespace Umbraco.Web.Search if (string.IsNullOrWhiteSpace(serviceName)) throw new ArgumentNullOrEmptyException(nameof(serviceName)); MethodName = ""; ServiceName = serviceName; + SortOrder = DefaultSortOrder; } public string MethodName { get; } public string ServiceName { get; } + public int SortOrder { get; } } } diff --git a/src/Umbraco.Web/Search/SearchableTreeCollection.cs b/src/Umbraco.Web/Search/SearchableTreeCollection.cs index 8f7c6ece0b..0e7b1cd268 100644 --- a/src/Umbraco.Web/Search/SearchableTreeCollection.cs +++ b/src/Umbraco.Web/Search/SearchableTreeCollection.cs @@ -32,7 +32,15 @@ namespace Umbraco.Web.Search var found = searchableTrees.FirstOrDefault(x => x.TreeAlias.InvariantEquals(appTree.TreeAlias)); if (found != null) { - dictionary[found.TreeAlias] = new SearchableApplicationTree(appTree.SectionAlias, appTree.TreeAlias, found); + var searchableTreeAttribute = found.GetType().GetCustomAttribute(false); + dictionary[found.TreeAlias] = new SearchableApplicationTree( + appTree.SectionAlias, + appTree.TreeAlias, + searchableTreeAttribute?.SortOrder ?? SearchableTreeAttribute.DefaultSortOrder, + searchableTreeAttribute?.ServiceName ?? string.Empty, + searchableTreeAttribute?.MethodName ?? string.Empty, + found + ); } } return dictionary; diff --git a/src/Umbraco.Web/Trees/ContentTreeController.cs b/src/Umbraco.Web/Trees/ContentTreeController.cs index 9f8bce8656..748c97c522 100644 --- a/src/Umbraco.Web/Trees/ContentTreeController.cs +++ b/src/Umbraco.Web/Trees/ContentTreeController.cs @@ -30,7 +30,7 @@ namespace Umbraco.Web.Trees [Tree(Constants.Applications.Content, Constants.Trees.Content)] [PluginController("UmbracoTrees")] [CoreTree] - [SearchableTree("searchResultFormatter", "configureContentResult")] + [SearchableTree("searchResultFormatter", "configureContentResult", 10)] public class ContentTreeController : ContentTreeControllerBase, ISearchableTree { private readonly UmbracoTreeSearcher _treeSearcher; diff --git a/src/Umbraco.Web/Trees/MediaTreeController.cs b/src/Umbraco.Web/Trees/MediaTreeController.cs index fc3d23f092..d050b51a91 100644 --- a/src/Umbraco.Web/Trees/MediaTreeController.cs +++ b/src/Umbraco.Web/Trees/MediaTreeController.cs @@ -30,7 +30,7 @@ namespace Umbraco.Web.Trees [Tree(Constants.Applications.Media, Constants.Trees.Media)] [PluginController("UmbracoTrees")] [CoreTree] - [SearchableTree("searchResultFormatter", "configureMediaResult")] + [SearchableTree("searchResultFormatter", "configureMediaResult", 20)] public class MediaTreeController : ContentTreeControllerBase, ISearchableTree { private readonly UmbracoTreeSearcher _treeSearcher;