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.

230 lines
6.2 KiB

5 years ago
5 years ago
  1. <template lang="pug">
  2. .search-results(v-if='searchIsFocused || search.length > 1')
  3. .search-results-container
  4. .search-results-help(v-if='search.length < 2')
  5. img(src='/svg/icon-search-alt.svg')
  6. .mt-4 Type at least 2 characters to start searching...
  7. .search-results-loader(v-else-if='searchIsLoading && results.length < 1')
  8. orbit-spinner(
  9. :animation-duration='1000'
  10. :size='100'
  11. color='#FFF'
  12. )
  13. .headline.mt-5 Searching...
  14. .search-results-none(v-else-if='!searchIsLoading && results.length < 1')
  15. img(src='/svg/icon-no-results.svg', alt='No Results')
  16. .subheading No pages matching your query.
  17. template(v-if='results.length > 0')
  18. v-subheader.white--text Found {{response.totalHits}} results
  19. v-list.search-results-items.radius-7(two-line)
  20. template(v-for='(item, idx) of results')
  21. v-list-tile(@click='goToPage(item)', :key='item.id', :class='idx === cursor ? `highlighted` : ``')
  22. v-list-tile-avatar(tile)
  23. img(src='/svg/icon-selective-highlighting.svg')
  24. v-list-tile-content
  25. v-list-tile-title(v-html='item.title')
  26. v-list-tile-sub-title(v-html='item.description')
  27. .caption.grey--text.mt-1(v-html='item.path')
  28. v-list-tile-action
  29. v-chip(label) {{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.length > 0')
  38. v-subheader.white--text.mt-3 Did you mean...
  39. v-list.search-results-suggestions.radius-7(dense, dark)
  40. template(v-for='(term, idx) of suggestions')
  41. v-list-tile(:key='term', @click='setSearchTerm(term)', :class='idx + results.length === cursor ? `highlighted` : ``')
  42. v-list-tile-avatar
  43. v-icon search
  44. v-list-tile-content
  45. v-list-tile-title(v-html='term')
  46. v-divider(v-if='idx < suggestions.length - 1')
  47. .text-xs-center.pt-5(v-if='search.length > 1')
  48. v-btn(outline, color='orange', @click='search = ``', v-if='results.length > 0')
  49. v-icon(left) save
  50. span Copy Search Link
  51. v-btn(outline, color='pink', @click='search = ``')
  52. v-icon(left) clear
  53. span Close
  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.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.cursor >= 0 && this.cursor < this.results.length) {
  116. this.goToPage(_.nth(this.results, this.cursor))
  117. } else if (this.cursor >= 0) {
  118. this.setSearchTerm(_.nth(this.suggestions, this.cursor - this.results.length))
  119. }
  120. })
  121. },
  122. methods: {
  123. setSearchTerm(term) {
  124. this.search = term
  125. },
  126. goToPage(item) {
  127. window.location.assign(`/${item.locale}/${item.path}`)
  128. }
  129. },
  130. apollo: {
  131. response: {
  132. query: searchPagesQuery,
  133. variables() {
  134. return {
  135. query: this.search
  136. }
  137. },
  138. fetchPolicy: 'network-only',
  139. debounce: 300,
  140. throttle: 1000,
  141. skip() {
  142. return !this.search || this.search.length < 2
  143. },
  144. update: (data) => _.get(data, 'pages.search', {}),
  145. watchLoading (isLoading) {
  146. this.searchIsLoading = isLoading
  147. }
  148. }
  149. }
  150. }
  151. </script>
  152. <style lang="scss">
  153. .search-results {
  154. position: fixed;
  155. top: 64px;
  156. left: 0;
  157. width: 100%;
  158. height: calc(100% - 64px);
  159. background-color: rgba(0,0,0,.9);
  160. z-index: 100;
  161. text-align: center;
  162. animation: searchResultsReveal .6s ease;
  163. @media #{map-get($display-breakpoints, 'sm-and-down')} {
  164. top: 112px;
  165. }
  166. &-container {
  167. margin: 12px auto;
  168. width: 90vw;
  169. max-width: 1024px;
  170. }
  171. &-help {
  172. text-align: center;
  173. padding: 32px 0;
  174. font-size: 18px;
  175. font-weight: 300;
  176. color: #FFF;
  177. img {
  178. width: 104px;
  179. }
  180. }
  181. &-loader {
  182. display: flex;
  183. justify-content: center;
  184. align-items: center;
  185. flex-direction: column;
  186. padding: 32px 0;
  187. color: #FFF;
  188. }
  189. &-none {
  190. color: #FFF;
  191. img {
  192. width: 200px;
  193. }
  194. }
  195. &-items {
  196. .highlighted {
  197. background: #FFF linear-gradient(to bottom, #FFF, mc('orange', '100'));
  198. }
  199. }
  200. &-suggestions {
  201. .highlighted {
  202. background: transparent linear-gradient(to bottom, mc('blue', '500'), mc('blue', '700'));
  203. }
  204. }
  205. }
  206. @keyframes searchResultsReveal {
  207. 0% {
  208. background-color: rgba(0,0,0,0);
  209. padding-top: 32px;
  210. }
  211. 100% {
  212. background-color: rgba(0,0,0,.9);
  213. padding-top: 0;
  214. }
  215. }
  216. </style>