DEV: Make `search` an actual service (#14998)

This commit is contained in:
Jarek Radosz 2021-11-18 16:52:03 +01:00 committed by GitHub
parent 42fff92d9f
commit 4021842628
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 31 deletions

View File

@ -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");

View File

@ -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");
});

View File

@ -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;
},
},
});

View File

@ -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) {

View File

@ -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) {

View File

@ -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;