import $http from '@/utils/http-service';
import { nextTick } from 'vue';
* 功能:触发keepAlive接口的调用(超过10s时)
* 注册一个全局自定义指令 `v-reqKeepAlive`
* 因为element-plus的dialog、drawer、tree在点击其内容区域的时候不触发window的点击事件,
* 导致触发不了会话保持(不调用keepAlive),所以定义自定义指令,为其绑定点击事件,触发keepAlive
* 注意:
* 1. v-reqKeepAlive
* 2. 添加类型修饰符,比如v-reqKeepAlive.dialog
* 所有的修饰符:dialog(弹窗)、drawer(抽屉)、tree(树)
* 3. 指令可以直接加到el-tree标签上,但是不能直接加到el-dialog、el-drawer标签上,否则会报警告,且指令绑定失败,需要加在外层
* 4. el-dialog和el-drawer不能直接挂载在body下,否则自定义指令找不到对应的dom,append-to-body默认是false,不要设置成true
*/
export function reqKeepAliveDerective(app: any) {
app.directive('reqKeepAlive', {
mounted(el: any, binding: any) {
mountedAction(el, binding);
}
});
}
function mountedAction(el: any, binding: any) {
Object.keys(binding.modifiers).forEach(key => {
switch (key) {
case 'dialog':
nextTick(() => {
action(el, 'el-dialog');
});
break;
case 'drawer':
nextTick(() => {
action(el, 'el-drawer');
});
break;
case 'tree':
nextTick(() => {
treeAction(el);
el.addEventListener('DOMNodeInserted', () => {
clickAction();
treeAction(el);
});
});
break;
default:
break;
}
});
}
function findAimClassDom(el: any, className: string): any {
if (el?.children) {
const children = el.children;
for (let i = 0; i < children.length; i++) {
const classList = children[i].classList;
for (let j = 0; j < classList.length; j++) {
if (classList[j] === className) {
return children[i];
}
}
const res = findAimClassDom(children[i], className);
if (res) {
return res;
}
}
}
return null;
}
function action(el: any, className: string) {
const dom = findAimClassDom(el, className);
if (dom) {
dom.addEventListener('click', () => {
$http.keepAlive('Activate');
});
}
}
function clickAction() {
$http.keepAlive('Activate');
}
function treeAction(el: any) {
let arr: any = [];
findAimClassDoms(el, 'el-tree-node__content', arr);
for (let i = 0; i < arr.length; i++) {
arr[i].removeEventListener('click', clickAction);
arr[i].addEventListener('click', clickAction);
}
}
function findAimClassDoms(dom: any, className: string, arr: any) {
if (dom?.children) {
const children = dom.children;
for (let i = 0; i < children.length; i++) {
const classList = children[i].classList;
for (let j = 0; j < classList.length; j++) {
if (classList[j] === className) {
arr.push(children[i]);
}
}
findAimClassDoms(children[i], className, arr);
}
}
}