diff --git a/ReadMe.md b/ReadMe.md index f17d017..f4baa5c 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -100,19 +100,19 @@ Build with the following commands, the output will be stored in "build/js/output For full not minified version ```sh -npm build utils-build +npm run utils-build ``` For minified build (but keeps the function names as is) ```sh -npm build utils-min-build +npm run utils-min-build ``` Or build both at the same time ```sh -npm build utils-build-all +npm run utils-build-all ``` ## Develop diff --git a/src/utils.mjs b/src/utils.mjs index 5aa89d4..8f3342a 100644 --- a/src/utils.mjs +++ b/src/utils.mjs @@ -61,7 +61,11 @@ import { } from './utils/FormatBytes.mjs'; import { parseQueryString as _parseQueryString, - getQueryStringParam as _getQueryStringParam + getQueryStringParam as _getQueryStringParam, + hasUrlParameter as _hasUrlParameter, + getUrlParameter as _getUrlParameter, + updateUrlParameter as _updateUrlParameter, + removeUrlParameter as _removeUrlParameter, } from './utils/UrlParser.mjs'; import { loginLogout as _loginLogout, @@ -937,16 +941,14 @@ function html_options_refill(name, data, sort = '') // eslint-disable-line no-un * ALTERNATIVE CODE * var url = new URL(window.location.href); * param_uid = url.searchParams.get('uid'); - * @param {String} [query=''] the query string to parse - * if not set will auto fill - * @param {String} [return_key=''] if set only returns this key entry - * or empty for none - * @return {Object|String} parameter entry list + * @param {String} [query=''] the query string to parse, if not set will auto fill + * @param {String} [return_key=''] if set only returns this key entry or empty for none + * @param {Boolean} [single=false] if set to true then only the first found will be returned */ // @ts-ignore -function parseQueryString(query = '', return_key = '') // eslint-disable-line no-unused-vars +function parseQueryString(query = '', return_key = '', single = false) // eslint-disable-line no-unused-vars { - return _parseQueryString(query, return_key); + return _parseQueryString(query, return_key, single); } /** @@ -955,15 +957,12 @@ function parseQueryString(query = '', return_key = '') // eslint-disable-line no * if a parameter is set several times it will be returned as an array * if search parameter set and nothing found and empty string is returned * if no parametes exist and no serach is set and empty object is returned - * @param {String} [search=''] if set searches for this entry, if empty - * all parameters are returned - * @param {String} [query=''] different query string to parse, if not - * set (default) the current window href is used - * @param {Boolean} [single=false] if set to true then only the first found - * will be returned - * @return {Object|Array|String} if search is empty, object, if search is set - * and only one entry, then string, else array - * unless single is true + * @param {String} [search=''] if set searches for this entry, if empty all parameters are returned + * @param {String} [query=''] different query string to parse, if not set (default) the current window href is used + * @param {Boolean} [single=false] if set to true then only the first found will be returned + * @return {Object|Array|String} if search is empty, object, if search is set + * and only one entry, then string, else array + * unless single is true */ // @ts-ignore function getQueryStringParam(search = '', query = '', single = false) // eslint-disable-line no-unused-vars @@ -971,6 +970,51 @@ function getQueryStringParam(search = '', query = '', single = false) // eslint- return _getQueryStringParam(search, query, single); } +/** + * Add or update a query parameter in the current URL and update the browser's address bar + * @param {string} key - The parameter name to add or update + * @param {string} value - The value to set for the parameter + * @param {boolean} [reload=false] - Whether to reload the page after updating the URL + */ +// @ts-ignore +function updateUrlParameter(key, value, reload = false) // eslint-disable-line no-unused-vars +{ + return _updateUrlParameter(key, value, reload); +} + +/** + * Remove a parameter from the current URL and update the browser's address bar + * @param {string} key - The parameter name to remove + * @param {boolean} [reload=false] - Whether to reload the page after updating the URL + */ +// @ts-ignore +function removeUrlParameter(key, reload = false) // eslint-disable-line no-unused-vars +{ + return _removeUrlParameter(key, reload); +} + +/** + * Check if key exists as URL parameter + * @param {String} key URL parameter to search + * @returns {Boolean} True if key exists + */ +// @ts-ignore +function hasUrlParameter(key) // eslint-disable-line no-unused-vars +{ + return _hasUrlParameter(key); +} + +/** + * Return one value for a URL paramter or null if not found + * @param {String} key Which URL parameter to get + * @returns {String|Null} URL parameter content + */ +// @ts-ignore +function getUrlParameter(key) // eslint-disable-line no-unused-vars +{ + return _getUrlParameter(key); +} + // MARK: ACL LOGIN // *** MASTER logout call /** diff --git a/src/utils/UrlParser.mjs b/src/utils/UrlParser.mjs index e40d87b..764fb61 100644 --- a/src/utils/UrlParser.mjs +++ b/src/utils/UrlParser.mjs @@ -4,7 +4,7 @@ Date: 2025//3/6 Creator: Clemens Schwaighofer */ -export { parseQueryString, getQueryStringParam }; +export { parseQueryString, getQueryStringParam, hasUrlParameter, getUrlParameter, updateUrlParameter, removeUrlParameter }; /** * NOTE: this original code was wrong, now using URL and parsing through @@ -13,13 +13,10 @@ export { parseQueryString, getQueryStringParam }; * ALTERNATIVE CODE * var url = new URL(window.location.href); * param_uid = url.searchParams.get('uid'); - * @param {String} [query=''] the query string to parse - * if not set will auto fill - * @param {String} [return_key=''] if set only returns this key entry - * or empty for none - * @param {Boolean} [single=false] if set to true then only the first found - * will be returned - * @return {Object|String} parameter entry list + * @param {String} [query=''] the query string to parse, if not set will auto fill + * @param {String} [return_key=''] if set only returns this key entry or empty for none + * @param {Boolean} [single=false] if set to true then only the first found will be returned + * @return {Object|String} parameter entry list */ function parseQueryString(query = '', return_key = '', single = false) { @@ -32,15 +29,12 @@ function parseQueryString(query = '', return_key = '', single = false) * if a parameter is set several times it will be returned as an array * if search parameter set and nothing found and empty string is returned * if no parametes exist and no serach is set and empty object is returned - * @param {String} [search=''] if set searches for this entry, if empty - * all parameters are returned - * @param {String} [query=''] different query string to parse, if not - * set (default) the current window href is used - * @param {Boolean} [single=false] if set to true then only the first found - * will be returned - * @return {Object|Array|String} if search is empty, object, if search is set - * and only one entry, then string, else array - * unless single is true + * @param {String} [search=''] if set searches for this entry, if empty all parameters are returned + * @param {String} [query=''] different query string to parse, if not set (default) the current window href is used + * @param {Boolean} [single=false] if set to true then only the first found will be returned + * @return {Object|Array|String} if search is empty, object, if search is set + * and only one entry, then string, else array + * unless single is true */ function getQueryStringParam(search = '', query = '', single = false) { @@ -75,4 +69,74 @@ function getQueryStringParam(search = '', query = '', single = false) return param; } +/** + * Check if key exists as URL parameter + * @param {String} key URL parameter to search + * @returns {Boolean} True if key exists + */ +function hasUrlParameter(key) +{ + var urlParams = new URLSearchParams(window.location.search); + return urlParams.has(key); +} + +/** + * Return one value for a URL paramter or null if not found + * @param {String} key Which URL parameter to get + * @returns {String|Null} URL parameter content + */ +function getUrlParameter(key) +{ + var urlParams = new URLSearchParams(window.location.search); + return urlParams.get(key); +} + +/** + * Add or update a query parameter in the current URL and update the browser's address bar + * @param {string} key - The parameter name to add or update + * @param {string} value - The value to set for the parameter + * @param {boolean} [reload=false] - Whether to reload the page after updating the URL + */ +function updateUrlParameter(key, value, reload = false) +{ + // Create a URL object from the current URL + const url = new URL(window.location.href); + + // Set or update the parameter + url.searchParams.set(key, value); + + const newUrl = url.toString(); + + // Update the browser's address bar without reloading the page + window.history.pushState({ path: newUrl }, '', newUrl); + + // Optionally reload the page + if (reload) { + // window.location.href = newUrl; + window.location.reload(); + } +} + +/** + * Remove a parameter from the current URL and update the browser's address bar + * @param {string} key - The parameter name to remove + * @param {boolean} [reload=false] - Whether to reload the page after updating the URL + */ +function removeUrlParameter(key, reload = false) +{ + // Create a URL object from the current URL + const url = new URL(window.location.href); + + // Remove the parameter if it exists + url.searchParams.delete(key); + + // Update the browser's address bar without reloading the page + window.history.pushState({}, '', url.toString()); + + // Optionally reload the page + if (reload) { + window.location.reload(); + } +} + // __EMD__