-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
139 lines (113 loc) · 3.62 KB
/
index.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
'use strict';
module.exports = function () {
function Storicu() {
var _this = this;
var states = [];
var stateIndex;
var actions = [];
var buildStoricuState = function buildStoricuState(historyState, index) {
return {
state: historyState,
index: index,
storicu: true
};
};
var handlePopState = function handlePopState(e) {
var newState = e.state;
if (null === newState || !newState.storicu) {
return;
}
var action = 0 === actions.length ? null : actions.splice(0, 1)[0];
var previousStateIndex = stateIndex;
stateIndex = newState.index;
if (action && 'CLEAN_FORWARD_HISTORY' === action.type) {
stateIndex += 1;
states.splice(stateIndex + 1, states.length - (stateIndex + 1));
window.history.pushState(action.payload, null, null);
return;
}
var triggeredByAPI = false;
if (action && 'GO' === action.type) {
triggeredByAPI = true;
} // skip ghost state
if (stateIndex <= 0) {
window.history.go(-1);
return;
} // state change callback ({state}, delta, isTriggeredByAPI)
if (undefined !== _this.onpopstate) {
_this.onpopstate({
state: newState.state
}, stateIndex - previousStateIndex, triggeredByAPI);
}
};
var init = function init() {
if ('about:srcdoc' === window.location.href) {
return;
}
var historyState = window.history.state;
if (historyState && historyState.storicu) {
historyState = historyState.state;
}
var ghostState = buildStoricuState(historyState, 0);
window.history.replaceState(ghostState, null, null);
states.push(ghostState);
var firstState = buildStoricuState(historyState, 1);
window.history.pushState(firstState, null, null);
states.push(firstState);
stateIndex = 1;
window.onpopstate = handlePopState;
};
this.replaceState = function (state, title, url) {
var currentState = states[stateIndex];
currentState.state = state;
window.history.replaceState(currentState, title, url);
};
this.pushState = function (state, title, url) {
stateIndex += 1;
var newState = buildStoricuState(state, stateIndex);
states.splice(stateIndex, states.length - stateIndex, newState);
window.history.pushState(newState, title, url);
};
this.go = function (delta) {
if (delta === 0) {
return;
}
if (stateIndex + delta <= 0) {
// skipping ghost state
delta -= 1;
}
stateIndex = stateIndex + delta;
actions.push({
type: 'GO',
payload: null
});
window.history.go(delta);
};
this.forward = function () {
var distance = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
return _this.go(distance);
};
this.back = function () {
var distance = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
return _this.go(-distance);
};
this.cleanForwardHistory = function () {
var delta = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
// HACK because successive calls may overlap
actions.push({
type: 'CLEAN_FORWARD_HISTORY',
payload: states[stateIndex + delta]
});
window.history.go(-1 + delta);
};
this.getStateIndex = function () {
return stateIndex - 1;
};
this.onpopstate = undefined;
init();
}
if (undefined === window.storicu) {
window.storicu = new Storicu();
}
return window.storicu;
}();