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

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