-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathsearch-input.js
73 lines (56 loc) · 1.68 KB
/
search-input.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* eslint-disable ember/no-get */
import { inject as service } from '@ember/service';
import Component from '@glimmer/component';
import { get } from '@ember/object';
import { isPresent } from '@ember/utils';
import { task, timeout } from 'ember-concurrency';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
const SEARCH_DEBOUNCE_PERIOD = 300;
const SEARCH_CLOSE_PERIOD = 200;
export default class SearchInput extends Component {
@tracked query = '';
@tracked _focused = false;
@service('search') searchService;
_resultTetherConstraints = null;
constructor() {
super(...arguments);
this._resultTetherConstraints = [
{
to: 'window',
pin: ['left', 'right'],
},
];
}
get queryIsPresent() {
return isPresent(this.query);
}
@task({ restartable: true }) *search(query) {
yield timeout(SEARCH_DEBOUNCE_PERIOD);
this.query = query;
// Hide and don't run query if there's no search query
if (!query) {
this._focused = false;
return;
}
// ensure search results are visible if the menu was previously closed above
this._focused = true;
yield get(this, 'searchService.search').perform(query);
}
@task *closeMenu() {
yield timeout(SEARCH_CLOSE_PERIOD);
this._focused = false;
}
@action onfocus() {
if (this.query.length > 0 && this.searchService.hasStaleResults()) {
// clearing the results avoids a flash of stale content while the search
// is performed
this.searchService.clearResults();
this.searchService.search.perform(this.query);
}
this._focused = true;
}
@action onblur() {
this.closeMenu.perform();
}
}