Commit 13df7a85 authored by Phil Hughes's avatar Phil Hughes

Moved URL utility methods into es modules

parent 12d33b88
/* eslint-disable func-names, space-before-function-paren, wrap-iife, no-var, no-param-reassign, no-cond-assign, one-var, one-var-declaration-per-line, no-void, guard-for-in, no-restricted-syntax, prefer-template, quotes, max-len */
var base;
var w = window;
if (w.gl == null) {
w.gl = {};
}
if ((base = w.gl).utils == null) {
base.utils = {};
}
// Returns an array containing the value(s) of the // Returns an array containing the value(s) of the
// of the key passed as an argument // of the key passed as an argument
w.gl.utils.getParameterValues = function(sParam) { export function getParameterValues(sParam) {
var i, sPageURL, sParameterName, sURLVariables, values; const sPageURL = decodeURIComponent(window.location.search.substring(1));
sPageURL = decodeURIComponent(window.location.search.substring(1));
sURLVariables = sPageURL.split('&'); return sPageURL.split('&').reduce((acc, urlParam) => {
sParameterName = void 0; const sParameterName = urlParam.split('=');
values = [];
i = 0;
while (i < sURLVariables.length) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) { if (sParameterName[0] === sParam) {
values.push(sParameterName[1].replace(/\+/g, ' ')); acc.push(sParameterName[1].replace(/\+/g, ' '));
} }
i += 1;
} return acc;
return values; }, []);
}; }
// @param {Object} params - url keys and value to merge // @param {Object} params - url keys and value to merge
// @param {String} url // @param {String} url
w.gl.utils.mergeUrlParams = function(params, url) { export function mergeUrlParams(params, url) {
var lastChar, newUrl, paramName, paramValue, pattern; let newUrl = Object.keys(params).reduce((accParam, paramName) => {
newUrl = decodeURIComponent(url); const paramValue = params[paramName];
for (paramName in params) { const pattern = new RegExp(`\\b(${paramName}=).*?(&|$)`);
paramValue = params[paramName]; let acc = accParam;
pattern = new RegExp("\\b(" + paramName + "=).*?(&|$)");
if (paramValue == null) { if (paramValue === null) {
newUrl = newUrl.replace(pattern, ''); acc = acc.replace(pattern, '');
} else if (url.search(pattern) !== -1) { } else if (url.search(pattern) !== -1) {
newUrl = newUrl.replace(pattern, "$1" + paramValue + "$2"); acc = acc.replace(pattern, `$1${paramValue}$2`);
} else { } else {
newUrl = "" + newUrl + (newUrl.indexOf('?') > 0 ? '&' : '?') + paramName + "=" + paramValue; acc = `${accParam}${accParam.indexOf('?') > 0 ? '&' : '?'}${paramName}=${paramValue}`;
} }
}
return acc;
}, decodeURIComponent(url));
// Remove a trailing ampersand // Remove a trailing ampersand
lastChar = newUrl[newUrl.length - 1]; const lastChar = newUrl[newUrl.length - 1];
if (lastChar === '&') { if (lastChar === '&') {
newUrl = newUrl.slice(0, -1); newUrl = newUrl.slice(0, -1);
} }
return newUrl; return newUrl;
}; }
// removes parameter query string from url. returns the modified url
w.gl.utils.removeParamQueryString = function(url, param) { export function removeParamQueryString(url, param) {
var urlVariables, variables; const decodedUrl = decodeURIComponent(url);
url = decodeURIComponent(url); const urlVariables = decodedUrl.split('&');
urlVariables = url.split('&');
return ((function() { return urlVariables.filter(variable => variable.indexOf(param) === -1).join('&');
var j, len, results; }
results = [];
for (j = 0, len = urlVariables.length; j < len; j += 1) { export function removeParams(params) {
variables = urlVariables[j];
if (variables.indexOf(param) === -1) {
results.push(variables);
}
}
return results;
})()).join('&');
};
w.gl.utils.removeParams = (params) => {
const url = document.createElement('a'); const url = document.createElement('a');
url.href = window.location.href; url.href = window.location.href;
params.forEach((param) => { params.forEach((param) => {
url.search = w.gl.utils.removeParamQueryString(url.search, param); url.search = removeParamQueryString(url.search, param);
}); });
return url.href; return url.href;
}; }
w.gl.utils.getLocationHash = function(url) {
var hashIndex;
if (typeof url === 'undefined') {
// Note: We can't use window.location.hash here because it's
// not consistent across browsers - Firefox will pre-decode it
url = window.location.href;
}
hashIndex = url.indexOf('#');
return hashIndex === -1 ? null : url.substring(hashIndex + 1);
};
w.gl.utils.refreshCurrentPage = () => gl.utils.visitUrl(window.location.href); export function getLocationHash(url) {
const hashIndex = window.location.href.indexOf('#');
return hashIndex === -1 ? null : url.substring(hashIndex + 1);
}
// eslint-disable-next-line import/prefer-default-export
export function visitUrl(url, external = false) { export function visitUrl(url, external = false) {
if (external) { if (external) {
// Simulate `target="blank" rel="noopener noreferrer"` // Simulate `target="blank" rel="noopener noreferrer"`
...@@ -100,6 +79,10 @@ export function visitUrl(url, external = false) { ...@@ -100,6 +79,10 @@ export function visitUrl(url, external = false) {
} }
} }
export function refreshCurrentPage() {
visitUrl(window.location.href);
}
export function redirectTo(url) { export function redirectTo(url) {
return window.location.assign(url); return window.location.assign(url);
} }
...@@ -107,5 +90,11 @@ export function redirectTo(url) { ...@@ -107,5 +90,11 @@ export function redirectTo(url) {
window.gl = window.gl || {}; window.gl = window.gl || {};
window.gl.utils = { window.gl.utils = {
...(window.gl.utils || {}), ...(window.gl.utils || {}),
mergeUrlParams,
getLocationHash,
getParameterValues,
redirectTo,
refreshCurrentPage,
removeParams,
visitUrl, visitUrl,
}; };
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment