dialogResizeStandard.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. export const dialogResizeStandard = {
  2. mounted(el: any, binding: any, vnode: any) {
  3. const bValue = binding.value
  4. const localKeyMap: any = {
  5. positions: {
  6. 'can-resize3': 'cet-standard-positions',
  7. },
  8. resize: {
  9. 'can-resize3': 'cet-standard-resize',
  10. },
  11. }
  12. const resizeEvent = new CustomEvent('drag-resize', {
  13. detail: '尺寸变化',
  14. bubbles: false,
  15. })
  16. // 初始化不最大化
  17. el.fullscreen = false
  18. const winHeight = window.innerHeight
  19. // 弹框可拉伸最小宽高
  20. const minWidth = 400
  21. const minHeight = winHeight / 2
  22. // 弹窗
  23. const dragDom: any = document.querySelector('.' + binding.value)
  24. const dialogHeaderEl = dragDom.querySelector('.el-dialog__header')
  25. el.style.overflow = 'initial'
  26. dragDom.className += ' el-drag-dialog'
  27. // 给弹窗加上overflow auto;不然缩小时框内的标签可能超出dialog;
  28. // dragDom.style.overflow = 'auto'
  29. dragDom.style.background = '#fff'
  30. const keyboardPositions = localStorage.getItem(localKeyMap.positions[bValue])
  31. const keyboardResize = localStorage.getItem(localKeyMap.resize[bValue])
  32. if (keyboardPositions) {
  33. dragDom.style.left = JSON.parse(keyboardPositions).left || 0
  34. dragDom.style.top = JSON.parse(keyboardPositions).top || 0
  35. }
  36. if (keyboardResize) {
  37. const localW = JSON.parse(keyboardResize).width
  38. const localH = JSON.parse(keyboardResize).height
  39. localW && (dragDom.style.width = localW)
  40. localH && (dragDom.style.height = localH)
  41. } else {
  42. dragDom.style.height = minHeight + 100 + 'px'
  43. dragDom.style.width = '800px'
  44. }
  45. // 清除选择头部文字效果
  46. // eslint-disable-next-line no-new-func
  47. dialogHeaderEl.onselectstart = new Function('return false')
  48. // 头部加上可拖动cursor
  49. dialogHeaderEl.style.cursor = 'move'
  50. // 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
  51. const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null)
  52. // 头部插入最大化最小化元素
  53. const maxMin = document.createElement('button')
  54. maxMin.className += ' el-dialog__headerbtn el-dialog__minmax'
  55. maxMin.style.right = '40px'
  56. maxMin.style.color = '#ffffff'
  57. maxMin.title = el.fullscreen ? '还原' : '最大化'
  58. maxMin.innerHTML =
  59. '<i class=' +
  60. (el.fullscreen ? '"el-icon-crop"' : '"el-icon-full-screen"') +
  61. ' onMouseOver="this.style.color=\'#409EFF\'" onMouseOut="this.style.color=\'inherit\'"></i>'
  62. dialogHeaderEl.insertBefore(maxMin, dialogHeaderEl.childNodes[1])
  63. const moveDown = (e: any) => {
  64. // 鼠标按下,计算当前元素距离可视区的距离
  65. const disX = e.clientX - dialogHeaderEl.offsetLeft
  66. const disY = e.clientY - dialogHeaderEl.offsetTop
  67. // 获取到的值带px 正则匹配替换
  68. let styL: any, styT: any
  69. // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
  70. if (sty.left.includes('%')) {
  71. styL = +document.body.clientWidth * (+sty.left.replace(/\\%/g, '') / 100)
  72. styT = +document.body.clientHeight * (+sty.top.replace(/\\%/g, '') / 100)
  73. } else {
  74. styL = +sty.left.replace(/\px/g, '')
  75. styT = +sty.top.replace(/\px/g, '')
  76. }
  77. document.onmousemove = function (e) {
  78. // 通过事件委托,计算移动的距离
  79. const l = e.clientX - disX
  80. const t = e.clientY - disY
  81. // 移动当前元素
  82. dragDom.style.left = `${l + styL}px`
  83. dragDom.style.top = `${t + styT}px`
  84. // 将此时的位置传出去
  85. // binding.value({x:e.pageX,y:e.pageY})
  86. }
  87. document.onmouseup = function (e) {
  88. document.onmousemove = null
  89. document.onmouseup = null
  90. console.log('dragDom.style.left:', dragDom.style.left, dragDom.style.top)
  91. localStorage.setItem(
  92. localKeyMap.positions[bValue],
  93. JSON.stringify({
  94. left: dragDom.style.left || 0,
  95. top: dragDom.style.top || 0,
  96. })
  97. )
  98. }
  99. }
  100. dialogHeaderEl.onmousedown = moveDown
  101. // 点击放大缩小效果
  102. // maxMin.onclick = setMaxMin
  103. // 双击头部效果
  104. // dialogHeaderEl.ondblclick = setMaxMin
  105. // 拉伸
  106. const resizeEl = document.createElement('div')
  107. dragDom.appendChild(resizeEl)
  108. // 在弹窗右下角加上一个10-10px的控制块
  109. resizeEl.style.cursor = 'se-resize'
  110. resizeEl.style.position = 'absolute'
  111. resizeEl.style.height = '10px'
  112. resizeEl.style.width = '10px'
  113. resizeEl.style.border = '3px solid transparent'
  114. resizeEl.style.borderRightColor = '#333'
  115. resizeEl.style.borderBottomColor = '#333'
  116. resizeEl.style.right = '0px'
  117. resizeEl.style.bottom = '0px'
  118. resizeEl.style.zIndex = '99'
  119. // 鼠标拉伸弹窗
  120. resizeEl.onmousedown = (e) => {
  121. // 记录初始x位置
  122. const clientX = e.clientX
  123. // 鼠标按下,计算当前元素距离可视区的距离
  124. const disX = e.clientX - resizeEl.offsetLeft
  125. const disY = e.clientY - resizeEl.offsetTop
  126. const iframeMask = document.getElementById('my-iframe-mask')
  127. if (iframeMask) {
  128. iframeMask.style.display = 'block'
  129. }
  130. document.onmousemove = function (e) {
  131. e.preventDefault() // 移动时禁用默认事件
  132. // 通过事件委托,计算移动的距离
  133. const x = e.clientX - disX + (e.clientX - clientX) // 这里 由于elementUI的dialog控制居中的,所以水平拉伸效果是双倍
  134. const y = e.clientY - disY
  135. // 比较是否小于最小宽高
  136. dragDom.style.width = x > minWidth ? `${x}px` : minWidth + 'px'
  137. dragDom.style.height = y > minHeight ? `${y}px` : minHeight + 'px'
  138. // dragDom.style.width = `${x}px`
  139. // dragDom.style.height = `${y}px`
  140. // if (!hasSetBodyHight) {
  141. // const footerHeight =
  142. // dragDom.querySelector('.el-dialog__footer') && dragDom.querySelector('.el-dialog__footer').offsetHeight
  143. // dragDom.querySelector('.el-dialog__body').style.height =
  144. // 'calc(100% - ' + (dialogHeaderEl.offsetHeight + footerHeight) + 'px)'
  145. // hasSetBodyHight = true
  146. // }
  147. }
  148. // 拉伸结束
  149. document.onmouseup = function (e) {
  150. document.onmousemove = null
  151. document.onmouseup = null
  152. localStorage.setItem(
  153. localKeyMap.resize[bValue],
  154. JSON.stringify({
  155. width: dragDom.style.width || 'auto',
  156. height: dragDom.style.height || 'auto',
  157. })
  158. )
  159. el.dispatchEvent(resizeEvent)
  160. const iframeMask = document.getElementById('my-iframe-mask')
  161. if (iframeMask) {
  162. iframeMask.style.display = 'none'
  163. }
  164. }
  165. }
  166. },
  167. }