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