2017-08-17 22:00:37 +05:30
|
|
|
// Returns an array containing the value(s) of the
|
|
|
|
// of the key passed as an argument
|
2018-03-17 18:26:18 +05:30
|
|
|
export function getParameterValues(sParam) {
|
|
|
|
const sPageURL = decodeURIComponent(window.location.search.substring(1));
|
|
|
|
|
|
|
|
return sPageURL.split('&').reduce((acc, urlParam) => {
|
|
|
|
const sParameterName = urlParam.split('=');
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
if (sParameterName[0] === sParam) {
|
2018-03-17 18:26:18 +05:30
|
|
|
acc.push(sParameterName[1].replace(/\+/g, ' '));
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
}
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
// @param {Object} params - url keys and value to merge
|
|
|
|
// @param {String} url
|
2018-03-17 18:26:18 +05:30
|
|
|
export function mergeUrlParams(params, url) {
|
2019-01-03 12:48:30 +05:30
|
|
|
let newUrl = Object.keys(params).reduce((acc, paramName) => {
|
|
|
|
const paramValue = encodeURIComponent(params[paramName]);
|
|
|
|
const pattern = new RegExp(`\\b(${paramName}=).*?(&|$)`);
|
|
|
|
|
|
|
|
if (paramValue === null) {
|
|
|
|
return acc.replace(pattern, '');
|
|
|
|
} else if (url.search(pattern) !== -1) {
|
|
|
|
return acc.replace(pattern, `$1${paramValue}$2`);
|
|
|
|
}
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
return `${acc}${acc.indexOf('?') > 0 ? '&' : '?'}${paramName}=${paramValue}`;
|
|
|
|
}, decodeURIComponent(url));
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
// Remove a trailing ampersand
|
|
|
|
const lastChar = newUrl[newUrl.length - 1];
|
|
|
|
|
|
|
|
if (lastChar === '&') {
|
|
|
|
newUrl = newUrl.slice(0, -1);
|
|
|
|
}
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
return newUrl;
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export function removeParamQueryString(url, param) {
|
|
|
|
const decodedUrl = decodeURIComponent(url);
|
|
|
|
const urlVariables = decodedUrl.split('&');
|
|
|
|
|
|
|
|
return urlVariables.filter(variable => variable.indexOf(param) === -1).join('&');
|
|
|
|
}
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
export function removeParams(params, source = window.location.href) {
|
2017-09-10 17:25:29 +05:30
|
|
|
const url = document.createElement('a');
|
2018-11-20 20:47:30 +05:30
|
|
|
url.href = source;
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
params.forEach(param => {
|
2018-03-17 18:26:18 +05:30
|
|
|
url.search = removeParamQueryString(url.search, param);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
return url.href;
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export function getLocationHash(url = window.location.href) {
|
|
|
|
const hashIndex = url.indexOf('#');
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
return hashIndex === -1 ? null : url.substring(hashIndex + 1);
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export function visitUrl(url, external = false) {
|
|
|
|
if (external) {
|
|
|
|
// Simulate `target="blank" rel="noopener noreferrer"`
|
|
|
|
// See https://mathiasbynens.github.io/rel-noopener/
|
|
|
|
const otherWindow = window.open();
|
|
|
|
otherWindow.opener = null;
|
|
|
|
otherWindow.location = url;
|
|
|
|
} else {
|
|
|
|
window.location.href = url;
|
|
|
|
}
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
export function refreshCurrentPage() {
|
|
|
|
visitUrl(window.location.href);
|
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
export function redirectTo(url) {
|
|
|
|
return window.location.assign(url);
|
|
|
|
}
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
export function webIDEUrl(route = undefined) {
|
2018-11-08 19:23:39 +05:30
|
|
|
let returnUrl = `${gon.relative_url_root || ''}/-/ide/`;
|
2018-05-09 12:01:36 +05:30
|
|
|
if (route) {
|
2018-11-08 19:23:39 +05:30
|
|
|
returnUrl += `project${route.replace(new RegExp(`^${gon.relative_url_root || ''}`), '')}`;
|
2018-05-09 12:01:36 +05:30
|
|
|
}
|
|
|
|
return returnUrl;
|
|
|
|
}
|