url_utility.js 2.46 KB
Newer Older
1 2
// Returns an array containing the value(s) of the
// of the key passed as an argument
3 4 5 6 7 8
export function getParameterValues(sParam) {
  const sPageURL = decodeURIComponent(window.location.search.substring(1));

  return sPageURL.split('&').reduce((acc, urlParam) => {
    const sParameterName = urlParam.split('=');

9
    if (sParameterName[0] === sParam) {
10
      acc.push(sParameterName[1].replace(/\+/g, ' '));
Fatih Acet's avatar
Fatih Acet committed
11
    }
12 13 14 15 16

    return acc;
  }, []);
}

17 18
// @param {Object} params - url keys and value to merge
// @param {String} url
19
export function mergeUrlParams(params, url) {
20
  let newUrl = Object.keys(params).reduce((acc, paramName) => {
21
    const paramValue = encodeURIComponent(params[paramName]);
22 23 24
    const pattern = new RegExp(`\\b(${paramName}=).*?(&|$)`);

    if (paramValue === null) {
25
      return acc.replace(pattern, '');
26
    } else if (url.search(pattern) !== -1) {
27
      return acc.replace(pattern, `$1${paramValue}$2`);
Fatih Acet's avatar
Fatih Acet committed
28
    }
29

30
    return `${acc}${acc.indexOf('?') > 0 ? '&' : '?'}${paramName}=${paramValue}`;
31 32
  }, decodeURIComponent(url));

33
  // Remove a trailing ampersand
34 35
  const lastChar = newUrl[newUrl.length - 1];

36 37 38
  if (lastChar === '&') {
    newUrl = newUrl.slice(0, -1);
  }
39

40
  return newUrl;
41 42 43 44 45 46 47 48 49 50
}

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) {
Simon Knox's avatar
Simon Knox committed
51 52
  const url = document.createElement('a');
  url.href = window.location.href;
53

54
  params.forEach(param => {
55
    url.search = removeParamQueryString(url.search, param);
56
  });
57

58
  return url.href;
59
}
Bryce Johnson's avatar
Bryce Johnson committed
60

61 62
export function getLocationHash(url = window.location.href) {
  const hashIndex = url.indexOf('#');
63 64 65

  return hashIndex === -1 ? null : url.substring(hashIndex + 1);
}
Bryce Johnson's avatar
Bryce Johnson committed
66

67 68
export function visitUrl(url, external = false) {
  if (external) {
69
    // Simulate `target="blank" rel="noopener noreferrer"`
70 71 72 73 74
    // See https://mathiasbynens.github.io/rel-noopener/
    const otherWindow = window.open();
    otherWindow.opener = null;
    otherWindow.location = url;
  } else {
Jacob Schatz's avatar
Jacob Schatz committed
75
    window.location.href = url;
76 77 78
  }
}

79 80 81 82
export function refreshCurrentPage() {
  visitUrl(window.location.href);
}

83 84 85
export function redirectTo(url) {
  return window.location.assign(url);
}
86

87
export function webIDEUrl(route = undefined) {
88
  let returnUrl = `${gon.relative_url_root}/-/ide/`;
89 90
  if (route) {
    returnUrl += `project${route}`;
91 92 93
  }
  return returnUrl;
}