Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Escue <scott.escue@gmail.com>2018-06-08 07:06:44 +0300
committerMike Greiling <mike@pixelcog.com>2019-01-10 09:00:39 +0300
commita3541a8d8dd1f4db690b7293d2a7674287020e84 (patch)
treee37de9ac774c853f64770ebac7916d41f6a4c8b7 /app/assets/javascripts/lib
parent6b067fe470857a478939a6037280beb07cf9680d (diff)
Removing the URL manipulation functions added to 'common_utils.js' in favor of the functions that already existed in 'url_utility.js'. Refactoring 'removeParams' function in 'url_utility.js' to allow url to be passed and to preserve the original host and/or path provided in the url.
Diffstat (limited to 'app/assets/javascripts/lib')
-rw-r--r--app/assets/javascripts/lib/utils/common_utils.js89
-rw-r--r--app/assets/javascripts/lib/utils/url_utility.js57
2 files changed, 51 insertions, 95 deletions
diff --git a/app/assets/javascripts/lib/utils/common_utils.js b/app/assets/javascripts/lib/utils/common_utils.js
index ccf1d924ef2..afdca012127 100644
--- a/app/assets/javascripts/lib/utils/common_utils.js
+++ b/app/assets/javascripts/lib/utils/common_utils.js
@@ -4,6 +4,14 @@ import { getLocationHash } from './url_utility';
import { convertToCamelCase } from './text_utility';
import { isObject } from './type_utility';
+/**
+ * Simply returns `window.location`. This function exists to provide a means to spy
+ * `window.location` in unit tests.
+ *
+ * @returns {Location | string | any} The browser's `window.location`
+ */
+export const windowLocation = () => window.location;
+
export const getPagePath = (index = 0) => {
const page = $('body').attr('data-page') || '';
@@ -180,87 +188,6 @@ export const urlParamsToObject = (path = '') =>
return data;
}, {});
-/**
- * Apply the query param and value to the given url by returning a new url string that includes
- * the param/value pair. If the given url already includes the query param, the query param value
- * will be updated in the new url string. Otherwise, the query param and value will by added in
- * the new url string.
- *
- * @param url {string} - url to which the query param will be applied
- * @param param {string} - name of the query param to set
- * @param value {string|number} - value to give the query param
- * @returns {string} A copy of the original url with the new or updated query param
- */
-export const setUrlParam = (url, param, value) => {
- const [rootAndQuery, fragment] = url.split('#');
- const [root, query] = rootAndQuery.split('?');
- const encodedParam = encodeURIComponent(param);
- const encodedPair = `${encodedParam}=${encodeURIComponent(value)}`;
-
- let paramExists = false;
- const paramArray =
- (query ? query.split('&') : [])
- .map(paramPair => {
- const [foundParam] = paramPair.split('=');
- if (foundParam === encodedParam) {
- paramExists = true;
- return encodedPair;
- }
- return paramPair;
- });
-
- if (paramExists === false) {
- paramArray.push(encodedPair);
- }
-
- const writableFragment = fragment ? `#${fragment}` : '';
- return `${root}?${paramArray.join('&')}${writableFragment}`;
-};
-
-/**
- * Remove the query param from the given url by returning a new url string that no longer includes
- * the param/value pair.
- *
- * @param url {string} - url from which the query param will be removed
- * @param param {string} - the name of the query param to remove
- * @returns {string} A copy of the original url but without the query param
- */
-export const removeUrlParam = (url, param) => {
- const [rootAndQuery, fragment] = url.split('#');
- const [root, query] = rootAndQuery.split('?');
-
- if (query === undefined) {
- return url;
- }
-
- const encodedParam = encodeURIComponent(param);
- const updatedQuery = query
- .split('&')
- .filter(paramPair => {
- const [foundParam] = paramPair.split('=');
- return foundParam !== encodedParam;
- })
- .join('&');
-
- const writableQuery = updatedQuery.length > 0 ? `?${updatedQuery}` : '';
- const writableFragment = fragment ? `#${fragment}` : '';
- return `${root}${writableQuery}${writableFragment}`;
-};
-
-/**
- * Apply the fragment to the given url by returning a new url string that includes
- * the fragment. If the given url already contains a fragment, the original fragment
- * will be removed.
- *
- * @param url {string} - url to which the fragment will be applied
- * @param fragment {string} - fragment to append
- */
-export const setUrlFragment = (url, fragment) => {
- const [rootUrl] = url.split('#');
- const encodedFragment = encodeURIComponent(fragment.replace(/^#/, ''));
- return `${rootUrl}#${encodedFragment}`;
-};
-
export const isMetaKey = e => e.metaKey || e.ctrlKey || e.altKey || e.shiftKey;
// Identify following special clicks
diff --git a/app/assets/javascripts/lib/utils/url_utility.js b/app/assets/javascripts/lib/utils/url_utility.js
index 9850f7ce782..61f53a632b8 100644
--- a/app/assets/javascripts/lib/utils/url_utility.js
+++ b/app/assets/javascripts/lib/utils/url_utility.js
@@ -1,3 +1,5 @@
+import { windowLocation } from './common_utils';
+
// Returns an array containing the value(s) of the
// of the key passed as an argument
export function getParameterValues(sParam) {
@@ -42,22 +44,35 @@ export function mergeUrlParams(params, url) {
return `${urlparts[1]}?${query}${urlparts[3]}`;
}
-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;
+/**
+ * Removes specified query params from the url by returning a new url string that no longer
+ * includes the param/value pair. If no url is provided, `window.location.href` is used as
+ * the default value.
+ *
+ * @param {string[]} params - the query param names to remove
+ * @param {string} [url=windowLocation().href] - url from which the query param will be removed
+ * @returns {string} A copy of the original url but without the query param
+ */
+export function removeParams(params, url = windowLocation().href) {
+ const [rootAndQuery, fragment] = url.split('#');
+ const [root, query] = rootAndQuery.split('?');
+
+ if (query === undefined) {
+ return url;
+ }
- params.forEach(param => {
- url.search = removeParamQueryString(url.search, param);
- });
+ const encodedParams = params.map(param => encodeURIComponent(param));
+ const updatedQuery = query
+ .split('&')
+ .filter(paramPair => {
+ const [foundParam] = paramPair.split('=');
+ return encodedParams.indexOf(foundParam) < 0;
+ })
+ .join('&');
- return url.href;
+ const writableQuery = updatedQuery.length > 0 ? `?${updatedQuery}` : '';
+ const writableFragment = fragment ? `#${fragment}` : '';
+ return `${root}${writableQuery}${writableFragment}`;
}
export function getLocationHash(url = window.location.href) {
@@ -66,6 +81,20 @@ export function getLocationHash(url = window.location.href) {
return hashIndex === -1 ? null : url.substring(hashIndex + 1);
}
+/**
+ * Apply the fragment to the given url by returning a new url string that includes
+ * the fragment. If the given url already contains a fragment, the original fragment
+ * will be removed.
+ *
+ * @param {string} url - url to which the fragment will be applied
+ * @param {string} fragment - fragment to append
+ */
+export const setUrlFragment = (url, fragment) => {
+ const [rootUrl] = url.split('#');
+ const encodedFragment = encodeURIComponent(fragment.replace(/^#/, ''));
+ return `${rootUrl}#${encodedFragment}`;
+};
+
export function visitUrl(url, external = false) {
if (external) {
// Simulate `target="blank" rel="noopener noreferrer"`