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

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
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
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. }else{
  113. this.showInitTopic.push(false)
  114. }
  115. });
  116. })
  117. this.showInputValue = true
  118. this.showSearchTopic = true
  119. }
  120. this.showAddTopic = false
  121. },
  122. Post(data,topics){
  123. this.$axios.post(this.postUrl,data).then(res=>{
  124. const viewDiv = $('#repo-topics1');
  125. viewDiv.children('.topic').remove();
  126. if (topics.length) {
  127. const topicArray = topics;
  128. const last = viewDiv.children('a').last();
  129. for (let i = 0; i < topicArray.length; i++) {
  130. const link = $('<a class="ui repo-topic small label topic"></a>');
  131. link.attr(
  132. 'href',
  133. `${AppSubUrl}/explore/repos?q=${encodeURIComponent(
  134. topicArray[i]
  135. )}&topic=1`
  136. );
  137. link.text(topicArray[i]);
  138. // link.insertBefore(last);
  139. viewDiv.append(link)
  140. }
  141. }
  142. viewDiv.show();
  143. })
  144. },
  145. postTopic(){
  146. const patter = /^[\u4e00-\u9fa5a-zA-Z0-9][\u4e00-\u9fa5a-zA-Z0-9-]{0,34}$/
  147. let regexp = patter.test(this.input)
  148. if(!regexp){
  149. this.$notify({
  150. message: '标签名必须以中文、字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符',
  151. duration: 3000,
  152. type:'error'
  153. });
  154. return
  155. }else{
  156. let topic = this.input
  157. if(this.arrayTopics.includes(topic)){
  158. return
  159. }
  160. else{
  161. this.arrayTopics.push(topic)
  162. let topics = this.arrayTopics
  163. let strTopics = topics.join(',')
  164. let data = this.qs.stringify({
  165. _csrf:csrf,
  166. topics:strTopics
  167. })
  168. this.Post(data,topics)
  169. $('#repo-topics1').children('span').remove()
  170. this.showInputValue = false
  171. this.showAddTopic = true
  172. this.showAddFlage = true
  173. }
  174. }
  175. },
  176. addPostTopic(){
  177. if(this.showAddFlage){
  178. this.arrayTopics.pop()
  179. let topics = this.arrayTopics
  180. let strTopics = topics.join(',')
  181. let data = this.qs.stringify({
  182. _csrf:csrf,
  183. topics:strTopics
  184. })
  185. this.Post(data,topics)
  186. if(this.arrayTopics.length===0){
  187. console.log("add postTopic")
  188. $('#repo-topics1').append('<span class="no-description text-italic">暂无标签</span>')
  189. }else{
  190. $('#repo-topics1').children('span').remove()
  191. }
  192. }
  193. else if(!this.showAddFlage){
  194. let topic = this.input
  195. this.arrayTopics.push(topic)
  196. let topics = this.arrayTopics
  197. let strTopics = topics.join(',')
  198. let data = this.qs.stringify({
  199. _csrf:csrf,
  200. topics:strTopics
  201. })
  202. this.Post(data,topics)
  203. $('#repo-topics1').children('span').remove()
  204. }
  205. this.showAddFlage = !this.showAddFlage
  206. },
  207. initTopics(){
  208. const mgrBtn = $('#manage_topic');
  209. const editDiv = $('#topic_edit');
  210. mgrBtn.on('click', (e) => {
  211. // viewDiv.hide();
  212. editDiv.css('display', ''); // show Semantic UI Grid
  213. this.input = ''
  214. if (this.input === ''){
  215. this.array = this.arrayTopics
  216. let data = []
  217. this.showInitTopic = []
  218. this.array.forEach((element,index) => {
  219. let item = {}
  220. item.topic_name = element
  221. data.push(item)
  222. this.showInitTopic.push(true)
  223. });
  224. this.array = data
  225. this.showInputValue = false
  226. this.showSearchTopic = true
  227. }
  228. stopPropagation(e);
  229. });
  230. $(document).bind('click',function(){
  231. editDiv.css('display','none');
  232. })
  233. editDiv.click(function(e){
  234. stopPropagation(e);
  235. })
  236. function stopPropagation(e) {
  237. var ev = e || window.event;
  238. if (ev.stopPropagation) {
  239. ev.stopPropagation();
  240. }
  241. else if (window.event) {
  242. window.event.cancelBubble = true;//兼容IE
  243. }
  244. }
  245. }
  246. },
  247. computed:{
  248. },
  249. watch: {
  250. input(newValue){
  251. if (newValue === ''){
  252. this.array = this.arrayTopics
  253. let data = []
  254. this.showInitTopic = []
  255. this.array.forEach((element,index) => {
  256. let item = {}
  257. item.topic_name = element
  258. data.push(item)
  259. this.showInitTopic.push(true)
  260. });
  261. this.array = data
  262. this.showInputValue = false
  263. this.showSearchTopic = true
  264. }
  265. }
  266. },
  267. mounted() {
  268. const context = this
  269. this.postUrl = `${window.location.pathname}/topics`;
  270. $('#repo-topics1').children('a').each(function(){
  271. context.arrayTopics.push($(this).text())
  272. });
  273. if(this.arrayTopics.length===0){
  274. $('#repo-topics1').append('<span class="no-description text-italic">暂无标签</span>')
  275. }
  276. this.changeValue()
  277. } ,
  278. created(){
  279. this.initTopics();
  280. this.input=''
  281. }
  282. };
  283. </script>
  284. <style scoped>
  285. .input-search {
  286. width: 100%;
  287. display: -webkit-box;
  288. display: -ms-flexbox;
  289. display: flex;
  290. min-width: 10rem;
  291. white-space: nowrap;
  292. font-size: 1rem;
  293. position: relative;
  294. display: inline-block;
  295. color: rgba(0,0,0,0.8);
  296. padding: 8px;
  297. }
  298. /deep/ .el-input__inner{
  299. border-color: #409eff;
  300. }
  301. .scrolling-menu{
  302. border-top: none !important;
  303. padding-top: 0 !important;
  304. padding-bottom: 0 !important;
  305. display: block;
  306. position: static;
  307. overflow-y: auto;
  308. border: none;
  309. -webkit-box-shadow: none !important;
  310. box-shadow: none !important;
  311. border-radius: 0 !important;
  312. margin: 0 !important;
  313. min-width: 100% !important;
  314. width: auto !important;
  315. border-top: 1px solid rgba(34,36,38,0.15);
  316. }
  317. .item-text{
  318. border-top: none;
  319. padding-right: calc(1.14285714rem + 17px ) !important;
  320. line-height: 1.333;
  321. padding-top: 0.7142857rem !important;
  322. padding-bottom: 0.7142857rem !important;
  323. position: relative;
  324. cursor: pointer;
  325. display: block;
  326. border: none;
  327. height: auto;
  328. text-align: left;
  329. border-top: none;
  330. line-height: 1em;
  331. color: rgba(0,0,0,0.87);
  332. padding: 0.78571429rem 1.14285714rem !important;
  333. font-size: 1rem;
  334. text-transform: none;
  335. font-weight: normal;
  336. -webkit-box-shadow: none;
  337. box-shadow: none;
  338. -webkit-touch-callout: none;
  339. display: -webkit-box;
  340. display: -ms-flexbox;
  341. display: flex;
  342. -webkit-box-align: center;
  343. -ms-flex-align: center;
  344. align-items: center;
  345. display: -webkit-box !important;
  346. display: -ms-flexbox !important;
  347. display: flex !important;
  348. }
  349. .icon-wrapper{
  350. text-align: left;
  351. width: 24px;
  352. height: 20px;
  353. -ms-flex-negative: 0;
  354. flex-shrink: 0;
  355. }
  356. .text{
  357. max-width: 80%;
  358. overflow: hidden;
  359. text-overflow: ellipsis;
  360. white-space: nowrap;
  361. font-size: 12px;
  362. font-weight: 400;
  363. color: #40485b;
  364. }
  365. .addition{
  366. background: #f6f6f6;
  367. }
  368. .user-add-label-text{
  369. max-width: 80%;
  370. overflow: hidden;
  371. text-overflow: ellipsis;
  372. white-space: nowrap;
  373. margin: 0 4px;
  374. }
  375. </style>