From f7dd7fcf0d49f63370ba5fc470652b6b30090d89 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 9 Apr 2024 16:35:51 +0100 Subject: [PATCH 1/3] DEV: Update in preparation for core header changes --- .../discourse-algolia.gjs} | 92 ++++++++----------- 1 file changed, 36 insertions(+), 56 deletions(-) rename assets/javascripts/discourse/{initializers/discourse-algolia.js => api-initializers/discourse-algolia.gjs} (83%) diff --git a/assets/javascripts/discourse/initializers/discourse-algolia.js b/assets/javascripts/discourse/api-initializers/discourse-algolia.gjs similarity index 83% rename from assets/javascripts/discourse/initializers/discourse-algolia.js rename to assets/javascripts/discourse/api-initializers/discourse-algolia.gjs index a234cae..48007cd 100644 --- a/assets/javascripts/discourse/initializers/discourse-algolia.js +++ b/assets/javascripts/discourse/api-initializers/discourse-algolia.gjs @@ -1,7 +1,7 @@ +import didInsert from "@ember/render-modifiers/modifiers/did-insert"; import { Promise } from "rsvp"; -import { h } from "virtual-dom"; +import { apiInitializer } from "discourse/lib/api"; import loadScript from "discourse/lib/load-script"; -import { withPluginApi } from "discourse/lib/plugin-api"; import DiscourseURL from "discourse/lib/url"; import { isDevelopment } from "discourse-common/config/environment"; import I18n from "I18n"; @@ -171,9 +171,9 @@ function initializeAutocomplete(options) {
@@ -281,59 +281,39 @@ function initializeAutocomplete(options) { return autocompleteSearch; } -export default { - name: "discourse-algolia", +export default apiInitializer("0.8", (api) => { + const siteSettings = api.container.lookup("service:site-settings"); + const currentUser = api.getCurrentUser(); + const shouldDisplay = + siteSettings.algolia_enabled && + siteSettings.algolia_autocomplete_enabled && + (!siteSettings.login_required || currentUser); - initialize() { - withPluginApi("0.8.8", (api) => { - api.createWidget("algolia", { - tagName: "li.algolia-holder", + let search; - didRenderWidget() { - if ( - this.siteSettings.algolia_enabled && - this.siteSettings.algolia_autocomplete_enabled - ) { - Promise.all([ - loadScript( - "/plugins/discourse-algolia/javascripts/autocomplete.js" - ), - loadScript( - "/plugins/discourse-algolia/javascripts/algoliasearch.js" - ), - ]).then(() => { - document.body.classList.add("algolia-enabled"); - this._search = initializeAutocomplete({ - algoliaApplicationId: this.siteSettings.algolia_application_id, - algoliaSearchApiKey: this.siteSettings.algolia_search_api_key, - imageBaseURL: "", - debug: isDevelopment(), - }); - }); - } - }, - - willRerenderWidget() { - if (this._search) { - this._search.destroy(); - } - }, + function renderAlgolia() { + search?.destroy(); - html() { - return [h(".algolia-search"), h(".algolia-autocomplete")]; - }, - }); - - api.decorateWidget("header-icons:before", function (helper) { - if ( - helper.widget.siteSettings.algolia_enabled && - helper.widget.siteSettings.algolia_autocomplete_enabled && - (!helper.widget.siteSettings.login_required || - helper.widget.currentUser) - ) { - return helper.attach("algolia"); - } + Promise.all([ + loadScript("/plugins/discourse-algolia/javascripts/autocomplete.js"), + loadScript("/plugins/discourse-algolia/javascripts/algoliasearch.js"), + ]).then(() => { + document.body.classList.add("algolia-enabled"); + search = initializeAutocomplete({ + algoliaApplicationId: siteSettings.algolia_application_id, + algoliaSearchApiKey: siteSettings.algolia_search_api_key, + imageBaseURL: "", + debug: isDevelopment(), }); }); - }, -}; + } + + api.headerIcons.add("algolia", ); +}); From 0f2d77b79d6bae4c862e49e730df62df0b52372c Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 9 Apr 2024 16:55:11 +0100 Subject: [PATCH 2/3] Helper for shouldDisplay So it autotracks setting changes --- .../discourse/api-initializers/discourse-algolia.gjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assets/javascripts/discourse/api-initializers/discourse-algolia.gjs b/assets/javascripts/discourse/api-initializers/discourse-algolia.gjs index 48007cd..048aa1e 100644 --- a/assets/javascripts/discourse/api-initializers/discourse-algolia.gjs +++ b/assets/javascripts/discourse/api-initializers/discourse-algolia.gjs @@ -284,7 +284,7 @@ function initializeAutocomplete(options) { export default apiInitializer("0.8", (api) => { const siteSettings = api.container.lookup("service:site-settings"); const currentUser = api.getCurrentUser(); - const shouldDisplay = + const shouldDisplay = () => siteSettings.algolia_enabled && siteSettings.algolia_autocomplete_enabled && (!siteSettings.login_required || currentUser); @@ -309,7 +309,7 @@ export default apiInitializer("0.8", (api) => { } api.headerIcons.add("algolia",