// 引入拖拽js import { startDrag } from './drag.js' /** * 为el-dialog弹框增加拖拽功能 * @param {*} el 指定dom * @param {*} binding 绑定对象 * desc 只要用到了el-dialog的组件,都可以通过增加v-draggable属性变为可拖拽的弹框 */ const draggable = (el, binding) => { // 绑定拖拽事件 [绑定拖拽触发元素为弹框头部、拖拽移动元素为整个弹框] startDrag(el.querySelector('.el-dialog__header'), el.querySelector('.el-dialog'), binding.value); }; //下拉框懒加载 const SelectLazyLoading={ inserted(el,binding){ let SELECT_DOM = el.querySelector(".el-select-dropdown .el-select-dropdown__wrap") || el.querySelector('.el-autocomplete-suggestion__wrap') ; // console.log(SELECT_DOM) if(!SELECT_DOM){ return false } SELECT_DOM.addEventListener("scroll", function () { // scrollHeight:当前所有选项的高度 // scrollTop:滚动的距离 // clientHeight:下拉框的高度 // console.log(SELECT_DOM,this.scrollHeight , this.scrollTop , this.clientHeight,this.scrollHeight - this.scrollTop - 1 <= this.clientHeight) let condition = this.scrollHeight - this.scrollTop - 1 <= this.clientHeight; if (condition) { binding.value(); } }); } } //块禁止样式 const disabled = (el,binding)=>{ if(binding.value){ // el.style.pointerEvents = 'none'; // el.style.cursor='not-allowed'; el.classList.add('pointerEvents') if(el.parentNode){ // el.parentNode.style.cursor='not-allowed'; // el.parentNode.style.pointerEvents='none'; el.parentNode.classList.add('disabled') } el.style.color='#bbb' }else{ // el.style.pointerEvents = ''; // el.style.cursor=''; el.classList.remove('pointerEvents') if(el.parentNode){ el.parentNode.classList.remove('disabled') // el.parentNode.style.cursor=''; // el.parentNode.style.pointerEvents=''; } el.style.color='' } } //获取高度 const DivHeight={ inserted(el,binding){ if(el){ let height = window.innerHeight binding.value(el.offsetHeight - 10) window.addEventListener('resize',()=>{ let newHeight = window.innerHeight; if (newHeight !== height) { binding.value(el.offsetHeight - 10) height = newHeight; } }) } }, unbind(el,binding){ if(el){ window.removeEventListener('resize',()=>{ binding.value(el.offsetHeight - 10) }) } }, } //等高 const equal_heights = { inserted(el, binding,vnode) { var that = vnode.context // 获取另一个容器的引用 const otherContainerSelector = binding.value; that.$nextTick(()=>{ const otherContainer = that.$refs[otherContainerSelector]; if (!otherContainer) { console.error('Unable to find the other container'); return; } window.addEventListener("resize",that.$commonJS.debounce(()=>{ const children = el.children; const otherChildren = otherContainer.children; for (let i = 0; i < children.length && i < otherChildren.length; i++) { children[i].style.height = 'auto'; otherChildren[i].style.height = 'auto'; const maxHeight = Math.max(children[i].offsetHeight, otherChildren[i].offsetHeight); children[i].style.height = maxHeight + 'px'; otherChildren[i].style.height = maxHeight + 'px'; otherChildren[i].style.background = '#f3f4f8'; } },500)) // 遍历当前容器和另一个容器的子div,并设置对应索引的高度 const children = el.children; const otherChildren = otherContainer.children; for (let i = 0; i < children.length && i < otherChildren.length; i++) { const maxHeight = Math.max(children[i].offsetHeight, otherChildren[i].offsetHeight); children[i].style.height = maxHeight + 'px'; otherChildren[i].style.height = maxHeight + 'px'; otherChildren[i].style.background = '#f3f4f8'; } }) }, // 如果需要,可以在更新时再次执行逻辑 componentUpdated(el, binding,vnode) { // 这里可能需要额外的逻辑来处理更新后的高度同步 var that = vnode.context // 获取另一个容器的引用 const otherContainerSelector = binding.value; that.$nextTick(()=>{ const otherContainer = that.$refs[otherContainerSelector]; if (!otherContainer) { console.error('Unable to find the other container'); return; } // 遍历当前容器和另一个容器的子div,并设置对应索引的高度 const children = el.children; const otherChildren = otherContainer.children; for (let i = 0; i < children.length && i < otherChildren.length; i++) { const maxHeight = Math.max(children[i].offsetHeight, otherChildren[i].offsetHeight); children[i].style.height = maxHeight + 'px'; otherChildren[i].style.height = maxHeight + 'px'; otherChildren[i].style.background = '#f3f4f8'; } }) }, unbind(el) { var eventListener = window.eventListeners_ if(!eventListener){ return false } const listeners = eventListener.resize if(listeners){ window.removeEventListener("resize"); } }, } const directives = { draggable, SelectLazyLoading, disabled, DivHeight, equal_heights }; // 这种写法可以批量注册指令 export default { install(Vue) { Object.keys(directives).forEach((key) => { Vue.directive(key, directives[key]); }); }, };