-
-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathcomponent.js
More file actions
148 lines (117 loc) · 4.34 KB
/
component.js
File metadata and controls
148 lines (117 loc) · 4.34 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { inject as service } from '@ember/service';
import Component from '@ember/component';
import layout from './template';
import { EKMixin, keyUp, keyDown } from 'ember-keyboard';
import { on } from '@ember/object/evented';
import { computed } from '@ember/object';
import { task } from 'ember-concurrency';
import config from 'dummy/config/environment';
const projectName = config['ember-cli-addon-docs'].projectName;
export default Component.extend(EKMixin, {
layout,
docsSearch: service(),
router: service(),
store: service(),
query: null, // passed in
selectedIndex: null,
didInsertElement() {
this._super();
this.set('keyboardActivated', true);
// Start downloading the search index immediately
this.get('docsSearch').loadSearchIndex();
},
didReceiveAttrs() {
this._super(...arguments);
this.get('search').perform();
},
project: computed(function() {
return this.get('store').peekRecord('project', projectName);
}),
trimmedQuery: computed('query', function() {
return this.get('query').trim();
}),
search: task(function*() {
let results;
if (this.get('trimmedQuery')) {
results = yield this.get('docsSearch').search(this.get('trimmedQuery'));
}
this.set('selectedIndex', (results.length ? 0 : null));
this.set('rawSearchResults', results);
}).restartable(),
searchResults: computed('rawSearchResults.[]', function() {
let rawSearchResults = this.get('rawSearchResults');
let router = this.get('router');
let routerMicrolib = router._router._routerMicrolib || router._router.router;
if (rawSearchResults) {
return this.get('rawSearchResults')
// If the doc has a route, ensure it exists
.filter(({ document }) => {
if (document.route) {
let routeExists = routerMicrolib.recognizer.names[document.route];
return routeExists && document.route !== 'not-found' && document.route !== 'application';
} else {
return true;
}
})
// Filter out the templates of the API items' pages, since we handle them separately
.filter(({ document }) => {
let isApiItemTemplate = (document.route === 'api.item' && document.type === 'template');
return !isApiItemTemplate;
})
// Filter out modules that are not in the navigationIndex
.filter(({ document }) => {
if (document.type === 'module') {
let navigableModules = this.get('project.navigationIndex.modules').map(x => x.name);
return navigableModules.includes(document.title);
} else {
return true;
}
})
// Add a reference to the Ember Data model to each API item search result
.map(searchResult => {
let { document } = searchResult;
if (document.type !== 'template') {
let store = this.get('store');
searchResult.model = store.peekRecord(document.type, document.item.id)
}
return searchResult;
});
}
}),
gotoSelectedItem: on(keyUp('Enter'), function() {
if (this.get('selectedIndex') !== null) {
let selectedResult = this.get('searchResults')[this.get('selectedIndex')];
if (selectedResult.document.type === 'template') {
this.get('router').transitionTo(selectedResult.document.route);
} else {
this.get('router').transitionTo('api.item', selectedResult.model.get('routingId'));
}
}
this.get('on-visit')();
}),
nextSearchResult: on(keyDown('ctrl+KeyN'), keyDown('ArrowDown'), function() {
let hasSearchResults = this.get('searchResults.length');
let lastResultIsSelected = (this.get('selectedIndex') + 1 === this.get('searchResults.length'));
if (hasSearchResults && !lastResultIsSelected) {
this.incrementProperty('selectedIndex');
}
}),
previousSearchResult: on(keyDown('ctrl+KeyP'), keyDown('ArrowUp'), function() {
let hasSearchResults = this.get('searchResults.length');
let firstResultIsSelected = (this.get('selectedIndex') === 0);
if (hasSearchResults && !firstResultIsSelected) {
this.decrementProperty('selectedIndex');
}
}),
clearSearch() {
this.set('query', null);
},
actions: {
selectResult(index) {
this.set('selectedIndex', index);
},
clearSearch() {
this.clearSearch();
},
}
});