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

96 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) {
2019-02-13 22:33:31 +05:30
const re = /^([^?#]*)(\?[^#]*)?(.*)/;
const merged = {};
const urlparts = url.match(re);
if (urlparts[2]) {
urlparts[2]
.substr(1)
.split('&')
.forEach(part => {
if (part.length) {
const kv = part.split('=');
merged[decodeURIComponent(kv[0])] = decodeURIComponent(kv.slice(1).join('='));
}
});
}
2018-03-17 18:26:18 +05:30
2019-02-13 22:33:31 +05:30
Object.assign(merged, params);
2019-01-03 12:48:30 +05:30
2019-02-13 22:33:31 +05:30
const query = Object.keys(merged)
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(merged[key])}`)
.join('&');
2018-03-17 18:26:18 +05:30
2019-02-13 22:33:31 +05:30
return `${urlparts[1]}?${query}${urlparts[3]}`;
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;
}