-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.js
45 lines (35 loc) · 1.09 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
"use strict";
// Handles and manipulates the window history using shebanged location anchors.
module.exports = History;
function History(window, handler) {
this.handler = handler;
this.window = window;
this.location = window.location;
this.history = window.history;
this.window.addEventListener("hashchange", this);
}
History.prototype.destroy = function destroy() {
this.window.removeEventListener("hashchange", this);
};
History.prototype.handleEvent = function handleEvent(event) {
event.preventDefault();
event.stopPropagation();
this.update();
};
History.prototype.update = function update() {
var hash = this.location.hash;
if (hash.lastIndexOf("#!", 0) === 0) {
this.handler.setPath(hash.slice(2));
} else {
this.handler.setPath("index");
}
};
History.prototype.push = function push(path) {
this.history.pushState(null, null, "#!" + path);
};
History.prototype.pop = function pop() {
this.history.popState();
};
History.prototype.replace = function replace(path) {
this.history.replaceState(null, null, "#!" + path);
};