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