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