Repository navigation
Expand file tree
/
Copy pathhashjs.js
More file actions
134 lines (114 loc) · 4.18 KB
/
Copy pathhashjs.js
File metadata and controls
134 lines (114 loc) · 4.18 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Hashjs http://github.com/kaerus/hashjs
* Released under the MIT License, feel free to use, share and modify.
*/
(function (global) {
"use strict"
var PREPEND = '', SEPARATE = '/', APPEND = '', POLLINTERVAL = 250;
function addListener(elm, eType, fn){
if(elm.addEventListener){
elm.addEventListener(eType, fn, false);
} else if (elm.attachEvent){
elm.attachEvent('on' + eType, fn);
}
}
function removeListener(elm, eType, fn){
if(elm.removeEventListener){
elm.removeEventListener(eType, fn, false);
} else if (elm.detachEvent){
elm.detachEvent('on' + eType, fn);
}
}
/* singleton */
var Hasher = {
hash: {
path:null,
onChange: null,
poll: POLLINTERVAL,
_a: APPEND,
_p: PREPEND,
_s: SEPARATE,
change: function(path){
if(this.path !== path){
var old = this.path;
this.path = path;
this.onChange(this, this.toString(old));
}
},
toString: function(path){
if(!path || path === undefined) path = this.path;
var a = path.indexOf(this._p),
b = path.lastIndexOf(this._a);
if(a < 0) a = 0;
else a+=this._p.length;
if(b <= 0) b = path.length;
return path.substr(a,b);
},
toArray: function(path){
return this.toString(path).split(this._s);
},
toPath: function(array){
return this._p + array.join(this._s) + this._a;
}
},
get: function(path) {
return this.hash.toString(path);
},
set: function(path){
this.hash.change(path);
},
update: function(path){
if(Array.isArray(path)){
path = this.hash.toPath(path);
} else {
path = this.hash.toPath([].slice.call(arguments));
}
if(path !== this.hash.path){
this.hash.change(path);
window.location.hash = '#'+ encodeURI(path);
}
},
event: function(event) {
event = event ? event : window.event;
var path = Hasher.uri(event ? event.newURL : undefined);
Hasher.set(path);
},
uri: function(url){
url = url || window.location.href;
var h = url.indexOf('#');
return h < 0 ? '' : decodeURIComponent(url.substr(h+1,url.length));
},
start: function(){
var i = 0;
this.hash.path = this.uri();
/* configure hash divisors */
if(typeof arguments[i] === 'string') this.hash._p = arguments[i++];
if(typeof arguments[i] === 'string') this.hash._s = arguments[i++];
if(typeof arguments[i] === 'string') this.hash._a = arguments[i++];
/* register onChange callback */
if(typeof arguments[i] === 'function') this.hash.onChange = arguments[i++];
/* poll interval in msec (used in fallback mode only) */
if(typeof arguments[i] === 'number') this.hash.poll = arguments[i++];
if('onhashchange' in window) {
addListener(window, 'hashchange', this.event);
} else {
/* fallback mode */
this._timer = setInterval(this.event, this.hash.poll);
}
if(this.hash.onChange) this.hash.onChange(this.hash);
},
stop: function() {
if('onhashchange' in window) {
removeListener(window, 'hashchange', this.event);
} else {
clearInterval(this._timer);
delete this._timer;
}
},
};
if (typeof exports === 'object') {
module.exports = Hasher;
} else if (typeof define === 'function' && define.amd) {
define(function () { return Hasher; });
} else { global.Hasher = Hasher; }
}(this));