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

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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')" autocomplete="off"
  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. static: { type: Boolean, default: false },
  69. staticTopicsData: { type: String, default: '[]' },
  70. },
  71. components: {},
  72. data() {
  73. return {
  74. searchInputValue: '',
  75. topicColors: COLOR_LIST,
  76. defaultColor: '#F6F6F6',
  77. selectedColor: '',
  78. selectTopic: '',
  79. topicOri: [],
  80. topics: [],
  81. };
  82. },
  83. methods: {
  84. setDefaultSearch(params) {
  85. this.searchInputValue = params.q || '';
  86. this.selectTopic = params.topic || '';
  87. this.selectTopic && this.changeTopic({
  88. k: this.selectTopic.toLocaleLowerCase(),
  89. v: this.selectTopic,
  90. }, true);
  91. },
  92. changeTopic(topicItem, noSearch) {
  93. const index_ori = this.topicOri.findIndex((item) => {
  94. return item.k == this.selectTopic.toLocaleLowerCase();
  95. });
  96. if (index_ori < 0 && this.selectTopic) {
  97. const index = this.topics.findIndex((item) => {
  98. return item.k == this.selectTopic.toLocaleLowerCase();
  99. });
  100. if (index > -1) {
  101. this.topics.splice(index, 1);
  102. }
  103. }
  104. this.selectTopic = topicItem.v;
  105. if (this.selectTopic && this.topics.indexOf(this.selectTopic) < 0) {
  106. const index = this.topics.findIndex(item => {
  107. return item.k == this.selectTopic.toLocaleLowerCase();
  108. })
  109. if (index < 0) {
  110. this.topics.push({
  111. k: this.selectTopic.toLocaleLowerCase(),
  112. v: this.selectTopic,
  113. });
  114. }
  115. }
  116. !noSearch && this.search();
  117. },
  118. handlerTopicsData(data) {
  119. try {
  120. const topicsData = JSON.parse(data);
  121. const topics = topicsData.map((item) => {
  122. return {
  123. k: item.toLocaleLowerCase(),
  124. v: item.trim(),
  125. }
  126. });
  127. this.topicOri = JSON.parse(JSON.stringify(topics));
  128. this.topics = topics;
  129. const selectTopic_key = this.selectTopic.toLocaleLowerCase();
  130. if (selectTopic_key) {
  131. const index = this.topics.findIndex((item) => {
  132. return item.k == selectTopic_key;
  133. });
  134. if (index < 0) {
  135. this.topics.push({
  136. k: this.selectTopic.toLocaleLowerCase(),
  137. v: this.selectTopic,
  138. });
  139. }
  140. }
  141. } catch (err) {
  142. console.log(err);
  143. }
  144. },
  145. search() {
  146. this.searchInputValue = this.searchInputValue.trim();
  147. if (this.type == 'square') {
  148. window.location.href = `/explore/repos?q=${this.searchInputValue}&sort=${this.sort}&topic=${this.selectTopic}`;
  149. } else {
  150. this.$emit('change', {
  151. q: this.searchInputValue,
  152. topic: this.selectTopic,
  153. });
  154. }
  155. }
  156. },
  157. mounted() {
  158. if (this.static) {
  159. try {
  160. this.handlerTopicsData(this.staticTopicsData);
  161. } catch (err) {
  162. console.log(err);
  163. }
  164. } else {
  165. getPromoteData('/repos/recommend_topics').then(res => {
  166. const data = res.data;
  167. this.handlerTopicsData(data);
  168. }).catch(err => {
  169. console.log(err);
  170. this.handlerTopicsData('[]');
  171. });
  172. }
  173. },
  174. };
  175. </script>
  176. <style scoped lang="less">
  177. ._repo_search {
  178. margin: 54px 0;
  179. }
  180. ._repo_search_input_c {
  181. margin: 0 0 35px;
  182. display: flex;
  183. justify-content: center;
  184. align-items: center;
  185. }
  186. ._repo_search_input {
  187. display: flex;
  188. align-items: center;
  189. width: 437px;
  190. height: 41px;
  191. border-color: rgba(47, 9, 69, 0.64);
  192. border-width: 1px;
  193. border-style: solid;
  194. color: rgba(16, 16, 16, 0.5);
  195. border-radius: 20px;
  196. font-size: 14px;
  197. padding: 20px;
  198. text-align: left;
  199. line-height: 20px;
  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.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");
  201. }
  202. ._repo_search_input input {
  203. width: 100%;
  204. height: 30px;
  205. border: none;
  206. background: transparent;
  207. outline: none;
  208. }
  209. ._repo_search_btn {
  210. margin-left: 10px;
  211. width: 110px;
  212. height: 40px;
  213. border-style: none;
  214. border-color: unset;
  215. color: rgb(255, 255, 255);
  216. border-radius: 21px;
  217. font-size: 14px;
  218. text-align: center;
  219. font-weight: normal;
  220. font-style: normal;
  221. 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");
  222. display: flex;
  223. align-items: center;
  224. justify-content: center;
  225. cursor: pointer;
  226. }
  227. ._repo_search_label_c {
  228. display: flex;
  229. justify-content: center;
  230. }
  231. ._repo_search_label {
  232. display: flex;
  233. justify-content: center;
  234. flex-wrap: wrap;
  235. max-width: 1200px;
  236. }
  237. ._repo_search_label a {
  238. background-color: rgb(2, 202, 253);
  239. color: rgb(255, 255, 255);
  240. border-radius: 5px;
  241. margin: 0 5px 10px 5px;
  242. padding: 5px 10px;
  243. cursor: pointer;
  244. font-size: 12px;
  245. }
  246. </style>