summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/lib/utils/url_utility.js
blob: a282c2df441648e9f93778e7331c8e1a0213657b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Returns an array containing the value(s) of the
// of the key passed as an argument
export function getParameterValues(sParam) {
  const sPageURL = decodeURIComponent(window.location.search.substring(1));

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

    if (sParameterName[0] === sParam) {
      acc.push(sParameterName[1].replace(/\+/g, ' '));
    }

    return acc;
  }, []);
}

// @param {Object} params - url keys and value to merge
// @param {String} url
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, '');
    } else if (url.search(pattern) !== -1) {
      return acc.replace(pattern, `$1${paramValue}$2`);
    }

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

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

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

  return newUrl;
}

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, source = window.location.href) {
  const url = document.createElement('a');
  url.href = source;

  params.forEach(param => {
    url.search = removeParamQueryString(url.search, param);
  });

  return url.href;
}

export function getLocationHash(url = window.location.href) {
  const hashIndex = url.indexOf('#');

  return hashIndex === -1 ? null : url.substring(hashIndex + 1);
}

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;
  }
}

export function refreshCurrentPage() {
  visitUrl(window.location.href);
}

export function redirectTo(url) {
  return window.location.assign(url);
}

export function webIDEUrl(route = undefined) {
  let returnUrl = `${gon.relative_url_root || ''}/-/ide/`;
  if (route) {
    returnUrl += `project${route.replace(new RegExp(`^${gon.relative_url_root || ''}`), '')}`;
  }
  return returnUrl;
}