2015-01-23 13:09:30 +00:00
|
|
|
/**
|
|
|
|
* Class to handle the URL.
|
2016-12-20 09:32:17 +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
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
getQuery: function (key)
|
|
|
|
{
|
|
|
|
var regExp = new RegExp ('[\?\&]'+ key +'=([^\&]*)(\&?)', 'i');
|
|
|
|
var value = location.search.match (regExp);
|
|
|
|
|
|
|
|
return value ? value[1] : value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the value of a URL variable.
|
|
|
|
*
|
|
|
|
* @param {string} key The variable name
|
|
|
|
* @param {string} value The new value
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
,setQuery: function (key, value)
|
|
|
|
{
|
|
|
|
var changed = true;
|
|
|
|
var found = false;
|
|
|
|
var newPair = key +'='+ value;
|
|
|
|
var kvPairs = location.search.substr(1).split ('?');
|
|
|
|
|
|
|
|
for (var i = 0; i < kvPairs.length; i++)
|
|
|
|
{
|
|
|
|
var kvPair = kvPairs[i].split ('=', 1);
|
|
|
|
|
|
|
|
if (kvPair[0] == key)
|
|
|
|
{
|
|
|
|
if (kvPair[1] != value)
|
|
|
|
kvPairs.splice (i, 1, newPair);
|
|
|
|
else
|
|
|
|
changed = false;
|
|
|
|
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
kvPairs.push (newPair);
|
|
|
|
|
|
|
|
if (changed)
|
|
|
|
document.location.hash = '?'+ kvPairs.join ('&');
|
|
|
|
}
|
|
|
|
|
|
|
|
,makeUri: function (map)
|
|
|
|
{
|
|
|
|
var post = '';
|
|
|
|
|
|
|
|
for (var key in map)
|
|
|
|
{
|
|
|
|
var value = map[key];
|
|
|
|
|
|
|
|
if (post.length > 2)
|
|
|
|
post += '&';
|
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';
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
post += key +'='+ encodeURIComponent (value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return post;
|
|
|
|
}
|
|
|
|
};
|
2016-09-26 09:28:47 +00:00
|
|
|
|