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-01-23 13:09:30 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Gets the value of a URL variable.
|
|
|
|
*
|
|
|
|
* @param {string} key The variable name
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2019-02-14 15:26:13 +00:00
|
|
|
getQuery: function(key) {
|
|
|
|
var regExp = new RegExp('[\?\&]'+ key +'=([^\&]*)(\&?)', 'i');
|
|
|
|
var value = location.search.match(regExp);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
return value ? value[1] : value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the value of a URL variable.
|
|
|
|
*
|
|
|
|
* @param {string} key The variable name
|
|
|
|
* @param {string} value The new value
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2019-02-14 15:26:13 +00:00
|
|
|
,setQuery: function(key, value) {
|
2015-01-23 13:09:30 +00:00
|
|
|
var changed = true;
|
|
|
|
var found = false;
|
|
|
|
var newPair = key +'='+ value;
|
2019-02-14 15:26:13 +00:00
|
|
|
var kvPairs = location.search.substr(1).split('?');
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2019-02-14 15:26:13 +00:00
|
|
|
for (var i = 0; i < kvPairs.length; i++) {
|
|
|
|
var kvPair = kvPairs[i].split('=', 1);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2019-02-14 15:26:13 +00:00
|
|
|
if (kvPair[0] == key) {
|
2015-01-23 13:09:30 +00:00
|
|
|
if (kvPair[1] != value)
|
2019-02-14 15:26:13 +00:00
|
|
|
kvPairs.splice(i, 1, newPair);
|
2015-01-23 13:09:30 +00:00
|
|
|
else
|
|
|
|
changed = false;
|
|
|
|
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
2019-02-14 15:26:13 +00:00
|
|
|
kvPairs.push(newPair);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
if (changed)
|
2019-02-14 15:26:13 +00:00
|
|
|
document.location.hash = '?'+ kvPairs.join('&');
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 15:26:13 +00:00
|
|
|
,makeUri: function(map) {
|
2015-01-23 13:09:30 +00:00
|
|
|
var post = '';
|
|
|
|
|
2019-02-14 15:26:13 +00:00
|
|
|
for (var key in map) {
|
2015-01-23 13:09:30 +00:00
|
|
|
var value = map[key];
|
2015-12-19 15:42:38 +00:00
|
|
|
|
|
|
|
if (value === null || value === undefined)
|
|
|
|
continue;
|
2015-01-23 13:09:30 +00:00
|
|
|
if (typeof value == 'boolean')
|
2015-12-19 15:42:38 +00:00
|
|
|
value = value ? '1' : '0';
|
2019-02-14 15:26:13 +00:00
|
|
|
if (post.length > 2)
|
|
|
|
post += '&';
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2019-02-14 15:26:13 +00:00
|
|
|
post += encodeURIComponent(key) +'='+ encodeURIComponent(value);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return post;
|
|
|
|
}
|
|
|
|
};
|
2016-09-26 09:28:47 +00:00
|
|
|
|