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.1 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. a.currentTarget.parentNode.nextElementSibling.getElementsByTagName("input")[0].setAttribute("value", a.currentTarget.getElementsByTagName("a")[0].getAttribute('title'))
  42. }
  43. let btn = a.currentTarget.parentNode.nextElementSibling.getElementsByTagName("button")[0]
  44. btn.addEventListener('click', function (e) {
  45. let postUrl = btn.getAttribute('data-postbasepath')
  46. let last_commit = btn.getAttribute('data-commit')
  47. let tree_path = btn.getAttribute('data-treepath') + e.target.parentNode.previousElementSibling.getElementsByTagName("input")[0].value
  48. let csrf = $("input[name='_csrf']").val()
  49. $.ajax({
  50. url: postUrl,
  51. type: "POST",
  52. contentType: "application/x-www-form-urlencoded",
  53. data: { last_commit: last_commit, tree_path: tree_path, _csrf: csrf },
  54. success: function (res) {
  55. console.log("--------------")
  56. console.log(res.Code)
  57. console.log(res.Message)
  58. if (res.Code === 0) {
  59. document.getElementById("mask").style.display = "block"
  60. location.reload()
  61. }
  62. else {
  63. $('.ui.negative.message').text(res.Msg).show().delay(10000).fadeOut();
  64. }
  65. }
  66. })
  67. })
  68. },
  69. },
  70. // {
  71. // label: '删除',
  72. // icon: "trash icon context-menu-icon",
  73. // active: (e, a) => {
  74. // console.dir(a)
  75. // $('.context-menu-delete.modal')
  76. // .modal({
  77. // onApprove() {
  78. // }
  79. // })
  80. // .modal('show');
  81. // }
  82. // },
  83. ]
  84. })
  85. }
  86. class Menu {
  87. constructor(param) {
  88. this.target = document.createElement('div')
  89. this.target.classList.add("ui", "menu", "compact", "vertical", "context-menu")
  90. this.data = param.data
  91. this.active = false
  92. this.clickZ = this.click.bind(this)
  93. this.closeZ = this.close2.bind(this)
  94. document.addEventListener('click', this.closeZ)
  95. for (let i = 0; i < this.data.length; i++) {
  96. let div = document.createElement('a')
  97. div.classList.add('item', 'context-menu-operation')
  98. if (this.data[i].disabled) {
  99. div.classList.add('disabled')
  100. }
  101. div.dataset.index = i.toString()
  102. div.innerHTML = `<i class="${this.data[i].icon}"></i>${this.data[i].label}`
  103. div.addEventListener('click', this.clickZ)
  104. this.target.append(div)
  105. }
  106. document.body.append(this.target)
  107. }
  108. click(e) {
  109. let index = parseInt(e.target.dataset.index)
  110. if (this.data[index].active) {
  111. this.data[index].active(e, this.acEvent)
  112. }
  113. this.close()
  114. }
  115. show(e) {
  116. this.active = true
  117. this.nodeList = this.target.querySelectorAll('.item')
  118. for (let i = 0; i < this.data.length; i++) {
  119. if (this.data[i].beforeDisabled) {
  120. let t = this.data[i].beforeDisabled(e)
  121. this.data[i].disabled = t
  122. if (t) {
  123. this.nodeList[i].classList.add('disabled')
  124. } else {
  125. this.nodeList[i].classList.remove('disabled')
  126. }
  127. }
  128. }
  129. this.acEvent = e
  130. this.target.style.top = `${e.pageY}px`
  131. this.target.style.left = `${e.pageX}px`
  132. this.target.style.minWidth = '90px'
  133. this.target.classList.add('active')
  134. }
  135. close() {
  136. this.active = false
  137. this.target.classList.remove('active')
  138. }
  139. close2(e) {
  140. if (!this.target.contains(e.target) && this.active) {
  141. this.active = false
  142. this.target.classList.remove('active')
  143. }
  144. }
  145. }