|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default class URLHandler { |
|
|
|
|
|
static basicURL() { |
|
|
const url_path = window.location.pathname.split('/').slice(0, -2).join('/'); |
|
|
|
|
|
return window.location.origin + (url_path.length ? url_path : ''); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static get parameters(): object { |
|
|
|
|
|
const query = window.location.search.substring(1); |
|
|
const vars = query.split('&'); |
|
|
|
|
|
const urlParameters = {}; |
|
|
|
|
|
const isInt = x => (/^[0-9]+$/).test(x); |
|
|
const isFloat = x => (/^[0-9]+\.[0-9]*$/).test(x); |
|
|
|
|
|
const typeCast = val => { |
|
|
if (isInt(val)) { |
|
|
return Number.parseInt(val, 10); |
|
|
} else if (isFloat(val)) { |
|
|
return Number.parseFloat(val); |
|
|
} |
|
|
|
|
|
return val; |
|
|
} |
|
|
|
|
|
|
|
|
vars.forEach(v => { |
|
|
if (v.length > 0) { |
|
|
const splits = v.split('='); |
|
|
const key = decodeURIComponent(splits[0]); |
|
|
const raw_value = decodeURIComponent(splits[1]); |
|
|
|
|
|
urlParameters[key] = raw_value.length < 1 ? '' : typeCast(raw_value); |
|
|
} |
|
|
}); |
|
|
|
|
|
return urlParameters; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static urlString(urlParameters: object) { |
|
|
const attr = []; |
|
|
Object.keys(urlParameters).forEach(k => { |
|
|
const v = urlParameters[k]; |
|
|
if (v !== undefined) { |
|
|
attr.push(encodeURI(k + '=' + v)) |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
const url = window.location.pathname; |
|
|
let res = url.substring(url.lastIndexOf('/') + 1); |
|
|
if (attr.length > 0) { |
|
|
res += '?' + attr.join('&') |
|
|
} |
|
|
|
|
|
return res; |
|
|
} |
|
|
|
|
|
static updateURLParam(key: string, value: string | any[], addToBrowserHistory = true) { |
|
|
const currentParams = URLHandler.parameters; |
|
|
currentParams[key] = value; |
|
|
URLHandler.updateUrl(currentParams, addToBrowserHistory); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static updateUrl(urlParameters: object, addToBrowserHistory = true) { |
|
|
if (addToBrowserHistory) { |
|
|
window.history.pushState(urlParameters, '', |
|
|
URLHandler.urlString(urlParameters)) |
|
|
} else { |
|
|
window.history.replaceState(urlParameters, '', |
|
|
URLHandler.urlString(urlParameters)) |
|
|
} |
|
|
} |
|
|
|
|
|
} |