2020-06-25 13:51:52 +00:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import { tagName } from '@ember-decorators/component';
|
|
|
|
import { computed, get } from '@ember/object';
|
|
|
|
import { alias } from '@ember/object/computed';
|
|
|
|
|
|
|
|
@tagName('')
|
|
|
|
export default class GlobalSearchMatch extends Component {
|
|
|
|
@alias('match.fuzzySearchMatches.firstObject') firstMatch;
|
|
|
|
|
|
|
|
@computed('match.name')
|
|
|
|
get label() {
|
|
|
|
return get(this, 'match.name') || '';
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:26:52 +00:00
|
|
|
@computed('label', 'firstMatch.indices.[]')
|
|
|
|
get substrings() {
|
|
|
|
const indices = get(this, 'firstMatch.indices');
|
|
|
|
const labelLength = this.label.length;
|
2020-06-25 13:51:52 +00:00
|
|
|
|
2020-06-25 20:26:52 +00:00
|
|
|
if (indices) {
|
|
|
|
return indices.reduce((substrings, [startIndex, endIndex], indicesIndex) => {
|
|
|
|
if (indicesIndex === 0 && startIndex > 0) {
|
|
|
|
substrings.push({
|
|
|
|
isHighlighted: false,
|
|
|
|
string: this.label.substring(0, startIndex)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
substrings.push({
|
|
|
|
isHighlighted: true,
|
|
|
|
string: this.label.substring(startIndex, endIndex + 1)
|
|
|
|
});
|
|
|
|
|
|
|
|
let endIndexOfNextUnhighlightedSubstring;
|
|
|
|
|
|
|
|
if (indicesIndex === indices.length - 1) {
|
|
|
|
endIndexOfNextUnhighlightedSubstring = labelLength;
|
|
|
|
} else {
|
|
|
|
const nextIndices = indices[indicesIndex + 1];
|
|
|
|
endIndexOfNextUnhighlightedSubstring = nextIndices[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
substrings.push({
|
|
|
|
isHighlighted: false,
|
|
|
|
string: this.label.substring(endIndex + 1, endIndexOfNextUnhighlightedSubstring)
|
|
|
|
});
|
2020-06-25 13:51:52 +00:00
|
|
|
|
2020-06-25 20:26:52 +00:00
|
|
|
return substrings;
|
|
|
|
}, []);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2020-06-25 13:51:52 +00:00
|
|
|
}
|
|
|
|
}
|