From 40218426289932d18da84f717d1e7226371763ec Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Thu, 18 Nov 2021 16:52:03 +0100 Subject: [PATCH] DEV: Make `search` an actual service (#14998) --- .../discourse/app/lib/keyboard-shortcuts.js | 2 +- .../inject-discourse-objects.js | 17 ++++++++++++++--- .../discourse/app/services/search.js | 17 +++++++---------- .../javascripts/discourse/app/widgets/header.js | 8 +++----- .../app/widgets/search-menu-results.js | 5 ++--- .../discourse/app/widgets/search-menu.js | 12 +++--------- 6 files changed, 30 insertions(+), 31 deletions(-) diff --git a/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js b/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js index b97787fed57..93efb024165 100644 --- a/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js +++ b/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js @@ -104,7 +104,7 @@ export default { this.container = container; this._stopCallback(); - this.searchService = this.container.lookup("search-service:main"); + this.searchService = this.container.lookup("service:search"); this.appEvents = this.container.lookup("service:app-events"); this.currentUser = this.container.lookup("current-user:main"); this.siteSettings = this.container.lookup("site-settings:main"); diff --git a/app/assets/javascripts/discourse/app/pre-initializers/inject-discourse-objects.js b/app/assets/javascripts/discourse/app/pre-initializers/inject-discourse-objects.js index e60955dd3f9..2a92f321fb7 100644 --- a/app/assets/javascripts/discourse/app/pre-initializers/inject-discourse-objects.js +++ b/app/assets/javascripts/discourse/app/pre-initializers/inject-discourse-objects.js @@ -5,7 +5,6 @@ import PrivateMessageTopicTrackingState from "discourse/models/private-message-t import DiscourseLocation from "discourse/lib/discourse-location"; import KeyValueStore from "discourse/lib/key-value-store"; import MessageBus from "message-bus-client"; -import SearchService from "discourse/services/search"; import Session from "discourse/models/session"; import Site from "discourse/models/site"; import User from "discourse/models/user"; @@ -79,14 +78,26 @@ export default { const keyValueStore = new KeyValueStore("discourse_"); app.register("key-value-store:main", keyValueStore, { instantiate: false }); - app.register("search-service:main", SearchService); + + app.register("search-service:main", { + create() { + deprecated( + `"search-service:main" is deprecated, use "service:search" instead`, + { + since: "2.8.0.beta8", + } + ); + + return container.lookup("service:search"); + }, + }); ALL_TARGETS.forEach((t) => { app.inject(t, "appEvents", "service:app-events"); app.inject(t, "pmTopicTrackingState", "pm-topic-tracking-state:main"); app.inject(t, "store", "service:store"); app.inject(t, "site", "site:main"); - app.inject(t, "searchService", "search-service:main"); + app.inject(t, "searchService", "service:search"); app.inject(t, "keyValueStore", "key-value-store:main"); }); diff --git a/app/assets/javascripts/discourse/app/services/search.js b/app/assets/javascripts/discourse/app/services/search.js index a702e9980b2..2fe2dacaecf 100644 --- a/app/assets/javascripts/discourse/app/services/search.js +++ b/app/assets/javascripts/discourse/app/services/search.js @@ -1,7 +1,7 @@ -import EmberObject, { get } from "@ember/object"; +import Service from "@ember/service"; import discourseComputed from "discourse-common/utils/decorators"; -export default EmberObject.extend({ +export default Service.extend({ searchContextEnabled: false, // checkbox to scope search searchContext: null, highlightTerm: null, @@ -9,16 +9,13 @@ export default EmberObject.extend({ @discourseComputed("searchContext") contextType: { get(searchContext) { - if (searchContext) { - return get(searchContext, "type"); - } + return searchContext?.type; }, + set(value, searchContext) { - // a bit hacky, consider cleaning this up, need to work through all observers though - const context = Object.assign({}, searchContext); - context.type = value; - this.set("searchContext", context); - return this.get("searchContext.type"); + this.set("searchContext", { ...searchContext, type: value }); + + return value; }, }, }); diff --git a/app/assets/javascripts/discourse/app/widgets/header.js b/app/assets/javascripts/discourse/app/widgets/header.js index 9cfd4091f67..377e76304db 100644 --- a/app/assets/javascripts/discourse/app/widgets/header.js +++ b/app/assets/javascripts/discourse/app/widgets/header.js @@ -328,7 +328,7 @@ export function attachAdditionalPanel(name, toggle, transformAttrs) { export default createWidget("header", { tagName: "header.d-header.clearfix", buildKey: () => `header`, - services: ["router"], + services: ["router", "search"], defaultState() { let states = { @@ -408,8 +408,7 @@ export default createWidget("header", { updateHighlight() { if (!this.state.searchVisible) { - const service = this.register.lookup("search-service:main"); - service.set("highlightTerm", ""); + this.search.set("highlightTerm", ""); } }, @@ -447,8 +446,7 @@ export default createWidget("header", { toggleSearchMenu() { if (this.site.mobileView) { - const searchService = this.register.lookup("search-service:main"); - const context = searchService.get("searchContext"); + const context = this.search.searchContext; let params = ""; if (context) { diff --git a/app/assets/javascripts/discourse/app/widgets/search-menu-results.js b/app/assets/javascripts/discourse/app/widgets/search-menu-results.js index f3f0b7af323..d607ad3de51 100644 --- a/app/assets/javascripts/discourse/app/widgets/search-menu-results.js +++ b/app/assets/javascripts/discourse/app/widgets/search-menu-results.js @@ -498,15 +498,14 @@ createWidget("search-menu-assistant", { createWidget("search-menu-initial-options", { tagName: "ul.search-menu-initial-options", + services: ["search"], html(attrs) { if (attrs.term?.match(MODIFIER_REGEXP)) { return this.defaultRow(attrs.term); } - const service = this.register.lookup("search-service:main"); - const ctx = service.get("searchContext"); - + const ctx = this.search.searchContext; const content = []; if (attrs.term || ctx) { diff --git a/app/assets/javascripts/discourse/app/widgets/search-menu.js b/app/assets/javascripts/discourse/app/widgets/search-menu.js index e5366f5be63..4b52448038a 100644 --- a/app/assets/javascripts/discourse/app/widgets/search-menu.js +++ b/app/assets/javascripts/discourse/app/widgets/search-menu.js @@ -185,6 +185,7 @@ const SearchHelper = { export default createWidget("search-menu", { tagName: "div.search-menu", + services: ["search"], searchData, buildKey: () => "search-menu", @@ -306,13 +307,6 @@ export default createWidget("search-menu", { this.triggerSearch(); }, - searchService() { - if (!this._searchService) { - this._searchService = this.register.lookup("search-service:main"); - } - return this._searchService; - }, - html(attrs, state) { if (attrs.inTopicContext === false) { state.inTopicContext = false; @@ -450,7 +444,7 @@ export default createWidget("search-menu", { searchData.noResults = false; if (SearchHelper.includesTopics()) { if (this.state.inTopicContext) { - this.searchService().set("highlightTerm", searchData.term); + this.search.set("highlightTerm", searchData.term); } searchData.loading = true; @@ -500,7 +494,7 @@ export default createWidget("search-menu", { searchContext() { if (this.state.inTopicContext) { - return this.searchService().get("searchContext"); + return this.search.searchContext; } return false;