search-results.vue 6.9 KB
Newer Older
1
<template lang="pug">
2
  .search-results(v-if='searchIsFocused || (search && search.length > 1)')
3
    .search-results-container
4
      .search-results-help(v-if='!search || (search && search.length < 2)')
Nick's avatar
Nick committed
5
        img(src='/svg/icon-search-alt.svg')
6
        .mt-4 {{$t('common:header.searchHint')}}
7
      .search-results-loader(v-else-if='searchIsLoading && (!results || results.length < 1)')
8 9 10 11 12
        orbit-spinner(
          :animation-duration='1000'
          :size='100'
          color='#FFF'
        )
13
        .headline.mt-5 {{$t('common:header.searchLoading')}}
14
      .search-results-none(v-else-if='!searchIsLoading && (!results || results.length < 1)')
15
        img(src='/svg/icon-no-results.svg', alt='No Results')
16
        .subheading {{$t('common:header.searchNoResult')}}
17
      template(v-if='results && results.length > 0')
18
        v-subheader.white--text {{$t('common:header.searchResultsCount', { total: response.totalHits })}}
19
        v-list.search-results-items.radius-7.py-0(two-line, dense)
20
          template(v-for='(item, idx) of results')
21 22
            v-list-item(@click='goToPage(item)', :key='item.id', :class='idx === cursor ? `highlighted` : ``')
              v-list-item-avatar(tile)
23
                img(src='/svg/icon-selective-highlighting.svg')
24 25
              v-list-item-content
                v-list-item-title(v-html='item.title')
26
                v-list-item-subtitle.caption(v-html='item.description')
27 28 29
                .caption.grey--text(v-html='item.path')
              v-list-item-action
                v-chip(label, outlined) {{item.locale.toUpperCase()}}
30 31 32 33 34 35
            v-divider(v-if='idx < results.length - 1')
        v-pagination.mt-3(
          v-if='paginationLength > 1'
          dark
          v-model='pagination'
          :length='paginationLength'
36
          circle
37
        )
38
      template(v-if='suggestions && suggestions.length > 0')
39
        v-subheader.white--text.mt-3 {{$t('common:header.searchDidYouMean')}}
40
        v-list.search-results-suggestions.radius-7(dense, dark)
41
          template(v-for='(term, idx) of suggestions')
42 43 44 45 46
            v-list-item(:key='term', @click='setSearchTerm(term)', :class='idx + results.length === cursor ? `highlighted` : ``')
              v-list-item-avatar
                v-icon mdi-magnify
              v-list-item-content
                v-list-item-title(v-html='term')
47
            v-divider(v-if='idx < suggestions.length - 1')
48
      .text-xs-center.pt-5(v-if='search && search.length > 1')
Nick's avatar
Nick committed
49 50 51
        //- v-btn.mx-2(outlined, color='orange', @click='search = ``', v-if='results.length > 0')
        //-   v-icon(left) mdi-content-save
        //-   span {{$t('common:header.searchCopyLink')}}
52 53
        v-btn.mx-2(outlined, color='pink', @click='search = ``')
          v-icon(left) mdi-close
54
          span {{$t('common:header.searchClose')}}
55 56 57 58
</template>

<script>
import _ from 'lodash'
Nick's avatar
Nick committed
59
import { sync } from 'vuex-pathify'
60 61 62 63 64 65 66 67 68 69
import { OrbitSpinner } from 'epic-spinners'

import searchPagesQuery from 'gql/common/common-pages-query-search.gql'

export default {
  components: {
    OrbitSpinner
  },
  data() {
    return {
70
      cursor: 0,
71
      pagination: 1,
72
      perPage: 10,
73 74 75 76 77 78 79 80 81
      response: {
        results: [],
        suggestions: [],
        totalHits: 0
      }
    }
  },
  computed: {
    search: sync('site/search'),
Nick's avatar
Nick committed
82
    searchIsFocused: sync('site/searchIsFocused'),
83 84 85 86
    searchIsLoading: sync('site/searchIsLoading'),
    searchRestrictLocale: sync('site/searchRestrictLocale'),
    searchRestrictPath: sync('site/searchRestrictPath'),
    results() {
87 88
      const currentIndex = (this.pagination - 1) * this.perPage
      return this.response.results ? _.slice(this.response.results, currentIndex, currentIndex + this.perPage) : []
89 90 91 92 93 94 95 96
    },
    hits() {
      return this.response.totalHits ? this.response.totalHits : 0
    },
    suggestions() {
      return this.response.suggestions ? this.response.suggestions : []
    },
    paginationLength() {
97
      return (this.response.totalHits > 0) ? Math.ceil(this.response.totalHits / this.perPage) : 0
98 99
    }
  },
Nick's avatar
Nick committed
100 101
  watch: {
    search(newValue, oldValue) {
102
      this.cursor = 0
103
      if (newValue && newValue.length < 2) {
Nick's avatar
Nick committed
104
        this.response.results = []
105 106 107
        this.response.suggestions = []
      } else {
        this.searchIsLoading = true
Nick's avatar
Nick committed
108 109 110
      }
    }
  },
111 112
  mounted() {
    this.$root.$on('searchMove', (dir) => {
113
      this.cursor += ((dir === 'up') ? -1 : 1)
114 115 116 117 118 119 120
      if (this.cursor < -1) {
        this.cursor = -1
      } else if (this.cursor > this.results.length + this.suggestions.length - 1) {
        this.cursor = this.results.length + this.suggestions.length - 1
      }
    })
    this.$root.$on('searchEnter', () => {
121 122 123 124
      if (!this.results) {
        return
      }

125 126 127 128 129 130 131
      if (this.cursor >= 0 && this.cursor < this.results.length) {
        this.goToPage(_.nth(this.results, this.cursor))
      } else if (this.cursor >= 0) {
        this.setSearchTerm(_.nth(this.suggestions, this.cursor - this.results.length))
      }
    })
  },
Nick's avatar
Nick committed
132 133 134 135 136 137 138 139
  methods: {
    setSearchTerm(term) {
      this.search = term
    },
    goToPage(item) {
      window.location.assign(`/${item.locale}/${item.path}`)
    }
  },
140 141 142 143 144 145 146 147
  apollo: {
    response: {
      query: searchPagesQuery,
      variables() {
        return {
          query: this.search
        }
      },
Nick's avatar
Nick committed
148 149
      fetchPolicy: 'network-only',
      debounce: 300,
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
      throttle: 1000,
      skip() {
        return !this.search || this.search.length < 2
      },
      update: (data) => _.get(data, 'pages.search', {}),
      watchLoading (isLoading) {
        this.searchIsLoading = isLoading
      }
    }
  }
}
</script>

<style lang="scss">
.search-results {
  position: fixed;
  top: 64px;
  left: 0;
  width: 100%;
  height: calc(100% - 64px);
  background-color: rgba(0,0,0,.9);
  z-index: 100;
  text-align: center;
  animation: searchResultsReveal .6s ease;

  @media #{map-get($display-breakpoints, 'sm-and-down')} {
    top: 112px;
  }

  &-container {
    margin: 12px auto;
    width: 90vw;
    max-width: 1024px;
  }

Nick's avatar
Nick committed
185 186 187 188 189 190 191 192 193 194 195 196
  &-help {
    text-align: center;
    padding: 32px 0;
    font-size: 18px;
    font-weight: 300;
    color: #FFF;

    img {
      width: 104px;
    }
  }

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
  &-loader {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    padding: 32px 0;
    color: #FFF;
  }

  &-none {
    color: #FFF;

    img {
      width: 200px;
    }
  }
213 214

  &-items {
215 216
    text-align: left;

217
    .highlighted {
218
      background: #FFF linear-gradient(to bottom, #FFF, mc('orange', '100'));
219 220 221 222

      @at-root .theme--dark & {
        background: mc('grey', '900') linear-gradient(to bottom, mc('orange', '900'), darken(mc('orange', '900'), 15%));
      }
223 224 225 226 227
    }
  }

  &-suggestions {
    .highlighted {
228
      background: transparent linear-gradient(to bottom, mc('blue', '500'), mc('blue', '700'));
229 230
    }
  }
231 232 233 234 235 236 237 238 239 240 241 242 243
}

@keyframes searchResultsReveal {
  0% {
    background-color: rgba(0,0,0,0);
    padding-top: 32px;
  }
  100% {
    background-color: rgba(0,0,0,.9);
    padding-top: 0;
  }
}
</style>