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.

245 lines
7.0 KiB

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='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='/_assets/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 && 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. },
  137. apollo: {
  138. response: {
  139. query: searchPagesQuery,
  140. variables() {
  141. return {
  142. query: this.search
  143. }
  144. },
  145. fetchPolicy: 'network-only',
  146. debounce: 300,
  147. throttle: 1000,
  148. skip() {
  149. return !this.search || this.search.length < 2
  150. },
  151. update: (data) => _.get(data, 'pages.search', {}),
  152. watchLoading (isLoading) {
  153. this.searchIsLoading = isLoading
  154. }
  155. }
  156. }
  157. }
  158. </script>
  159. <style lang="scss">
  160. .search-results {
  161. position: fixed;
  162. top: 64px;
  163. left: 0;
  164. overflow-y: auto;
  165. width: 100%;
  166. height: calc(100% - 64px);
  167. background-color: rgba(0,0,0,.9);
  168. z-index: 100;
  169. text-align: center;
  170. animation: searchResultsReveal .6s ease;
  171. @media #{map-get($display-breakpoints, 'sm-and-down')} {
  172. top: 112px;
  173. }
  174. &-container {
  175. margin: 12px auto;
  176. width: 90vw;
  177. max-width: 1024px;
  178. }
  179. &-help {
  180. text-align: center;
  181. padding: 32px 0;
  182. font-size: 18px;
  183. font-weight: 300;
  184. color: #FFF;
  185. img {
  186. width: 104px;
  187. }
  188. }
  189. &-loader {
  190. display: flex;
  191. justify-content: center;
  192. align-items: center;
  193. flex-direction: column;
  194. padding: 32px 0;
  195. color: #FFF;
  196. }
  197. &-none {
  198. color: #FFF;
  199. img {
  200. width: 200px;
  201. }
  202. }
  203. &-items {
  204. text-align: left;
  205. .highlighted {
  206. background: #FFF linear-gradient(to bottom, #FFF, mc('orange', '100'));
  207. @at-root .theme--dark & {
  208. background: mc('grey', '900') linear-gradient(to bottom, mc('orange', '900'), darken(mc('orange', '900'), 15%));
  209. }
  210. }
  211. }
  212. &-suggestions {
  213. .highlighted {
  214. background: transparent linear-gradient(to bottom, mc('blue', '500'), mc('blue', '700'));
  215. }
  216. }
  217. }
  218. @keyframes searchResultsReveal {
  219. 0% {
  220. background-color: rgba(0,0,0,0);
  221. padding-top: 32px;
  222. }
  223. 100% {
  224. background-color: rgba(0,0,0,.9);
  225. padding-top: 0;
  226. }
  227. }
  228. </style>