2020-02-03 21:22:14 +08:00
|
|
|
import Component from "@ember/component";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { computed, get } from "@ember/object";
|
2021-01-18 22:34:18 +08:00
|
|
|
import { reads } from "@ember/object/computed";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { guidFor } from "@ember/object/internals";
|
2024-08-23 19:17:07 +08:00
|
|
|
import { tagName } from "@ember-decorators/component";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { makeArray } from "discourse-common/lib/helpers";
|
|
|
|
import UtilsMixin from "select-kit/mixins/utils";
|
2020-02-03 21:22:14 +08:00
|
|
|
|
2024-08-23 19:17:07 +08:00
|
|
|
@tagName("")
|
|
|
|
export default class SelectedName extends Component.extend(UtilsMixin) {
|
|
|
|
name = null;
|
|
|
|
value = null;
|
|
|
|
headerTitle = null;
|
|
|
|
headerLang = null;
|
|
|
|
headerLabel = null;
|
|
|
|
id = null;
|
|
|
|
|
|
|
|
@reads("headerLang") lang;
|
2020-02-03 21:22:14 +08:00
|
|
|
|
2021-08-23 16:44:19 +08:00
|
|
|
init() {
|
2024-08-23 19:17:07 +08:00
|
|
|
super.init(...arguments);
|
2021-08-23 16:44:19 +08:00
|
|
|
|
|
|
|
this.set("id", guidFor(this));
|
2024-08-23 19:17:07 +08:00
|
|
|
}
|
2020-02-03 21:22:14 +08:00
|
|
|
|
|
|
|
didReceiveAttrs() {
|
2024-08-23 19:17:07 +08:00
|
|
|
super.didReceiveAttrs(...arguments);
|
2020-02-03 21:22:14 +08:00
|
|
|
|
|
|
|
// we can't listen on `item.nameProperty` given it's variable
|
|
|
|
this.setProperties({
|
2020-05-28 14:30:31 +08:00
|
|
|
headerLabel: this.getProperty(this.item, "labelProperty"),
|
|
|
|
headerTitle: this.getProperty(this.item, "titleProperty"),
|
2021-01-18 22:34:18 +08:00
|
|
|
headerLang: this.getProperty(this.item, "langProperty"),
|
2020-02-03 21:22:14 +08:00
|
|
|
name: this.getName(this.item),
|
|
|
|
value:
|
|
|
|
this.item === this.selectKit.noneItem ? null : this.getValue(this.item),
|
|
|
|
});
|
2024-08-23 19:17:07 +08:00
|
|
|
}
|
2021-01-18 22:34:18 +08:00
|
|
|
|
2024-08-23 19:17:07 +08:00
|
|
|
@computed("item", "sanitizedTitle")
|
|
|
|
get ariaLabel() {
|
2020-02-03 21:22:14 +08:00
|
|
|
return this._safeProperty("ariaLabel", this.item) || this.sanitizedTitle;
|
2024-08-23 19:17:07 +08:00
|
|
|
}
|
2020-02-03 21:22:14 +08:00
|
|
|
|
|
|
|
// this might need a more advanced solution
|
|
|
|
// but atm it's the only case we have to handle
|
2024-08-23 19:17:07 +08:00
|
|
|
@computed("title")
|
|
|
|
get sanitizedTitle() {
|
2020-02-03 21:22:14 +08:00
|
|
|
return String(this.title).replace("…", "");
|
2024-08-23 19:17:07 +08:00
|
|
|
}
|
2020-02-03 21:22:14 +08:00
|
|
|
|
2024-08-23 19:17:07 +08:00
|
|
|
@computed("headerTitle", "item")
|
|
|
|
get title() {
|
2020-05-28 14:30:31 +08:00
|
|
|
return (
|
|
|
|
this.headerTitle ||
|
|
|
|
this._safeProperty("title", this.item) ||
|
|
|
|
this.name ||
|
|
|
|
""
|
|
|
|
);
|
2024-08-23 19:17:07 +08:00
|
|
|
}
|
2020-02-03 21:22:14 +08:00
|
|
|
|
2024-08-23 19:17:07 +08:00
|
|
|
@computed("headerLabel", "title", "name")
|
|
|
|
get label() {
|
2020-05-28 14:30:31 +08:00
|
|
|
return (
|
|
|
|
this.headerLabel ||
|
|
|
|
this._safeProperty("label", this.item) ||
|
|
|
|
this.title ||
|
|
|
|
this.name
|
|
|
|
);
|
2024-08-23 19:17:07 +08:00
|
|
|
}
|
2020-02-03 21:22:14 +08:00
|
|
|
|
2024-08-23 19:17:07 +08:00
|
|
|
@computed("item.{icon,icons}")
|
|
|
|
get icons() {
|
2020-02-03 21:22:14 +08:00
|
|
|
const icon = makeArray(this._safeProperty("icon", this.item));
|
|
|
|
const icons = makeArray(this._safeProperty("icons", this.item));
|
|
|
|
return icon.concat(icons).filter(Boolean);
|
2024-08-23 19:17:07 +08:00
|
|
|
}
|
2020-02-03 21:22:14 +08:00
|
|
|
|
|
|
|
_safeProperty(name, content) {
|
|
|
|
if (!content) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return get(content, name);
|
2024-08-23 19:17:07 +08:00
|
|
|
}
|
|
|
|
}
|