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.7 kB

3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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="font-weight:bold;"
  38. :style="{ backgroundColor: selectTopic == '' ? selectedColor : defaultColor, color: selectTopic == '' ? 'white' : '#40485b' }">{{
  39. $t('repos.allFields')
  40. }}</a>
  41. <a v-if="type == 'search'" href="javascript:;" @click="changeTopic(item)"
  42. :style="{ backgroundColor: selectTopic.toLocaleLowerCase() == item.k ? selectedColor : defaultColor, color: selectTopic.toLocaleLowerCase() == item.k ? 'white' : '#40485b' }"
  43. v-for="(item, index) in topics" :key="index">{{ item.v }}</a>
  44. </div>
  45. </div>
  46. </div>
  47. </div>
  48. </template>
  49. <script>
  50. import { getPromoteData } from '~/apis/modules/common';
  51. const COLOR_LIST = [
  52. 'rgb(255, 104, 104)',
  53. 'rgb(22, 132, 252)',
  54. 'rgb(2, 202, 253)',
  55. 'rgb(164, 145, 215)',
  56. 'rgb(232, 64, 247)',
  57. 'rgb(245, 182, 110)',
  58. 'rgb(54, 187, 166)',
  59. 'rgb(123, 50, 178)'
  60. ];
  61. export default {
  62. name: "SearchBar",
  63. props: {
  64. type: { type: String, default: 'square' }, // square|search
  65. searchValue: { type: String, default: '' },
  66. topic: { type: String, default: '' },
  67. sort: { type: String, default: '' },
  68. },
  69. components: {},
  70. data() {
  71. return {
  72. searchInputValue: '',
  73. topicColors: COLOR_LIST,
  74. defaultColor: '#F6F6F6',
  75. selectedColor: '',
  76. selectTopic: '',
  77. topicOri: [],
  78. topics: [],
  79. };
  80. },
  81. methods: {
  82. setDefaultSearch(params) {
  83. this.searchInputValue = params.q || '';
  84. this.selectTopic = params.topic || '';
  85. },
  86. changeTopic(topicItem) {
  87. const index_ori = this.topicOri.findIndex((item) => {
  88. return item.k == this.selectTopic.toLocaleLowerCase();
  89. });
  90. if (index_ori < 0 && this.selectTopic) {
  91. const index = this.topics.findIndex((item) => {
  92. return item.k == this.selectTopic.toLocaleLowerCase();
  93. });
  94. this.topics.splice(index, 1);
  95. }
  96. this.selectTopic = topicItem.v;
  97. if (this.selectTopic && this.topics.indexOf(this.selectTopic) < 0) {
  98. const index = this.topics.findIndex(item => {
  99. return item.k == this.selectTopic.toLocaleLowerCase();
  100. })
  101. if (index < 0) {
  102. this.topics.push({
  103. k: this.selectTopic.toLocaleLowerCase(),
  104. v: this.selectTopic,
  105. });
  106. }
  107. }
  108. this.search();
  109. },
  110. search() {
  111. if (this.type == 'square') {
  112. window.location.href = `/explore/repos?q=${this.searchInputValue}&sort=${this.sort}&topic=${this.selectTopic}`;
  113. } else {
  114. this.$emit('change', {
  115. q: this.searchInputValue,
  116. topic: this.selectTopic,
  117. });
  118. }
  119. }
  120. },
  121. watch: {},
  122. mounted() {
  123. getPromoteData('/repos/recommend_topics').then(res => {
  124. const data = res.data;
  125. try {
  126. const topicsData = JSON.parse(data);
  127. const topics = topicsData.map((item) => {
  128. return {
  129. k: item.toLocaleLowerCase(),
  130. v: item,
  131. }
  132. });
  133. this.topicOri = JSON.parse(JSON.stringify(topics));
  134. this.topics = topics;
  135. const selectTopic_key = this.selectTopic.toLocaleLowerCase();
  136. if (selectTopic_key) {
  137. const index = this.topics.findIndex((item) => {
  138. return item.k == selectTopic_key;
  139. });
  140. if (index < 0) {
  141. this.topics.push({
  142. k: this.selectTopic.toLocaleLowerCase(),
  143. v: this.selectTopic,
  144. });
  145. }
  146. }
  147. } catch (err) {
  148. console.log(err);
  149. }
  150. }).catch(err => {
  151. console.log(err);
  152. });
  153. },
  154. };
  155. </script>
  156. <style scoped lang="less">
  157. ._repo_search {
  158. margin: 54px 0;
  159. }
  160. ._repo_search_input_c {
  161. margin: 0 0 35px;
  162. display: flex;
  163. justify-content: center;
  164. align-items: center;
  165. }
  166. ._repo_search_input {
  167. display: flex;
  168. align-items: center;
  169. width: 437px;
  170. height: 41px;
  171. border-color: rgba(47, 9, 69, 0.64);
  172. border-width: 1px;
  173. border-style: solid;
  174. color: rgba(16, 16, 16, 0.5);
  175. border-radius: 20px;
  176. font-size: 14px;
  177. padding: 20px;
  178. text-align: left;
  179. line-height: 20px;
  180. 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");
  181. }
  182. ._repo_search_input input {
  183. width: 100%;
  184. height: 30px;
  185. border: none;
  186. background: transparent;
  187. outline: none;
  188. }
  189. ._repo_search_btn {
  190. margin-left: 10px;
  191. width: 110px;
  192. height: 40px;
  193. border-style: none;
  194. border-color: unset;
  195. color: rgb(255, 255, 255);
  196. border-radius: 21px;
  197. font-size: 14px;
  198. text-align: center;
  199. font-weight: normal;
  200. font-style: normal;
  201. 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");
  202. display: flex;
  203. align-items: center;
  204. justify-content: center;
  205. cursor: pointer;
  206. }
  207. ._repo_search_label_c {
  208. display: flex;
  209. justify-content: center;
  210. }
  211. ._repo_search_label {
  212. display: flex;
  213. justify-content: center;
  214. flex-wrap: wrap;
  215. max-width: 1200px;
  216. }
  217. ._repo_search_label a {
  218. background-color: rgb(2, 202, 253);
  219. color: rgb(255, 255, 255);
  220. border-radius: 5px;
  221. margin: 0 5px 10px 5px;
  222. padding: 5px 10px;
  223. cursor: pointer;
  224. font-size: 12px;
  225. }
  226. </style>