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.

contexmenu.js 6.4 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. export default async function initContextMenu() {
  2. $('.popup-close').on('click', function (e) {
  3. $(this).parents('tr').prev().css('display', 'table-row')
  4. $(this).closest('tr').css('cssText', 'display:none !important')
  5. })
  6. function contextMenu() {
  7. let canContextMenu = $('.ui.single.line.table.can-context-menu').data('can-editfile')
  8. if (canContextMenu) {
  9. $('.name.four.wide').on('contextmenu', function (e) {
  10. let ev = window.event || e;
  11. ev.preventDefault();
  12. menu.show(e)
  13. })
  14. } else {
  15. return
  16. }
  17. }
  18. contextMenu()
  19. const menu = new Menu({
  20. data: [
  21. {
  22. label: '新标签打开',
  23. icon: "file outline icon context-menu-icon",
  24. active: (e, a) => {
  25. window.open(a.currentTarget.getElementsByTagName("a")[0].getAttribute('href'))
  26. }
  27. },
  28. {
  29. label: '重命名',
  30. icon: "edit icon context-menu-icon",
  31. active: (e, a) => {
  32. document.querySelectorAll(".context-menu-one").forEach((ele) => {
  33. if (ele.style.display === 'table-row') {
  34. ele.style.display = 'none'
  35. ele.previousElementSibling.style.display = 'table-row'
  36. }
  37. })
  38. if (a.currentTarget.parentNode.nextElementSibling) {
  39. a.currentTarget.parentNode.style.setProperty('display', 'none', 'important')
  40. a.currentTarget.parentNode.nextElementSibling.style.display = 'table-row'
  41. const renameFile = a.currentTarget.getElementsByTagName("a")[0].getAttribute('title')
  42. let renameFileValue = renameFile.indexOf('/') !== -1 ? renameFile.substr(0, renameFile.indexOf('/')) : renameFile
  43. a.currentTarget.parentNode.nextElementSibling.getElementsByTagName("input")[0].setAttribute("value", renameFileValue)
  44. }
  45. let btn = a.currentTarget.parentNode.nextElementSibling.getElementsByTagName("button")[0]
  46. btn.addEventListener('click', function (e) {
  47. let postUrl = btn.getAttribute('data-postbasepath')
  48. const postUrlArr = postUrl.split('/')
  49. postUrlArr[postUrlArr.length - 1] = encodeURIComponent(postUrlArr[postUrlArr.length - 1])
  50. postUrl = postUrlArr.join('/')
  51. console.log(postUrl)
  52. let last_commit = btn.getAttribute('data-commit')
  53. let tree_path = btn.getAttribute('data-treepath') + e.target.parentNode.previousElementSibling.getElementsByTagName("input")[0].value
  54. let csrf = $("input[name='_csrf']").val()
  55. $.ajax({
  56. url: postUrl,
  57. type: "POST",
  58. contentType: "application/x-www-form-urlencoded",
  59. data: { last_commit: last_commit, tree_path: tree_path, _csrf: csrf },
  60. success: function (res) {
  61. if (res.Code === 0) {
  62. document.getElementById("mask").style.display = "block"
  63. location.reload()
  64. }
  65. else {
  66. $('.ui.negative.message').text(res.Msg).show().delay(10000).fadeOut();
  67. }
  68. }
  69. })
  70. })
  71. },
  72. },
  73. // {
  74. // label: '删除',
  75. // icon: "trash icon context-menu-icon",
  76. // active: (e, a) => {
  77. // console.dir(a)
  78. // $('.context-menu-delete.modal')
  79. // .modal({
  80. // onApprove() {
  81. // }
  82. // })
  83. // .modal('show');
  84. // }
  85. // },
  86. ]
  87. })
  88. }
  89. class Menu {
  90. constructor(param) {
  91. this.target = document.createElement('div')
  92. this.target.classList.add("ui", "menu", "compact", "vertical", "context-menu-click")
  93. this.data = param.data
  94. this.active = false
  95. this.clickZ = this.click.bind(this)
  96. this.closeZ = this.close2.bind(this)
  97. document.addEventListener('click', this.closeZ)
  98. for (let i = 0; i < this.data.length; i++) {
  99. let div = document.createElement('a')
  100. div.classList.add('item', 'context-menu-operation')
  101. if (this.data[i].disabled) {
  102. div.classList.add('disabled')
  103. }
  104. div.dataset.index = i.toString()
  105. div.innerHTML = `<i class="${this.data[i].icon}"></i>${this.data[i].label}`
  106. div.addEventListener('click', this.clickZ)
  107. this.target.append(div)
  108. }
  109. document.body.append(this.target)
  110. }
  111. click(e) {
  112. let index = parseInt(e.target.dataset.index)
  113. if (this.data[index].active) {
  114. this.data[index].active(e, this.acEvent)
  115. }
  116. this.close()
  117. }
  118. show(e) {
  119. this.active = true
  120. this.nodeList = this.target.querySelectorAll('.item')
  121. for (let i = 0; i < this.data.length; i++) {
  122. if (this.data[i].beforeDisabled) {
  123. let t = this.data[i].beforeDisabled(e)
  124. this.data[i].disabled = t
  125. if (t) {
  126. this.nodeList[i].classList.add('disabled')
  127. } else {
  128. this.nodeList[i].classList.remove('disabled')
  129. }
  130. }
  131. }
  132. this.acEvent = e
  133. this.target.style.top = `${e.pageY}px`
  134. this.target.style.left = `${e.pageX}px`
  135. this.target.style.minWidth = '90px'
  136. this.target.classList.add('active')
  137. }
  138. close() {
  139. this.active = false
  140. this.target.classList.remove('active')
  141. }
  142. close2(e) {
  143. if (!this.target.contains(e.target) && this.active) {
  144. this.active = false
  145. this.target.classList.remove('active')
  146. }
  147. }
  148. }