2022-01-15 22:02:15 -08:00
|
|
|
import { inject, onUnmounted, Ref } from 'vue';
|
2022-01-16 04:31:09 -08:00
|
|
|
import { onBeforeRouteLeave } from 'vue-router';
|
2022-01-15 22:02:15 -08:00
|
|
|
import { i18n } from '@/i18n';
|
|
|
|
import * as os from '@/os';
|
|
|
|
|
|
|
|
export function useLeaveGuard(enabled: Ref<boolean>) {
|
|
|
|
const setLeaveGuard = inject('setLeaveGuard');
|
|
|
|
|
|
|
|
if (setLeaveGuard) {
|
|
|
|
setLeaveGuard(async () => {
|
|
|
|
if (!enabled.value) return false;
|
|
|
|
|
|
|
|
const { canceled } = await os.confirm({
|
|
|
|
type: 'warning',
|
|
|
|
text: i18n.locale.leaveConfirm,
|
|
|
|
});
|
|
|
|
|
|
|
|
return canceled;
|
|
|
|
});
|
2022-01-16 04:31:09 -08:00
|
|
|
} else {
|
|
|
|
onBeforeRouteLeave(async (to, from) => {
|
|
|
|
if (!enabled.value) return true;
|
|
|
|
|
|
|
|
const { canceled } = await os.confirm({
|
|
|
|
type: 'warning',
|
|
|
|
text: i18n.locale.leaveConfirm,
|
|
|
|
});
|
|
|
|
|
|
|
|
return !canceled;
|
|
|
|
});
|
2022-01-15 22:02:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
function onBeforeLeave(ev: BeforeUnloadEvent) {
|
|
|
|
if (enabled.value) {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.returnValue = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('beforeunload', onBeforeLeave);
|
|
|
|
onUnmounted(() => {
|
|
|
|
window.removeEventListener('beforeunload', onBeforeLeave);
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
}
|