-
-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathcomponent.js
88 lines (74 loc) · 2.86 KB
/
component.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import Component from '@ember/component';
import layout from './template';
import { computed } from '@ember/object';
export default Component.extend({
layout,
result: null,
'on-click'() {},
'on-mouse-enter'() {},
linkArgs: computed('result.document', function() {
let args = [];
let type = this.get('result.document.type');
if (type === 'template') {
args = [ this.get('result.document.route') ];
} else {
args = [ 'api.item', this.get('result.model.routingId') ];
}
return args;
}),
click() {
this.get('on-click')();
},
mouseEnter() {
this.get('on-mouse-enter')();
},
icon: computed(function() {
if (this.get('result.document.type') === 'template') {
return 'guide';
} else {
return 'api-item';
}
}),
matches: computed(function() {
let metadata = this.get('result.resultInfo.matchData.metadata');
return Object.keys(metadata).reduce((matches, term) => {
let match = metadata[term];
let query = this.get('query');
let normalizedQuery = query.toLowerCase();
Object.keys(match).forEach((key) => {
if (key === 'text') {
let text = this.get('result.document.text');
let spaceIndices = text.split("")
.map((char, index) => (char === ' ') ? index : null)
.filter(val => val > 0);
match.text.position.forEach(([ wordStart, length ]) => {
let spaceAfterWord = spaceIndices.find(i => i > wordStart);
let indexOfSpaceAfterWord = spaceIndices.indexOf(spaceAfterWord);
let indexOfSpaceBeforeWord = indexOfSpaceAfterWord - 1;
let indexOfStartingSpace = (indexOfSpaceBeforeWord > 3) ? indexOfSpaceBeforeWord - 3 : 0;
let indexOfEndingSpace = ((indexOfSpaceAfterWord + 3) < spaceIndices.length) ? indexOfSpaceAfterWord + 3 : spaceIndices.length;
let matchingText = text.slice(spaceIndices[indexOfStartingSpace], spaceIndices[indexOfEndingSpace]);
matchingText = this._highlight(matchingText, matchingText.indexOf(query), query.length);
matches.push(matchingText);
});
} else {
let normalizedTerm = term.toLowerCase();
this.get('result.document.keywords').forEach((keyword) => {
let normalizedKeyword = keyword.toLowerCase();
if (keyword.toLowerCase().indexOf(normalizedTerm) !== -1) {
let index = normalizedKeyword.indexOf(normalizedQuery);
matches.push(this._highlight(keyword, index, normalizedQuery.length));
}
});
}
});
return matches;
}, [])
.slice(0, 5)
.join(' · ');
}),
_highlight(text, start, length) {
return `${text.slice(0, start)}<em class='docs-bg-yellow'>${text.slice(start, start + length)}</em>${text.slice(start + length)}`;
},
'data-test-search-result': true,
});