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