2020-05-13 16:23:41 -04:00
|
|
|
import { action, computed } from "@ember/object";
|
2019-10-23 12:30:52 -04:00
|
|
|
import Component from "@ember/component";
|
2020-09-18 13:50:26 -04:00
|
|
|
import I18n from "I18n";
|
2017-11-21 11:53:09 +01:00
|
|
|
import UtilsMixin from "select-kit/mixins/utils";
|
2020-02-05 10:14:42 -06:00
|
|
|
import { guidFor } from "@ember/object/internals";
|
2020-08-28 15:30:20 -04:00
|
|
|
import layout from "select-kit/templates/components/select-kit/select-kit-row";
|
2020-02-03 14:22:14 +01:00
|
|
|
import { makeArray } from "discourse-common/lib/helpers";
|
2021-01-18 15:34:18 +01:00
|
|
|
import { reads } from "@ember/object/computed";
|
2022-05-13 10:56:23 -05:00
|
|
|
import { dasherize } from "@ember/string";
|
2017-11-21 11:53:09 +01:00
|
|
|
|
2019-10-23 12:30:52 -04:00
|
|
|
export default Component.extend(UtilsMixin, {
|
2020-08-28 15:30:20 -04:00
|
|
|
layout,
|
2018-02-05 18:25:39 +01:00
|
|
|
classNames: ["select-kit-row"],
|
2017-11-21 11:53:09 +01:00
|
|
|
tagName: "li",
|
2021-08-23 10:44:19 +02:00
|
|
|
tabIndex: 0,
|
2017-11-21 11:53:09 +01:00
|
|
|
attributeBindings: [
|
|
|
|
"tabIndex",
|
|
|
|
"title",
|
2020-02-03 14:22:14 +01:00
|
|
|
"rowValue:data-value",
|
|
|
|
"rowName:data-name",
|
2021-08-23 10:44:19 +02:00
|
|
|
"role",
|
|
|
|
"ariaChecked:aria-checked",
|
2018-03-22 11:29:55 +01:00
|
|
|
"guid:data-guid",
|
2021-01-18 15:34:18 +01:00
|
|
|
"rowLang:lang",
|
2017-11-21 11:53:09 +01:00
|
|
|
],
|
2019-02-07 14:43:33 +01:00
|
|
|
classNameBindings: [
|
|
|
|
"isHighlighted",
|
|
|
|
"isSelected",
|
2020-02-03 14:22:14 +01:00
|
|
|
"isNone",
|
|
|
|
"isNone:none",
|
|
|
|
"item.classNames",
|
2019-02-07 14:43:33 +01:00
|
|
|
],
|
2017-11-21 11:53:09 +01:00
|
|
|
|
2021-08-23 10:44:19 +02:00
|
|
|
role: "menuitemradio",
|
|
|
|
|
2020-09-18 13:50:26 -04:00
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
2021-08-23 10:44:19 +02:00
|
|
|
|
2021-08-23 19:57:42 +02:00
|
|
|
if (!this?.site?.mobileView) {
|
2021-08-23 10:44:19 +02:00
|
|
|
this.element.addEventListener("mouseenter", this.handleMouseEnter);
|
|
|
|
this.element.addEventListener("focus", this.handleMouseEnter);
|
|
|
|
this.element.addEventListener("blur", this.handleBlur);
|
|
|
|
}
|
2020-09-18 13:50:26 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement() {
|
|
|
|
this._super(...arguments);
|
2021-08-23 19:57:42 +02:00
|
|
|
if (!this?.site?.mobileView && this.element) {
|
2021-08-23 10:44:19 +02:00
|
|
|
this.element.removeEventListener("mouseenter", this.handleBlur);
|
|
|
|
this.element.removeEventListener("focus", this.handleMouseEnter);
|
|
|
|
this.element.removeEventListener("blur", this.handleMouseEnter);
|
2020-09-18 13:50:26 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-02-03 14:22:14 +01:00
|
|
|
isNone: computed("rowValue", function () {
|
|
|
|
return this.rowValue === this.getValue(this.selectKit.noneItem);
|
|
|
|
}),
|
|
|
|
|
|
|
|
guid: computed("item", function () {
|
2020-02-05 10:14:42 -06:00
|
|
|
return guidFor(this.item);
|
2020-02-03 14:22:14 +01:00
|
|
|
}),
|
|
|
|
|
2021-01-18 15:34:18 +01:00
|
|
|
lang: reads("item.lang"),
|
|
|
|
|
2021-08-23 10:44:19 +02:00
|
|
|
ariaChecked: computed("isSelected", function () {
|
2021-01-20 15:50:53 +01:00
|
|
|
return this.isSelected ? "true" : "false";
|
|
|
|
}),
|
|
|
|
|
2020-05-28 08:30:31 +02:00
|
|
|
title: computed("rowTitle", "item.title", "rowName", function () {
|
|
|
|
return (
|
|
|
|
this.rowTitle || this.getProperty(this.item, "title") || this.rowName
|
|
|
|
);
|
2020-02-03 14:22:14 +01:00
|
|
|
}),
|
|
|
|
|
2021-07-21 13:49:21 +02:00
|
|
|
dasherizedTitle: computed("title", function () {
|
2022-05-13 10:56:23 -05:00
|
|
|
return dasherize((this.title || "").replace(".", "-"));
|
2021-07-21 13:49:21 +02:00
|
|
|
}),
|
|
|
|
|
2020-05-28 08:30:31 +02:00
|
|
|
label: computed("rowLabel", "item.label", "title", "rowName", function () {
|
2020-02-03 14:22:14 +01:00
|
|
|
const label =
|
2020-05-28 08:30:31 +02:00
|
|
|
this.rowLabel ||
|
|
|
|
this.getProperty(this.item, "label") ||
|
|
|
|
this.title ||
|
|
|
|
this.rowName;
|
2020-02-03 14:22:14 +01:00
|
|
|
if (
|
|
|
|
this.selectKit.options.allowAny &&
|
|
|
|
this.rowValue === this.selectKit.filter &&
|
2020-02-07 14:12:17 +01:00
|
|
|
this.getName(this.selectKit.noneItem) !== this.rowName &&
|
|
|
|
this.getName(this.selectKit.newItem) === this.rowName
|
2020-02-03 14:22:14 +01:00
|
|
|
) {
|
|
|
|
return I18n.t("select_kit.create", { content: label });
|
|
|
|
}
|
|
|
|
return label;
|
|
|
|
}),
|
2017-11-21 11:53:09 +01:00
|
|
|
|
2020-02-03 14:22:14 +01:00
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
2017-11-21 11:53:09 +01:00
|
|
|
|
2019-05-10 17:12:10 +02:00
|
|
|
this.setProperties({
|
2020-02-03 14:22:14 +01:00
|
|
|
rowName: this.getName(this.item),
|
2020-05-28 08:30:31 +02:00
|
|
|
rowValue: this.getValue(this.item),
|
|
|
|
rowLabel: this.getProperty(this.item, "labelProperty"),
|
|
|
|
rowTitle: this.getProperty(this.item, "titleProperty"),
|
2021-01-18 15:34:18 +01:00
|
|
|
rowLang: this.getProperty(this.item, "langProperty"),
|
2019-05-10 17:12:10 +02:00
|
|
|
});
|
2017-11-21 11:53:09 +01:00
|
|
|
},
|
|
|
|
|
2020-02-03 14:22:14 +01:00
|
|
|
icons: computed("item.{icon,icons}", function () {
|
|
|
|
const icon = makeArray(this.getProperty(this.item, "icon"));
|
|
|
|
const icons = makeArray(this.getProperty(this.item, "icons"));
|
|
|
|
return icon.concat(icons).filter(Boolean);
|
|
|
|
}),
|
2017-11-21 11:53:09 +01:00
|
|
|
|
2020-02-03 14:22:14 +01:00
|
|
|
highlightedValue: computed("selectKit.highlighted", function () {
|
|
|
|
return this.getValue(this.selectKit.highlighted);
|
|
|
|
}),
|
|
|
|
|
2021-08-23 19:57:42 +02:00
|
|
|
isHighlighted: computed("rowValue", "highlightedValue", function () {
|
|
|
|
return this.rowValue === this.highlightedValue;
|
|
|
|
}),
|
2020-02-03 14:22:14 +01:00
|
|
|
|
2021-08-23 19:57:42 +02:00
|
|
|
isSelected: computed("rowValue", "value", function () {
|
|
|
|
return this.rowValue === this.value;
|
|
|
|
}),
|
2017-11-21 11:53:09 +01:00
|
|
|
|
2020-09-18 13:50:26 -04:00
|
|
|
@action
|
|
|
|
handleMouseEnter() {
|
2020-02-03 14:22:14 +01:00
|
|
|
if (!this.isDestroying || !this.isDestroyed) {
|
2021-08-23 10:44:19 +02:00
|
|
|
this.element.focus({ preventScroll: true });
|
2020-02-03 14:22:14 +01:00
|
|
|
this.selectKit.onHover(this.rowValue, this.item);
|
|
|
|
}
|
|
|
|
return false;
|
2017-11-21 11:53:09 +01:00
|
|
|
},
|
|
|
|
|
2021-08-23 10:44:19 +02:00
|
|
|
@action
|
|
|
|
handleBlur(event) {
|
|
|
|
if (
|
|
|
|
(!this.isDestroying || !this.isDestroyed) &&
|
|
|
|
event.relatedTarget &&
|
|
|
|
this.selectKit.mainElement()
|
|
|
|
) {
|
|
|
|
if (!this.selectKit.mainElement().contains(event.relatedTarget)) {
|
2021-09-03 18:29:16 +02:00
|
|
|
this.selectKit.close(event);
|
2021-08-23 10:44:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
click(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2020-02-03 14:22:14 +01:00
|
|
|
this.selectKit.select(this.rowValue, this.item);
|
|
|
|
return false;
|
2017-11-21 11:53:09 +01:00
|
|
|
},
|
2021-04-16 11:10:02 -04:00
|
|
|
|
|
|
|
mouseDown(event) {
|
|
|
|
if (this.selectKit.options.preventHeaderFocus) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
2021-08-23 10:44:19 +02:00
|
|
|
|
2021-09-02 18:30:52 +02:00
|
|
|
focusIn(event) {
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
},
|
|
|
|
|
2021-08-23 10:44:19 +02:00
|
|
|
keyDown(event) {
|
|
|
|
if (this.selectKit.isExpanded) {
|
|
|
|
if (event.key === "Backspace") {
|
|
|
|
if (this.selectKit.isFilterExpanded) {
|
|
|
|
this.selectKit.set("filter", this.selectKit.filter.slice(0, -1));
|
|
|
|
this.selectKit.triggerSearch();
|
|
|
|
this.selectKit.focusFilter();
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (event.key === "ArrowUp") {
|
|
|
|
this.selectKit.highlightPrevious();
|
|
|
|
return false;
|
|
|
|
} else if (event.key === "ArrowDown") {
|
|
|
|
this.selectKit.highlightNext();
|
|
|
|
return false;
|
|
|
|
} else if (event.key === "Enter") {
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
|
|
|
|
this.selectKit.select(
|
|
|
|
this.getValue(this.selectKit.highlighted),
|
|
|
|
this.selectKit.highlighted
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
} else if (event.key === "Escape") {
|
2021-09-03 18:29:16 +02:00
|
|
|
this.selectKit.close(event);
|
2021-08-23 10:44:19 +02:00
|
|
|
this.selectKit.headerElement().focus();
|
|
|
|
} else {
|
|
|
|
if (this.isValidInput(event.key)) {
|
|
|
|
this.selectKit.set("filter", event.key);
|
|
|
|
this.selectKit.triggerSearch();
|
|
|
|
this.selectKit.focusFilter();
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2017-11-21 11:53:09 +01:00
|
|
|
});
|