Skip to content

A platform for community discussion. Free, open, simple.

DEV: replace select-kit tags mixin with tag-utils service (#32509)

Kelvd3fd65dcf102
diff --git a/app/assets/javascripts/select-kit/addon/components/mini-tag-chooser.js b/app/assets/javascripts/select-kit/addon/components/mini-tag-chooser.js
index 6058123c80c..d911c96916d 100644
--- a/app/assets/javascripts/select-kit/addon/components/mini-tag-chooser.js
+++ b/app/assets/javascripts/select-kit/addon/components/mini-tag-chooser.js
@@ -1,11 +1,13 @@
import { computed } from "@ember/object";
import { empty, or } from "@ember/object/computed";
+import { service } from "@ember/service";
import {
attributeBindings,
classNameBindings,
classNames,
} from "@ember-decorators/component";
import { setting } from "discourse/lib/computed";
+import { bind } from "discourse/lib/decorators";
import { makeArray } from "discourse/lib/helpers";
import { i18n } from "discourse-i18n";
import MultiSelectComponent from "select-kit/components/multi-select";
@@ -13,12 +15,12 @@ import {
pluginApiIdentifiers,
selectKitOptions,
} from "select-kit/components/select-kit";
-import TagsMixin from "select-kit/mixins/tags";
@attributeBindings("selectKit.options.categoryId:category-id")
@classNames("mini-tag-chooser")
@classNameBindings("noTags")
@selectKitOptions({
+ allowAny: "allowAnyTag",
fullWidthOnMobile: true,
filterable: true,
caretDownIcon: "caretIcon",
@@ -32,9 +34,9 @@ import TagsMixin from "select-kit/mixins/tags";
useHeaderFilter: false,
})
@pluginApiIdentifiers(["mini-tag-chooser"])
-export default class MiniTagChooser extends MultiSelectComponent.extend(
- TagsMixin
-) {
+export default class MiniTagChooser extends MultiSelectComponent {
+ @service tagUtils;
+
@empty("value") noTags;
@or("allowCreate", "site.can_create_tag") allowAnyTag;
@@ -81,6 +83,22 @@ export default class MiniTagChooser extends MultiSelectComponent.extend(
return values.map((x) => this.defaultItem(x, x));
}
+ validateCreate(filter, content) {
+ return this.tagUtils.validateCreate(
+ filter,
+ content,
+ this.selectKit.options.maximum,
+ (e) => this.addError(e),
+ this.termMatchesForbidden,
+ (value) => this.getValue(value),
+ this.value
+ );
+ }
+
+ createContentFromInput(input) {
+ return this.tagUtils.createContentFromInput(input);
+ }
+
search(filter) {
const maximum = this.selectKit.options.maximum;
if (maximum === 0) {
@@ -103,27 +121,32 @@ export default class MiniTagChooser extends MultiSelectComponent.extend(
data.filterForInput = true;
}
- return this.searchTags("/tags/filter/search", data, this._transformJson);
+ return this.tagUtils.searchTags(
+ "/tags/filter/search",
+ data,
+ this._transformJson
+ );
}
- _transformJson(context, json) {
- if (context.isDestroyed || context.isDestroying) {
+ @bind
+ _transformJson(json) {
+ if (this.isDestroyed || this.isDestroying) {
return [];
}
let results = json.results;
- context.setProperties({
+ this.setProperties({
termMatchesForbidden: json.forbidden ? true : false,
termMatchErrorMessage: json.forbidden_message,
});
- if (context.siteSettings.tags_sort_alphabetically) {
+ if (this.siteSettings.tags_sort_alphabetically) {
results = results.sort((a, b) => a.text.localeCompare(b.text));
}
if (json.required_tag_group) {
- context.set(
+ this.set(
"selectKit.options.translatedFilterPlaceholder",
i18n("tagging.choose_for_topic_required_group", {
count: json.required_tag_group.min_count,
@@ -131,9 +154,9 @@ export default class MiniTagChooser extends MultiSelectComponent.extend(
})
);
} else {
- context.set("selectKit.options.translatedFilterPlaceholder", null);
+ this.set("selectKit.options.translatedFilterPlaceholder", null);
}
- return results.filter((r) => !makeArray(context.tags).includes(r.id));
+ return results.filter((r) => !makeArray(this.tags).includes(r.id));
}
}
diff --git a/app/assets/javascripts/select-kit/addon/components/tag-chooser.js b/app/assets/javascripts/select-kit/addon/components/tag-chooser.js
index ba653d8e9d4..6e04c9a04f3 100644
--- a/app/assets/javascripts/select-kit/addon/components/tag-chooser.js
+++ b/app/assets/javascripts/select-kit/addon/components/tag-chooser.js
@@ -1,12 +1,13 @@
import { action, computed } from "@ember/object";
+import { service } from "@ember/service";
import { attributeBindings, classNames } from "@ember-decorators/component";
+import { bind } from "discourse/lib/decorators";
import { makeArray } from "discourse/lib/helpers";
import MultiSelectComponent from "select-kit/components/multi-select";
import {
pluginApiIdentifiers,
selectKitOptions,
} from "select-kit/components/select-kit";
-import TagsMixin from "select-kit/mixins/tags";
@classNames("tag-chooser")
@attributeBindings("categoryId")
@@ -18,7 +19,9 @@ import TagsMixin from "select-kit/mixins/tags";
maximum: "maximumTagCount",
})
@pluginApiIdentifiers("tag-chooser")
-export default class TagChooser extends MultiSelectComponent.extend(TagsMixin) {
+export default class TagChooser extends MultiSelectComponent {
+ @service tagUtils;
+
blockedTags = null;
excludeSynonyms = false;
excludeHasSynonyms = false;
@@ -81,6 +84,22 @@ export default class TagChooser extends MultiSelectComponent.extend(TagsMixin) {
}
}
+ validateCreate(filter, content) {
+ return this.tagUtils.validateCreate(
+ filter,
+ content,
+ this.selectKit.options.maximum,
+ (e) => this.addError(e),
+ this.termMatchesForbidden,
+ (value) => this.getValue(value),
+ this.value
+ );
+ }
+
+ createContentFromInput(input) {
+ return this.tagUtils.createContentFromInput(input);
+ }
+
search(query) {
const selectedTags = makeArray(this.tags).filter(Boolean);
@@ -107,28 +126,33 @@ export default class TagChooser extends MultiSelectComponent.extend(TagsMixin) {
data.excludeHasSynonyms = true;
}
- return this.searchTags("/tags/filter/search", data, this._transformJson);
+ return this.tagUtils.searchTags(
+ "/tags/filter/search",
+ data,
+ this._transformJson
+ );
}
- _transformJson(context, json) {
- if (context.isDestroyed || context.isDestroying) {
+ @bind
+ _transformJson(json) {
+ if (this.isDestroyed || this.isDestroying) {
return [];
}
let results = json.results;
- context.setProperties({
+ this.setProperties({
termMatchesForbidden: json.forbidden ? true : false,
termMatchErrorMessage: json.forbidden_message,
});
- if (context.blockedTags) {
+ if (this.blockedTags) {
results = results.filter((result) => {
- return !context.blockedTags.includes(result.id);
+ return !this.blockedTags.includes(result.id);
});
}
- if (context.siteSettings.tags_sort_alphabetically) {
+ if (this.siteSettings.tags_sort_alphabetically) {
results = results.sort((a, b) => a.id > b.id);
}
diff --git a/app/assets/javascripts/select-kit/addon/components/tag-drop.js b/app/assets/javascripts/select-kit/addon/components/tag-drop.js
index 7de7303b503..7cd6ce8f4f5 100644
--- a/app/assets/javascripts/select-kit/addon/components/tag-drop.js
+++ b/app/assets/javascripts/select-kit/addon/components/tag-drop.js
@@ -1,7 +1,9 @@
import { action, computed } from "@ember/object";
import { readOnly } from "@ember/object/computed";
+import { service } from "@ember/service";
import { classNameBindings, classNames } from "@ember-decorators/component";
import { setting } from "discourse/lib/computed";
+import { bind } from "discourse/lib/decorators";
import { makeArray } from "discourse/lib/helpers";
import DiscourseURL, { getCategoryAndTagUrl } from "discourse/lib/url";
import { i18n } from "discourse-i18n";
@@ -12,7 +14,6 @@ import {
pluginApiIdentifiers,
selectKitOptions,
} from "select-kit/components/select-kit";
-import TagsMixin from "select-kit/mixins/tags";
export const NO_TAG_ID = "no-tags";
export const ALL_TAGS_ID = "all-tags";
@@ -33,7 +34,9 @@ const MORE_TAGS_COLLECTION = "MORE_TAGS_COLLECTION";
autoInsertNoneItem: false,
})
@pluginApiIdentifiers("tag-drop")
-export default class TagDrop extends ComboBoxComponent.extend(TagsMixin) {
+export default class TagDrop extends ComboBoxComponent {
+ @service tagUtils;
+
@setting("max_tag_search_results") maxTagSearchResults;
@setting("tags_sort_alphabetically") sortTagsAlphabetically;
@setting("max_tags_in_filter_list") maxTagsInFilterList;
@@ -142,6 +145,22 @@ export default class TagDrop extends ComboBoxComponent.extend(TagsMixin) {
}
}
+ validateCreate(filter, content) {
+ return this.tagUtils.validateCreate(
+ filter,
+ content,
+ this.selectKit.options.maximum,
+ (e) => this.addError(e),
+ this.termMatchesForbidden,
+ (value) => this.getValue(value),
+ this.value
+ );
+ }
+
+ createContentFromInput(input) {
+ return this.tagUtils.createContentFromInput(input);
+ }
+
search(filter) {
if (filter) {
const data = {
@@ -149,7 +168,11 @@ export default class TagDrop extends ComboBoxComponent.extend(TagsMixin) {
limit: this.maxTagSearchResults,
};
- return this.searchTags("/tags/filter/search", data, this._transformJson);
+ return this.tagUtils.searchTags(
+ "/tags/filter/search",
+ data,
+ this._transformJson
+ );
} else {
return (this.content || []).map((tag) => {
if (tag.id && tag.name) {
@@ -160,13 +183,18 @@ export default class TagDrop extends ComboBoxComponent.extend(TagsMixin) {
}
}
- _transformJson(context, json) {
+ @bind
+ _transformJson(json) {
+ if (this.isDestroyed || this.isDestroying) {
+ return [];
+ }
+
return json.results
.sort((a, b) => a.id > b.id)
.map((r) => {
- const content = context.defaultItem(r.id, r.text);
+ const content = this.defaultItem(r.id, r.text);
content.targetTagId = r.target_tag || r.id;
- if (!context.currentCategory) {
+ if (!this.currentCategory) {
content.count = r.count;
}
content.pmCount = r.pm_count;
diff --git a/app/assets/javascripts/select-kit/addon/components/tag-group-chooser.js b/app/assets/javascripts/select-kit/addon/components/tag-group-chooser.js
index 3262aaf3f86..60f2dac5115 100644
--- a/app/assets/javascripts/select-kit/addon/components/tag-group-chooser.js
+++ b/app/assets/javascripts/select-kit/addon/components/tag-group-chooser.js
@@ -1,12 +1,13 @@
import { computed } from "@ember/object";
+import { service } from "@ember/service";
import { classNames } from "@ember-decorators/component";
+import { bind } from "discourse/lib/decorators";
import { makeArray } from "discourse/lib/helpers";
import MultiSelectComponent from "select-kit/components/multi-select";
import {
pluginApiIdentifiers,
selectKitOptions,
} from "select-kit/components/select-kit";
-import TagsMixin from "select-kit/mixins/tags";
@classNames("tag-group-chooser", "tag-chooser")
@selectKitOptions({
@@ -16,9 +17,9 @@ import TagsMixin from "select-kit/mixins/tags";
limit: null,
})
@pluginApiIdentifiers("tag-group-chooser")
-export default class TagGroupChooser extends MultiSelectComponent.extend(
- TagsMixin
-) {
+export default class TagGroupChooser extends MultiSelectComponent {
+ @service tagUtils;
+
modifyComponentForRow() {
return "tag-chooser-row";
}
@@ -35,26 +36,44 @@ export default class TagGroupChooser extends MultiSelectComponent.extend(
.map((t) => this.defaultItem(t, t));
}
+ validateCreate(filter, content) {
+ return this.tagUtils.validateCreate(
+ filter,
+ content,
+ this.selectKit.options.maximum,
+ (e) => this.addError(e),
+ this.termMatchesForbidden,
+ (value) => this.getValue(value),
+ this.value
+ );
+ }
+
+ createContentFromInput(input) {
+ return this.tagUtils.createContentFromInput(input);
+ }
+
search(query) {
const data = {
q: query,
limit: this.siteSettings.max_tag_search_results,
};
- return this.searchTags(
- "/tag_groups/filter/search",
- data,
- this._transformJson
- ).then((results) => {
- if (results && results.length) {
- return results.filter((r) => {
- return !makeArray(this.tagGroups).includes(this.getValue(r));
- });
- }
- });
+ return this.tagUtils
+ .searchTags("/tag_groups/filter/search", data, this._transformJson)
+ .then((results) => {
+ if (results && results.length) {
+ return results.filter((r) => {
+ return !makeArray(this.tagGroups).includes(this.getValue(r));
+ });
+ }
+ });
}
- _transformJson(context, json) {
+ @bind
+ _transformJson(json) {
+ if (this.isDestroyed || this.isDestroying) {
+ return [];
+ }
return json.results
.sort((a, b) => a.name > b.name)
.map((result) => {
diff --git a/app/assets/javascripts/select-kit/addon/mixins/tags.js b/app/assets/javascripts/select-kit/addon/services/tag-utils.js
similarity index 70%
rename from app/assets/javascripts/select-kit/addon/mixins/tags.js
rename to app/assets/javascripts/select-kit/addon/services/tag-utils.js
index c4adc0d172d..da6da806826 100644
--- a/app/assets/javascripts/select-kit/addon/mixins/tags.js
+++ b/app/assets/javascripts/select-kit/addon/services/tag-utils.js
@@ -1,32 +1,35 @@
-import { reads } from "@ember/object/computed";
-import Mixin from "@ember/object/mixin";
+import Service, { service } from "@ember/service";
import { isEmpty } from "@ember/utils";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { makeArray } from "discourse/lib/helpers";
import { i18n } from "discourse-i18n";
-export default Mixin.create({
+export default class TagUtils extends Service {
+ @service siteSettings;
+ @service site;
+
searchTags(url, data, callback) {
return ajax(url, { data })
- .then((json) => callback(this, json))
+ .then((json) => callback(json))
.catch(popupAjaxError);
- },
-
- selectKitOptions: {
- allowAny: "allowAnyTag",
- },
-
- allowAnyTag: reads("site.can_create_tag"),
+ }
- validateCreate(filter, content) {
+ validateCreate(
+ filter,
+ content,
+ maximum,
+ addError,
+ termMatchesForbidden,
+ getValue,
+ value
+ ) {
if (!filter.length) {
return;
}
- const maximum = this.selectKit.options.maximum;
- if (maximum && makeArray(this.value).length >= parseInt(maximum, 10)) {
- this.addError(
+ if (maximum && makeArray(value).length >= parseInt(maximum, 10)) {
+ addError(
i18n("select_kit.max_content_reached", {
count: parseInt(maximum, 10),
})
@@ -37,7 +40,7 @@ export default Mixin.create({
const filterRegexp = new RegExp(this.site.tags_filter_regexp, "g");
filter = filter.replace(filterRegexp, "").trim().toLowerCase();
- if (this.termMatchesForbidden) {
+ if (termMatchesForbidden) {
return false;
}
@@ -46,11 +49,11 @@ export default Mixin.create({
};
const inCollection = content
- .map((c) => toLowerCaseOrUndefined(this.getValue(c)))
+ .map((c) => toLowerCaseOrUndefined(getValue(c)))
.filter(Boolean)
.includes(filter);
- const inSelection = (this.value || [])
+ const inSelection = (value || [])
.map((s) => toLowerCaseOrUndefined(s))
.filter(Boolean)
.includes(filter);
@@ -60,7 +63,7 @@ export default Mixin.create({
}
return true;
- },
+ }
createContentFromInput(input) {
// See lib/discourse_tagging#clean_tag.
@@ -75,5 +78,5 @@ export default Mixin.create({
}
return input;
- },
-});
+ }
+}