You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

SearchBar.vue 8.6 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <div>
  3. <div class="_repo_search">
  4. <div class="_repo_search_input_c">
  5. <div class="_repo_search_input">
  6. <input type="text" v-model="searchInputValue" :placeholder="$t('repos.searchRepositories')"
  7. @keyup.enter="search" />
  8. </div>
  9. <div class="_repo_search_btn" @click="search">
  10. <svg xmlns="http://www.w3.org/2000/svg"
  11. class="styles__StyledSVGIconPathComponent-sc-16fsqc8-0 kdvdTY svg-icon-path-icon fill" viewBox="0 0 32 32"
  12. width="24" height="24">
  13. <defs data-reactroot="">
  14. <linearGradient id="ilac9fwnydq3lcx1,1,rs,1,f0dwf0xj,ezu9f0dw,f000,00lwrsktrs,rs1bhv8urs" x1="0" x2="100%"
  15. y1="0" y2="0"
  16. gradientTransform="matrix(-0.7069999999999999, -0.707, 0.707, -0.7069999999999999, 16, 38.624)"
  17. gradientUnits="userSpaceOnUse">
  18. <stop stop-color="#c9ffbf" stop-opacity="1" offset="0"></stop>
  19. <stop stop-color="#0ca451" stop-opacity="1" offset="1"></stop>
  20. </linearGradient>
  21. </defs>
  22. <g>
  23. <path fill="url(#ilac9fwnydq3lcx1,1,rs,1,f0dwf0xj,ezu9f0dw,f000,00lwrsktrs,rs1bhv8urs)"
  24. d="M14.667 2.667c6.624 0 12 5.376 12 12s-5.376 12-12 12-12-5.376-12-12 5.376-12 12-12zM14.667 24c5.156 0 9.333-4.177 9.333-9.333 0-5.157-4.177-9.333-9.333-9.333-5.157 0-9.333 4.176-9.333 9.333 0 5.156 4.176 9.333 9.333 9.333zM25.98 24.095l3.772 3.771-1.887 1.887-3.771-3.772 1.885-1.885z">
  25. </path>
  26. </g>
  27. </svg>
  28. <span style="margin-left:10px;">{{ $t('repos.search') }}</span>
  29. </div>
  30. </div>
  31. <div class="_repo_search_label_c">
  32. <div class="_repo_search_label">
  33. <a v-if="type == 'square'" :href="`/explore/repos?q=${searchInputValue.trim()}&topic=${item.v}&sort=${sort}`"
  34. :style="{ backgroundColor: topicColors[index % topicColors.length] }" v-for="(item, index) in topics"
  35. :key="index">{{ item.v }}</a>
  36. <a v-if="type == 'search'" href="javascript:;" @click="changeTopic({ k: '', v: '' })"
  37. style="color:#40485b;font-weight:bold;"
  38. :style="{ backgroundColor: selectTopic == '' ? selectedColor : defaultColor }">{{ $t('repos.allFields')
  39. }}</a>
  40. <a v-if="type == 'search'" href="javascript:;" @click="changeTopic(item)" style="color:#40485b;"
  41. :style="{ backgroundColor: selectTopic.toLocaleLowerCase() == item.k ? selectedColor : defaultColor }"
  42. v-for="(item, index) in topics" :key="index">{{ item.v }}</a>
  43. </div>
  44. </div>
  45. </div>
  46. </div>
  47. </template>
  48. <script>
  49. import { getPromoteData } from '~/apis/modules/common';
  50. const COLOR_LIST = [
  51. 'rgb(255, 104, 104)',
  52. 'rgb(22, 132, 252)',
  53. 'rgb(2, 202, 253)',
  54. 'rgb(164, 145, 215)',
  55. 'rgb(232, 64, 247)',
  56. 'rgb(245, 182, 110)',
  57. 'rgb(54, 187, 166)',
  58. 'rgb(123, 50, 178)'
  59. ];
  60. export default {
  61. name: "SearchBar",
  62. props: {
  63. type: { type: String, default: 'square' }, // square|search
  64. searchValue: { type: String, default: '' },
  65. topic: { type: String, default: '' },
  66. sort: { type: String, default: '' },
  67. },
  68. components: {},
  69. data() {
  70. return {
  71. searchInputValue: '',
  72. topicColors: COLOR_LIST,
  73. defaultColor: '#F6F6F6',
  74. selectedColor: '',
  75. selectTopic: '',
  76. topicOri: [],
  77. topics: [],
  78. };
  79. },
  80. methods: {
  81. setDefaultSearch(params) {
  82. this.searchInputValue = params.q || '';
  83. this.selectTopic = params.topic || '';
  84. },
  85. changeTopic(topicItem) {
  86. const index_ori = this.topicOri.findIndex((item) => {
  87. return item.k == this.selectTopic.toLocaleLowerCase();
  88. });
  89. if (index_ori < 0 && this.selectTopic) {
  90. const index = this.topics.findIndex((item) => {
  91. return item.k == this.selectTopic.toLocaleLowerCase();
  92. });
  93. this.topics.splice(index, 1);
  94. }
  95. this.selectTopic = topicItem.v;
  96. if (this.selectTopic && this.topics.indexOf(this.selectTopic) < 0) {
  97. const index = this.topics.findIndex(item => {
  98. return item.k == this.selectTopic.toLocaleLowerCase();
  99. })
  100. if (index < 0) {
  101. this.topics.push({
  102. k: this.selectTopic.toLocaleLowerCase(),
  103. v: this.selectTopic,
  104. });
  105. }
  106. }
  107. this.search();
  108. },
  109. search() {
  110. if (this.type == 'square') {
  111. window.location.href = `/explore/repos?q=${this.searchInputValue}&sort=${this.sort}&topic=${this.selectTopic}`;
  112. } else {
  113. this.$emit('change', {
  114. q: this.searchInputValue,
  115. topic: this.selectTopic,
  116. });
  117. }
  118. }
  119. },
  120. watch: {},
  121. mounted() {
  122. getPromoteData('/repos/recommend_topics').then(res => {
  123. const data = res.data;
  124. try {
  125. const topicsData = JSON.parse(data);
  126. const topics = topicsData.map((item) => {
  127. return {
  128. k: item.toLocaleLowerCase(),
  129. v: item,
  130. }
  131. });
  132. this.topicOri = JSON.parse(JSON.stringify(topics));
  133. this.topics = topics;
  134. const selectTopic_key = this.selectTopic.toLocaleLowerCase();
  135. if (selectTopic_key) {
  136. const index = this.topics.findIndex((item) => {
  137. return item.k == selectTopic_key;
  138. });
  139. if (index < 0) {
  140. this.topics.push({
  141. k: this.selectTopic.toLocaleLowerCase(),
  142. v: this.selectTopic,
  143. });
  144. }
  145. }
  146. } catch (err) {
  147. console.log(err);
  148. }
  149. }).catch(err => {
  150. console.log(err);
  151. });
  152. },
  153. };
  154. </script>
  155. <style scoped lang="less">
  156. ._repo_search {
  157. margin: 54px 0;
  158. }
  159. ._repo_search_input_c {
  160. margin: 0 0 35px;
  161. display: flex;
  162. justify-content: center;
  163. align-items: center;
  164. }
  165. ._repo_search_input {
  166. display: flex;
  167. align-items: center;
  168. width: 437px;
  169. height: 41px;
  170. border-color: rgba(47, 9, 69, 0.64);
  171. border-width: 1px;
  172. border-style: solid;
  173. color: rgba(16, 16, 16, 0.5);
  174. border-radius: 20px;
  175. font-size: 14px;
  176. padding: 20px;
  177. text-align: left;
  178. line-height: 20px;
  179. background: url("data:image/svg+xml;charset=utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20version%3D%221.1%22%3E%3Cdefs%3E%3ClinearGradient%20id%3D%221%22%20x1%3D%220%22%20x2%3D%221%22%20y1%3D%220%22%20y2%3D%220%22%20gradientTransform%3D%22matrix(6.123233995736765e-17%2C%20-0.9999999999999999%2C%200.008802475794500678%2C%206.123233995736765e-17%2C%201%2C%201.003)%22%3E%3Cstop%20stop-color%3D%22%23eeeade%22%20stop-opacity%3D%220.2%22%20offset%3D%220.76%22%3E%3C%2Fstop%3E%3Cstop%20stop-color%3D%22%23ece0e9%22%20stop-opacity%3D%221%22%20offset%3D%221%22%3E%3C%2Fstop%3E%3C%2FlinearGradient%3E%3C%2Fdefs%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22url(%231)%22%3E%3C%2Frect%3E%3C%2Fsvg%3E");
  180. }
  181. ._repo_search_input input {
  182. width: 100%;
  183. height: 30px;
  184. border: none;
  185. background: transparent;
  186. outline: none;
  187. }
  188. ._repo_search_btn {
  189. margin-left: 10px;
  190. width: 110px;
  191. height: 40px;
  192. border-style: none;
  193. border-color: unset;
  194. color: rgb(255, 255, 255);
  195. border-radius: 21px;
  196. font-size: 14px;
  197. text-align: center;
  198. font-weight: normal;
  199. font-style: normal;
  200. background: url("data:image/svg+xml;charset=utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20version%3D%221.1%22%3E%3Cdefs%3E%3ClinearGradient%20id%3D%221%22%20x1%3D%220%22%20x2%3D%221%22%20y1%3D%220%22%20y2%3D%220%22%20gradientTransform%3D%22matrix(6.123233995736766e-17%2C%201%2C%20-0.17728531855955676%2C%206.123233995736766e-17%2C%201%2C%200)%22%3E%3Cstop%20stop-color%3D%22%232f0945%22%20stop-opacity%3D%221%22%20offset%3D%220%22%3E%3C%2Fstop%3E%3Cstop%20stop-color%3D%22%23341a7b%22%20stop-opacity%3D%221%22%20offset%3D%221%22%3E%3C%2Fstop%3E%3C%2FlinearGradient%3E%3C%2Fdefs%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22url(%231)%22%3E%3C%2Frect%3E%3C%2Fsvg%3E");
  201. display: flex;
  202. align-items: center;
  203. justify-content: center;
  204. cursor: pointer;
  205. }
  206. ._repo_search_label_c {
  207. display: flex;
  208. justify-content: center;
  209. }
  210. ._repo_search_label {
  211. display: flex;
  212. justify-content: center;
  213. flex-wrap: wrap;
  214. max-width: 1200px;
  215. }
  216. ._repo_search_label a {
  217. background-color: rgb(2, 202, 253);
  218. color: rgb(255, 255, 255);
  219. border-radius: 5px;
  220. margin: 0 5px 10px 5px;
  221. padding: 5px 10px;
  222. cursor: pointer;
  223. font-size: 12px;
  224. }
  225. </style>