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