mirror of
https://github.com/discourse/discourse.git
synced 2025-02-20 11:48:26 -06:00
DEV: Make search
an actual service (#14998)
This commit is contained in:
parent
42fff92d9f
commit
4021842628
@ -104,7 +104,7 @@ export default {
|
|||||||
this.container = container;
|
this.container = container;
|
||||||
this._stopCallback();
|
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.appEvents = this.container.lookup("service:app-events");
|
||||||
this.currentUser = this.container.lookup("current-user:main");
|
this.currentUser = this.container.lookup("current-user:main");
|
||||||
this.siteSettings = this.container.lookup("site-settings:main");
|
this.siteSettings = this.container.lookup("site-settings:main");
|
||||||
|
@ -5,7 +5,6 @@ import PrivateMessageTopicTrackingState from "discourse/models/private-message-t
|
|||||||
import DiscourseLocation from "discourse/lib/discourse-location";
|
import DiscourseLocation from "discourse/lib/discourse-location";
|
||||||
import KeyValueStore from "discourse/lib/key-value-store";
|
import KeyValueStore from "discourse/lib/key-value-store";
|
||||||
import MessageBus from "message-bus-client";
|
import MessageBus from "message-bus-client";
|
||||||
import SearchService from "discourse/services/search";
|
|
||||||
import Session from "discourse/models/session";
|
import Session from "discourse/models/session";
|
||||||
import Site from "discourse/models/site";
|
import Site from "discourse/models/site";
|
||||||
import User from "discourse/models/user";
|
import User from "discourse/models/user";
|
||||||
@ -79,14 +78,26 @@ export default {
|
|||||||
|
|
||||||
const keyValueStore = new KeyValueStore("discourse_");
|
const keyValueStore = new KeyValueStore("discourse_");
|
||||||
app.register("key-value-store:main", keyValueStore, { instantiate: false });
|
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) => {
|
ALL_TARGETS.forEach((t) => {
|
||||||
app.inject(t, "appEvents", "service:app-events");
|
app.inject(t, "appEvents", "service:app-events");
|
||||||
app.inject(t, "pmTopicTrackingState", "pm-topic-tracking-state:main");
|
app.inject(t, "pmTopicTrackingState", "pm-topic-tracking-state:main");
|
||||||
app.inject(t, "store", "service:store");
|
app.inject(t, "store", "service:store");
|
||||||
app.inject(t, "site", "site:main");
|
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");
|
app.inject(t, "keyValueStore", "key-value-store:main");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import EmberObject, { get } from "@ember/object";
|
import Service from "@ember/service";
|
||||||
import discourseComputed from "discourse-common/utils/decorators";
|
import discourseComputed from "discourse-common/utils/decorators";
|
||||||
|
|
||||||
export default EmberObject.extend({
|
export default Service.extend({
|
||||||
searchContextEnabled: false, // checkbox to scope search
|
searchContextEnabled: false, // checkbox to scope search
|
||||||
searchContext: null,
|
searchContext: null,
|
||||||
highlightTerm: null,
|
highlightTerm: null,
|
||||||
@ -9,16 +9,13 @@ export default EmberObject.extend({
|
|||||||
@discourseComputed("searchContext")
|
@discourseComputed("searchContext")
|
||||||
contextType: {
|
contextType: {
|
||||||
get(searchContext) {
|
get(searchContext) {
|
||||||
if (searchContext) {
|
return searchContext?.type;
|
||||||
return get(searchContext, "type");
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
set(value, searchContext) {
|
set(value, searchContext) {
|
||||||
// a bit hacky, consider cleaning this up, need to work through all observers though
|
this.set("searchContext", { ...searchContext, type: value });
|
||||||
const context = Object.assign({}, searchContext);
|
|
||||||
context.type = value;
|
return value;
|
||||||
this.set("searchContext", context);
|
|
||||||
return this.get("searchContext.type");
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -328,7 +328,7 @@ export function attachAdditionalPanel(name, toggle, transformAttrs) {
|
|||||||
export default createWidget("header", {
|
export default createWidget("header", {
|
||||||
tagName: "header.d-header.clearfix",
|
tagName: "header.d-header.clearfix",
|
||||||
buildKey: () => `header`,
|
buildKey: () => `header`,
|
||||||
services: ["router"],
|
services: ["router", "search"],
|
||||||
|
|
||||||
defaultState() {
|
defaultState() {
|
||||||
let states = {
|
let states = {
|
||||||
@ -408,8 +408,7 @@ export default createWidget("header", {
|
|||||||
|
|
||||||
updateHighlight() {
|
updateHighlight() {
|
||||||
if (!this.state.searchVisible) {
|
if (!this.state.searchVisible) {
|
||||||
const service = this.register.lookup("search-service:main");
|
this.search.set("highlightTerm", "");
|
||||||
service.set("highlightTerm", "");
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -447,8 +446,7 @@ export default createWidget("header", {
|
|||||||
|
|
||||||
toggleSearchMenu() {
|
toggleSearchMenu() {
|
||||||
if (this.site.mobileView) {
|
if (this.site.mobileView) {
|
||||||
const searchService = this.register.lookup("search-service:main");
|
const context = this.search.searchContext;
|
||||||
const context = searchService.get("searchContext");
|
|
||||||
let params = "";
|
let params = "";
|
||||||
|
|
||||||
if (context) {
|
if (context) {
|
||||||
|
@ -498,15 +498,14 @@ createWidget("search-menu-assistant", {
|
|||||||
|
|
||||||
createWidget("search-menu-initial-options", {
|
createWidget("search-menu-initial-options", {
|
||||||
tagName: "ul.search-menu-initial-options",
|
tagName: "ul.search-menu-initial-options",
|
||||||
|
services: ["search"],
|
||||||
|
|
||||||
html(attrs) {
|
html(attrs) {
|
||||||
if (attrs.term?.match(MODIFIER_REGEXP)) {
|
if (attrs.term?.match(MODIFIER_REGEXP)) {
|
||||||
return this.defaultRow(attrs.term);
|
return this.defaultRow(attrs.term);
|
||||||
}
|
}
|
||||||
|
|
||||||
const service = this.register.lookup("search-service:main");
|
const ctx = this.search.searchContext;
|
||||||
const ctx = service.get("searchContext");
|
|
||||||
|
|
||||||
const content = [];
|
const content = [];
|
||||||
|
|
||||||
if (attrs.term || ctx) {
|
if (attrs.term || ctx) {
|
||||||
|
@ -185,6 +185,7 @@ const SearchHelper = {
|
|||||||
|
|
||||||
export default createWidget("search-menu", {
|
export default createWidget("search-menu", {
|
||||||
tagName: "div.search-menu",
|
tagName: "div.search-menu",
|
||||||
|
services: ["search"],
|
||||||
searchData,
|
searchData,
|
||||||
|
|
||||||
buildKey: () => "search-menu",
|
buildKey: () => "search-menu",
|
||||||
@ -306,13 +307,6 @@ export default createWidget("search-menu", {
|
|||||||
this.triggerSearch();
|
this.triggerSearch();
|
||||||
},
|
},
|
||||||
|
|
||||||
searchService() {
|
|
||||||
if (!this._searchService) {
|
|
||||||
this._searchService = this.register.lookup("search-service:main");
|
|
||||||
}
|
|
||||||
return this._searchService;
|
|
||||||
},
|
|
||||||
|
|
||||||
html(attrs, state) {
|
html(attrs, state) {
|
||||||
if (attrs.inTopicContext === false) {
|
if (attrs.inTopicContext === false) {
|
||||||
state.inTopicContext = false;
|
state.inTopicContext = false;
|
||||||
@ -450,7 +444,7 @@ export default createWidget("search-menu", {
|
|||||||
searchData.noResults = false;
|
searchData.noResults = false;
|
||||||
if (SearchHelper.includesTopics()) {
|
if (SearchHelper.includesTopics()) {
|
||||||
if (this.state.inTopicContext) {
|
if (this.state.inTopicContext) {
|
||||||
this.searchService().set("highlightTerm", searchData.term);
|
this.search.set("highlightTerm", searchData.term);
|
||||||
}
|
}
|
||||||
|
|
||||||
searchData.loading = true;
|
searchData.loading = true;
|
||||||
@ -500,7 +494,7 @@ export default createWidget("search-menu", {
|
|||||||
|
|
||||||
searchContext() {
|
searchContext() {
|
||||||
if (this.state.inTopicContext) {
|
if (this.state.inTopicContext) {
|
||||||
return this.searchService().get("searchContext");
|
return this.search.searchContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
Reference in New Issue
Block a user