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 11 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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">
  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="vertical-align: middle;color: #303643;" 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="vertical-align: middle;color: #303643;" 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. this.arrayTopics.push(array.topic_name)
  49. let topics = this.arrayTopics
  50. let strTopics = topics.join(',')
  51. let data = this.qs.stringify({
  52. _csrf:csrf,
  53. topics:strTopics
  54. })
  55. this.Post(data,topics)
  56. this.$set(this.showInitTopic,item,true)
  57. }else{
  58. this.arrayTopics=this.arrayTopics.filter(ele=>{
  59. return ele !== array.topic_name
  60. })
  61. let topics = this.arrayTopics
  62. let strTopics = topics.join(',')
  63. let data = this.qs.stringify({
  64. _csrf:csrf,
  65. topics:strTopics
  66. })
  67. this.Post(data,topics)
  68. this.$set(this.showInitTopic,item,false)
  69. }
  70. },
  71. changeValue(){
  72. if (this.input === ''){
  73. this.array = this.arrayTopics
  74. let data = []
  75. this.showInitTopic = []
  76. this.array.forEach((element,index) => {
  77. let item = {}
  78. item.topic_name = element
  79. data.push(item)
  80. this.showInitTopic.push(true)
  81. });
  82. this.array = data
  83. this.showInputValue = false
  84. this.showSearchTopic = true
  85. }
  86. else if(this.arrayTopics.indexOf(this.input)>-1){
  87. this.showInputValue = false
  88. this.showSearchTopic = false
  89. }else{
  90. this.showInitTopic = []
  91. let timestamp=new Date().getTime()
  92. this.params.q = this.input
  93. this.params._ = timestamp
  94. this.$axios.get('/api/v1/topics/search',{
  95. params:this.params
  96. }).then((res)=>{
  97. this.array = res.data.topics
  98. this.array.forEach((element,index) => {
  99. if (this.arrayTopics.indexOf(element.topic_name)>-1){
  100. this.showInitTopic.push(true)
  101. }else{
  102. this.showInitTopic.push(false)
  103. }
  104. });
  105. })
  106. this.showInputValue = true
  107. this.showSearchTopic = true
  108. }
  109. this.showAddTopic = false
  110. },
  111. Post(data,topics){
  112. this.$axios.post(this.postUrl,data).then(res=>{
  113. const viewDiv = $('#repo-topics1');
  114. viewDiv.children('.topic').remove();
  115. if (topics.length) {
  116. const topicArray = topics;
  117. const last = viewDiv.children('a').last();
  118. for (let i = 0; i < topicArray.length; i++) {
  119. const link = $('<a class="ui repo-topic small label topic"></a>');
  120. link.attr(
  121. 'href',
  122. `${AppSubUrl}/explore/repos?q=${encodeURIComponent(
  123. topicArray[i]
  124. )}&topic=1`
  125. );
  126. link.text(topicArray[i]);
  127. // link.insertBefore(last);
  128. viewDiv.append(link)
  129. }
  130. }
  131. viewDiv.show();
  132. })
  133. },
  134. postTopic(){
  135. let topic = this.input
  136. this.arrayTopics.push(topic)
  137. let topics = this.arrayTopics
  138. let strTopics = topics.join(',')
  139. let data = this.qs.stringify({
  140. _csrf:csrf,
  141. topics:strTopics
  142. })
  143. this.Post(data,topics)
  144. this.showInputValue = false
  145. this.showAddTopic = true
  146. this.showAddFlage = true
  147. },
  148. addPostTopic(){
  149. if(this.showAddFlage){
  150. this.arrayTopics.pop()
  151. let topics = this.arrayTopics
  152. let strTopics = topics.join(',')
  153. let data = this.qs.stringify({
  154. _csrf:csrf,
  155. topics:strTopics
  156. })
  157. this.Post(data,topics)
  158. }
  159. else if(!this.showAddFlage){
  160. let topic = this.input
  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. }
  170. this.showAddFlage = !this.showAddFlage
  171. },
  172. initTopics(){
  173. const mgrBtn = $('#manage_topic');
  174. const editDiv = $('#topic_edit');
  175. mgrBtn.on('click', (e) => {
  176. // viewDiv.hide();
  177. editDiv.css('display', ''); // show Semantic UI Grid
  178. this.input = ''
  179. stopPropagation(e);
  180. });
  181. $(document).bind('click',function(){
  182. editDiv.css('display','none');
  183. })
  184. editDiv.click(function(e){
  185. stopPropagation(e);
  186. })
  187. function stopPropagation(e) {
  188. var ev = e || window.event;
  189. if (ev.stopPropagation) {
  190. ev.stopPropagation();
  191. }
  192. else if (window.event) {
  193. window.event.cancelBubble = true;//兼容IE
  194. }
  195. }
  196. }
  197. },
  198. computed:{
  199. },
  200. watch: {
  201. input(newValue){
  202. if (newValue === ''){
  203. this.array = this.arrayTopics
  204. let data = []
  205. this.showInitTopic = []
  206. this.array.forEach((element,index) => {
  207. let item = {}
  208. item.topic_name = element
  209. data.push(item)
  210. this.showInitTopic.push(true)
  211. });
  212. this.array = data
  213. this.showInputValue = false
  214. this.showSearchTopic = true
  215. }
  216. }
  217. },
  218. mounted() {
  219. const context = this
  220. this.postUrl = `${window.location.pathname}/topics`;
  221. $('#repo-topics1').children('a').each(function(){
  222. context.arrayTopics.push($(this).text())
  223. });
  224. this.changeValue()
  225. } ,
  226. created(){
  227. this.initTopics();
  228. this.input=''
  229. }
  230. };
  231. </script>
  232. <style scoped>
  233. .input-search {
  234. width: 100%;
  235. display: -webkit-box;
  236. display: -ms-flexbox;
  237. display: flex;
  238. min-width: 10rem;
  239. white-space: nowrap;
  240. font-size: 1rem;
  241. position: relative;
  242. display: inline-block;
  243. color: rgba(0,0,0,0.8);
  244. padding: 8px;
  245. }
  246. /deep/ .el-input__inner{
  247. border-color: #409eff;
  248. }
  249. .scrolling-menu{
  250. border-top: none !important;
  251. padding-top: 0 !important;
  252. padding-bottom: 0 !important;
  253. display: block;
  254. position: static;
  255. overflow-y: auto;
  256. border: none;
  257. -webkit-box-shadow: none !important;
  258. box-shadow: none !important;
  259. border-radius: 0 !important;
  260. margin: 0 !important;
  261. min-width: 100% !important;
  262. width: auto !important;
  263. border-top: 1px solid rgba(34,36,38,0.15);
  264. }
  265. .item-text{
  266. border-top: none;
  267. padding-right: calc(1.14285714rem + 17px ) !important;
  268. line-height: 1.333;
  269. padding-top: 0.7142857rem !important;
  270. padding-bottom: 0.7142857rem !important;
  271. position: relative;
  272. cursor: pointer;
  273. display: block;
  274. border: none;
  275. height: auto;
  276. text-align: left;
  277. border-top: none;
  278. line-height: 1em;
  279. color: rgba(0,0,0,0.87);
  280. padding: 0.78571429rem 1.14285714rem !important;
  281. font-size: 1rem;
  282. text-transform: none;
  283. font-weight: normal;
  284. -webkit-box-shadow: none;
  285. box-shadow: none;
  286. -webkit-touch-callout: none;
  287. display: -webkit-box;
  288. display: -ms-flexbox;
  289. display: flex;
  290. -webkit-box-align: center;
  291. -ms-flex-align: center;
  292. align-items: center;
  293. display: -webkit-box !important;
  294. display: -ms-flexbox !important;
  295. display: flex !important;
  296. }
  297. .icon-wrapper{
  298. text-align: left;
  299. width: 24px;
  300. height: 20px;
  301. -ms-flex-negative: 0;
  302. flex-shrink: 0;
  303. }
  304. .text{
  305. max-width: 80%;
  306. overflow: hidden;
  307. text-overflow: ellipsis;
  308. white-space: nowrap;
  309. font-size: 12px;
  310. font-weight: 400;
  311. color: #40485b;
  312. }
  313. .addition{
  314. background: #f6f6f6;
  315. }
  316. .user-add-label-text{
  317. max-width: 80%;
  318. overflow: hidden;
  319. text-overflow: ellipsis;
  320. white-space: nowrap;
  321. margin: 0 4px;
  322. }
  323. </style>