-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathhistory.js
166 lines (155 loc) · 4.46 KB
/
history.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { getWindow, getDocument } from 'ssr-window';
import $ from './dom7.js';
import { extend } from './utils.js';
const History = {
queue: [],
clearQueue() {
if (History.queue.length === 0) return;
const currentQueue = History.queue.shift();
currentQueue();
},
routerQueue: [],
clearRouterQueue() {
if (History.routerQueue.length === 0) return;
const currentQueue = History.routerQueue.pop();
const { router, stateUrl, action } = currentQueue;
let animate = router.params.animate;
if (router.params.browserHistoryAnimate === false) animate = false;
if (action === 'back') {
router.back({ animate, browserHistory: false });
}
if (action === 'load') {
router.navigate(stateUrl, { animate, browserHistory: false });
}
},
handle(e) {
if (History.blockPopstate) return;
const app = this;
// const mainView = app.views.main;
let state = e.state;
History.previousState = History.state;
History.state = state;
History.allowChange = true;
History.clearQueue();
state = History.state;
if (!state) state = {};
app.views.forEach((view) => {
const router = view.router;
let viewState = state[view.id];
if (!viewState && view.params.browserHistory) {
viewState = {
url: view.router.history[0],
};
}
if (!viewState) return;
const stateUrl = viewState.url || undefined;
let animate = router.params.animate;
if (router.params.browserHistoryAnimate === false) animate = false;
if (stateUrl !== router.url) {
if (router.history.indexOf(stateUrl) >= 0) {
// Go Back
if (router.allowPageChange) {
router.back({ animate, browserHistory: false });
} else {
History.routerQueue.push({
action: 'back',
router,
});
}
} else if (router.allowPageChange) {
// Load page
router.navigate(stateUrl, { animate, browserHistory: false });
} else {
History.routerQueue.unshift({
action: 'load',
stateUrl,
router,
});
}
}
});
},
initViewState(viewId, viewState) {
const window = getWindow();
const newState = extend({}, History.state || {}, {
[viewId]: viewState,
});
History.state = newState;
window.history.replaceState(newState, '');
},
push(viewId, viewState, url) {
const window = getWindow();
const document = getDocument();
/* eslint-disable no-param-reassign */
if (url.substr(-3) === '#!/') {
url = url.replace('#!/', '');
if (url === '') {
url = document.location.href;
if (url.includes('#!/')) {
url = document.location.href.split('#!/')[0];
}
}
}
/* eslint-enable no-param-reassign */
if (!History.allowChange) {
History.queue.push(() => {
History.push(viewId, viewState, url);
});
return;
}
History.previousState = History.state;
const newState = extend({}, History.previousState || {}, {
[viewId]: viewState,
});
History.state = newState;
window.history.pushState(newState, '', url);
},
replace(viewId, viewState, url) {
const window = getWindow();
if (url.substr(-3) === '#!/') {
// eslint-disable-next-line
url = url.replace('#!/', '');
}
if (!History.allowChange) {
History.queue.push(() => {
History.replace(viewId, viewState, url);
});
return;
}
History.previousState = History.state;
const newState = extend({}, History.previousState || {}, {
[viewId]: viewState,
});
History.state = newState;
window.history.replaceState(newState, '', url);
},
go(index) {
const window = getWindow();
History.allowChange = false;
window.history.go(index);
},
back() {
const window = getWindow();
History.allowChange = false;
window.history.back();
},
allowChange: true,
previousState: {},
state: {},
blockPopstate: true,
init(app) {
const window = getWindow();
const document = getDocument();
History.state = window.history.state;
$(window).on('load', () => {
setTimeout(() => {
History.blockPopstate = false;
}, 0);
});
if (document.readyState && document.readyState === 'complete') {
History.blockPopstate = false;
}
$(window).on('popstate', History.handle.bind(app));
},
};
export default History;