Welcome to mirror list, hosted at ThFree Co, Russian Federation.

google_search_form.vue « components « search « frontend « content - gitlab.com/gitlab-org/gitlab-docs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2b0c8fe644478ff550cabd54c163175ca4e02ed1 (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<script>
import {
  GlSearchBoxByType,
  GlLink,
  GlSafeHtmlDirective as SafeHtml,
  GlTooltipDirective as GlTooltip,
} from '@gitlab/ui';
import { debounce } from 'lodash';
import { directive as clickOutside } from 'v-click-outside';
import { fetchResults, MAX_RESULTS_PER_PAGE } from '../../services/google_search_api';
import RecentHistory from './recently_viewed.vue';

export default {
  components: {
    GlSearchBoxByType,
    GlLink,
    RecentHistory,
  },
  directives: {
    clickOutside,
    SafeHtml,
    GlTooltip,
  },
  props: {
    borderless: {
      type: Boolean,
      required: true,
    },
  },
  data() {
    return {
      isLoading: false,
      moreResultsPath: '',
      results: [],
      searchQuery: '',
      showResultPanel: false,
      submitted: false,
      totalCount: 0,
      activeLink: -1,
      showTooltip: true,
      suggestion: '',
      historyItems: 0,
    };
  },
  computed: {
    hasMoreResults() {
      return this.results.length >= MAX_RESULTS_PER_PAGE;
    },
    hasNoResults() {
      return !this.results.length && this.submitted && this.searchQuery;
    },
    showHistory() {
      return !this.searchQuery;
    },
  },
  watch: {
    searchQuery() {
      this.showTooltip = this.searchQuery.length === 0;
      this.submitted = false;
      this.moreResultsPath = `/search/?q=${encodeURI(this.searchQuery)}`;
      this.debouncedGetResults();
    },
  },
  created() {
    this.debouncedGetResults = debounce(this.getResults, 500);
  },
  methods: {
    async getResults() {
      this.showResultPanel = false;
      this.isLoading = true;

      const query = this.suggestion ? this.suggestion : this.searchQuery;
      const response = await fetchResults(query, [], 1, 10);
      this.isLoading = false;
      this.suggestion = '';

      this.totalCount =
        response.searchInformation && response.searchInformation.totalCount
          ? response.searchInformation.totalCount
          : 0;
      this.results = response.items ? response.items : [];

      // If there were no results, try the spelling suggestion if present.
      if (!this.results.length && response.spelling) {
        this.suggestion = response.spelling.correctedQuery;
        this.getResults();
      }

      this.submitted = true;
      this.showResultPanel = true;
    },
    showAllResults() {
      // Sends the user to the advanced search page if they hit Enter.
      if (this.searchQuery) {
        window.location.href = this.moreResultsPath;
      }
    },
    keyboardNav(e) {
      const isArrowUp = e.key === 'ArrowUp';
      const isArrowDown = e.key === 'ArrowDown';
      const searchBox = document.querySelector('input[type=search]');

      if (isArrowUp || isArrowDown) {
        const activeIndex = this.activeLink + (isArrowUp ? -1 : 1);

        // If we're at the top or bottom of the list, go back to the search box.
        const listLength = this.searchQuery ? this.results.length : this.historyItems - 1;
        if (activeIndex < 0 || activeIndex > listLength) {
          this.activeLink = -1;
          searchBox.focus();
          // Reset the value after focus so that the cursor is at the end of the text.
          searchBox.value = this.searchQuery;
          return;
        }
        // Otherwise, select the previous or next link.
        this.setActiveResult(document.querySelector(`[data-link-index="${activeIndex}"]`));
      }
    },
    setActiveResult(result) {
      result.focus();
      this.activeLink = Number(result.dataset.linkIndex);
    },
    deactivate() {
      this.showResultPanel = false;
      this.activeLink = -1;
      this.showTooltip = this.searchQuery.length === 0;
    },
  },
};
</script>

<template>
  <div
    v-click-outside="() => deactivate()"
    class="gs-wrapper gl-m-auto gl-my-3 gl-md-mt-0 gl-md-mb-0"
    @keydown.arrow-down.prevent="keyboardNav"
    @keydown.arrow-up.prevent="keyboardNav"
    @keydown.escape="deactivate()"
  >
    <form class="gl-relative">
      <gl-search-box-by-type
        v-model="searchQuery"
        :is-loading="isLoading"
        :borderless="borderless"
        placeholder=""
        autocomplete="off"
        aria-label="Search"
        @focus="showResultPanel = true"
        @keydown.enter.prevent="showAllResults()"
      />
      <kbd
        v-show="showTooltip && !isLoading"
        v-gl-tooltip.bottom.hover.html
        class="gl-absolute gl-z-index-1 gl-bg-gray-100 gl-text-gray-700"
        title="Use the shortcut keys<br><kbd>/</kbd> or <kbd>s</kbd> to start a search"
        >/</kbd
      >
    </form>

    <div
      v-show="showResultPanel"
      class="gs-results gl-absolute gl-z-index-200 gl-bg-white gl-rounded gl-px-3 gl-shadow"
    >
      <ul v-show="results.length" data-testid="search-results" class="gl-pl-0 gl-mb-3 gl-pt-3">
        <li v-for="(result, index) in results" :key="result.cacheId" class="gl-list-style-none">
          <gl-link
            v-safe-html="result.formattedTitle"
            data-result-type="dropdown"
            :data-search-query="searchQuery"
            :href="result.relativeLink"
            :data-link-index="index"
            class="gl-text-gray-700 gl-py-3 gl-px-2 gl-display-block gl-text-left"
          />
        </li>
        <li v-if="hasMoreResults" class="gl-list-style-none gl-border-t gl-my-2 gl-py-2">
          <gl-link
            :data-link-index="results.length"
            data-testid="more-results"
            :href="moreResultsPath"
            class="gl-text-gray-700 gl-py-3 gl-pb-2 gl-px-2 gl-display-block gl-text-left"
          >
            See all results
          </gl-link>
        </li>
      </ul>
      <p
        v-if="hasNoResults && !suggestion"
        data-testid="no-results"
        class="gl-text-left gl-pt-3 gl-my-2 gl-pb-2"
      >
        No results found.
      </p>
      <recent-history v-if="showHistory" @pageHistoryInit="(items) => (historyItems = items)" />
    </div>
  </div>
</template>