A platform for community discussion. Free, open, simple.
DEV: refactor select-kit utils mixin to property-utils lib (#32665)
Kelv6cda3c728a63
diff --git a/app/assets/javascripts/select-kit/addon/components/multi-select/format-selected-content.gjs b/app/assets/javascripts/select-kit/addon/components/multi-select/format-selected-content.gjsindex efb95c1d4b1..8137c4193b5 100644--- a/app/assets/javascripts/select-kit/addon/components/multi-select/format-selected-content.gjs+++ b/app/assets/javascripts/select-kit/addon/components/multi-select/format-selected-content.gjs@@ -2,12 +2,11 @@ import Component from "@ember/component";import { computed } from "@ember/object";import { tagName } from "@ember-decorators/component";import { makeArray } from "discourse/lib/helpers";-import UtilsMixin from "select-kit/mixins/utils";+import selectKitPropUtils from "select-kit/lib/select-kit-prop-utils";@tagName("")-export default class FormatSelectedContent extends Component.extend(- UtilsMixin-) {+@selectKitPropUtils+export default class FormatSelectedContent extends Component {content = null;selectKit = null;diff --git a/app/assets/javascripts/select-kit/addon/components/select-kit.js b/app/assets/javascripts/select-kit/addon/components/select-kit.jsindex c096889232e..5d71bf39cbd 100644--- a/app/assets/javascripts/select-kit/addon/components/select-kit.js+++ b/app/assets/javascripts/select-kit/addon/components/select-kit.js@@ -23,7 +23,7 @@ import {applyContentPluginApiCallbacks,applyOnChangePluginApiCallbacks,} from "select-kit/lib/plugin-api";-import UtilsMixin from "select-kit/mixins/utils";+import selectKitPropUtils from "select-kit/lib/select-kit-prop-utils";export const MAIN_COLLECTION = "MAIN_COLLECTION";export const ERRORS_COLLECTION = "ERRORS_COLLECTION";@@ -135,7 +135,8 @@ function protoProp(prototype, key, descriptor) {formName: null,})@pluginApiIdentifiers(["select-kit"])-export default class SelectKit extends Component.extend(UtilsMixin) {+@selectKitPropUtils+export default class SelectKit extends Component {@service appEvents;singleSelect = false;diff --git a/app/assets/javascripts/select-kit/addon/components/select-kit/select-kit-filter.gjs b/app/assets/javascripts/select-kit/addon/components/select-kit/select-kit-filter.gjsindex 76bee3df1d7..f28ccf3a7af 100644--- a/app/assets/javascripts/select-kit/addon/components/select-kit/select-kit-filter.gjs+++ b/app/assets/javascripts/select-kit/addon/components/select-kit/select-kit-filter.gjs@@ -11,12 +11,13 @@ import {import icon from "discourse/helpers/d-icon";import discourseComputed from "discourse/lib/decorators";import { i18n } from "discourse-i18n";-import UtilsMixin from "select-kit/mixins/utils";+import selectKitPropUtils from "select-kit/lib/select-kit-prop-utils";@classNames("select-kit-filter")@classNameBindings("isExpanded:is-expanded")@attributeBindings("role")-export default class SelectKitFilter extends Component.extend(UtilsMixin) {+@selectKitPropUtils+export default class SelectKitFilter extends Component {tabIndex = -1;@not("isHidden") isExpanded;diff --git a/app/assets/javascripts/select-kit/addon/components/select-kit/select-kit-header.js b/app/assets/javascripts/select-kit/addon/components/select-kit/select-kit-header.jsindex 64cefc5f31b..7d6aa69aaf1 100644--- a/app/assets/javascripts/select-kit/addon/components/select-kit/select-kit-header.js+++ b/app/assets/javascripts/select-kit/addon/components/select-kit/select-kit-header.js@@ -7,7 +7,7 @@ import {} from "@ember-decorators/component";import { makeArray } from "discourse/lib/helpers";import { isValidInput } from "select-kit/lib/input-utils";-import UtilsMixin from "select-kit/mixins/utils";+import selectKitPropUtils from "select-kit/lib/select-kit-prop-utils";@classNames("select-kit-header")@classNameBindings("isFocused")@@ -19,7 +19,8 @@ import UtilsMixin from "select-kit/mixins/utils";"buttonTitle:title","selectKit.options.autofocus:autofocus")-export default class SelectKitHeader extends Component.extend(UtilsMixin) {+@selectKitPropUtils+export default class SelectKitHeader extends Component {selectKit = null;role = "listbox";tabindex = 0;diff --git a/app/assets/javascripts/select-kit/addon/components/select-kit/select-kit-row.gjs b/app/assets/javascripts/select-kit/addon/components/select-kit/select-kit-row.gjsindex 17b77fc8439..f1e3d5bd10b 100644--- a/app/assets/javascripts/select-kit/addon/components/select-kit/select-kit-row.gjs+++ b/app/assets/javascripts/select-kit/addon/components/select-kit/select-kit-row.gjs@@ -13,7 +13,7 @@ import icon from "discourse/helpers/d-icon";import { makeArray } from "discourse/lib/helpers";import { i18n } from "discourse-i18n";import { isValidInput } from "select-kit/lib/input-utils";-import UtilsMixin from "select-kit/mixins/utils";+import selectKitPropUtils from "select-kit/lib/select-kit-prop-utils";@classNames("select-kit-row")@tagName("li")@@ -35,7 +35,8 @@ import UtilsMixin from "select-kit/mixins/utils";"isNone:none","item.classNames")-export default class SelectKitRow extends Component.extend(UtilsMixin) {+@selectKitPropUtils+export default class SelectKitRow extends Component {tabIndex = 0;index = 0;role = "menuitemradio";diff --git a/app/assets/javascripts/select-kit/addon/components/selected-choice.gjs b/app/assets/javascripts/select-kit/addon/components/selected-choice.gjsindex 2bd760f73f3..e6679a51c35 100644--- a/app/assets/javascripts/select-kit/addon/components/selected-choice.gjs+++ b/app/assets/javascripts/select-kit/addon/components/selected-choice.gjs@@ -6,10 +6,11 @@ import { guidFor } from "@ember/object/internals";import { tagName } from "@ember-decorators/component";import icon from "discourse/helpers/d-icon";import { i18n } from "discourse-i18n";-import UtilsMixin from "select-kit/mixins/utils";+import selectKitPropUtils from "select-kit/lib/select-kit-prop-utils";@tagName("")-export default class SelectedChoice extends Component.extend(UtilsMixin) {+@selectKitPropUtils+export default class SelectedChoice extends Component {item = null;selectKit = null;extraClass = null;diff --git a/app/assets/javascripts/select-kit/addon/components/selected-name.gjs b/app/assets/javascripts/select-kit/addon/components/selected-name.gjsindex c7b7978a861..5d8c779fdb0 100644--- a/app/assets/javascripts/select-kit/addon/components/selected-name.gjs+++ b/app/assets/javascripts/select-kit/addon/components/selected-name.gjs@@ -8,10 +8,11 @@ import { and } from "truth-helpers";import DButton from "discourse/components/d-button";import icon from "discourse/helpers/d-icon";import { makeArray } from "discourse/lib/helpers";-import UtilsMixin from "select-kit/mixins/utils";+import selectKitPropUtils from "select-kit/lib/select-kit-prop-utils";@tagName("")-export default class SelectedName extends Component.extend(UtilsMixin) {+@selectKitPropUtils+export default class SelectedName extends Component {name = null;value = null;headerTitle = null;diff --git a/app/assets/javascripts/select-kit/addon/lib/select-kit-prop-utils.js b/app/assets/javascripts/select-kit/addon/lib/select-kit-prop-utils.jsnew file mode 100644index 00000000000..e1627f876d1--- /dev/null+++ b/app/assets/javascripts/select-kit/addon/lib/select-kit-prop-utils.js@@ -0,0 +1,84 @@+import { get } from "@ember/object";++export default function selectKitPropUtils(target) {+ target.prototype.defaultItem = function (value, name) {+ const { valueProperty, nameProperty } = this.selectKit;++ if (valueProperty) {+ return {+ [valueProperty]: value,+ [nameProperty]: name,+ };+ }+ return name || value;+ };++ target.prototype.itemForValue = function (value, content) {+ const { valueProperty } = this.selectKit;+ if (valueProperty) {+ return content.findBy(valueProperty, value);+ }+ return value;+ };++ target.prototype.getProperty = function (+ item,+ property,+ options = { definedOnly: true }+ ) {+ if (!item) {+ return null;+ }+ if (item && typeof property === "string") {+ const attempt = get(item, property);+ if (attempt) {+ return attempt;+ }+ }+ property = get(this.selectKit, property);+ if (!property) {+ return options.definedOnly ? null : item;+ }+ if (typeof property === "string") {+ return get(item, property);+ }++ return property(item);+ };++ target.prototype.getValue = function (item) {+ return this.getProperty(item, "valueProperty", { definedOnly: false });+ };++ target.prototype.getName = function (item) {+ return this.getProperty(item, "nameProperty", { definedOnly: false });+ };++ target.prototype.findValue = function (content, item) {+ return this._findInContent(content, item, "valueProperty", (i) =>+ this.getValue(i)+ );+ };++ target.prototype.findName = function (content, item) {+ return this._findInContent(content, item, "nameProperty", (i) =>+ this.getName(i)+ );+ };++ target.prototype._findInContent = function (content, item, type, getterFunc) {+ const property = get(this.selectKit, type);+ if (!property) {+ return content.includes(item) ? item : undefined;+ }++ if (typeof property === "string") {+ return content.findBy(property, getterFunc(item));+ }++ const name = getterFunc(item);+ return content.find((contentItem) => {+ return getterFunc(contentItem) === name;+ });+ };+}diff --git a/app/assets/javascripts/select-kit/addon/mixins/utils.js b/app/assets/javascripts/select-kit/addon/mixins/utils.jsdeleted file mode 100644index 9006f72d11c..00000000000--- a/app/assets/javascripts/select-kit/addon/mixins/utils.js+++ /dev/null@@ -1,83 +0,0 @@-import { get } from "@ember/object";-import Mixin from "@ember/object/mixin";--export default Mixin.create({- defaultItem(value, name) {- if (this.selectKit.valueProperty) {- const item = {};- item[this.selectKit.valueProperty] = value;- item[this.selectKit.nameProperty] = name;- return item;- } else {- return name || value;- }- },-- itemForValue(value, content) {- if (this.selectKit.valueProperty) {- return content.findBy(this.selectKit.valueProperty, value);- } else {- return value;- }- },-- getProperty(item, property, options = { definedOnly: true }) {- const { definedOnly } = options;-- if (item && typeof property === "string") {- const attempt = get(item, property);- if (attempt) {- return attempt;- }- }-- property = get(this.selectKit, property);-- if (!item) {- return null;- }-- if (!property && definedOnly) {- return null;- } else if (!property) {- return item;- } else if (typeof property === "string") {- return get(item, property);- } else {- return property(item);- }- },-- getValue(item) {- return this.getProperty(item, "valueProperty", { definedOnly: false });- },-- getName(item) {- return this.getProperty(item, "nameProperty", { definedOnly: false });- },-- findValue(content, item) {- return this._findInContent(content, item, "valueProperty", "getValue");- },-- findName(content, item) {- return this._findInContent(content, item, "nameProperty", "getName");- },-- _findInContent(content, item, type, getter) {- const property = get(this.selectKit, type);-- if (!property) {- if (content.includes(item)) {- return item;- }- } else if (typeof property === "string") {- return content.findBy(property, this[getter](item));- } else {- const name = this[getter](item);- return content.find((contentItem) => {- return this[getter](contentItem) === name;- });- }- },-});