debian-mirror-gitlab/app/assets/javascripts/lib/utils/url_utility.js

94 lines
2.5 KiB
JavaScript
Raw Normal View History

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) {
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, '');
2017-08-17 22:00:37 +05:30
} else if (url.search(pattern) !== -1) {
2018-03-17 18:26:18 +05:30
return acc.replace(pattern, `$1${paramValue}$2`);
2016-09-13 17:45:13 +05:30
}
2018-03-17 18:26:18 +05:30
return `${acc}${acc.indexOf('?') > 0 ? '&' : '?'}${paramName}=${paramValue}`;
}, decodeURIComponent(url));
2017-08-17 22:00:37 +05:30
// Remove a trailing ampersand
2018-03-17 18:26:18 +05:30
const lastChar = newUrl[newUrl.length - 1];
2017-08-17 22:00:37 +05:30
if (lastChar === '&') {
newUrl = newUrl.slice(0, -1);
}
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +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('&');
}
export function removeParams(params) {
2017-09-10 17:25:29 +05:30
const url = document.createElement('a');
url.href = window.location.href;
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) {
let returnUrl = `${gon.relative_url_root}/-/ide/`;
if (route) {
returnUrl += `project${route}`;
}
return returnUrl;
}