You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
6.9 KiB

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