github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/app/components/global-search/match.js (about)

     1  import Component from '@ember/component';
     2  import { tagName } from '@ember-decorators/component';
     3  import { computed, get } from '@ember/object';
     4  import { alias } from '@ember/object/computed';
     5  
     6  @tagName('')
     7  export default class GlobalSearchMatch extends Component {
     8    @alias('match.fuzzySearchMatches.firstObject') firstMatch;
     9  
    10    @computed('match.name')
    11    get label() {
    12      return get(this, 'match.name') || '';
    13    }
    14  
    15    @computed('firstMatch.indices.[]', 'label.length')
    16    get substrings() {
    17      const indices = get(this, 'firstMatch.indices');
    18      const labelLength = this.label.length;
    19  
    20      if (indices) {
    21        return indices.reduce((substrings, [startIndex, endIndex], indicesIndex) => {
    22          if (indicesIndex === 0 && startIndex > 0) {
    23            substrings.push({
    24              isHighlighted: false,
    25              string: this.label.substring(0, startIndex)
    26            });
    27          }
    28  
    29          substrings.push({
    30            isHighlighted: true,
    31            string: this.label.substring(startIndex, endIndex + 1)
    32          });
    33  
    34          let endIndexOfNextUnhighlightedSubstring;
    35  
    36          if (indicesIndex === indices.length - 1) {
    37            endIndexOfNextUnhighlightedSubstring = labelLength;
    38          } else {
    39            const nextIndices = indices[indicesIndex + 1];
    40            endIndexOfNextUnhighlightedSubstring = nextIndices[0];
    41          }
    42  
    43          substrings.push({
    44            isHighlighted: false,
    45            string: this.label.substring(endIndex + 1, endIndexOfNextUnhighlightedSubstring)
    46          });
    47  
    48          return substrings;
    49        }, []);
    50      } else {
    51        return null;
    52      }
    53    }
    54  }