-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory-state.js
136 lines (108 loc) · 3.34 KB
/
history-state.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import calculateState from './calculate-state.js';
import defaultState from './default-state.js';
import isNormalLeftClick from './is-normal-left-click.js';
import isNormalLink from './is-normal-link.js';
let dirtyAttempt = 0;
let inRevert = false;
let currentState = defaultState;
let defaultInterceptOptions;
const updateState = (newSettings, title, url) => {
history.replaceState(calculateState(0, {...currentState, ...newSettings}), title, url);
currentState = history.state;
};
class HistoryState extends EventTarget {
replaceState(state, title, url) {
updateState({state}, title, url);
}
pushState(state, title, url) {
history.pushState(calculateState(1, {state}), title, url);
spaUpdate();
}
get state() {
return currentState.state;
}
get length() {
return history.length;
}
get scrollRestoration() {
return history.scrollRestoration;
}
set scrollRestoration(value) {
history.scrollRestoration = value;
}
back() {
history.back();
}
forward() {
history.forward();
}
go(delta) {
history.go(delta);
}
}
updateState(history.state);
window.addEventListener('load', () => {
if (defaultInterceptOptions !== false) {
linkInterceptor(document, defaultInterceptOptions);
}
spaUpdate(true);
setTimeout(() => window.addEventListener('popstate', onpopstate), 0);
});
window.addEventListener('beforeunload', event => {
if (currentState.dirty) {
event.preventDefault();
event.returnValue = '';
}
});
const historyState = new HistoryState();
const spaUpdate = initializing => {
if (currentState.dirty && !initializing) {
inRevert = true;
dirtyAttempt = history.state.index - currentState.index;
history.go(-dirtyAttempt);
return;
}
currentState = history.state;
historyState.dispatchEvent(new Event('update'));
};
const onpopstate = () => {
if (inRevert) {
inRevert = false;
historyState.dispatchEvent(new Event('refuse'));
} else {
spaUpdate();
}
};
export const navigateTo = url => historyState.pushState(null, '', new URL(url, location.href).href);
export const linkInterceptor = (listener, listenerOptions) => {
listener.addEventListener('click', event => {
if (event.defaultPrevented || !isNormalLeftClick(event)) {
return;
}
const element = event.composedPath().find(element => element.tagName === 'A');
if (!isNormalLink(element)) {
return;
}
const destination = new URL(element.href, location.href).toString();
if (destination.startsWith(document.baseURI.replace(/[?#].*/u, ''))) {
element.blur();
event.preventDefault();
event.stopPropagation();
historyState.pushState(null, '', destination);
}
}, listenerOptions);
};
export const setDefaultInterceptOptions = options => {
defaultInterceptOptions = options;
};
export const bypassDirty = () => {
if (dirtyAttempt !== 0) {
const attempt = dirtyAttempt;
dirtyAttempt = 0;
currentState.dirty = false;
history.go(attempt);
}
};
export const setDirty = dirty => updateState({dirty});
export const isDirty = () => currentState.dirty;
export default historyState;