vben
authored
5 years ago
1
2
3
4
5
6
7
import type { Ref } from 'vue';
import { computed, unref, onMounted, nextTick, ref } from 'vue';
import { TriggerEnum } from '/@/enums/menuEnum';
import { useMenuSetting } from '/@/hooks/setting/useMenuSetting';
Vben
authored
4 years ago
8
import { useDebounceFn } from '@vueuse/core';
vben
authored
5 years ago
9
10
11
12
13
14
15
/**
* Handle related operations of menu events
*/
export function useSiderEvent() {
const brokenRef = ref(false);
Vben
authored
4 years ago
16
const { getMiniWidthNumber } = useMenuSetting();
vben
authored
5 years ago
17
18
19
20
21
22
23
24
25
const getCollapsedWidth = computed(() => {
return unref(brokenRef) ? 0 : unref(getMiniWidthNumber);
});
function onBreakpointChange(broken: boolean) {
brokenRef.value = broken;
}
Vben
authored
4 years ago
26
return { getCollapsedWidth, onBreakpointChange };
vben
authored
5 years ago
27
28
29
30
31
}
/**
* Handle related operations of menu folding
*/
vben
authored
5 years ago
32
33
export function useTrigger(getIsMobile: Ref<boolean>) {
const { getTrigger, getSplit } = useMenuSetting();
vben
authored
5 years ago
34
vben
authored
5 years ago
35
const getShowTrigger = computed(() => {
vben
authored
5 years ago
36
const trigger = unref(getTrigger);
vben
authored
5 years ago
37
38
39
40
41
42
return (
trigger !== TriggerEnum.NONE &&
!unref(getIsMobile) &&
(trigger === TriggerEnum.FOOTER || unref(getSplit))
);
vben
authored
5 years ago
43
44
45
});
const getTriggerAttr = computed(() => {
vben
authored
5 years ago
46
if (unref(getShowTrigger)) {
vben
authored
5 years ago
47
48
49
50
51
52
53
return {};
}
return {
trigger: null,
};
});
vben
authored
5 years ago
54
return { getTriggerAttr, getShowTrigger };
vben
authored
5 years ago
55
56
57
58
59
60
61
}
/**
* Handle menu drag and drop related operations
* @param siderRef
* @param dragBarRef
*/
vben
authored
5 years ago
62
export function useDragLine(siderRef: Ref<any>, dragBarRef: Ref<any>, mix = false) {
vben
authored
5 years ago
63
const { getMiniWidthNumber, getCollapsed, setMenuSetting } = useMenuSetting();
vben
authored
5 years ago
64
65
66
onMounted(() => {
nextTick(() => {
Vben
authored
4 years ago
67
const exec = useDebounceFn(changeWrapWidth, 80);
vben
authored
5 years ago
68
69
70
71
exec();
});
});
vben
authored
5 years ago
72
73
74
75
76
77
78
79
80
function getEl(elRef: Ref<ElRef | ComponentRef>): any {
const el = unref(elRef);
if (!el) return null;
if (Reflect.has(el, '$el')) {
return (unref(elRef) as ComponentRef)?.$el;
}
return unref(elRef);
}
vben
authored
5 years ago
81
82
83
84
function handleMouseMove(ele: HTMLElement, wrap: HTMLElement, clientX: number) {
document.onmousemove = function (innerE) {
let iT = (ele as any).left + (innerE.clientX - clientX);
innerE = innerE || window.event;
vben
authored
5 years ago
85
const maxT = 800;
vben
authored
5 years ago
86
87
88
89
90
91
92
93
94
95
96
const minT = unref(getMiniWidthNumber);
iT < 0 && (iT = 0);
iT > maxT && (iT = maxT);
iT < minT && (iT = minT);
ele.style.left = wrap.style.width = iT + 'px';
return false;
};
}
// Drag and drop in the menu area-release the mouse
function removeMouseup(ele: any) {
vben
authored
5 years ago
97
const wrap = getEl(siderRef);
vben
authored
5 years ago
98
99
100
document.onmouseup = function () {
document.onmousemove = null;
document.onmouseup = null;
vben
authored
5 years ago
101
wrap.style.transition = 'width 0.2s';
vben
authored
5 years ago
102
103
const width = parseInt(wrap.style.width);
vben
authored
5 years ago
104
105
106
107
108
109
110
111
112
if (!mix) {
const miniWidth = unref(getMiniWidthNumber);
if (!unref(getCollapsed)) {
width > miniWidth + 20
? setMenuSetting({ menuWidth: width })
: setMenuSetting({ collapsed: true });
} else {
width > miniWidth && setMenuSetting({ collapsed: false, menuWidth: width });
}
vben
authored
5 years ago
113
} else {
vben
authored
5 years ago
114
setMenuSetting({ menuWidth: width });
vben
authored
5 years ago
115
}
vben
authored
5 years ago
116
vben
authored
5 years ago
117
118
119
120
121
ele.releaseCapture?.();
};
}
function changeWrapWidth() {
vben
authored
5 years ago
122
123
124
125
126
const ele = getEl(dragBarRef);
if (!ele) return;
const wrap = getEl(siderRef);
if (!wrap) return;
vben
authored
5 years ago
127
128
129
130
131
132
133
134
135
ele.onmousedown = (e: any) => {
wrap.style.transition = 'unset';
const clientX = e?.clientX;
ele.left = ele.offsetLeft;
handleMouseMove(ele, wrap, clientX);
removeMouseup(ele);
ele.setCapture?.();
return false;
};
vben
authored
5 years ago
136
137
}
vben
authored
5 years ago
138
return {};
vben
authored
5 years ago
139
}