search-results.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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)')
  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. orbit-spinner(
  9. :animation-duration='1000'
  10. :size='100'
  11. color='#FFF'
  12. )
  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)
  20. template(v-for='(item, idx) of results')
  21. v-list-item(@click='goToPage(item)', :key='item.id', :class='idx === cursor ? `highlighted` : ``')
  22. v-list-item-avatar(tile)
  23. img(src='/svg/icon-selective-highlighting.svg')
  24. v-list-item-content
  25. v-list-item-title(v-html='item.title')
  26. v-list-item-subtitle(v-html='item.description')
  27. .caption.grey--text(v-html='item.path')
  28. v-list-item-action
  29. v-chip(label, outlined) {{item.locale.toUpperCase()}}
  30. v-divider(v-if='idx < results.length - 1')
  31. v-pagination.mt-3(
  32. v-if='paginationLength > 1'
  33. dark
  34. v-model='pagination'
  35. :length='paginationLength'
  36. )
  37. template(v-if='suggestions && suggestions.length > 0')
  38. v-subheader.white--text.mt-3 {{$t('common:header.searchDidYouMean')}}
  39. v-list.search-results-suggestions.radius-7(dense, dark)
  40. template(v-for='(term, idx) of suggestions')
  41. v-list-item(:key='term', @click='setSearchTerm(term)', :class='idx + results.length === cursor ? `highlighted` : ``')
  42. v-list-item-avatar
  43. v-icon mdi-magnify
  44. v-list-item-content
  45. v-list-item-title(v-html='term')
  46. v-divider(v-if='idx < suggestions.length - 1')
  47. .text-xs-center.pt-5(v-if='search && search.length > 1')
  48. //- v-btn.mx-2(outlined, color='orange', @click='search = ``', v-if='results.length > 0')
  49. //- v-icon(left) mdi-content-save
  50. //- span {{$t('common:header.searchCopyLink')}}
  51. v-btn.mx-2(outlined, color='pink', @click='search = ``')
  52. v-icon(left) mdi-close
  53. span {{$t('common:header.searchClose')}}
  54. </template>
  55. <script>
  56. import _ from 'lodash'
  57. import { sync } from 'vuex-pathify'
  58. import { OrbitSpinner } from 'epic-spinners'
  59. import searchPagesQuery from 'gql/common/common-pages-query-search.gql'
  60. export default {
  61. components: {
  62. OrbitSpinner
  63. },
  64. data() {
  65. return {
  66. cursor: 0,
  67. pagination: 1,
  68. response: {
  69. results: [],
  70. suggestions: [],
  71. totalHits: 0
  72. }
  73. }
  74. },
  75. computed: {
  76. search: sync('site/search'),
  77. searchIsFocused: sync('site/searchIsFocused'),
  78. searchIsLoading: sync('site/searchIsLoading'),
  79. searchRestrictLocale: sync('site/searchRestrictLocale'),
  80. searchRestrictPath: sync('site/searchRestrictPath'),
  81. results() {
  82. return this.response.results ? this.response.results : []
  83. },
  84. hits() {
  85. return this.response.totalHits ? this.response.totalHits : 0
  86. },
  87. suggestions() {
  88. return this.response.suggestions ? this.response.suggestions : []
  89. },
  90. paginationLength() {
  91. return (this.response.totalHits > 0) ? 0 : Math.ceil(this.response.totalHits / 10)
  92. }
  93. },
  94. watch: {
  95. search(newValue, oldValue) {
  96. this.cursor = 0
  97. if (newValue && newValue.length < 2) {
  98. this.response.results = []
  99. this.response.suggestions = []
  100. } else {
  101. this.searchIsLoading = true
  102. }
  103. }
  104. },
  105. mounted() {
  106. this.$root.$on('searchMove', (dir) => {
  107. this.cursor += ((dir === 'up') ? -1 : 1)
  108. if (this.cursor < -1) {
  109. this.cursor = -1
  110. } else if (this.cursor > this.results.length + this.suggestions.length - 1) {
  111. this.cursor = this.results.length + this.suggestions.length - 1
  112. }
  113. })
  114. this.$root.$on('searchEnter', () => {
  115. if (!this.results) {
  116. return
  117. }
  118. if (this.cursor >= 0 && this.cursor < this.results.length) {
  119. this.goToPage(_.nth(this.results, this.cursor))
  120. } else if (this.cursor >= 0) {
  121. this.setSearchTerm(_.nth(this.suggestions, this.cursor - this.results.length))
  122. }
  123. })
  124. },
  125. methods: {
  126. setSearchTerm(term) {
  127. this.search = term
  128. },
  129. goToPage(item) {
  130. window.location.assign(`/${item.locale}/${item.path}`)
  131. }
  132. },
  133. apollo: {
  134. response: {
  135. query: searchPagesQuery,
  136. variables() {
  137. return {
  138. query: this.search
  139. }
  140. },
  141. fetchPolicy: 'network-only',
  142. debounce: 300,
  143. throttle: 1000,
  144. skip() {
  145. return !this.search || this.search.length < 2
  146. },
  147. update: (data) => _.get(data, 'pages.search', {}),
  148. watchLoading (isLoading) {
  149. this.searchIsLoading = isLoading
  150. }
  151. }
  152. }
  153. }
  154. </script>
  155. <style lang="scss">
  156. .search-results {
  157. position: fixed;
  158. top: 64px;
  159. left: 0;
  160. width: 100%;
  161. height: calc(100% - 64px);
  162. background-color: rgba(0,0,0,.9);
  163. z-index: 100;
  164. text-align: center;
  165. animation: searchResultsReveal .6s ease;
  166. @media #{map-get($display-breakpoints, 'sm-and-down')} {
  167. top: 112px;
  168. }
  169. &-container {
  170. margin: 12px auto;
  171. width: 90vw;
  172. max-width: 1024px;
  173. }
  174. &-help {
  175. text-align: center;
  176. padding: 32px 0;
  177. font-size: 18px;
  178. font-weight: 300;
  179. color: #FFF;
  180. img {
  181. width: 104px;
  182. }
  183. }
  184. &-loader {
  185. display: flex;
  186. justify-content: center;
  187. align-items: center;
  188. flex-direction: column;
  189. padding: 32px 0;
  190. color: #FFF;
  191. }
  192. &-none {
  193. color: #FFF;
  194. img {
  195. width: 200px;
  196. }
  197. }
  198. &-items {
  199. text-align: left;
  200. .highlighted {
  201. background: #FFF linear-gradient(to bottom, #FFF, mc('orange', '100'));
  202. @at-root .theme--dark & {
  203. background: mc('grey', '900') linear-gradient(to bottom, mc('orange', '900'), darken(mc('orange', '900'), 15%));
  204. }
  205. }
  206. }
  207. &-suggestions {
  208. .highlighted {
  209. background: transparent linear-gradient(to bottom, mc('blue', '500'), mc('blue', '700'));
  210. }
  211. }
  212. }
  213. @keyframes searchResultsReveal {
  214. 0% {
  215. background-color: rgba(0,0,0,0);
  216. padding-top: 32px;
  217. }
  218. 100% {
  219. background-color: rgba(0,0,0,.9);
  220. padding-top: 0;
  221. }
  222. }
  223. </style>