index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // 引入拖拽js
  2. import { startDrag } from './drag.js'
  3. //引入抖动
  4. import shake from './shake.js'
  5. /**
  6. * 为el-dialog弹框增加拖拽功能
  7. * @param {*} el 指定dom
  8. * @param {*} binding 绑定对象
  9. * desc 只要用到了el-dialog的组件,都可以通过增加v-draggable属性变为可拖拽的弹框
  10. */
  11. const draggable = (el, binding) => {
  12. // 绑定拖拽事件 [绑定拖拽触发元素为弹框头部、拖拽移动元素为整个弹框]
  13. startDrag(el.querySelector('.el-dialog__header'), el.querySelector('.el-dialog'), binding.value);
  14. };
  15. /*
  16. * 使用方法
  17. * 将以下代码复制到一个drag.js文件中,然后在入口文件main.js中导入:import ‘./util/drog.js’;
  18. * 给elementUI的dialog上加上 v-dialogDrag 指令就可以实现弹窗的全屏和拉伸了。
  19. * 给dialog设置 :close-on-click-modal="false" , 禁止点击遮罩层关闭弹出层
  20. * 如果是form表单,不要将提交等按钮放置el-form-item,以免在上下拉伸时被隐藏
  21. */
  22. // v-dialogDrag: 弹窗拖拽+水平方向伸缩
  23. const dialogDrag = {
  24. bind(el, binding, vnode, oldVnode) {
  25. // 弹框可拉伸最小宽高
  26. const minWidth = 400
  27. const minHeight = 300
  28. // 初始非全屏
  29. let isFullScreen = false
  30. // 当前顶部高度
  31. let nowMarginTop = 0
  32. // 获取弹框头部(这部分可双击全屏)
  33. const dialogHeaderEl = el.querySelector('.el-dialog__header')
  34. // 弹窗
  35. const dragDom = el.querySelector('.el-dialog')
  36. // 给弹窗加上overflow auto;不然缩小时框内的标签可能超出dialog;
  37. dragDom.style.overflow = 'auto'
  38. // 清除选择头部文字效果
  39. // dialogHeaderEl.onselectstart = new Function("return false");
  40. // 头部加上可拖动cursor
  41. dialogHeaderEl.style.cursor = 'move'
  42. // 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
  43. const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null)
  44. const moveDown = e => {
  45. // 鼠标按下,计算当前元素距离可视区的距离
  46. const disX = e.clientX - dialogHeaderEl.offsetLeft
  47. const disY = e.clientY - dialogHeaderEl.offsetTop
  48. // 获取到的值带px 正则匹配替换
  49. let styL, styT
  50. // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
  51. if (sty.left.includes('%')) {
  52. styL = +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / 100)
  53. styT = +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / 100)
  54. } else {
  55. styL = +sty.left.replace(/\px/g, '')
  56. styT = +sty.top.replace(/\px/g, '')
  57. }
  58. document.onmousemove = function(e) {
  59. // 通过事件委托,计算移动的距离
  60. const l = e.clientX - disX
  61. const t = e.clientY - disY
  62. // 移动当前元素
  63. dragDom.style.left = `${l + styL}px`
  64. dragDom.style.top = `${t + styT}px`
  65. // 将此时的位置传出去
  66. // binding.value({x:e.pageX,y:e.pageY})
  67. }
  68. document.onmouseup = function(e) {
  69. document.onmousemove = null
  70. document.onmouseup = null
  71. }
  72. }
  73. dialogHeaderEl.onmousedown = moveDown
  74. // 当前宽高
  75. let nowWidth = 0
  76. // let nowHight = 0
  77. // 双击头部全屏效果
  78. dialogHeaderEl.ondblclick = e => {
  79. if (isFullScreen === false) {
  80. // nowHight = dragDom.clientHeight
  81. nowWidth = dragDom.clientWidth
  82. nowMarginTop = dragDom.style.marginTop
  83. dragDom.style.left = 0
  84. dragDom.style.top = 0
  85. dragDom.style.height = '100VH'
  86. dragDom.style.width = '100VW'
  87. dragDom.style.marginTop = 0
  88. isFullScreen = true
  89. dialogHeaderEl.style.cursor = 'initial'
  90. dialogHeaderEl.onmousedown = null
  91. } else {
  92. dragDom.style.height = 'auto'
  93. dragDom.style.width = nowWidth + 'px'
  94. dragDom.style.marginTop = nowMarginTop
  95. isFullScreen = false
  96. dialogHeaderEl.style.cursor = 'move'
  97. dialogHeaderEl.onmousedown = moveDown
  98. }
  99. }
  100. dragDom.onmousemove = function(e) {
  101. // let moveE = e
  102. if (
  103. e.clientX > dragDom.offsetLeft + dragDom.clientWidth - 10 ||
  104. dragDom.offsetLeft + 10 > e.clientX
  105. ) {
  106. dragDom.style.cursor = 'w-resize'
  107. } else if (
  108. el.scrollTop + e.clientY >
  109. dragDom.offsetTop + dragDom.clientHeight - 10
  110. ) {
  111. dragDom.style.cursor = 's-resize'
  112. } else {
  113. dragDom.style.cursor = 'default'
  114. dragDom.onmousedown = null
  115. }
  116. dragDom.onmousedown = e => {
  117. const clientX = e.clientX
  118. const clientY = e.clientY
  119. const elW = dragDom.clientWidth
  120. const elH = dragDom.clientHeight
  121. const EloffsetLeft = dragDom.offsetLeft
  122. const EloffsetTop = dragDom.offsetTop
  123. dragDom.style.userSelect = 'none'
  124. const ELscrollTop = el.scrollTop
  125. // 判断点击的位置是不是为头部
  126. if (
  127. clientX > EloffsetLeft &&
  128. clientX < EloffsetLeft + elW &&
  129. clientY > EloffsetTop &&
  130. clientY < EloffsetTop + 100
  131. ) {
  132. // 如果是头部在此就不做任何动作,以上有绑定dialogHeaderEl.onmousedown = moveDown;
  133. } else {
  134. document.onmousemove = function(e) {
  135. // 移动时禁用默认事件
  136. e.preventDefault()
  137. // 左侧鼠标拖拽位置
  138. if (clientX > EloffsetLeft && clientX < EloffsetLeft + 10) {
  139. // 往左拖拽
  140. if (clientX > e.clientX) {
  141. dragDom.style.width = elW + (clientX - e.clientX) * 2 + 'px'
  142. }
  143. // 往右拖拽
  144. if (clientX < e.clientX) {
  145. if (dragDom.clientWidth < minWidth) {
  146. console.log()
  147. } else {
  148. dragDom.style.width = elW - (e.clientX - clientX) * 2 + 'px'
  149. }
  150. }
  151. }
  152. // 右侧鼠标拖拽位置
  153. if (
  154. clientX > EloffsetLeft + elW - 10 &&
  155. clientX < EloffsetLeft + elW
  156. ) {
  157. // 往左拖拽
  158. if (clientX > e.clientX) {
  159. if (dragDom.clientWidth < minWidth) {
  160. console.log()
  161. } else {
  162. dragDom.style.width = elW - (clientX - e.clientX) * 2 + 'px'
  163. }
  164. }
  165. // 往右拖拽
  166. if (clientX < e.clientX) {
  167. dragDom.style.width = elW + (e.clientX - clientX) * 2 + 'px'
  168. }
  169. }
  170. // 底部鼠标拖拽位置
  171. if (
  172. ELscrollTop + clientY > EloffsetTop + elH - 20 &&
  173. ELscrollTop + clientY < EloffsetTop + elH
  174. ) {
  175. // 往上拖拽
  176. if (clientY > e.clientY) {
  177. if (dragDom.clientHeight < minHeight) {
  178. console.log()
  179. } else {
  180. dragDom.style.height = elH - (clientY - e.clientY) * 2 + 'px'
  181. }
  182. }
  183. // 往下拖拽
  184. if (clientY < e.clientY) {
  185. dragDom.style.height = elH + (e.clientY - clientY) * 2 + 'px'
  186. }
  187. }
  188. }
  189. // 拉伸结束
  190. document.onmouseup = function(e) {
  191. document.onmousemove = null
  192. document.onmouseup = null
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. //下拉框懒加载
  200. const SelectLazyLoading={
  201. inserted(el,binding){
  202. let SELECT_DOM = el.querySelector(".el-select-dropdown .el-select-dropdown__wrap") || el.querySelector('.el-autocomplete-suggestion__wrap') ;
  203. // console.log(SELECT_DOM)
  204. if(!SELECT_DOM){
  205. return false
  206. }
  207. SELECT_DOM.addEventListener("scroll", function () {
  208. // scrollHeight:当前所有选项的高度
  209. // scrollTop:滚动的距离
  210. // clientHeight:下拉框的高度
  211. // console.log(SELECT_DOM,this.scrollHeight , this.scrollTop , this.clientHeight,this.scrollHeight - this.scrollTop - 1 <= this.clientHeight)
  212. let condition = this.scrollHeight - this.scrollTop - 1 <= this.clientHeight;
  213. if (condition) {
  214. binding.value();
  215. }
  216. });
  217. }
  218. }
  219. //块禁止样式
  220. const disabled = (el,binding)=>{
  221. if(binding.value){
  222. // el.style.pointerEvents = 'none';
  223. // el.style.cursor='not-allowed';
  224. el.classList.add('pointerEvents')
  225. if(el.parentNode){
  226. // el.parentNode.style.cursor='not-allowed';
  227. // el.parentNode.style.pointerEvents='none';
  228. el.parentNode.classList.add('disabled')
  229. }
  230. el.style.color='#bbb'
  231. }else{
  232. // el.style.pointerEvents = '';
  233. // el.style.cursor='';
  234. el.classList.remove('pointerEvents')
  235. if(el.parentNode){
  236. el.parentNode.classList.remove('disabled')
  237. // el.parentNode.style.cursor='';
  238. // el.parentNode.style.pointerEvents='';
  239. }
  240. el.style.color=''
  241. }
  242. }
  243. //获取高度
  244. const DivHeight={
  245. inserted(el,binding){
  246. if(el){
  247. let height = window.innerHeight
  248. binding.value(el.offsetHeight - 10)
  249. window.addEventListener('resize',()=>{
  250. let newHeight = window.innerHeight;
  251. if (newHeight !== height) {
  252. binding.value(el.offsetHeight - 10)
  253. height = newHeight;
  254. }
  255. })
  256. }
  257. },
  258. unbind(el,binding){
  259. if(el){
  260. window.removeEventListener('resize',()=>{
  261. binding.value(el.offsetHeight - 10)
  262. })
  263. }
  264. },
  265. }
  266. //等高
  267. const equal_heights = {
  268. inserted(el, binding,vnode) {
  269. var that = vnode.context
  270. // 获取另一个容器的引用
  271. const otherContainerSelector = binding.value;
  272. that.$nextTick(()=>{
  273. const otherContainer = that.$refs[otherContainerSelector];
  274. if (!otherContainer) {
  275. console.error('Unable to find the other container');
  276. return;
  277. }
  278. window.addEventListener("resize",that.$commonJS.debounce(()=>{
  279. const children = el.children;
  280. const otherChildren = otherContainer.children;
  281. for (let i = 0; i < children.length && i < otherChildren.length; i++) {
  282. children[i].style.height = 'auto';
  283. otherChildren[i].style.height = 'auto';
  284. const maxHeight = Math.max(children[i].offsetHeight, otherChildren[i].offsetHeight);
  285. children[i].style.height = maxHeight + 'px';
  286. otherChildren[i].style.height = maxHeight + 'px';
  287. otherChildren[i].style.background = '#f3f4f8';
  288. }
  289. },500))
  290. // 遍历当前容器和另一个容器的子div,并设置对应索引的高度
  291. const children = el.children;
  292. const otherChildren = otherContainer.children;
  293. for (let i = 0; i < children.length && i < otherChildren.length; i++) {
  294. const maxHeight = Math.max(children[i].offsetHeight, otherChildren[i].offsetHeight);
  295. children[i].style.height = maxHeight + 'px';
  296. otherChildren[i].style.height = maxHeight + 'px';
  297. otherChildren[i].style.background = '#f3f4f8';
  298. }
  299. })
  300. },
  301. // 如果需要,可以在更新时再次执行逻辑
  302. componentUpdated(el, binding,vnode) {
  303. // 这里可能需要额外的逻辑来处理更新后的高度同步
  304. var that = vnode.context
  305. // 获取另一个容器的引用
  306. const otherContainerSelector = binding.value;
  307. that.$nextTick(()=>{
  308. const otherContainer = that.$refs[otherContainerSelector];
  309. if (!otherContainer) {
  310. console.error('Unable to find the other container');
  311. return;
  312. }
  313. // 遍历当前容器和另一个容器的子div,并设置对应索引的高度
  314. const children = el.children;
  315. const otherChildren = otherContainer.children;
  316. for (let i = 0; i < children.length && i < otherChildren.length; i++) {
  317. const maxHeight = Math.max(children[i].offsetHeight, otherChildren[i].offsetHeight);
  318. children[i].style.height = maxHeight + 'px';
  319. otherChildren[i].style.height = maxHeight + 'px';
  320. otherChildren[i].style.background = '#f3f4f8';
  321. }
  322. })
  323. },
  324. unbind(el) {
  325. var eventListener = window.eventListeners_
  326. if(!eventListener){
  327. return false
  328. }
  329. const listeners = eventListener.resize
  330. if(listeners){
  331. window.removeEventListener("resize");
  332. }
  333. },
  334. }
  335. import elTableInfiniteScroll from './tableInfiniteScroll.js'
  336. const directives = {
  337. draggable,
  338. SelectLazyLoading,
  339. disabled,
  340. DivHeight,
  341. equal_heights,
  342. shake,
  343. dialogDrag,
  344. elTableInfiniteScroll
  345. };
  346. // 这种写法可以批量注册指令
  347. export default {
  348. install(Vue) {
  349. Object.keys(directives).forEach((key) => {
  350. Vue.directive(key, directives[key]);
  351. });
  352. },
  353. };