-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathunsaved-changes-alert.js
More file actions
81 lines (69 loc) · 1.99 KB
/
Copy pathunsaved-changes-alert.js
File metadata and controls
81 lines (69 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
window.setUpUnsavedDataChangesAlert = ({ body, livewireComponent, $wire }) => {
window.addEventListener('beforeunload', (event) => {
if (
window.jsMd5(JSON.stringify($wire.data).replace(/\\/g, '')) ===
$wire.savedDataHash ||
$wire?.__instance?.effects?.redirect
) {
return
}
event.preventDefault()
event.returnValue = true
})
}
window.setUpSpaModeUnsavedDataChangesAlert = ({
body,
resolveLivewireComponentUsing,
$wire,
}) => {
const shouldPreventNavigation = () => {
if ($wire?.__instance?.effects?.redirect) {
return false
}
return (
window.jsMd5(JSON.stringify($wire.data).replace(/\\/g, '')) !==
$wire.savedDataHash
)
}
const showUnsavedChangesAlert = () => {
return confirm(body)
}
document.addEventListener('livewire:navigate', (event) => {
if (typeof resolveLivewireComponentUsing() !== 'undefined') {
if (!shouldPreventNavigation()) {
return
}
if (showUnsavedChangesAlert()) {
return
}
event.preventDefault()
}
})
window.addEventListener('beforeunload', (event) => {
if (!shouldPreventNavigation()) {
return
}
event.preventDefault()
event.returnValue = true
})
}
window.setUpUnsavedActionChangesAlert = ({
resolveLivewireComponentUsing,
$wire,
}) => {
window.addEventListener('beforeunload', (event) => {
if (typeof resolveLivewireComponentUsing() === 'undefined') {
return
}
if (
($wire.mountedActions ?? []).some(
(mountedAction) => mountedAction.hasUnsavedChangesAlert ?? true,
) &&
!$wire?.__instance?.effects?.redirect
) {
event.preventDefault()
event.returnValue = true
return
}
})
}