2019-06-18 13:47:21 +03:00
|
|
|
/* global key */
|
2020-05-18 00:35:53 +03:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import {action} from '@ember/object';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {isBlank} from '@ember/utils';
|
2016-04-19 18:55:10 +03:00
|
|
|
|
2020-05-18 00:35:53 +03:00
|
|
|
export default class GhSearchInputTrigger extends Component {
|
|
|
|
@action
|
|
|
|
registerInput(elem) {
|
|
|
|
this.inputElem = elem;
|
|
|
|
}
|
2016-04-19 18:55:10 +03:00
|
|
|
|
2020-05-18 00:35:53 +03:00
|
|
|
@action
|
|
|
|
captureMousedown(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
2016-04-19 18:55:10 +03:00
|
|
|
|
2020-05-18 00:35:53 +03:00
|
|
|
@action
|
|
|
|
search(event) {
|
|
|
|
let term = event.target.value;
|
2016-04-19 18:55:10 +03:00
|
|
|
|
2020-05-18 00:35:53 +03:00
|
|
|
// 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();
|
2016-04-19 18:55:10 +03:00
|
|
|
|
2020-05-18 00:35:53 +03:00
|
|
|
// ensure focus isn't lost when dropdown is closed
|
|
|
|
if (isBlank(term) && this.inputElem) {
|
|
|
|
this.inputElem.focus();
|
2016-04-19 18:55:10 +03:00
|
|
|
}
|
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
|
2020-05-18 00:35:53 +03:00
|
|
|
this.args.select.actions.search(term);
|
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
|
2020-05-18 00:35:53 +03:00
|
|
|
// hacky workaround to let Escape clear the input if there's text,
|
|
|
|
// but still allow it to close the search modal if there's no text
|
|
|
|
@action
|
|
|
|
handleKeydown(e) {
|
|
|
|
if ((e.key === 'Escape' && e.target.value) || e.key === 'Enter') {
|
|
|
|
this._previousKeyScope = key.getScope();
|
|
|
|
key.setScope('ignore');
|
|
|
|
}
|
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
|
2020-05-18 00:35:53 +03:00
|
|
|
@action
|
|
|
|
handleKeyup() {
|
|
|
|
if (key.getScope() === 'ignore') {
|
|
|
|
key.setScope(this._previousKeyScope);
|
2019-05-20 13:25:59 +03:00
|
|
|
}
|
2016-04-19 18:55:10 +03:00
|
|
|
}
|
2020-05-18 00:35:53 +03:00
|
|
|
|
|
|
|
open() {
|
|
|
|
this.args.select.actions.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
this.args.select.actions.close();
|
|
|
|
}
|
|
|
|
}
|