forked from josdejong/jsoneditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.js
133 lines (120 loc) · 3.04 KB
/
hash.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
/**
* @prototype Hash
* This prototype contains methods to manipulate the hash of the web page
*/
function Hash() {}
/**
* get an object value with all parameters in the hash
* @return {Object} query object containing key/values
*/
Hash.prototype.getQuery = function () {
var hash = window.location.hash.substring(1); // skip the # character
var params = hash.split('&');
var query = {};
for (var i = 0, iMax = params.length; i < iMax; i++) {
var keyvalue = params[i].split('=');
if (keyvalue.length == 2) {
var key = decodeURIComponent(keyvalue[0]);
var value = decodeURIComponent(keyvalue[1]);
query[key] = value;
}
}
return query;
};
/**
* Register a callback function which will be called when the hash of the web
* page changes.
* @param {String} key
* @param {function} callback Will be called with the new value as parameter
*/
Hash.prototype.onChange = function (key, callback) {
this.prevHash = '';
var me = this;
if (!me.callbacks) {
me.callbacks = [];
}
me.callbacks.push({
'key': key,
'value': undefined,
'callback': callback
});
function checkForChanges() {
for (var i = 0; i < me.callbacks.length; i++) {
var obj = me.callbacks[i];
var value = me.getValue(obj.key);
var changed = (value !== obj.value);
obj.value = value;
if (changed) {
obj.callback(value);
}
}
}
// source: http://stackoverflow.com/questions/2161906/handle-url-anchor-change-event-in-js
if ('onhashchange' in window) {
window.onhashchange = function () {
checkForChanges();
}
}
else {
// onhashchange event not supported
me.prevHash = window.location.hash;
window.setInterval(function () {
var hash = window.location.hash;
if (hash != me.prevHash) {
me.prevHash = hash;
checkForChanges();
}
}, 500);
}
};
/**
* Set hash parameters
* @param {Object} query object with strings
*/
Hash.prototype.setQuery = function (query) {
var hash = '';
for (var key in query) {
if (query.hasOwnProperty(key)) {
var value = query[key];
if (value != undefined) {
if (hash.length) {
hash += '&';
}
hash += encodeURIComponent(key);
hash += '=';
hash += encodeURIComponent(query[key]);
}
}
}
window.location.hash = (hash.length ? ('#' + hash) : '');
};
/**
* Retrieve a parameter value from the hash
* @param {String} key
* @return {String | undefined} value undefined when the value is not found
*/
Hash.prototype.getValue = function (key) {
var query = this.getQuery();
return query[key];
};
/**
* Set an hash parameter
* @param {String} key
* @param {String} value
*/
Hash.prototype.setValue = function (key, value) {
var query = this.getQuery();
query[key] = value;
this.setQuery(query);
};
/**
* Remove an hash parameter
* @param {String} key
*/
Hash.prototype.removeValue = function (key) {
var query = this.getQuery();
if (query[key]) {
delete query[key];
this.setQuery(query);
}
};