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

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