Search.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <template>
  2. <div class="search-box" :class="{'search-box2': !SelectItem}">
  3. <div class="input-group">
  4. <template v-if="!isPcb">
  5. <select v-model="searchType" class="form-control select-type select-adder" v-if="SelectItem">
  6. <option value="product">产品</option>
  7. <option value="store">店铺</option>
  8. </select>
  9. <input v-model="keyword" type="text" class="search-input form-control input-primary"
  10. :placeholder="searchType === 'product' ? '品牌/物料名称/型号' : '店铺名称'"
  11. @focus.stop.prevent="onFocus()"
  12. @blur.stop.prevent="onBlur()"
  13. @keyup.40="onSelectChange(1)"
  14. @keyup.38="onSelectChange(-1)"
  15. @keyup.13="onSearch()"/>
  16. <span class="input-group-btn" @click="onSearch()" style="z-index: 10">
  17. <button class="btn btn-primary search-btn" type="button" :class="{'Isblue':!SelectItem}">搜&nbsp;索</button>
  18. </span>
  19. </template>
  20. <template v-if="isPcb">
  21. <input v-model="keyword" type="text" class="search-input form-control input-primary"
  22. :placeholder="'请输入pcb型号'"
  23. @focus.stop.prevent="onFocus()"
  24. @blur.stop.prevent="onBlur()"
  25. style="width: 441px;"
  26. @keyup.13="onSearch()"/>
  27. <span class="input-group-btn" @click="onSearch()" style="z-index: 10">
  28. <button class="btn btn-primary search-btn" type="button" :class="{'Isblue':!SelectItem}">搜&nbsp;索</button>
  29. </span>
  30. </template>
  31. </div>
  32. <ul class="association"
  33. :class="{'association2': !SelectItem, 'pcb-asso': isPcb}"
  34. v-show="showAssociate && searchType == 'product'"
  35. @mouseenter="associate.focus=true"
  36. @mouseleave="associate.focus=false">
  37. <li v-if="similarKeywords.data.component && similarKeywords.data.component.length > 0" class="similar-title">型号:</li>
  38. <li v-for="(k, index) in similarKeywords.data.component" class="item"
  39. :class="{'active': index==associate.activeIndex}"
  40. @click.stop.prevent="onAssociateClick(k.code)">{{ k.code }}
  41. </li>
  42. <li v-if="similarKeywords.data.brand && similarKeywords.data.brand.length > 0" class="similar-title">品牌:</li>
  43. <li v-for="(k, index) in similarKeywords.data.brand" class="item"
  44. :class="{'active': index==associate.activeIndex}"
  45. @click.stop.prevent="onAssociateClick(isCnStart() ? k.nameCn : k.nameEn)">{{ isCnStart() ? k.nameCn : k.nameEn }}
  46. </li>
  47. <li v-if="similarKeywords.data.kind && similarKeywords.data.kind.length > 0" class="similar-title">物料名称(类目):</li>
  48. <li v-for="(k, index) in similarKeywords.data.kind" class="item"
  49. :class="{'active': index==associate.activeIndex}"
  50. @click.stop.prevent="onAssociateClick(k.nameCn)">{{ k.nameCn }}
  51. </li>
  52. </ul>
  53. <div class="search-hot" v-if="SelectItem">
  54. <ul class="list-untyled">
  55. <li class="item item-first">热门搜索</li>
  56. <li class="item" v-for="w in hotBrand" v-if="ifFloorsHotSearchInValid">
  57. <nuxt-link :to="'/product/brand/' + w.uuid" target="_blank">{{ w.nameEn }}</nuxt-link>
  58. </li>
  59. <li class="item" v-for="w in hotDevice" v-if="ifFloorsHotSearchInValid">
  60. <nuxt-link :to="'/product/component/' + w.uuid" target="_blank">{{ w.code }}</nuxt-link>
  61. </li>
  62. <li class="item" v-if="!ifFloorsHotSearchInValid && index > 0" v-for="(w, index) in hotSearchData.items">
  63. <a :href="w.hrefUrl" target="_blank">{{w.body}}</a>
  64. </li>
  65. </ul>
  66. </div>
  67. </div>
  68. </template>
  69. <script>
  70. export default {
  71. name: 'search-box',
  72. props: {
  73. SelectItem: {
  74. type: Boolean,
  75. default: true
  76. },
  77. isPcb: {
  78. type: Boolean,
  79. default: false
  80. }
  81. },
  82. data () {
  83. return {
  84. keyword: '',
  85. associate: {
  86. focus: false,
  87. show: false,
  88. activeIndex: null
  89. },
  90. click_flag: false,
  91. searchType: 'product'
  92. }
  93. },
  94. computed: {
  95. hotDevice () {
  96. return this.$store.state.hotSearchDevice.hot.data
  97. },
  98. hotBrand () {
  99. return this.$store.state.hotSearchBrand.hot.data
  100. },
  101. similarKeywords () {
  102. return this.$store.state.search.keywords
  103. },
  104. showAssociate () {
  105. return this.keyword &&
  106. this.associate.show &&
  107. this.similarKeywords.data &&
  108. (this.similarKeywords.data.brand || this.similarKeywords.data.component || this.similarKeywords.data.kind)
  109. },
  110. hotSearchData () {
  111. let list = this.$store.state.floor.list_v3.data
  112. let obj = {}
  113. if (list && list.length) {
  114. for (let i = 0; i < list.length; i++) {
  115. if (list[i].floorNumber === 2) {
  116. obj = list[i]
  117. }
  118. }
  119. // obj = list.find(item => item.floorNumber === 2) || {}
  120. }
  121. return obj
  122. },
  123. ifFloorsHotSearchInValid () {
  124. let obj = this.hotSearchData
  125. let tmp = false
  126. if (obj.items && obj.items.length) {
  127. for (let i = 0; i < obj.items.length; i++) {
  128. if (!obj.items[i].body || obj.items[i].body === '') {
  129. tmp = obj.items[i]
  130. break
  131. }
  132. }
  133. // let result = obj.items.find(item => !item.body || item.body === '') || true
  134. return tmp || true
  135. }
  136. return true
  137. }
  138. },
  139. watch: {
  140. 'keyword': {
  141. handler (val, oldVal) {
  142. let keywords = this.similarKeywords.data
  143. if (!keywords || !keywords.length || this.associate.activeIndex === null || val !== keywords[this.associate.activeIndex]) {
  144. this.onChange()
  145. }
  146. }
  147. }
  148. },
  149. methods: {
  150. // onSelectTypeChange: function (e) {
  151. // let type = e.target[e.target.selectedIndex].innerHTML
  152. // if (type === '产品') {
  153. // this.searchType = 'product'
  154. // } else if (type === '店铺') {
  155. // this.searchType = 'store'
  156. // }
  157. // },
  158. onFocus () {
  159. this.associate.show = true
  160. },
  161. onBlur () {
  162. this.associate.show = this.associate.focus
  163. },
  164. onSelectChange (count) {
  165. let keywords = this.similarKeywords.data
  166. if (keywords && keywords.length) {
  167. let index = this.associate.activeIndex
  168. if (index === null) {
  169. index = -1
  170. }
  171. index += count
  172. if (index >= keywords.length) {
  173. index = 0
  174. } else if (index < 0) {
  175. index = keywords.length - 1
  176. }
  177. this.associate.activeIndex = index
  178. this.keyword = keywords[index]
  179. }
  180. },
  181. onChange () {
  182. this.associate.activeIndex = null
  183. if (!this.keyword) {
  184. this.associate.show = false
  185. this.$store.dispatch('resetSearchKeywords')
  186. } else {
  187. this.searchKeywords()
  188. }
  189. if (this.click_flag) {
  190. this.associate.show = false
  191. this.click_flag = false
  192. }
  193. },
  194. searchKeywords () {
  195. if (this.searchType === 'product') {
  196. this.associate.show = true
  197. this.$store.dispatch('searchKeywords', { keyword: this.keyword })
  198. }
  199. },
  200. onSearch () {
  201. document.getElementsByClassName('search-input')[0].blur()
  202. if (this.keyword) {
  203. this.associate.show = false
  204. this.$store.dispatch('resetSearchKeywords')
  205. if (this.isPcb) {
  206. this.$router.push({path: '/pcb/search?w=' + encodeURIComponent(this.keyword)})
  207. } else {
  208. if (this.searchType === 'product') {
  209. this.$router.push({path: '/search?w=' + encodeURIComponent(this.keyword)})
  210. } else if (this.searchType === 'store') {
  211. this.$router.push({path: '/searchStore?w=' + encodeURIComponent(this.keyword)})
  212. }
  213. }
  214. }
  215. },
  216. onAssociateClick (word) {
  217. this.click_flag = true
  218. this.keyword = word
  219. this.onSearch()
  220. },
  221. isCnStart () {
  222. if (this.keyword && this.keyword.length > 0) {
  223. return this.keyword.charCodeAt(0) > 255
  224. }
  225. }
  226. },
  227. created () {
  228. this.$store.dispatch('resetSearchKeywords')
  229. }
  230. }
  231. </script>
  232. <style lang="scss" scoped type="text/scss">
  233. @import '~assets/scss/variables';
  234. .form-control{
  235. border-radius: 0;
  236. }
  237. .search-box {
  238. width: 520px;
  239. height: 40px;
  240. position: relative;
  241. .search-input{
  242. width: 372px;
  243. float: left;
  244. }
  245. .search-input, .search-btn {
  246. height: 40px;
  247. border-width: 2px;
  248. }
  249. .select-type{
  250. width: 70px;
  251. float: left;
  252. border: #5078cb 2px solid;
  253. height: 40px;
  254. border-right: none;
  255. margin-right: -1px;
  256. }
  257. .search-btn {
  258. font-size: 16px;
  259. width: 79px;
  260. border-radius: 0;
  261. }
  262. .Isblue{
  263. vertical-align: middle;
  264. display: inline-block;
  265. text-align: center;
  266. width: 72px;
  267. border-left: 0;
  268. background: #d3e1fc;
  269. color: #5078cb;
  270. text-align: center;
  271. font-size: 14px;
  272. position: absolute;
  273. right: 0;
  274. top: 0;
  275. &:hover {
  276. background: #d2272d;
  277. color: #fff;
  278. transition: all .3s;
  279. }
  280. }
  281. .search-hot ul{
  282. line-height: 12px;
  283. }
  284. .search-hot ul li a{
  285. color: #838383;
  286. }
  287. .search-hot {
  288. margin-top:5px;
  289. .item {
  290. display: inline-block;
  291. max-width:22%;
  292. text-align: center;
  293. vertical-align: middle;
  294. font-size: $font-size-small;
  295. padding-right: $pad;
  296. a{
  297. display:block;
  298. overflow: hidden;
  299. text-overflow: ellipsis;
  300. white-space: nowrap;
  301. }
  302. &.item-first {
  303. width:12%;
  304. color: $red;
  305. }
  306. }
  307. }
  308. .association {
  309. position: absolute;
  310. left: 69px;
  311. top: 100%;
  312. right: 81px;
  313. background: $white;
  314. border: $border;
  315. border-top-width: 0;
  316. z-index: 21;
  317. &.pcb-asso {
  318. left: 0;
  319. }
  320. .item {
  321. padding: 0 15px;
  322. line-height: 30px;
  323. cursor: pointer;
  324. &.active {
  325. background-color: $dark-bg;
  326. }
  327. &:hover {
  328. background-color: $grey-bg;
  329. }
  330. }
  331. .similar-title {
  332. padding: 0 15px;
  333. line-height: 30px;
  334. font-size: 16px;
  335. font-weight: bold;
  336. border-top: 1px solid #ccc;
  337. cursor: default;
  338. }
  339. }
  340. .association2 {
  341. left: 2px;
  342. right: 71px
  343. }
  344. }
  345. .search-box2 {
  346. width: 442px;
  347. }
  348. </style>