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.

252 lines
7.2 KiB

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