Search.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <template>
  2. <div class="search-box">
  3. <div class="input-group">
  4. <select @change="onSelectTypeChange" class="form-control select-type select-adder">
  5. <option value="">产品</option>
  6. <option value="">店铺</option>
  7. </select>
  8. <input v-model="keyword" type="text" class="search-input form-control input-primary"
  9. placeholder="型号/类目/品牌"
  10. @focus.stop.prevent="onFocus()"
  11. @blur.stop.prevent="onBlur()"
  12. @keyup.40="onSelectChange(1)"
  13. @keyup.38="onSelectChange(-1)"
  14. @keyup.13="onSearch()"/>
  15. <span class="input-group-btn" @click="onSearch()">
  16. <button class="btn btn-primary search-btn" type="button">搜&nbsp;索</button>
  17. </span>
  18. </div>
  19. <ul class="association" v-show="showAssociate && searchType == 'product'"
  20. @mouseenter="associate.focus=true" @mouseleave="associate.focus=false">
  21. <li v-for="(k, index) in similarKeywords.data" :key="k" class="item"
  22. :class="{'active': index==associate.activeIndex}"
  23. @click.stop.prevent="onAssociateClick(k)">{{ k }}
  24. </li>
  25. </ul>
  26. <div class="search-hot">
  27. <ul class="list-untyled">
  28. <li class="item item-first">热门搜索</li>
  29. <li class="item" v-for="w in hotwords">
  30. <nuxt-link :to="w.url" target="_blank">{{ w.name }}</nuxt-link>
  31. </li>
  32. </ul>
  33. </div>
  34. </div>
  35. </template>
  36. <script>
  37. export default {
  38. name: 'search-box',
  39. data () {
  40. return {
  41. keyword: '',
  42. associate: {
  43. focus: false,
  44. show: false,
  45. activeIndex: null
  46. },
  47. click_flag: false,
  48. searchType: 'product'
  49. }
  50. },
  51. computed: {
  52. similarKeywords () {
  53. return this.$store.state.search.keywords
  54. },
  55. showAssociate () {
  56. return this.keyword && this.associate.show && this.similarKeywords.data && this.similarKeywords.data.length
  57. }
  58. },
  59. props: {
  60. hotwords: {
  61. type: Array,
  62. default () {
  63. return [{
  64. name: 'SCT2080KEC',
  65. url: '/product/component/1100400300009990'
  66. }, {
  67. name: '电池组',
  68. url: '/product/kind/346'
  69. }, {
  70. name: 'Vishay',
  71. url: '/product/brand/30327265e42a871be050007f01003d96'
  72. }, {
  73. name: 'Panasonic Battery',
  74. url: '/product/brand/30327265e4e7871be050007f01003d96'
  75. }]
  76. }
  77. }
  78. },
  79. watch: {
  80. 'keyword': {
  81. handler (val, oldVal) {
  82. let keywords = this.similarKeywords.data
  83. if (!keywords || !keywords.length || this.associate.activeIndex === null || val !== keywords[this.associate.activeIndex]) {
  84. this.onChange()
  85. }
  86. }
  87. }
  88. },
  89. methods: {
  90. onSelectTypeChange: function (e) {
  91. let type = e.target[e.target.selectedIndex].innerHTML
  92. if (type === '产品') {
  93. this.searchType = 'product'
  94. } else if (type === '店铺') {
  95. this.searchType = 'store'
  96. }
  97. },
  98. onFocus () {
  99. this.associate.show = true
  100. },
  101. onBlur () {
  102. this.associate.show = this.associate.focus
  103. },
  104. onSelectChange (count) {
  105. let keywords = this.similarKeywords.data
  106. if (keywords && keywords.length) {
  107. let index = this.associate.activeIndex
  108. if (index === null) {
  109. index = -1
  110. }
  111. index += count
  112. if (index >= keywords.length) {
  113. index = 0
  114. } else if (index < 0) {
  115. index = keywords.length - 1
  116. }
  117. this.associate.activeIndex = index
  118. this.keyword = keywords[index]
  119. }
  120. },
  121. onChange () {
  122. this.associate.activeIndex = null
  123. if (!this.keyword) {
  124. this.associate.show = false
  125. this.$store.dispatch('resetSearchKeywords')
  126. } else {
  127. this.searchKeywords()
  128. }
  129. if (this.click_flag) {
  130. this.associate.show = false
  131. this.click_flag = false
  132. }
  133. },
  134. searchKeywords () {
  135. this.associate.show = true
  136. this.$store.dispatch('searchKeywords', { keyword: this.keyword })
  137. },
  138. onSearch () {
  139. if (this.keyword) {
  140. this.associate.show = false
  141. this.$store.dispatch('resetSearchKeywords')
  142. if (this.searchType === 'product') {
  143. this.$router.push({path: '/search?w=' + encodeURIComponent(this.keyword)})
  144. } else if (this.searchType === 'store') {
  145. this.$router.push({path: '/searchStore?w=' + encodeURIComponent(this.keyword)})
  146. }
  147. }
  148. },
  149. onAssociateClick (word) {
  150. this.click_flag = true
  151. this.keyword = word
  152. this.onSearch()
  153. }
  154. },
  155. created () {
  156. this.$store.dispatch('resetSearchKeywords')
  157. }
  158. }
  159. </script>
  160. <style lang="scss" scoped>
  161. @import '~assets/scss/variables';
  162. .form-control{
  163. border-radius: 0;
  164. }
  165. .search-box {
  166. width: 520px;
  167. height: 40px;
  168. position: relative;
  169. .search-input{
  170. width: 364px;
  171. float: left;
  172. }
  173. .search-input, .search-btn {
  174. height: 40px;
  175. border-width: 2px;
  176. }
  177. .select-type{
  178. width: 78px;
  179. float: left;
  180. border: #5078cb 2px solid;
  181. height: 40px;
  182. border-right: none;
  183. }
  184. .search-btn {
  185. font-size: 16px;
  186. width: 78px;
  187. border-radius: 0;
  188. }
  189. .search-hot ul li a{
  190. color: #838383;
  191. }
  192. .search-hot {
  193. .item {
  194. display: inline-block;
  195. font-size: $font-size-small;
  196. margin-right: $pad;
  197. &.item-first {
  198. color: $red;
  199. }
  200. }
  201. }
  202. .association {
  203. position: absolute;
  204. left: 78px;
  205. top: 100%;
  206. right: 79px;
  207. background: $white;
  208. border: $border;
  209. border-top-width: 0;
  210. z-index: 21;
  211. .item {
  212. padding: 0 15px;
  213. line-height: 30px;
  214. cursor: pointer;
  215. &.active {
  216. background-color: $dark-bg;
  217. }
  218. &:hover {
  219. background-color: $grey-bg;
  220. }
  221. }
  222. }
  223. }
  224. </style>