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.

248 lines
7.1 KiB

5 years ago
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='/_assets/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='/_assets/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)', @click.middle="goToPageInNewTab(item)", :key='item.id', :class='idx === cursor ? `highlighted` : ``')
  22. v-list-item-avatar(tile)
  23. img(src='/_assets/svg/icon-selective-highlighting.svg')
  24. v-list-item-content
  25. v-list-item-title(v-text='item.title')
  26. v-list-item-subtitle.caption(v-text='item.description')
  27. .caption.grey--text(v-text='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-text='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 && newValue.length < 2)) {
  101. this.response.results = []
  102. this.response.suggestions = []
  103. this.searchIsLoading = false
  104. } else {
  105. this.searchIsLoading = true
  106. }
  107. }
  108. },
  109. mounted() {
  110. this.$root.$on('searchMove', (dir) => {
  111. this.cursor += ((dir === 'up') ? -1 : 1)
  112. if (this.cursor < -1) {
  113. this.cursor = -1
  114. } else if (this.cursor > this.results.length + this.suggestions.length - 1) {
  115. this.cursor = this.results.length + this.suggestions.length - 1
  116. }
  117. })
  118. this.$root.$on('searchEnter', () => {
  119. if (!this.results) {
  120. return
  121. }
  122. if (this.cursor >= 0 && this.cursor < this.results.length) {
  123. this.goToPage(_.nth(this.results, this.cursor))
  124. } else if (this.cursor >= 0) {
  125. this.setSearchTerm(_.nth(this.suggestions, this.cursor - this.results.length))
  126. }
  127. })
  128. },
  129. methods: {
  130. setSearchTerm(term) {
  131. this.search = term
  132. },
  133. goToPage(item) {
  134. window.location.assign(`/${item.locale}/${item.path}`)
  135. },
  136. goToPageInNewTab(item) {
  137. window.open(`/${item.locale}/${item.path}`, '_blank')
  138. }
  139. },
  140. apollo: {
  141. response: {
  142. query: searchPagesQuery,
  143. variables() {
  144. return {
  145. query: this.search
  146. }
  147. },
  148. fetchPolicy: 'network-only',
  149. debounce: 300,
  150. throttle: 1000,
  151. skip() {
  152. return !this.search || this.search.length < 2
  153. },
  154. update: (data) => _.get(data, 'pages.search', {}),
  155. watchLoading (isLoading) {
  156. this.searchIsLoading = isLoading
  157. }
  158. }
  159. }
  160. }
  161. </script>
  162. <style lang="scss">
  163. .search-results {
  164. position: fixed;
  165. top: 64px;
  166. left: 0;
  167. overflow-y: auto;
  168. width: 100%;
  169. height: calc(100% - 64px);
  170. background-color: rgba(0,0,0,.9);
  171. z-index: 100;
  172. text-align: center;
  173. animation: searchResultsReveal .6s ease;
  174. @media #{map-get($display-breakpoints, 'sm-and-down')} {
  175. top: 112px;
  176. }
  177. &-container {
  178. margin: 12px auto;
  179. width: 90vw;
  180. max-width: 1024px;
  181. }
  182. &-help {
  183. text-align: center;
  184. padding: 32px 0;
  185. font-size: 18px;
  186. font-weight: 300;
  187. color: #FFF;
  188. img {
  189. width: 104px;
  190. }
  191. }
  192. &-loader {
  193. display: flex;
  194. justify-content: center;
  195. align-items: center;
  196. flex-direction: column;
  197. padding: 32px 0;
  198. color: #FFF;
  199. }
  200. &-none {
  201. color: #FFF;
  202. img {
  203. width: 200px;
  204. }
  205. }
  206. &-items {
  207. text-align: left;
  208. .highlighted {
  209. background: #FFF linear-gradient(to bottom, #FFF, mc('orange', '100'));
  210. @at-root .theme--dark & {
  211. background: mc('grey', '900') linear-gradient(to bottom, mc('orange', '900'), darken(mc('orange', '900'), 15%));
  212. }
  213. }
  214. }
  215. &-suggestions {
  216. .highlighted {
  217. background: transparent linear-gradient(to bottom, mc('blue', '500'), mc('blue', '700'));
  218. }
  219. }
  220. }
  221. @keyframes searchResultsReveal {
  222. 0% {
  223. background-color: rgba(0,0,0,0);
  224. padding-top: 32px;
  225. }
  226. 100% {
  227. background-color: rgba(0,0,0,.9);
  228. padding-top: 0;
  229. }
  230. }
  231. </style>