From 519528daa2cdeeec18bc507698ac3dbe6856f51c Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Wed, 21 Jul 2021 13:49:21 +0200 Subject: [PATCH] FIX: allows to use icon-picker in wizard (#13786) - inlines dasherize helper in sk - uses an ajax helper to load wizard's ajax lib when in wizard - amends wizard's ajax lib to work with string as first arg - disabled loading spinner in wizard as it's not available --- .../select-kit/addon/components/icon-picker.js | 2 +- .../addon/components/select-kit/select-kit-row.js | 4 ++++ .../javascripts/select-kit/addon/lib/ajax-helper.js | 8 ++++++++ .../javascripts/select-kit/addon/mixins/tags.js | 2 +- .../addon/templates/components/multi-select.hbs | 4 +++- .../components/select-kit/select-kit-row.hbs | 2 +- app/assets/javascripts/wizard/lib/ajax.js | 11 ++++++++++- 7 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 app/assets/javascripts/select-kit/addon/lib/ajax-helper.js diff --git a/app/assets/javascripts/select-kit/addon/components/icon-picker.js b/app/assets/javascripts/select-kit/addon/components/icon-picker.js index 69b7767b1d8..6d506ab64dc 100644 --- a/app/assets/javascripts/select-kit/addon/components/icon-picker.js +++ b/app/assets/javascripts/select-kit/addon/components/icon-picker.js @@ -4,10 +4,10 @@ import { enableMissingIconWarning, } from "discourse-common/lib/icon-library"; import MultiSelectComponent from "select-kit/components/multi-select"; -import { ajax } from "discourse/lib/ajax"; import { computed } from "@ember/object"; import { isDevelopment } from "discourse-common/config/environment"; import { makeArray } from "discourse-common/lib/helpers"; +import { ajax } from "select-kit/lib/ajax-helper"; export default MultiSelectComponent.extend({ pluginApiIdentifiers: ["icon-picker"], diff --git a/app/assets/javascripts/select-kit/addon/components/select-kit/select-kit-row.js b/app/assets/javascripts/select-kit/addon/components/select-kit/select-kit-row.js index 26f8b810bb5..9e8b734e558 100644 --- a/app/assets/javascripts/select-kit/addon/components/select-kit/select-kit-row.js +++ b/app/assets/javascripts/select-kit/addon/components/select-kit/select-kit-row.js @@ -69,6 +69,10 @@ export default Component.extend(UtilsMixin, { ); }), + dasherizedTitle: computed("title", function () { + return (this.title || "").replace(".", "-").dasherize(); + }), + label: computed("rowLabel", "item.label", "title", "rowName", function () { const label = this.rowLabel || diff --git a/app/assets/javascripts/select-kit/addon/lib/ajax-helper.js b/app/assets/javascripts/select-kit/addon/lib/ajax-helper.js new file mode 100644 index 00000000000..4cd5d97ece6 --- /dev/null +++ b/app/assets/javascripts/select-kit/addon/lib/ajax-helper.js @@ -0,0 +1,8 @@ +let ajax; +if (window.Discourse) { + ajax = requirejs("discourse/lib/ajax").ajax; +} else { + ajax = requirejs("wizard/lib/ajax").ajax; +} + +export { ajax }; diff --git a/app/assets/javascripts/select-kit/addon/mixins/tags.js b/app/assets/javascripts/select-kit/addon/mixins/tags.js index 400e921dfc7..fa3dfabd7e7 100644 --- a/app/assets/javascripts/select-kit/addon/mixins/tags.js +++ b/app/assets/javascripts/select-kit/addon/mixins/tags.js @@ -1,6 +1,6 @@ import I18n from "I18n"; import Mixin from "@ember/object/mixin"; -import { ajax } from "discourse/lib/ajax"; +import { ajax } from "select-kit/lib/ajax-helper"; import getURL from "discourse-common/lib/get-url"; import { isEmpty } from "@ember/utils"; import { makeArray } from "discourse-common/lib/helpers"; diff --git a/app/assets/javascripts/select-kit/addon/templates/components/multi-select.hbs b/app/assets/javascripts/select-kit/addon/templates/components/multi-select.hbs index 8672c07becf..8f68a91b5ed 100644 --- a/app/assets/javascripts/select-kit/addon/templates/components/multi-select.hbs +++ b/app/assets/javascripts/select-kit/addon/templates/components/multi-select.hbs @@ -10,7 +10,9 @@ {{#select-kit/select-kit-body selectKit=selectKit id=(concat selectKit.uniqueID "-body")}} {{#if selectKit.isLoading}} - {{loading-spinner size="small"}} + {{#if site}} + {{loading-spinner size="small"}} + {{/if}} {{else}} {{#if selectKit.filter}} diff --git a/app/assets/javascripts/select-kit/addon/templates/components/select-kit/select-kit-row.hbs b/app/assets/javascripts/select-kit/addon/templates/components/select-kit/select-kit-row.hbs index b27647f8b4b..e3e3dcbe1cf 100644 --- a/app/assets/javascripts/select-kit/addon/templates/components/select-kit/select-kit-row.hbs +++ b/app/assets/javascripts/select-kit/addon/templates/components/select-kit/select-kit-row.hbs @@ -1,5 +1,5 @@ {{#each icons as |icon|}} - {{d-icon icon translatedtitle=(dasherize title)}} + {{d-icon icon translatedtitle=dasherizedTitle}} {{/each}} diff --git a/app/assets/javascripts/wizard/lib/ajax.js b/app/assets/javascripts/wizard/lib/ajax.js index ec0dd1c5cd2..670747e8079 100644 --- a/app/assets/javascripts/wizard/lib/ajax.js +++ b/app/assets/javascripts/wizard/lib/ajax.js @@ -14,11 +14,20 @@ export function getToken() { } export function ajax(args) { + let url; + + if (arguments.length === 2) { + url = arguments[0]; + args = arguments[1]; + } else { + url = args.url; + } + return new Promise((resolve, reject) => { args.headers = { "X-CSRF-Token": getToken() }; args.success = (data) => run(null, resolve, data); args.error = (xhr) => run(null, reject, xhr); - args.url = getUrl(args.url); + args.url = getUrl(url); jQuery.ajax(args); }); }