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