# All files / src Searchbar.vue

0% Statements 0/51
0% Branches 0/30
0% Functions 0/18
0% Lines 0/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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

     <script>
    import typeahead from './Typeahead.vue';
    import searchbarPageItem from './SearchbarPageItem.vue';

    export default {
      extends: typeahead,
      computed: {
        primitiveData() {
          // Returns the total number of matches between an array of regex patterns and string search targets.
          function getTotalMatches(searchTargets, regexes) {
            const searchTarget = searchTargets.join(' ');

            return regexes.reduce((total, regex) => {
              const matches = searchTarget.match(regex);
              return total + (matches ? matches.length : 0);
            }, 0);
          }

          if (this.value.length < 2 || !this.data) {
            return [];
          }
          const pages = [];
          const regexes = this.value.split(' ')
            .filter((searchKeyword) => searchKeyword !== '')
            .map((searchKeyword) => searchKeyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
            .map((searchKeyword) => new RegExp(searchKeyword, 'ig'));
          this.data.forEach((entry) => {
            const {
              headings,
              src,
              title,
              headingKeywords,
            } = entry;
            const keywords = entry.keywords || '';
            const displayTitle = title || src;

            const pageSearchTargets = [
              displayTitle,
              keywords,
              ...Object.values(headings),
              ...Object.values(headingKeywords),
            ];
            const totalPageMatches = getTotalMatches(pageSearchTargets, regexes);

            if (totalPageMatches > 0) {
              const pageHeadings = [];
              Object.entries(headings).forEach(([id, text]) => {
                const matchesHeading = regexes.some((regex) => regex.test(text));
                const matchesKeywords = headingKeywords[id] && headingKeywords[id]
                  .some((keyword) => regexes.some((regex) => regex.test(keyword)));

                if (matchesHeading || matchesKeywords) {
                  const headingSearchTargets = [
                    text,
                    ...(headingKeywords[id] || []),
                  ];
                  const totalHeadingMatches = getTotalMatches(headingSearchTargets, regexes);

                  pageHeadings.push({
                    heading: { id, text },
                    keywords: headingKeywords[id],
                    src,
                    totalMatches: totalHeadingMatches,
                  });
                }
              });
              pageHeadings.sort((a, b) => b.totalMatches - a.totalMatches);

              pages.push({
                headings: pageHeadings,
                keywords,
                src,
                title: displayTitle,
                totalMatches: totalPageMatches,
              });
            }
          });

          return pages
            .sort((a, b) => b.totalMatches - a.totalMatches)
            .flatMap((page) => {
              if (page.headings) {
                return [page, ...page.headings];
              }
              return page;
            });
        },
        entryTemplate() {
          return 'searchbarPageItem';
        },
      },
      methods: {
        down() {
          if (this.current < this.items.length - 1) {
            this.current += 1;
            this.scrollListView();
          }
        },
        up() {
          if (this.current > 0) {
            this.current -= 1;
            this.scrollListView();
          }
        },
        scrollListView() {
          const { dropdown } = this.$refs;
          const currentEntry = dropdown.children[this.current];
          const upperBound = dropdown.scrollTop;
          const lowerBound = upperBound + dropdown.clientHeight;
          const currentEntryOffsetBottom = currentEntry.offsetTop + currentEntry.offsetHeight;
          if (currentEntry.offsetTop < upperBound) {
            dropdown.scrollTop = currentEntry.offsetTop;
          } else if (currentEntryOffsetBottom > lowerBound) {
            dropdown.scrollTop = currentEntryOffsetBottom - dropdown.clientHeight;
          }
        },
      },
      components: {
        searchbarPageItem,
      },
    };
    </script>

    <style>
    .search-dropdown-menu {
      min-width: 30em;
      max-height: 30em;
      overflow-y: scroll;
    }

    @media screen and (max-width: 768px) {
      .search-dropdown-menu {
        min-width: auto;
        max-height: 30em;
        overflow-y: scroll;
      }
    }
    </style>