forked from framework7io/framework7
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush-state.js
More file actions
112 lines (106 loc) · 4.04 KB
/
push-state.js
File metadata and controls
112 lines (106 loc) · 4.04 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
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
/*======================================================
************ Handle Browser's History ************
======================================================*/
app.pushStateQueue = [];
app.pushStateClearQueue = function () {
if (app.pushStateQueue.length === 0) return;
var queue = app.pushStateQueue.pop();
var animatePages;
if (app.params.pushStateNoAnimation === true) animatePages = false;
if (queue.action === 'back') {
app.router.back(queue.view, {animatePages: animatePages});
}
if (queue.action === 'loadPage') {
app.router.load(queue.view, {url: queue.stateUrl, animatePages: animatePages, pushState: false});
}
if (queue.action === 'loadContent') {
app.router.load(queue.view, {content: queue.stateContent, animatePages: animatePages, pushState: false});
}
if (queue.action === 'loadPageName') {
app.router.load(queue.view, {pageName: queue.statePageName, url: queue.stateUrl, animatePages: animatePages, pushState: false});
}
};
app.initPushState = function () {
var blockPopstate = true;
$(window).on('load', function () {
setTimeout(function () {
blockPopstate = false;
}, 0);
});
if (document.readyState && document.readyState === 'complete') {
blockPopstate = false;
}
function handlePopState(e) {
if (blockPopstate) return;
var mainView = app.mainView;
if (!mainView) return;
var state = e.state;
if (!state) {
state = {
viewIndex: app.views.indexOf(mainView),
url : mainView.history[0]
};
}
if (state.viewIndex < 0) return;
var view = app.views[state.viewIndex];
var stateUrl = state && state.url || undefined;
var stateContent = state && state.content || undefined;
var statePageName = state && state.pageName || undefined;
var animatePages;
if (app.params.pushStateNoAnimation === true) animatePages = false;
if (stateUrl !== view.url) {
if (view.history.indexOf(stateUrl) >= 0) {
// Go Back
if (view.allowPageChange) {
app.router.back(view, {url:undefined, animatePages: animatePages, pushState: false, preloadOnly:false});
}
else {
app.pushStateQueue.push({
action: 'back',
view: view
});
}
}
else if (stateContent) {
// Load Page
if (view.allowPageChange) {
app.router.load(view, {content:stateContent, animatePages: animatePages, pushState: false});
}
else {
app.pushStateQueue.unshift({
action: 'loadContent',
stateContent: stateContent,
view: view
});
}
}
else if (statePageName) {
// Load Page by page name with Dom Cache
if (view.allowPageChange) {
app.router.load(view, {pageName:statePageName, url: stateUrl, animatePages: animatePages, pushState: false});
}
else {
app.pushStateQueue.unshift({
action: 'loadPageName',
statePageName: statePageName,
view: view
});
}
}
else {
// Load Page
if (view.allowPageChange) {
app.router.load(view, {url:stateUrl, animatePages: animatePages, pushState: false});
}
else {
app.pushStateQueue.unshift({
action: 'loadPage',
stateUrl: stateUrl,
view: view
});
}
}
}
}
$(window).on('popstate', handlePopState);
};