Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Experimental API for custom full-page search types. (#20915)

Roman Rizziea4f7fb660cb
diff --git a/app/assets/javascripts/discourse/app/controllers/full-page-search.js b/app/assets/javascripts/discourse/app/controllers/full-page-search.js
index 59157ac2646..e27ce685fb8 100644
--- a/app/assets/javascripts/discourse/app/controllers/full-page-search.js
+++ b/app/assets/javascripts/discourse/app/controllers/full-page-search.js
@@ -37,6 +37,16 @@ export const SEARCH_TYPE_USERS = "users";
const PAGE_LIMIT = 10;
+const customSearchTypes = [];
+
+export function registerFullPageSearchType(
+ translationKey,
+ searchTypeId,
+ searchFunc
+) {
+ customSearchTypes.push({ translationKey, searchTypeId, searchFunc });
+}
+
export default Controller.extend({
application: controller(),
composer: controller(),
@@ -68,7 +78,7 @@ export default Controller.extend({
init() {
this._super(...arguments);
- this.set("searchTypes", [
+ const searchTypes = [
{ name: I18n.t("search.type.default"), id: SEARCH_TYPE_DEFAULT },
{
name: this.siteSettings.tagging_enabled
@@ -77,7 +87,16 @@ export default Controller.extend({
id: SEARCH_TYPE_CATS_TAGS,
},
{ name: I18n.t("search.type.users"), id: SEARCH_TYPE_USERS },
- ]);
+ ];
+
+ customSearchTypes.forEach((type) => {
+ searchTypes.push({
+ name: I18n.t(type.translationKey),
+ id: type.searchTypeId,
+ });
+ });
+
+ this.set("searchTypes", searchTypes);
},
@discourseComputed("resultCount")
@@ -274,6 +293,13 @@ export default Controller.extend({
return searchType === SEARCH_TYPE_DEFAULT;
},
+ @discourseComputed("search_type")
+ customSearchType(searchType) {
+ return customSearchTypes.find(
+ (type) => searchType === type["searchTypeId"]
+ );
+ },
+
@discourseComputed("bulkSelectEnabled")
searchInfoClassNames(bulkSelectEnabled) {
return bulkSelectEnabled
@@ -324,6 +350,12 @@ export default Controller.extend({
const searchKey = getSearchKey(args);
+ if (this.customSearchType) {
+ const customSearch = this.customSearchType["searchFunc"];
+ customSearch(this, args, searchKey);
+ return;
+ }
+
switch (this.search_type) {
case SEARCH_TYPE_CATS_TAGS:
const categoryTagSearch = searchCategoryTag(
diff --git a/app/assets/javascripts/discourse/app/lib/plugin-api.js b/app/assets/javascripts/discourse/app/lib/plugin-api.js
index 95f4da95eee..0d456f64d48 100644
--- a/app/assets/javascripts/discourse/app/lib/plugin-api.js
+++ b/app/assets/javascripts/discourse/app/lib/plugin-api.js
@@ -111,6 +111,7 @@ import { registerNotificationTypeRenderer } from "discourse/lib/notification-typ
import { registerUserMenuTab } from "discourse/lib/user-menu/tab";
import { registerModelTransformer } from "discourse/lib/model-transformers";
import { registerCustomUserNavMessagesDropdownRow } from "discourse/controllers/user-private-messages";
+import { registerFullPageSearchType } from "discourse/controllers/full-page-search";
// If you add any methods to the API ensure you bump up the version number
// based on Semantic Versioning 2.0.0. Please update the changelog at
@@ -2124,6 +2125,18 @@ class PluginApi {
addUserMessagesNavigationDropdownRow(routeName, name, icon) {
registerCustomUserNavMessagesDropdownRow(routeName, name, icon);
}
+
+ /**
+ * EXPERIMENTAL. Do not use.
+ * Adds a new search type which can be selected when visiting the full page search UI.
+ *
+ * @param {string} translationKey
+ * @param {string} searchTypeId
+ * @param {function} searchFunc - Available arguments: fullPage controller, search args, searchKey.
+ */
+ addFullPageSearchType(translationKey, searchTypeId, searchFunc) {
+ registerFullPageSearchType(translationKey, searchTypeId, searchFunc);
+ }
}
// from http://stackoverflow.com/questions/6832596/how-to-compare-software-version-number-using-js-only-number
diff --git a/app/assets/javascripts/discourse/app/templates/full-page-search.hbs b/app/assets/javascripts/discourse/app/templates/full-page-search.hbs
index 47e85ab567f..139017a085f 100644
--- a/app/assets/javascripts/discourse/app/templates/full-page-search.hbs
+++ b/app/assets/javascripts/discourse/app/templates/full-page-search.hbs
@@ -150,7 +150,7 @@
{{else}}
<div class="search-results" role="region">
<LoadMore @selector=".fps-result" @action={{action "loadMore"}}>
- {{#if this.usingDefaultSearchType}}
+ {{#if (or this.usingDefaultSearchType this.customSearchType)}}
<SearchResultEntries
@posts={{this.model.posts}}
@bulkSelectEnabled={{this.bulkSelectEnabled}}