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

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