2019-01-11 23:41:56 +00:00
|
|
|
import Component from '@ember/component';
|
2020-06-10 13:49:16 +00:00
|
|
|
import { action } from '@ember/object';
|
2019-10-15 18:32:58 +00:00
|
|
|
import { computed as overridable } from 'ember-overridable-computed';
|
2022-03-08 17:28:36 +00:00
|
|
|
import { scheduleOnce } from '@ember/runloop';
|
2020-06-10 13:49:16 +00:00
|
|
|
import { classNames } from '@ember-decorators/component';
|
|
|
|
import classic from 'ember-classic-decorator';
|
2019-01-11 23:41:56 +00:00
|
|
|
|
2019-01-15 01:59:41 +00:00
|
|
|
const TAB = 9;
|
|
|
|
const ESC = 27;
|
|
|
|
const SPACE = 32;
|
|
|
|
const ARROW_UP = 38;
|
|
|
|
const ARROW_DOWN = 40;
|
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@classic
|
|
|
|
@classNames('dropdown')
|
|
|
|
export default class MultiSelectDropdown extends Component {
|
|
|
|
@overridable(() => []) options;
|
|
|
|
@overridable(() => []) selection;
|
2019-01-11 23:41:56 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
onSelect() {}
|
2019-01-11 23:41:56 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
isOpen = false;
|
|
|
|
dropdown = null;
|
2019-01-15 01:59:41 +00:00
|
|
|
|
2019-01-17 00:28:13 +00:00
|
|
|
capture(dropdown) {
|
|
|
|
// It's not a good idea to grab a dropdown reference like this, but it's necessary
|
|
|
|
// in order to invoke dropdown.actions.close in traverseList as well as
|
|
|
|
// dropdown.actions.reposition when the label or selection length changes.
|
|
|
|
this.set('dropdown', dropdown);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2019-01-17 00:28:13 +00:00
|
|
|
|
|
|
|
didReceiveAttrs() {
|
2021-12-28 14:45:20 +00:00
|
|
|
super.didReceiveAttrs();
|
2019-03-26 07:46:44 +00:00
|
|
|
const dropdown = this.dropdown;
|
|
|
|
if (this.isOpen && dropdown) {
|
2022-03-08 17:28:36 +00:00
|
|
|
scheduleOnce('afterRender', this, this.repositionDropdown);
|
2019-01-17 00:28:13 +00:00
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2019-01-17 00:28:13 +00:00
|
|
|
|
2020-06-09 21:03:28 +00:00
|
|
|
repositionDropdown() {
|
|
|
|
this.dropdown.actions.reposition();
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2020-06-09 21:03:28 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@action
|
|
|
|
toggle({ key }) {
|
|
|
|
const newSelection = this.selection.slice();
|
|
|
|
if (newSelection.includes(key)) {
|
|
|
|
newSelection.removeObject(key);
|
|
|
|
} else {
|
|
|
|
newSelection.addObject(key);
|
|
|
|
}
|
|
|
|
this.onSelect(newSelection);
|
|
|
|
}
|
2019-01-15 01:59:41 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@action
|
|
|
|
openOnArrowDown(dropdown, e) {
|
|
|
|
this.capture(dropdown);
|
2019-01-15 01:59:41 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
if (!this.isOpen && e.keyCode === ARROW_DOWN) {
|
|
|
|
dropdown.actions.open(e);
|
|
|
|
e.preventDefault();
|
|
|
|
} else if (this.isOpen && (e.keyCode === TAB || e.keyCode === ARROW_DOWN)) {
|
2021-12-28 16:08:12 +00:00
|
|
|
const optionsId = this.element
|
|
|
|
.querySelector('.dropdown-trigger')
|
|
|
|
.getAttribute('aria-owns');
|
|
|
|
const firstElement = document.querySelector(
|
|
|
|
`#${optionsId} .dropdown-option`
|
|
|
|
);
|
2019-01-15 01:59:41 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
if (firstElement) {
|
|
|
|
firstElement.focus();
|
|
|
|
e.preventDefault();
|
2019-01-15 01:59:41 +00:00
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-15 01:59:41 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@action
|
|
|
|
traverseList(option, e) {
|
|
|
|
if (e.keyCode === ESC) {
|
|
|
|
// Close the dropdown
|
|
|
|
const dropdown = this.dropdown;
|
|
|
|
if (dropdown) {
|
|
|
|
dropdown.actions.close(e);
|
|
|
|
// Return focus to the trigger so tab works as expected
|
|
|
|
const trigger = this.element.querySelector('.dropdown-trigger');
|
|
|
|
if (trigger) trigger.focus();
|
2019-01-15 01:59:41 +00:00
|
|
|
e.preventDefault();
|
2020-06-10 13:49:16 +00:00
|
|
|
this.set('dropdown', null);
|
2019-01-15 01:59:41 +00:00
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
} else if (e.keyCode === ARROW_UP) {
|
|
|
|
// previous item
|
|
|
|
const prev = e.target.previousElementSibling;
|
|
|
|
if (prev) {
|
|
|
|
prev.focus();
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
} else if (e.keyCode === ARROW_DOWN) {
|
|
|
|
// next item
|
|
|
|
const next = e.target.nextElementSibling;
|
|
|
|
if (next) {
|
|
|
|
next.focus();
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
} else if (e.keyCode === SPACE) {
|
|
|
|
this.send('toggle', option);
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|