-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (85 loc) · 1.52 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
/**
* history
* History component
*
* @copyright 2013 Enrico Marino and Federico Spini
* @license MIT
*/
/**
* Expose `History` singleton
*/
module.exports = history;
/**
* singleton
*/
var singleton;
/**
* History
* Create an history.
*
* @constructor
*/
function history () {
if (!singleton) {
singleton = new History();
}
return singleton;
}
function History () {
this._onchange = this._onchange.bind(this);
this.onchange = function () {};
}
history.History = History;
/**
* History.use
* Use a plugin
*
* @params {Function} fn plugin
* @return {History} History constructor
* @api public
*/
History.use = function (fn) {
fn(this);
return this;
};
/**
* onchange
* Load the url, if it's changed.
* It's called by the browser.
*
* @param {Event} event event
* @api private
*/
History.prototype._onchange = function (event) {
this.prev = this.current;
this.current = '#' + event.newURL.split('#')[1]; //window.location.hash;
this.onchange(this);
};
/**
* start
*
* @return {History} this for chaining
* @api public
*/
History.prototype.start = function (current) {
if (this.started) {
return this;
}
this.prev = '#';
this.current = current || window.location.hash;
this.started = true;
window.onhashchange = this._onchange;
this.onchange(this);
return this;
};
/**
* stop
*
* @return {History} this for chaining
* @api public
*/
History.prototype.stop = function () {
this.started = false;
window.onhashchange = this._onchange;
return this;
};