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.

EditTopics.vue 14 kB

4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. <template>
  2. <div>
  3. <div class="input-search">
  4. <el-input v-model="input" clearable :autofocus="true" @input="changeValue" id="topics_input" @keyup.enter.native="postTopic" placeholder="$t('message.search_or_create_tags')搜索或创建标签">
  5. </el-input>
  6. <div class="scrolling-menu">
  7. <div v-if="showSearchTopic" class="item-text" v-for="(arr,i) in array" @click="addTopics(i,arr)">
  8. <div class="icon-wrapper">
  9. <i style="line-height: 1.5;color: #303643;font-weight: 900;" v-if="showInitTopic[i]" class="el-icon-check" ></i>
  10. </div>
  11. <div class="text">{{arr.topic_name.toLowerCase()}} </div>
  12. </div>
  13. <div v-if="showInputValue" class="addition item-text" @click="postTopic">
  14. {{$t('message.click_or_enter_to_add')}}点击或回车添加<b class="user-add-label-text">{{input.toLowerCase()}}</b>标签
  15. </div>
  16. <div v-if="showAddTopic" class="item-text" @click="addPostTopic">
  17. <div class="icon-wrapper">
  18. <i style="line-height: 1.5;color: #303643;font-weight: 900;" v-if="showAddFlage" class="el-icon-check" ></i>
  19. </div>
  20. <div class="text">{{input.toLowerCase()}}</div>
  21. </div>
  22. </div>
  23. </div>
  24. </div>
  25. </template>
  26. <script>
  27. import enLocale from 'element-ui/lib/locale/lang/en'
  28. import zhLocale from 'element-ui/lib/locale/lang/zh-CN'
  29. import locale from 'element-ui/lib/locale'
  30. const {AppSubUrl, StaticUrlPrefix, csrf} = window.config;
  31. import $ from 'jquery'
  32. export default {
  33. data() {
  34. return {
  35. input:'',
  36. params:{},
  37. showInputValue:false,
  38. showFlag:-1,
  39. array:[],
  40. showAddTopic:false,
  41. showAddFlage:false,
  42. showSearchTopic:true,
  43. postUrl:'',
  44. arrayTopics:[],
  45. showInitTopic:[],
  46. };
  47. },
  48. methods: {
  49. addTopics(item,array){
  50. if(!this.showInitTopic[item]){
  51. if(this.arrayTopics.includes(array.topic_name)){
  52. return
  53. }
  54. else{
  55. this.arrayTopics.push(array.topic_name)
  56. let topics = this.arrayTopics
  57. let strTopics = topics.join(',')
  58. let data = this.qs.stringify({
  59. _csrf:csrf,
  60. topics:strTopics
  61. })
  62. this.Post(data,topics)
  63. this.$set(this.showInitTopic,item,true)
  64. $('#repo-topics1').children('span').remove()
  65. }
  66. }else{
  67. this.arrayTopics=this.arrayTopics.filter(ele=>{
  68. return ele !== array.topic_name
  69. })
  70. let topics = this.arrayTopics
  71. let strTopics = topics.join(',')
  72. let data = this.qs.stringify({
  73. _csrf:csrf,
  74. topics:strTopics
  75. })
  76. this.Post(data,topics)
  77. this.$set(this.showInitTopic,item,false)
  78. if(this.arrayTopics.length===0){
  79. $('#repo-topics1').append('<span class="no-description text-italic">暂无标签</span>')
  80. }else{
  81. $('#repo-topics1').children('span').remove()
  82. }
  83. }
  84. },
  85. changeValue(){
  86. if (this.input === ''){
  87. this.array = this.arrayTopics
  88. let data = []
  89. this.showInitTopic = []
  90. this.array.forEach((element,index) => {
  91. let item = {}
  92. item.topic_name = element
  93. data.push(item)
  94. this.showInitTopic.push(true)
  95. });
  96. this.array = data
  97. this.showInputValue = false
  98. this.showSearchTopic = true
  99. }
  100. else if(this.arrayTopics.indexOf(this.input.toLowerCase())>-1){
  101. this.showInputValue = false
  102. this.showSearchTopic = false
  103. }else{
  104. this.showInitTopic = []
  105. let timestamp=new Date().getTime()
  106. this.params.q = this.input.toLowerCase()
  107. this.params._ = timestamp
  108. this.$axios.get('/api/v1/topics/search',{
  109. params:this.params
  110. }).then((res)=>{
  111. this.array = res.data.topics
  112. this.array.forEach((element,index) => {
  113. if (this.arrayTopics.indexOf(element.topic_name)>-1){
  114. this.showInitTopic.push(true)
  115. }
  116. else{
  117. this.showInitTopic.push(false)
  118. }
  119. this.showInputValue = true
  120. });
  121. let findelement = this.array.some((item)=>{
  122. return item.topic_name===this.input.toLowerCase()
  123. })
  124. this.showInputValue = !findelement
  125. })
  126. this.showSearchTopic = true
  127. }
  128. this.showAddTopic = false
  129. },
  130. Post(data,topics){
  131. this.$axios.post(this.postUrl,data).then(res=>{
  132. const viewDiv = $('#repo-topics1');
  133. viewDiv.children('.topic').remove();
  134. if (topics.length) {
  135. const topicArray = topics;
  136. const last = viewDiv.children('a').last();
  137. for (let i = 0; i < topicArray.length; i++) {
  138. const link = $('<a class="ui repo-topic small label topic"></a>');
  139. link.attr(
  140. 'href',
  141. `${AppSubUrl}/explore/repos?q=${encodeURIComponent(
  142. topicArray[i]
  143. )}&topic=`
  144. );
  145. link.text(topicArray[i]);
  146. // link.insertBefore(last);
  147. viewDiv.append(link)
  148. }
  149. }
  150. viewDiv.show();
  151. })
  152. },
  153. postTopic(){
  154. if(!this.showInputValue){
  155. return
  156. }
  157. const patter = /^[\u4e00-\u9fa5a-zA-Z0-9][\u4e00-\u9fa5a-zA-Z0-9-]{0,34}$/
  158. let regexp = patter.test(this.input)
  159. if(!regexp){
  160. this.$notify({
  161. message: '标签名必须以中文、字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符',
  162. duration: 3000,
  163. type:'error'
  164. });
  165. return
  166. }else{
  167. let topic = this.input
  168. if(this.arrayTopics.includes(topic.toLowerCase())){
  169. return
  170. }
  171. else{
  172. this.arrayTopics.push(topic.toLowerCase())
  173. let topics = this.arrayTopics
  174. let strTopics = topics.join(',')
  175. let data = this.qs.stringify({
  176. _csrf:csrf,
  177. topics:strTopics
  178. })
  179. this.Post(data,topics)
  180. $('#repo-topics1').children('span').remove()
  181. this.showInputValue = false
  182. this.showAddTopic = true
  183. this.showAddFlage = true
  184. }
  185. }
  186. },
  187. addPostTopic(){
  188. if(this.showAddFlage){
  189. // this.arrayTopics.pop()
  190. let cancleIndex = this.arrayTopics.indexOf(this.input)
  191. this.arrayTopics.splice(cancleIndex,1)
  192. let topics = this.arrayTopics
  193. let strTopics = topics.join(',')
  194. let data = this.qs.stringify({
  195. _csrf:csrf,
  196. topics:strTopics
  197. })
  198. this.Post(data,topics)
  199. if(this.arrayTopics.length===0){
  200. $('#repo-topics1').append('<span class="no-description text-italic">暂无标签</span>')
  201. }else{
  202. $('#repo-topics1').children('span').remove()
  203. }
  204. }
  205. else if(!this.showAddFlage){
  206. let topic = this.input
  207. this.arrayTopics.push(topic.toLowerCase())
  208. let topics = this.arrayTopics
  209. let strTopics = topics.join(',')
  210. let data = this.qs.stringify({
  211. _csrf:csrf,
  212. topics:strTopics
  213. })
  214. this.Post(data,topics)
  215. $('#repo-topics1').children('span').remove()
  216. }
  217. this.showAddFlage = !this.showAddFlage
  218. },
  219. initTopics(){
  220. const mgrBtn = $('#manage_topic');
  221. const editDiv = $('#topic_edit');
  222. mgrBtn.on('click', (e) => {
  223. // viewDiv.hide();
  224. editDiv.css('display', ''); // show Semantic UI Grid
  225. this.input = ''
  226. if (this.input === ''){
  227. this.array = this.arrayTopics
  228. let data = []
  229. this.showInitTopic = []
  230. this.array.forEach((element,index) => {
  231. let item = {}
  232. item.topic_name = element
  233. data.push(item)
  234. this.showInitTopic.push(true)
  235. });
  236. this.array = data
  237. this.showInputValue = false
  238. this.showSearchTopic = true
  239. this.showAddTopic = false
  240. }
  241. stopPropagation(e);
  242. });
  243. $(document).bind('click',function(){
  244. editDiv.css('display','none');
  245. })
  246. editDiv.click(function(e){
  247. stopPropagation(e);
  248. })
  249. function stopPropagation(e) {
  250. var ev = e || window.event;
  251. if (ev.stopPropagation) {
  252. ev.stopPropagation();
  253. }
  254. else if (window.event) {
  255. window.event.cancelBubble = true;//兼容IE
  256. }
  257. }
  258. },
  259. getLang(){
  260. this.text_langName = document.getElementById("menutext_1").innerText
  261. if (this.text_langName == '简体中文'){
  262. this.$i18n.locale = 'zh'
  263. locale.use(zhLocale)
  264. }
  265. else if (this.text_langName == 'English'){
  266. this.$i18n.locale = 'en'
  267. locale.use(enLocale)
  268. }
  269. }
  270. },
  271. computed:{
  272. },
  273. watch: {
  274. // input(newValue){
  275. // if (newValue === ''){
  276. // this.array = this.arrayTopics
  277. // let data = []
  278. // this.showInitTopic = []
  279. // this.array.forEach((element,index) => {
  280. // let item = {}
  281. // item.topic_name = element
  282. // data.push(item)
  283. // this.showInitTopic.push(true)
  284. // });
  285. // this.array = data
  286. // this.showInputValue = false
  287. // this.showSearchTopic = true
  288. // }
  289. // }
  290. },
  291. mounted() {
  292. const context = this
  293. this.postUrl = `${window.location.pathname}/topics`;
  294. $('#repo-topics1').children('a').each(function(){
  295. context.arrayTopics.push($(this).text())
  296. });
  297. if(this.arrayTopics.length===0){
  298. $('#repo-topics1').append('<span class="no-description text-italic">暂无标签</span>')
  299. }
  300. this.changeValue();
  301. this.getLang()
  302. } ,
  303. created(){
  304. this.initTopics();
  305. this.input='';
  306. this.text_langName='' ;
  307. this.getLang()
  308. }
  309. };
  310. </script>
  311. <style scoped>
  312. .input-search {
  313. width: 100%;
  314. display: -webkit-box;
  315. display: -ms-flexbox;
  316. display: flex;
  317. min-width: 10rem;
  318. white-space: nowrap;
  319. font-size: 1rem;
  320. position: relative;
  321. display: inline-block;
  322. color: rgba(0,0,0,0.8);
  323. padding: 8px;
  324. }
  325. /deep/ .el-input__inner{
  326. border-color: #409eff;
  327. }
  328. .scrolling-menu{
  329. border-top: none !important;
  330. padding-top: 0 !important;
  331. padding-bottom: 0 !important;
  332. display: block;
  333. position: static;
  334. overflow-y: auto;
  335. border: none;
  336. -webkit-box-shadow: none !important;
  337. box-shadow: none !important;
  338. border-radius: 0 !important;
  339. margin: 0 !important;
  340. min-width: 100% !important;
  341. width: auto !important;
  342. border-top: 1px solid rgba(34,36,38,0.15);
  343. }
  344. .item-text{
  345. border-top: none;
  346. padding-right: calc(1.14285714rem + 17px ) !important;
  347. line-height: 1.333;
  348. padding-top: 0.7142857rem !important;
  349. padding-bottom: 0.7142857rem !important;
  350. position: relative;
  351. cursor: pointer;
  352. display: block;
  353. border: none;
  354. height: auto;
  355. text-align: left;
  356. border-top: none;
  357. line-height: 1em;
  358. color: rgba(0,0,0,0.87);
  359. padding: 0.78571429rem 1.14285714rem !important;
  360. font-size: 1rem;
  361. text-transform: none;
  362. font-weight: normal;
  363. -webkit-box-shadow: none;
  364. box-shadow: none;
  365. -webkit-touch-callout: none;
  366. display: -webkit-box;
  367. display: -ms-flexbox;
  368. display: flex;
  369. -webkit-box-align: center;
  370. -ms-flex-align: center;
  371. align-items: center;
  372. display: -webkit-box !important;
  373. display: -ms-flexbox !important;
  374. display: flex !important;
  375. }
  376. .icon-wrapper{
  377. text-align: left;
  378. width: 24px;
  379. height: 20px;
  380. -ms-flex-negative: 0;
  381. flex-shrink: 0;
  382. }
  383. .text{
  384. max-width: 80%;
  385. overflow: hidden;
  386. text-overflow: ellipsis;
  387. white-space: nowrap;
  388. font-size: 12px;
  389. font-weight: 400;
  390. color: #40485b;
  391. }
  392. .addition{
  393. background: #f6f6f6;
  394. }
  395. .user-add-label-text{
  396. max-width: 80%;
  397. overflow: hidden;
  398. text-overflow: ellipsis;
  399. white-space: nowrap;
  400. margin: 0 4px;
  401. }
  402. </style>