2021-08-20 16:26:36 +03:00
|
|
|
/* global key */
|
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import {action} from '@ember/object';
|
|
|
|
import {isBlank} from '@ember/utils';
|
2021-11-12 18:26:57 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2021-08-20 16:26:36 +03:00
|
|
|
|
|
|
|
export default class GhSearchInputTrigger extends Component {
|
2021-11-12 18:26:57 +03:00
|
|
|
@service dropdown;
|
|
|
|
|
2021-08-20 16:26:36 +03:00
|
|
|
inputElem = null;
|
|
|
|
|
2021-11-12 18:26:57 +03:00
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
this.dropdown.on('close', this, this.closeFromDropdown);
|
|
|
|
}
|
|
|
|
|
|
|
|
willDestroy() {
|
|
|
|
super.willDestroy(...arguments);
|
|
|
|
this.dropdown.off('close', this, this.closeFromDropdown);
|
|
|
|
}
|
|
|
|
|
|
|
|
closeFromDropdown() {
|
|
|
|
this.args.select.actions.close();
|
|
|
|
}
|
|
|
|
|
2021-08-20 16:26:36 +03:00
|
|
|
@action
|
|
|
|
registerInput(elem) {
|
|
|
|
this.inputElem = elem;
|
2021-08-23 17:59:53 +03:00
|
|
|
|
|
|
|
if (this.args.extra?.autofocus) {
|
|
|
|
this.inputElem.focus();
|
|
|
|
}
|
2021-08-20 16:26:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
handleInput(event) {
|
|
|
|
let term = event.target.value;
|
|
|
|
|
|
|
|
// open dropdown if not open and term is present
|
|
|
|
// close dropdown if open and term is blank
|
|
|
|
if (isBlank(term) === this.args.select.isOpen) {
|
|
|
|
isBlank(term) ? this.close() : this.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.args.onInput?.(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
// hacky workaround to let Escape clear the input if there's text,
|
|
|
|
// but still allow it to bubble if there's no text (used for closing modals, etc)
|
|
|
|
@action
|
|
|
|
handleKeydown(e) {
|
|
|
|
if ((e.key === 'Escape' && e.target.value) || e.key === 'Enter') {
|
|
|
|
this._previousKeyScope = key.getScope();
|
|
|
|
key.setScope('ignore');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
handleKeyup() {
|
|
|
|
if (key.getScope() === 'ignore') {
|
|
|
|
key.setScope(this._previousKeyScope);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
handleFocus() {
|
|
|
|
if (this.args.extra?.openOnFocus && this.args.select.results.length > 0) {
|
|
|
|
this.open();
|
|
|
|
}
|
2021-08-26 19:19:00 +03:00
|
|
|
|
|
|
|
this.args.onFocus?.(...arguments);
|
2022-01-27 21:43:08 +03:00
|
|
|
|
|
|
|
if (this.args.extra.showSearchMessage === false && this.args.select.results.length === 0) {
|
|
|
|
this.close();
|
|
|
|
}
|
2021-08-20 16:26:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2021-08-25 17:41:17 +03:00
|
|
|
handleBlur(event) {
|
|
|
|
if (event?.relatedTarget) {
|
|
|
|
const thisInputTrigger = this.inputElem.closest('.ember-basic-dropdown-trigger');
|
|
|
|
const relatedInputTrigger = event.relatedTarget.closest('.ember-basic-dropdown-trigger');
|
|
|
|
|
|
|
|
if (relatedInputTrigger !== thisInputTrigger) {
|
|
|
|
this.args.select.actions.search('');
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 19:41:44 +03:00
|
|
|
if (this.args.extra?.value && this.args.select.searchText === this.args.extra.value) {
|
2021-08-20 16:26:36 +03:00
|
|
|
this.args.select.actions.search('');
|
|
|
|
this.close();
|
|
|
|
}
|
2021-08-26 19:19:00 +03:00
|
|
|
|
|
|
|
this.args.onBlur?.(...arguments);
|
2021-08-20 16:26:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
closeWhenEmpty() {
|
|
|
|
if (document.activeElement === this.inputElem) {
|
|
|
|
if (this.args.extra?.closeWhenEmpty) {
|
|
|
|
if (this.args.select.results.length > 0) {
|
|
|
|
this.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.args.select.results.length === 0) {
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
open() {
|
|
|
|
if (!this.args.select.isOpen) {
|
|
|
|
// second argument skips ember-basic-dropdown focus
|
|
|
|
this.args.select.actions.open(null, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
if (this.args.select.isOpen) {
|
|
|
|
// second argument skips ember-basic-dropdown focus
|
|
|
|
this.args.select.actions.close(null, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|