2020-05-23 12:56:13 +08:00
|
|
|
import EmberObject, { computed, get } from "@ember/object";
|
|
|
|
import PluginApiMixin, {
|
2019-10-24 00:30:52 +08:00
|
|
|
applyContentPluginApiCallbacks,
|
2020-02-03 21:22:14 +08:00
|
|
|
applyOnChangePluginApiCallbacks,
|
2017-11-21 18:53:09 +08:00
|
|
|
} from "select-kit/mixins/plugin-api";
|
2020-12-18 21:18:52 +08:00
|
|
|
import { bind, cancel, next, schedule, throttle } from "@ember/runloop";
|
2020-02-19 06:41:15 +08:00
|
|
|
import { isEmpty, isNone, isPresent } from "@ember/utils";
|
2019-10-24 00:30:52 +08:00
|
|
|
import Component from "@ember/component";
|
2020-05-14 04:23:41 +08:00
|
|
|
import I18n from "I18n";
|
2020-02-06 00:14:42 +08:00
|
|
|
import Mixin from "@ember/object/mixin";
|
2020-02-03 21:22:14 +08:00
|
|
|
import { Promise } from "rsvp";
|
2017-11-22 17:34:12 +08:00
|
|
|
import UtilsMixin from "select-kit/mixins/utils";
|
2020-09-15 00:33:14 +08:00
|
|
|
import { createPopper } from "@popperjs/core";
|
2020-02-03 21:22:14 +08:00
|
|
|
import deprecated from "discourse-common/lib/deprecated";
|
2020-12-18 21:18:52 +08:00
|
|
|
import discourseDebounce from "discourse-common/lib/debounce";
|
2020-05-23 12:56:13 +08:00
|
|
|
import { guidFor } from "@ember/object/internals";
|
2017-11-22 17:34:12 +08:00
|
|
|
import { makeArray } from "discourse-common/lib/helpers";
|
2017-11-21 18:53:09 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
export const MAIN_COLLECTION = "MAIN_COLLECTION";
|
|
|
|
export const ERRORS_COLLECTION = "ERRORS_COLLECTION";
|
|
|
|
|
|
|
|
const EMPTY_OBJECT = Object.freeze({});
|
2020-02-06 00:14:42 +08:00
|
|
|
const SELECT_KIT_OPTIONS = Mixin.create({
|
2020-02-03 21:22:14 +08:00
|
|
|
mergedProperties: ["selectKitOptions"],
|
|
|
|
selectKitOptions: EMPTY_OBJECT,
|
|
|
|
});
|
|
|
|
|
2020-10-22 22:08:11 +08:00
|
|
|
function isDocumentRTL() {
|
|
|
|
return (
|
|
|
|
window.getComputedStyle(document.querySelector("html")).direction === "rtl"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-24 00:30:52 +08:00
|
|
|
export default Component.extend(
|
2020-02-03 21:22:14 +08:00
|
|
|
SELECT_KIT_OPTIONS,
|
2017-11-21 18:53:09 +08:00
|
|
|
PluginApiMixin,
|
2020-02-03 21:22:14 +08:00
|
|
|
UtilsMixin,
|
2017-11-21 18:53:09 +08:00
|
|
|
{
|
|
|
|
pluginApiIdentifiers: ["select-kit"],
|
2018-02-06 01:25:39 +08:00
|
|
|
classNames: ["select-kit"],
|
2017-11-21 18:53:09 +08:00
|
|
|
classNameBindings: [
|
2020-02-03 21:22:14 +08:00
|
|
|
"selectKit.isLoading:is-loading",
|
|
|
|
"selectKit.isExpanded:is-expanded",
|
2020-10-13 17:31:07 +08:00
|
|
|
"selectKit.options.disabled:is-disabled",
|
2020-02-03 21:22:14 +08:00
|
|
|
"selectKit.isHidden:is-hidden",
|
2020-02-05 23:01:58 +08:00
|
|
|
"selectKit.hasSelection:has-selection",
|
2017-11-21 18:53:09 +08:00
|
|
|
],
|
2018-06-15 23:03:24 +08:00
|
|
|
tabindex: 0,
|
2017-11-21 18:53:09 +08:00
|
|
|
content: null,
|
2020-02-03 21:22:14 +08:00
|
|
|
value: null,
|
|
|
|
selectKit: null,
|
|
|
|
mainCollection: null,
|
|
|
|
errorsCollection: null,
|
|
|
|
options: null,
|
|
|
|
valueProperty: "id",
|
|
|
|
nameProperty: "name",
|
2020-02-11 22:54:56 +08:00
|
|
|
singleSelect: false,
|
|
|
|
multiSelect: false,
|
2020-05-28 14:30:31 +08:00
|
|
|
labelProperty: null,
|
|
|
|
titleProperty: null,
|
2021-01-18 22:34:18 +08:00
|
|
|
langProperty: null,
|
2018-06-15 23:03:24 +08:00
|
|
|
|
|
|
|
init() {
|
2019-01-10 18:06:01 +08:00
|
|
|
this._super(...arguments);
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
this._searchPromise = null;
|
|
|
|
|
|
|
|
this.set("errorsCollection", []);
|
|
|
|
this._collections = [ERRORS_COLLECTION, MAIN_COLLECTION];
|
|
|
|
|
|
|
|
!this.options && this.set("options", EmberObject.create({}));
|
|
|
|
|
|
|
|
this.handleDeprecations();
|
|
|
|
|
2018-09-12 18:19:04 +08:00
|
|
|
this.set(
|
2020-02-03 21:22:14 +08:00
|
|
|
"selectKit",
|
2019-10-30 03:23:50 +08:00
|
|
|
EmberObject.create({
|
2020-03-07 06:49:28 +08:00
|
|
|
uniqueID: guidFor(this),
|
2020-02-03 21:22:14 +08:00
|
|
|
valueProperty: this.valueProperty,
|
|
|
|
nameProperty: this.nameProperty,
|
2020-05-28 14:30:31 +08:00
|
|
|
labelProperty: this.labelProperty,
|
|
|
|
titleProperty: this.titleProperty,
|
2021-01-18 22:34:18 +08:00
|
|
|
langProperty: this.langProperty,
|
2020-02-03 21:22:14 +08:00
|
|
|
options: EmberObject.create(),
|
|
|
|
|
|
|
|
isLoading: false,
|
|
|
|
isHidden: false,
|
|
|
|
isExpanded: false,
|
|
|
|
isFilterExpanded: false,
|
2021-02-01 18:07:11 +08:00
|
|
|
enterDisabled: false,
|
2020-02-03 21:22:14 +08:00
|
|
|
hasSelection: false,
|
|
|
|
hasNoContent: true,
|
|
|
|
highlighted: null,
|
|
|
|
noneItem: null,
|
2020-02-07 21:12:17 +08:00
|
|
|
newItem: null,
|
2020-02-03 21:22:14 +08:00
|
|
|
filter: null,
|
|
|
|
|
|
|
|
modifyContent: bind(this, this._modifyContentWrapper),
|
|
|
|
modifySelection: bind(this, this._modifySelectionWrapper),
|
|
|
|
modifyComponentForRow: bind(this, this._modifyComponentForRowWrapper),
|
|
|
|
modifyContentForCollection: bind(
|
|
|
|
this,
|
|
|
|
this._modifyContentForCollectionWrapper
|
|
|
|
),
|
|
|
|
modifyComponentForCollection: bind(
|
|
|
|
this,
|
|
|
|
this._modifyComponentForCollectionWrapper
|
|
|
|
),
|
|
|
|
|
|
|
|
toggle: bind(this, this._toggle),
|
|
|
|
close: bind(this, this._close),
|
|
|
|
open: bind(this, this._open),
|
|
|
|
highlightNext: bind(this, this._highlightNext),
|
|
|
|
highlightPrevious: bind(this, this._highlightPrevious),
|
|
|
|
change: bind(this, this._onChangeWrapper),
|
|
|
|
select: bind(this, this.select),
|
|
|
|
deselect: bind(this, this.deselect),
|
2021-02-22 21:34:31 +08:00
|
|
|
deselectByValue: bind(this, this.deselectByValue),
|
2021-01-06 19:57:13 +08:00
|
|
|
append: bind(this, this.append),
|
2020-02-03 21:22:14 +08:00
|
|
|
|
|
|
|
onOpen: bind(this, this._onOpenWrapper),
|
|
|
|
onClose: bind(this, this._onCloseWrapper),
|
|
|
|
onInput: bind(this, this._onInput),
|
|
|
|
onClearSelection: bind(this, this._onClearSelection),
|
|
|
|
onHover: bind(this, this._onHover),
|
|
|
|
onKeydown: bind(this, this._onKeydownWrapper),
|
2018-09-12 18:19:04 +08:00
|
|
|
})
|
|
|
|
);
|
2020-02-03 21:22:14 +08:00
|
|
|
},
|
|
|
|
|
2020-04-21 22:20:05 +08:00
|
|
|
click(event) {
|
2020-04-21 23:11:40 +08:00
|
|
|
if (this.selectKit.options.preventsClickPropagation) {
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
2020-04-21 22:20:05 +08:00
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_modifyComponentForRowWrapper(collection, item) {
|
|
|
|
let component = this.modifyComponentForRow(collection, item);
|
|
|
|
return component || "select-kit/select-kit-row";
|
|
|
|
},
|
|
|
|
|
|
|
|
modifyComponentForRow() {},
|
|
|
|
|
|
|
|
_modifyContentForCollectionWrapper(identifier) {
|
|
|
|
let collection = this.modifyContentForCollection(identifier);
|
|
|
|
|
|
|
|
if (!collection) {
|
|
|
|
switch (identifier) {
|
|
|
|
case ERRORS_COLLECTION:
|
|
|
|
collection = this.errorsCollection;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
collection = this.mainCollection;
|
|
|
|
break;
|
|
|
|
}
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
return collection;
|
|
|
|
},
|
|
|
|
|
|
|
|
modifyContentForCollection() {},
|
|
|
|
|
|
|
|
_modifyComponentForCollectionWrapper(identifier) {
|
|
|
|
let component = this.modifyComponentForCollection(identifier);
|
|
|
|
|
|
|
|
if (!component) {
|
|
|
|
switch (identifier) {
|
|
|
|
case ERRORS_COLLECTION:
|
|
|
|
component = "select-kit/errors-collection";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
component = "select-kit/select-kit-collection";
|
|
|
|
break;
|
|
|
|
}
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
return component;
|
|
|
|
},
|
|
|
|
|
|
|
|
modifyComponentForCollection() {},
|
|
|
|
|
|
|
|
didUpdateAttrs() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
this.handleDeprecations();
|
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
this._searchPromise && cancel(this._searchPromise);
|
|
|
|
|
|
|
|
if (this.popper) {
|
|
|
|
this.popper.destroy();
|
|
|
|
this.popper = null;
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
2018-01-24 18:48:20 +08:00
|
|
|
},
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
const computedOptions = {};
|
|
|
|
Object.keys(this.selectKitOptions).forEach((key) => {
|
|
|
|
const value = this.selectKitOptions[key];
|
|
|
|
|
|
|
|
if (
|
|
|
|
key === "componentForRow" ||
|
|
|
|
key === "contentForCollection" ||
|
|
|
|
key === "componentForCollection"
|
|
|
|
) {
|
|
|
|
if (typeof value === "string") {
|
|
|
|
computedOptions[key] = () => value;
|
|
|
|
} else {
|
|
|
|
computedOptions[key] = bind(this, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
if (
|
|
|
|
typeof value === "string" &&
|
|
|
|
value.indexOf(".") < 0 &&
|
|
|
|
value in this
|
|
|
|
) {
|
|
|
|
const computedValue = get(this, value);
|
|
|
|
if (typeof computedValue !== "function") {
|
|
|
|
computedOptions[key] = get(this, value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
computedOptions[key] = value;
|
|
|
|
});
|
|
|
|
this.selectKit.options.setProperties(
|
|
|
|
Object.assign(computedOptions, this.options || {})
|
|
|
|
);
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
this.selectKit.setProperties({
|
2020-02-06 00:14:42 +08:00
|
|
|
hasSelection: !isEmpty(this.value),
|
2020-02-07 21:12:17 +08:00
|
|
|
noneItem: this._modifyNoSelectionWrapper(),
|
|
|
|
newItem: null,
|
2020-02-03 21:22:14 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
if (this.selectKit.isExpanded) {
|
2020-03-13 19:41:08 +08:00
|
|
|
this.triggerSearch();
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
if (this.computeContent) {
|
|
|
|
this._deprecated(
|
|
|
|
`The \`computeContent()\` function is deprecated pass a \`content\` attribute or define a \`content\` computed property in your component.`
|
|
|
|
);
|
|
|
|
|
|
|
|
this.set("content", this.computeContent());
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
selectKitOptions: {
|
|
|
|
showFullTitle: true,
|
|
|
|
none: null,
|
|
|
|
translatedNone: null,
|
|
|
|
filterable: false,
|
|
|
|
autoFilterable: "autoFilterable",
|
|
|
|
filterIcon: "search",
|
|
|
|
filterPlaceholder: "filterPlaceholder",
|
2020-04-23 02:39:55 +08:00
|
|
|
translatedFilterPlaceholder: null,
|
2020-02-03 21:22:14 +08:00
|
|
|
icon: null,
|
|
|
|
icons: null,
|
|
|
|
maximum: null,
|
2020-02-06 00:47:20 +08:00
|
|
|
maximumLabel: null,
|
2020-02-03 21:22:14 +08:00
|
|
|
minimum: null,
|
|
|
|
minimumLabel: null,
|
|
|
|
autoInsertNoneItem: true,
|
|
|
|
clearOnClick: false,
|
|
|
|
closeOnChange: true,
|
|
|
|
limitMatches: null,
|
2020-10-22 22:08:11 +08:00
|
|
|
placement: isDocumentRTL() ? "bottom-end" : "bottom-start",
|
2020-03-07 03:27:33 +08:00
|
|
|
placementStrategy: null,
|
2020-02-03 21:22:14 +08:00
|
|
|
filterComponent: "select-kit/select-kit-filter",
|
2020-02-14 17:00:40 +08:00
|
|
|
selectedNameComponent: "selected-name",
|
2020-04-21 23:11:40 +08:00
|
|
|
castInteger: false,
|
2020-05-09 16:26:23 +08:00
|
|
|
preventsClickPropagation: false,
|
2020-05-23 12:56:13 +08:00
|
|
|
focusAfterOnChange: true,
|
|
|
|
triggerOnChangeOnTab: true,
|
2020-02-03 21:22:14 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
autoFilterable: computed("content.[]", "selectKit.filter", function () {
|
|
|
|
return (
|
|
|
|
this.selectKit.filter &&
|
|
|
|
this.options.autoFilterable &&
|
|
|
|
this.content.length > 15
|
2018-06-15 23:03:24 +08:00
|
|
|
);
|
2020-02-03 21:22:14 +08:00
|
|
|
}),
|
|
|
|
|
|
|
|
filterPlaceholder: computed("options.allowAny", function () {
|
|
|
|
return this.options.allowAny
|
|
|
|
? "select_kit.filter_placeholder_with_any"
|
|
|
|
: "select_kit.filter_placeholder";
|
|
|
|
}),
|
|
|
|
|
|
|
|
collections: computed(
|
|
|
|
"selectedContent.[]",
|
|
|
|
"mainCollection.[]",
|
|
|
|
"errorsCollection.[]",
|
|
|
|
function () {
|
|
|
|
return this._collections.map((identifier) => {
|
|
|
|
return {
|
|
|
|
identifier,
|
|
|
|
content: this.selectKit.modifyContentForCollection(identifier),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
),
|
|
|
|
|
|
|
|
createContentFromInput(input) {
|
|
|
|
return input;
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
validateCreate(filter, content) {
|
|
|
|
this.clearErrors();
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
return (
|
|
|
|
filter.length > 0 &&
|
|
|
|
content &&
|
|
|
|
!content.map((c) => this.getValue(c)).includes(filter) &&
|
|
|
|
!makeArray(this.value).includes(filter)
|
2019-02-07 21:43:33 +08:00
|
|
|
);
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
2020-02-03 21:22:14 +08:00
|
|
|
|
|
|
|
validateSelect() {
|
|
|
|
this.clearErrors();
|
|
|
|
|
2020-02-19 07:57:58 +08:00
|
|
|
const selection = makeArray(this.value);
|
2020-02-03 21:22:14 +08:00
|
|
|
|
|
|
|
const maximum = this.selectKit.options.maximum;
|
|
|
|
if (maximum && selection.length >= maximum) {
|
|
|
|
const key =
|
|
|
|
this.selectKit.options.maximumLabel ||
|
|
|
|
"select_kit.max_content_reached";
|
|
|
|
this.addError(I18n.t(key, { count: maximum }));
|
|
|
|
return false;
|
2018-08-04 04:41:37 +08:00
|
|
|
}
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
return true;
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
2017-11-22 20:49:45 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
addError(error) {
|
|
|
|
this.errorsCollection.pushObject(error);
|
|
|
|
|
|
|
|
this._safeAfterRender(() => this.popper && this.popper.update());
|
2017-11-22 20:49:45 +08:00
|
|
|
},
|
2020-02-03 21:22:14 +08:00
|
|
|
|
|
|
|
clearErrors() {
|
|
|
|
if (!this.element || this.isDestroyed || this.isDestroying) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set("errorsCollection", []);
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
2020-02-03 21:22:14 +08:00
|
|
|
|
|
|
|
prependCollection(identifier) {
|
|
|
|
this._collections.unshift(identifier);
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
2017-11-28 02:50:04 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
appendCollection(identifier) {
|
|
|
|
this._collections.push(identifier);
|
|
|
|
},
|
2018-02-26 18:42:57 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
insertCollectionAtIndex(identifier, index) {
|
|
|
|
this._collections.insertAt(index, identifier);
|
|
|
|
},
|
2018-03-22 18:29:55 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
insertBeforeCollection(identifier, insertedIdentifier) {
|
|
|
|
const index = this._collections.indexOf(identifier);
|
|
|
|
this.insertCollectionAtIndex(insertedIdentifier, index - 1);
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
2018-03-22 18:29:55 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
insertAfterCollection(identifier, insertedIdentifier) {
|
|
|
|
const index = this._collections.indexOf(identifier);
|
|
|
|
this.insertCollectionAtIndex(insertedIdentifier, index + 1);
|
2018-03-22 18:29:55 +08:00
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_onInput(event) {
|
|
|
|
this.popper && this.popper.update();
|
|
|
|
|
|
|
|
if (this._searchPromise) {
|
|
|
|
cancel(this._searchPromise);
|
|
|
|
}
|
|
|
|
|
2020-12-18 21:18:52 +08:00
|
|
|
discourseDebounce(this, this._debouncedInput, event.target.value, 200);
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_debouncedInput(filter) {
|
2020-02-27 22:20:04 +08:00
|
|
|
this.selectKit.setProperties({ filter, isLoading: true });
|
2020-03-13 19:41:08 +08:00
|
|
|
this.triggerSearch(filter);
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_onChangeWrapper(value, items) {
|
|
|
|
this.selectKit.set("filter", null);
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
if (
|
|
|
|
!this.selectKit.valueProperty &&
|
|
|
|
this.selectKit.noneItem === value
|
|
|
|
) {
|
|
|
|
value = null;
|
|
|
|
items = [];
|
|
|
|
}
|
|
|
|
|
2020-02-11 22:54:56 +08:00
|
|
|
value = makeArray(value);
|
|
|
|
items = makeArray(items);
|
|
|
|
|
|
|
|
if (this.multiSelect) {
|
|
|
|
items = items.filter(
|
|
|
|
(i) =>
|
|
|
|
i !== this.newItem &&
|
|
|
|
i !== this.noneItem &&
|
|
|
|
this.getValue(i) !== null
|
|
|
|
);
|
|
|
|
|
|
|
|
if (this.selectKit.options.maximum === 1) {
|
|
|
|
value = value.slice(0, 1);
|
|
|
|
items = items.slice(0, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.singleSelect) {
|
2020-02-19 06:41:15 +08:00
|
|
|
value = isPresent(value.firstObject) ? value.firstObject : null;
|
|
|
|
items = isPresent(items.firstObject) ? items.firstObject : null;
|
2020-02-11 22:54:56 +08:00
|
|
|
}
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
this._boundaryActionHandler("onChange", value, items);
|
2020-05-06 23:16:20 +08:00
|
|
|
|
|
|
|
applyOnChangePluginApiCallbacks(value, items, this);
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
resolve(items);
|
|
|
|
}).finally(() => {
|
|
|
|
if (!this.isDestroying && !this.isDestroyed) {
|
|
|
|
if (this.selectKit.options.closeOnChange) {
|
|
|
|
this.selectKit.close();
|
|
|
|
}
|
|
|
|
|
2020-05-16 17:20:37 +08:00
|
|
|
if (this.selectKit.options.focusAfterOnChange) {
|
2020-05-09 16:26:23 +08:00
|
|
|
this._safeAfterRender(() => {
|
|
|
|
this._focusFilter();
|
|
|
|
this.popper && this.popper.update();
|
|
|
|
});
|
|
|
|
}
|
2020-02-03 21:22:14 +08:00
|
|
|
}
|
|
|
|
});
|
2018-04-05 22:45:19 +08:00
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_modifyContentWrapper(content) {
|
|
|
|
content = this.modifyContent(content);
|
|
|
|
|
2020-05-06 23:16:20 +08:00
|
|
|
return applyContentPluginApiCallbacks(content, this);
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
modifyContent(content) {
|
|
|
|
return content;
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_modifyNoSelectionWrapper() {
|
2020-05-06 23:16:20 +08:00
|
|
|
return this.modifyNoSelection();
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
modifyNoSelection() {
|
|
|
|
if (this.selectKit.options.translatedNone) {
|
|
|
|
return this.defaultItem(null, this.selectKit.options.translatedNone);
|
|
|
|
}
|
|
|
|
|
|
|
|
let none = this.selectKit.options.none;
|
2020-09-22 22:28:28 +08:00
|
|
|
if (isNone(none) && !this.selectKit.options.allowAny) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-02-03 21:22:14 +08:00
|
|
|
|
2017-11-21 18:53:09 +08:00
|
|
|
if (
|
2020-02-03 21:22:14 +08:00
|
|
|
isNone(none) &&
|
|
|
|
this.selectKit.options.allowAny &&
|
|
|
|
!this.selectKit.isExpanded
|
2017-11-21 18:53:09 +08:00
|
|
|
) {
|
2020-04-23 04:17:53 +08:00
|
|
|
return null;
|
2017-11-21 18:53:09 +08:00
|
|
|
}
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
let item;
|
|
|
|
switch (typeof none) {
|
|
|
|
case "string":
|
|
|
|
item = this.defaultItem(null, I18n.t(none));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
item = none;
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
2018-03-29 19:42:00 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
return item;
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
2017-11-21 18:53:09 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_modifySelectionWrapper(item) {
|
|
|
|
return this.modifySelection(item);
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
modifySelection(item) {
|
|
|
|
return item;
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
2017-11-21 18:53:09 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_onKeydownWrapper(event) {
|
|
|
|
return this._boundaryActionHandler("onKeydown", event);
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
2017-11-21 18:53:09 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_onHover(value, item) {
|
|
|
|
throttle(this, this._highlight, item, 25, true);
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
2018-01-11 16:39:51 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_highlight(item) {
|
|
|
|
this.selectKit.set("highlighted", item);
|
2018-03-22 18:29:55 +08:00
|
|
|
},
|
2018-01-11 16:39:51 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_boundaryActionHandler(actionName, ...params) {
|
|
|
|
if (!this.element || this.isDestroying || this.isDestroyed) {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-21 18:53:09 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
let boundaryAction = true;
|
2017-11-21 18:53:09 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
const privateActionName = `_${actionName}`;
|
|
|
|
const privateAction = get(this, privateActionName);
|
|
|
|
if (privateAction) {
|
|
|
|
boundaryAction = privateAction.call(this, ...params);
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
2018-03-22 18:29:55 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
if (this.actions) {
|
|
|
|
const componentAction = get(this.actions, actionName);
|
|
|
|
if (boundaryAction && componentAction) {
|
|
|
|
boundaryAction = componentAction.call(this, ...params);
|
|
|
|
}
|
|
|
|
}
|
2018-03-22 18:29:55 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
const action = get(this, actionName);
|
|
|
|
if (boundaryAction && action) {
|
|
|
|
boundaryAction = action.call(this, ...params);
|
|
|
|
}
|
|
|
|
|
|
|
|
return boundaryAction;
|
2018-03-22 18:29:55 +08:00
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
deselect() {
|
|
|
|
this.clearErrors();
|
|
|
|
this.selectKit.change(null, null);
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
2021-02-22 21:34:31 +08:00
|
|
|
deselectByValue(value) {
|
|
|
|
if (!value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const item = this.itemForValue(value, this.selectedContent);
|
|
|
|
this.deselect(item);
|
|
|
|
},
|
|
|
|
|
2021-01-06 19:57:13 +08:00
|
|
|
append() {
|
|
|
|
// do nothing on general case
|
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
search(filter) {
|
|
|
|
let content = this.content || [];
|
|
|
|
if (filter) {
|
|
|
|
filter = this._normalize(filter);
|
|
|
|
content = content.filter((c) => {
|
|
|
|
const name = this._normalize(this.getName(c));
|
|
|
|
return name && name.indexOf(filter) > -1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return content;
|
2018-02-14 00:23:12 +08:00
|
|
|
},
|
|
|
|
|
2020-03-13 19:41:08 +08:00
|
|
|
triggerSearch(filter) {
|
|
|
|
if (this._searchPromise) {
|
|
|
|
cancel(this._searchPromise);
|
|
|
|
}
|
|
|
|
this._searchPromise = this._searchWrapper(
|
|
|
|
filter || this.selectKit.filter
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_searchWrapper(filter) {
|
|
|
|
this.clearErrors();
|
2021-02-01 18:07:11 +08:00
|
|
|
this.setProperties({
|
|
|
|
mainCollection: [],
|
|
|
|
"selectKit.isLoading": true,
|
|
|
|
"selectKit.enterDisabled": true,
|
|
|
|
});
|
2020-02-03 21:22:14 +08:00
|
|
|
this._safeAfterRender(() => this.popper && this.popper.update());
|
|
|
|
|
|
|
|
let content = [];
|
|
|
|
|
2021-02-01 18:07:11 +08:00
|
|
|
return Promise.resolve(this.search(filter))
|
|
|
|
.then((result) => {
|
|
|
|
content = content.concat(makeArray(result));
|
|
|
|
content = this.selectKit.modifyContent(content).filter(Boolean);
|
2020-02-03 21:22:14 +08:00
|
|
|
|
2021-02-01 18:07:11 +08:00
|
|
|
if (this.selectKit.valueProperty) {
|
|
|
|
content = content.uniqBy(this.selectKit.valueProperty);
|
|
|
|
} else {
|
|
|
|
content = content.uniq();
|
2020-02-03 21:22:14 +08:00
|
|
|
}
|
|
|
|
|
2021-02-01 18:07:11 +08:00
|
|
|
if (this.selectKit.options.limitMatches) {
|
|
|
|
content = content.slice(0, this.selectKit.options.limitMatches);
|
|
|
|
}
|
2020-02-03 21:22:14 +08:00
|
|
|
|
2021-02-01 18:07:11 +08:00
|
|
|
const noneItem = this.selectKit.noneItem;
|
|
|
|
if (
|
|
|
|
this.selectKit.options.allowAny &&
|
|
|
|
filter &&
|
|
|
|
this.getName(noneItem) !== filter
|
|
|
|
) {
|
|
|
|
filter = this.createContentFromInput(filter);
|
|
|
|
if (this.validateCreate(filter, content)) {
|
|
|
|
this.selectKit.set("newItem", this.defaultItem(filter, filter));
|
|
|
|
content.unshift(this.selectKit.newItem);
|
|
|
|
}
|
|
|
|
}
|
2020-02-03 21:22:14 +08:00
|
|
|
|
2021-02-01 18:07:11 +08:00
|
|
|
const hasNoContent = isEmpty(content);
|
2020-02-03 21:22:14 +08:00
|
|
|
|
2021-02-01 18:07:11 +08:00
|
|
|
if (
|
|
|
|
this.selectKit.hasSelection &&
|
|
|
|
noneItem &&
|
|
|
|
this.selectKit.options.autoInsertNoneItem
|
|
|
|
) {
|
|
|
|
content.unshift(noneItem);
|
|
|
|
}
|
2020-02-03 21:22:14 +08:00
|
|
|
|
2021-02-01 18:07:11 +08:00
|
|
|
this.set("mainCollection", content);
|
|
|
|
|
|
|
|
this.selectKit.setProperties({
|
|
|
|
highlighted:
|
|
|
|
this.singleSelect && this.value
|
|
|
|
? this.itemForValue(this.value, this.mainCollection)
|
|
|
|
: this.mainCollection.firstObject,
|
|
|
|
isLoading: false,
|
|
|
|
hasNoContent,
|
|
|
|
});
|
|
|
|
|
|
|
|
this._safeAfterRender(() => {
|
|
|
|
this.popper && this.popper.update();
|
|
|
|
this._focusFilter();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
if (this.isDestroyed || this.isDestroying) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.set("selectKit.enterDisabled", false);
|
2020-02-03 21:22:14 +08:00
|
|
|
});
|
2018-05-28 22:18:25 +08:00
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_safeAfterRender(fn) {
|
|
|
|
next(() => {
|
|
|
|
schedule("afterRender", () => {
|
|
|
|
if (!this.element || this.isDestroyed || this.isDestroying) {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-09 17:52:32 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
fn();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2019-01-04 01:03:01 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_scrollToRow(rowItem) {
|
|
|
|
const value = this.getValue(rowItem);
|
|
|
|
const rowContainer = this.element.querySelector(
|
|
|
|
`.select-kit-row[data-value="${value}"]`
|
|
|
|
);
|
2018-03-22 18:29:55 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
if (rowContainer) {
|
2020-04-24 18:45:47 +08:00
|
|
|
const collectionContainer = rowContainer.parentNode;
|
2020-02-03 21:22:14 +08:00
|
|
|
|
2020-04-24 18:45:47 +08:00
|
|
|
collectionContainer.scrollTop =
|
|
|
|
rowContainer.offsetTop - collectionContainer.offsetTop;
|
2020-02-03 21:22:14 +08:00
|
|
|
}
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
2018-01-11 16:39:51 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_highlightNext() {
|
2020-05-07 22:41:26 +08:00
|
|
|
let highlightedIndex = this.mainCollection.indexOf(
|
2020-02-03 21:22:14 +08:00
|
|
|
this.selectKit.highlighted
|
|
|
|
);
|
|
|
|
const count = this.mainCollection.length;
|
|
|
|
|
|
|
|
if (highlightedIndex < count - 1) {
|
2020-05-07 22:41:26 +08:00
|
|
|
highlightedIndex = highlightedIndex + 1;
|
2020-02-03 21:22:14 +08:00
|
|
|
} else {
|
2020-05-07 22:41:26 +08:00
|
|
|
highlightedIndex = 0;
|
2020-02-03 21:22:14 +08:00
|
|
|
}
|
|
|
|
|
2020-05-07 22:41:26 +08:00
|
|
|
const highlighted = this.mainCollection.objectAt(highlightedIndex);
|
2020-02-03 21:22:14 +08:00
|
|
|
if (highlighted) {
|
|
|
|
this._scrollToRow(highlighted);
|
|
|
|
this.set("selectKit.highlighted", highlighted);
|
|
|
|
}
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_highlightPrevious() {
|
2020-05-07 22:41:26 +08:00
|
|
|
let highlightedIndex = this.mainCollection.indexOf(
|
2020-02-03 21:22:14 +08:00
|
|
|
this.selectKit.highlighted
|
|
|
|
);
|
|
|
|
const count = this.mainCollection.length;
|
|
|
|
|
|
|
|
if (highlightedIndex > 0) {
|
2020-05-07 22:41:26 +08:00
|
|
|
highlightedIndex = highlightedIndex - 1;
|
2020-02-03 21:22:14 +08:00
|
|
|
} else {
|
2020-05-07 22:41:26 +08:00
|
|
|
highlightedIndex = count - 1;
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
|
|
|
|
2020-05-07 22:41:26 +08:00
|
|
|
const highlighted = this.mainCollection.objectAt(highlightedIndex);
|
2020-02-03 21:22:14 +08:00
|
|
|
if (highlighted) {
|
|
|
|
this._scrollToRow(highlighted);
|
|
|
|
this.set("selectKit.highlighted", highlighted);
|
|
|
|
}
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
select(value, item) {
|
2020-02-19 06:41:15 +08:00
|
|
|
if (!isPresent(value)) {
|
2020-02-03 21:22:14 +08:00
|
|
|
if (!this.validateSelect(this.selectKit.highlighted)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.selectKit.change(
|
|
|
|
this.getValue(this.selectKit.highlighted),
|
|
|
|
this.selectKit.highlighted
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const existingItem = this.findValue(this.mainCollection, item);
|
|
|
|
if (existingItem) {
|
|
|
|
if (!this.validateSelect(item)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.selectKit.change(value, item || this.defaultItem(value, value));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_onClearSelection() {
|
|
|
|
this.selectKit.change(null, null);
|
|
|
|
},
|
|
|
|
|
2020-05-06 23:16:20 +08:00
|
|
|
_onOpenWrapper() {
|
|
|
|
return this._boundaryActionHandler("onOpen");
|
2018-03-22 18:29:55 +08:00
|
|
|
},
|
|
|
|
|
2020-05-06 23:16:20 +08:00
|
|
|
_onCloseWrapper() {
|
2020-02-03 21:22:14 +08:00
|
|
|
this.set("selectKit.highlighted", null);
|
|
|
|
|
2020-05-06 23:16:20 +08:00
|
|
|
return this._boundaryActionHandler("onClose");
|
2018-03-22 18:29:55 +08:00
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_toggle(event) {
|
|
|
|
if (this.selectKit.isExpanded) {
|
|
|
|
this._close(event);
|
|
|
|
} else {
|
|
|
|
this._open(event);
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
2018-03-22 18:29:55 +08:00
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_close(event) {
|
|
|
|
if (!this.selectKit.isExpanded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.clearErrors();
|
2018-02-14 00:23:12 +08:00
|
|
|
|
2020-05-06 23:16:20 +08:00
|
|
|
this.selectKit.onClose(event);
|
2020-02-03 21:22:14 +08:00
|
|
|
|
|
|
|
this.selectKit.setProperties({
|
|
|
|
isExpanded: false,
|
|
|
|
filter: null,
|
|
|
|
});
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
2018-03-22 18:29:55 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_open(event) {
|
|
|
|
if (this.selectKit.isExpanded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.clearErrors();
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2020-05-06 23:16:20 +08:00
|
|
|
this.selectKit.onOpen(event);
|
2020-02-03 21:22:14 +08:00
|
|
|
|
|
|
|
if (!this.popper) {
|
|
|
|
const anchor = document.querySelector(
|
2021-02-15 19:32:09 +08:00
|
|
|
`#${this.selectKit.uniqueID}-header`
|
2020-02-03 21:22:14 +08:00
|
|
|
);
|
|
|
|
const popper = document.querySelector(
|
2021-02-15 19:32:09 +08:00
|
|
|
`#${this.selectKit.uniqueID}-body`
|
2020-02-03 21:22:14 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
const inModal = $(this.element).parents("#discourse-modal").length;
|
|
|
|
|
2020-03-07 03:27:33 +08:00
|
|
|
let placementStrategy = this.selectKit.options.placementStrategy;
|
|
|
|
if (!placementStrategy) {
|
2020-03-10 19:22:56 +08:00
|
|
|
placementStrategy = inModal ? "fixed" : "absolute";
|
2020-03-07 03:27:33 +08:00
|
|
|
}
|
|
|
|
|
2021-02-04 00:45:54 +08:00
|
|
|
const verticalOffset = 3;
|
2020-04-22 19:02:20 +08:00
|
|
|
|
2020-09-15 00:33:14 +08:00
|
|
|
this.popper = createPopper(anchor, popper, {
|
2020-02-03 21:22:14 +08:00
|
|
|
eventsEnabled: false,
|
2020-03-07 03:27:33 +08:00
|
|
|
strategy: placementStrategy,
|
2020-02-03 21:22:14 +08:00
|
|
|
placement: this.selectKit.options.placement,
|
|
|
|
modifiers: [
|
2020-04-22 16:54:02 +08:00
|
|
|
{
|
|
|
|
name: "offset",
|
|
|
|
options: {
|
2020-04-22 19:02:20 +08:00
|
|
|
offset: [0, verticalOffset],
|
2020-04-22 16:54:02 +08:00
|
|
|
},
|
|
|
|
},
|
2020-05-05 18:59:47 +08:00
|
|
|
{
|
|
|
|
name: "applySmallScreenOffset",
|
|
|
|
enabled: window.innerWidth <= 450,
|
|
|
|
phase: "main",
|
|
|
|
fn({ state }) {
|
2020-05-07 15:10:29 +08:00
|
|
|
if (!inModal) {
|
|
|
|
let { x } = state.elements.reference.getBoundingClientRect();
|
|
|
|
state.modifiersData.popperOffsets.x = -x + 10;
|
|
|
|
}
|
2020-05-05 18:59:47 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "applySmallScreenMaxWidth",
|
|
|
|
enabled: window.innerWidth <= 450,
|
|
|
|
phase: "beforeWrite",
|
2020-06-22 21:48:00 +08:00
|
|
|
fn: ({ state }) => {
|
2020-05-07 15:10:29 +08:00
|
|
|
if (inModal) {
|
|
|
|
const innerModal = document.querySelector(
|
|
|
|
"#discourse-modal div.modal-inner-container"
|
|
|
|
);
|
|
|
|
|
|
|
|
if (innerModal) {
|
2020-06-22 21:48:00 +08:00
|
|
|
if (this.multiSelect) {
|
|
|
|
state.styles.popper.width = `${this.element.offsetWidth}px`;
|
|
|
|
} else {
|
|
|
|
state.styles.popper.width = `${
|
|
|
|
innerModal.clientWidth - 20
|
|
|
|
}px`;
|
|
|
|
}
|
2020-05-07 15:10:29 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
state.styles.popper.width = `${window.innerWidth - 20}px`;
|
|
|
|
}
|
2020-05-05 18:59:47 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "sameWidth",
|
2020-05-05 19:01:02 +08:00
|
|
|
enabled: window.innerWidth > 450,
|
2020-05-05 18:59:47 +08:00
|
|
|
phase: "beforeWrite",
|
|
|
|
requires: ["computeStyles"],
|
|
|
|
fn: ({ state }) => {
|
|
|
|
state.styles.popper.minWidth = `${state.rects.reference.width}px`;
|
|
|
|
},
|
|
|
|
effect: ({ state }) => {
|
|
|
|
state.elements.popper.style.minWidth = `${state.elements.reference.offsetWidth}px`;
|
|
|
|
},
|
|
|
|
},
|
2020-02-03 21:22:14 +08:00
|
|
|
{
|
|
|
|
name: "positionWrapper",
|
|
|
|
phase: "afterWrite",
|
|
|
|
enabled: true,
|
|
|
|
fn: (data) => {
|
|
|
|
const wrapper = this.element.querySelector(
|
|
|
|
".select-kit-wrapper"
|
|
|
|
);
|
|
|
|
if (wrapper) {
|
2020-04-22 19:02:20 +08:00
|
|
|
let height = this.element.offsetHeight + verticalOffset;
|
2020-02-03 21:22:14 +08:00
|
|
|
|
|
|
|
const body = this.element.querySelector(".select-kit-body");
|
|
|
|
if (body) {
|
|
|
|
height += body.offsetHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
const popperElement = data.state.elements.popper;
|
2021-02-01 18:07:11 +08:00
|
|
|
const topPlacement =
|
2020-02-03 21:22:14 +08:00
|
|
|
popperElement &&
|
2021-02-01 18:07:11 +08:00
|
|
|
popperElement
|
|
|
|
.getAttribute("data-popper-placement")
|
|
|
|
.startsWith("top-");
|
|
|
|
if (topPlacement) {
|
2020-02-03 21:22:14 +08:00
|
|
|
this.element.classList.remove("is-under");
|
|
|
|
this.element.classList.add("is-above");
|
|
|
|
} else {
|
|
|
|
this.element.classList.remove("is-above");
|
|
|
|
this.element.classList.add("is-under");
|
|
|
|
}
|
|
|
|
|
2020-04-24 05:10:40 +08:00
|
|
|
wrapper.style.width = `${this.element.offsetWidth}px`;
|
2020-02-03 21:22:14 +08:00
|
|
|
wrapper.style.height = `${height}px`;
|
2021-02-01 18:07:11 +08:00
|
|
|
if (placementStrategy === "fixed") {
|
|
|
|
const rects = this.element.getClientRects()[0];
|
2021-02-02 21:43:13 +08:00
|
|
|
|
|
|
|
if (rects) {
|
|
|
|
const bodyRects = body && body.getClientRects()[0];
|
|
|
|
|
|
|
|
wrapper.style.position = "fixed";
|
|
|
|
wrapper.style.left = `${rects.left}px`;
|
|
|
|
if (topPlacement && bodyRects) {
|
|
|
|
wrapper.style.top = `${rects.top - bodyRects.height}px`;
|
|
|
|
} else {
|
|
|
|
wrapper.style.top = `${rects.top}px`;
|
|
|
|
}
|
|
|
|
if (isDocumentRTL()) {
|
|
|
|
wrapper.style.right = "unset";
|
|
|
|
}
|
2021-02-01 18:07:11 +08:00
|
|
|
}
|
|
|
|
}
|
2020-02-03 21:22:14 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.selectKit.setProperties({
|
|
|
|
isExpanded: true,
|
|
|
|
isFilterExpanded:
|
|
|
|
this.selectKit.options.filterable || this.selectKit.options.allowAny,
|
|
|
|
});
|
|
|
|
|
2020-03-13 19:41:08 +08:00
|
|
|
this.triggerSearch();
|
2020-02-03 21:22:14 +08:00
|
|
|
|
|
|
|
this._safeAfterRender(() => {
|
|
|
|
this._focusFilter();
|
2020-04-21 22:48:49 +08:00
|
|
|
this._scrollToCurrent();
|
2020-02-03 21:22:14 +08:00
|
|
|
this.popper && this.popper.update();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-04-21 22:48:49 +08:00
|
|
|
_scrollToCurrent() {
|
|
|
|
if (this.value && this.mainCollection) {
|
|
|
|
let highlighted;
|
|
|
|
if (this.valueProperty) {
|
|
|
|
highlighted = this.mainCollection.findBy(
|
|
|
|
this.valueProperty,
|
|
|
|
this.value
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const index = this.mainCollection.indexOf(this.value);
|
|
|
|
highlighted = this.mainCollection.objectAt(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (highlighted) {
|
|
|
|
this._scrollToRow(highlighted);
|
|
|
|
this.set("selectKit.highlighted", highlighted);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_focusFilter(forceHeader = false) {
|
|
|
|
this._safeAfterRender(() => {
|
|
|
|
const input = this.getFilterInput();
|
|
|
|
if (!forceHeader && input) {
|
|
|
|
input.focus({ preventScroll: true });
|
2018-06-15 23:03:24 +08:00
|
|
|
} else {
|
2020-02-03 21:22:14 +08:00
|
|
|
const headerContainer = this.getHeader();
|
|
|
|
headerContainer && headerContainer.focus({ preventScroll: true });
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
2020-02-03 21:22:14 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
getFilterInput() {
|
2021-02-15 19:32:09 +08:00
|
|
|
return document.querySelector(`#${this.selectKit.uniqueID}-filter input`);
|
2020-02-03 21:22:14 +08:00
|
|
|
},
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
getHeader() {
|
2021-02-15 19:32:09 +08:00
|
|
|
return document.querySelector(`#${this.selectKit.uniqueID}-header`);
|
2020-02-03 21:22:14 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
handleDeprecations() {
|
|
|
|
this._deprecateValueAttribute();
|
|
|
|
this._deprecateMutations();
|
|
|
|
this._deprecateOptions();
|
|
|
|
},
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_deprecated(text) {
|
|
|
|
const discourseSetup = document.getElementById("data-discourse-setup");
|
|
|
|
if (
|
|
|
|
discourseSetup &&
|
|
|
|
discourseSetup.getAttribute("data-environment") === "development"
|
|
|
|
) {
|
|
|
|
deprecated(text, { since: "v2.4.0" });
|
|
|
|
}
|
|
|
|
},
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_deprecateValueAttribute() {
|
2020-02-07 17:39:39 +08:00
|
|
|
if (this.valueAttribute || this.valueAttribute === null) {
|
2020-02-03 21:22:14 +08:00
|
|
|
this._deprecated(
|
|
|
|
"The `valueAttribute` is deprecated. Use `valueProperty` instead"
|
|
|
|
);
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
this.set("valueProperty", this.valueAttribute);
|
|
|
|
}
|
|
|
|
},
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_deprecateMutations() {
|
|
|
|
this.actions = this.actions || {};
|
|
|
|
this.attrs = this.attrs || {};
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
if (!this.attrs.onChange && !this.actions.onChange) {
|
|
|
|
this._deprecated(
|
|
|
|
"Implicit mutation has been deprecated, please use `onChange` handler"
|
|
|
|
);
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
this.actions.onChange =
|
|
|
|
this.attrs.onSelect ||
|
|
|
|
this.actions.onSelect ||
|
|
|
|
((value) => this.set("value", value));
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
2020-02-03 21:22:14 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_deprecateOptions() {
|
|
|
|
const migrations = {
|
|
|
|
headerIcon: "icon",
|
|
|
|
onExpand: "onOpen",
|
|
|
|
onCollapse: "onClose",
|
|
|
|
allowAny: "options.allowAny",
|
|
|
|
allowCreate: "options.allowAny",
|
|
|
|
filterable: "options.filterable",
|
|
|
|
excludeCategoryId: "options.excludeCategoryId",
|
|
|
|
scopedCategoryId: "options.scopedCategoryId",
|
|
|
|
allowUncategorized: "options.allowUncategorized",
|
|
|
|
none: "options.none",
|
|
|
|
rootNone: "options.none",
|
2020-10-13 17:31:07 +08:00
|
|
|
disabled: "options.disabled",
|
2020-02-03 21:22:14 +08:00
|
|
|
rootNoneLabel: "options.none",
|
|
|
|
showFullTitle: "options.showFullTitle",
|
|
|
|
title: "options.translatedNone",
|
|
|
|
maximum: "options.maximum",
|
|
|
|
minimum: "options.minimum",
|
|
|
|
i18nPostfix: "options.i18nPostfix",
|
2020-02-14 17:00:40 +08:00
|
|
|
i18nPrefix: "options.i18nPrefix",
|
|
|
|
castInteger: "options.castInteger",
|
2020-02-03 21:22:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Object.keys(migrations).forEach((from) => {
|
|
|
|
const to = migrations[from];
|
|
|
|
if (this.get(from) && !this.get(to)) {
|
|
|
|
this._deprecated(
|
|
|
|
`The \`${from}\` attribute is deprecated. Use \`${to}\` instead`
|
|
|
|
);
|
|
|
|
|
|
|
|
this.set(to, this.get(from));
|
|
|
|
}
|
|
|
|
});
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|