2016-09-26 09:28:47 +00:00
|
|
|
|
2019-02-14 15:26:13 +00:00
|
|
|
var HashListener = require('./hash-listener');
|
2016-09-26 09:28:47 +00:00
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
/**
|
|
|
|
* Class to handle the URL.
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2016-09-26 09:28:47 +00:00
|
|
|
module.exports =
|
2015-07-28 19:14:26 +00:00
|
|
|
{
|
|
|
|
_hash: null
|
|
|
|
,_hashMap: {}
|
|
|
|
,_listener: null
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2019-02-05 16:20:24 +00:00
|
|
|
,initialize: function() {
|
2019-02-14 15:26:13 +00:00
|
|
|
this._listener = new HashListener();
|
2015-07-28 19:14:26 +00:00
|
|
|
|
2019-02-14 15:26:13 +00:00
|
|
|
this._hashChangedHandler = this._hashChanged.bind(this);
|
|
|
|
window.addEventListener('hashchange', this._hashChangedHandler);
|
|
|
|
this._hashChanged();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 16:20:24 +00:00
|
|
|
,destroy: function() {
|
2019-02-14 15:26:13 +00:00
|
|
|
window.removeEventListener('hashchange', this._hashChangedHandler);
|
2015-07-28 19:14:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 16:20:24 +00:00
|
|
|
,getListener: function() {
|
2015-07-28 19:14:26 +00:00
|
|
|
return this._listener;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the hash part of the URL.
|
|
|
|
*
|
|
|
|
* @param {string} key The variable name
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2019-02-05 16:20:24 +00:00
|
|
|
,get: function(key) {
|
2015-07-28 19:14:26 +00:00
|
|
|
return this._hashMap[key];
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2021-03-31 10:18:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsets a hash key.
|
|
|
|
*
|
|
|
|
* @param {string} key The variable name
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2021-03-31 10:18:25 +00:00
|
|
|
,unset: function(key) {
|
|
|
|
this.add({[key]: undefined});
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the hash part of the URL, respecting the current hash variables.
|
|
|
|
*
|
|
|
|
* @param {Object} map A key-value map
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2019-02-05 16:20:24 +00:00
|
|
|
,add: function(map) {
|
2015-07-28 19:14:26 +00:00
|
|
|
var newMap = this._hashMap;
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
for (var key in map)
|
|
|
|
newMap[key] = map[key];
|
|
|
|
|
2019-02-14 15:26:13 +00:00
|
|
|
this.set(newMap);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the hash part of the URL.
|
|
|
|
*
|
|
|
|
* @param {Object} map A key-value map
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2019-02-05 16:20:24 +00:00
|
|
|
,set: function(map) {
|
2015-12-10 13:48:43 +00:00
|
|
|
if (map)
|
2015-01-23 13:09:30 +00:00
|
|
|
for (var key in map)
|
|
|
|
if (map[key] === null || map[key] === undefined)
|
|
|
|
delete map[key];
|
|
|
|
|
2019-02-14 15:26:13 +00:00
|
|
|
var newHash = this.make(map);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-12-10 13:48:43 +00:00
|
|
|
if (!map)
|
|
|
|
map = {};
|
|
|
|
|
2019-02-05 16:20:24 +00:00
|
|
|
if (newHash !== this._hash) {
|
2015-07-28 19:14:26 +00:00
|
|
|
this._hashMap = map;
|
|
|
|
this._hash = newHash;
|
|
|
|
|
|
|
|
this._blockChanged = true;
|
|
|
|
location.hash = newHash;
|
|
|
|
this._blockChanged = false;
|
|
|
|
|
2019-02-14 15:26:13 +00:00
|
|
|
this._listener.changed();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a URL with the given hash data.
|
|
|
|
*
|
|
|
|
* @param {Object} map A key-value map
|
|
|
|
* @param {boolean} add %true to combine with the current map, %false otherwise
|
|
|
|
* @return {String} The URL
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2019-02-05 16:20:24 +00:00
|
|
|
,make: function(map, add) {
|
2015-01-23 13:09:30 +00:00
|
|
|
var hash = '#!';
|
|
|
|
|
2015-12-10 13:48:43 +00:00
|
|
|
if (add && map)
|
2015-07-28 19:14:26 +00:00
|
|
|
for (var key in this._hashMap)
|
2015-01-23 13:09:30 +00:00
|
|
|
if (!map[key])
|
2015-07-28 19:14:26 +00:00
|
|
|
map[key] = this._hashMap[key];
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2019-02-05 16:20:24 +00:00
|
|
|
for (var key in map) {
|
2015-01-23 13:09:30 +00:00
|
|
|
if (hash.length > 2)
|
|
|
|
hash += '&';
|
|
|
|
|
2019-02-05 16:20:24 +00:00
|
|
|
hash += encodeURIComponent(key) +'='+ encodeURIComponent(map[key]);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2019-02-05 16:20:24 +00:00
|
|
|
,_hashChanged: function() {
|
2015-01-23 13:09:30 +00:00
|
|
|
var newHash = location.hash;
|
|
|
|
|
2015-07-28 19:14:26 +00:00
|
|
|
if (this._blockChanged || newHash === this._hash)
|
2015-01-23 13:09:30 +00:00
|
|
|
return;
|
|
|
|
|
2015-07-28 19:14:26 +00:00
|
|
|
var newMap = hashMap = {};
|
2019-02-14 15:26:13 +00:00
|
|
|
var kvPairs = newHash.substr(2).split('&');
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2019-02-05 16:20:24 +00:00
|
|
|
for (var i = 0; i < kvPairs.length; i++) {
|
2019-02-14 15:26:13 +00:00
|
|
|
var kvPair = kvPairs[i].split('=', 2);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
if (kvPair[0])
|
2019-02-05 16:20:24 +00:00
|
|
|
newMap[decodeURIComponent(kvPair[0])] = decodeURIComponent(kvPair[1]);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 19:14:26 +00:00
|
|
|
this._hashMap = newMap;
|
|
|
|
this._hash = newHash;
|
2019-02-14 15:26:13 +00:00
|
|
|
this._listener.changed();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-07-28 19:14:26 +00:00
|
|
|
};
|