-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashLocationTracker.js
40 lines (34 loc) · 1.02 KB
/
hashLocationTracker.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
class HashLocationTracker {
constructor() {
this.registerByPattern={};
window.addEventListener("hashchange", () => {
this.matchNow();
});
}
given(pattern){
let registered={
then: function(fn) {
this.functionObject=fn;
},
invoke: function(matchResult) {
if(this.functionObject) {
this.functionObject(matchResult);
}
}
};
this.registerByPattern["^"+pattern+"$"]=registered;
return registered;
}
matchNow() {
let hash=window.location.hash;
console.log("hash: "+window.location.hash);
for(let pattern in this.registerByPattern) {
let matchResult=new RegExp(pattern).exec(hash);
if(matchResult){
this.registerByPattern[pattern].invoke(matchResult);
}
}
}
}
const hashLocationTracker=new HashLocationTracker();
export {hashLocationTracker};