From 0ae041f001d5a3ac68525a46cccd80335225d9c6 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Fri, 4 Sep 2020 15:10:58 -0700 Subject: [PATCH] Add whitelist/blacklist functionality to merge_params. --- frontends/ycdl_flask/static/js/common.js | 39 +++++++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/frontends/ycdl_flask/static/js/common.js b/frontends/ycdl_flask/static/js/common.js index aaf6fff..0e59d37 100644 --- a/frontends/ycdl_flask/static/js/common.js +++ b/frontends/ycdl_flask/static/js/common.js @@ -157,19 +157,48 @@ function init_atag_merge_params() "merge_params". If the URL and href contain the same parameter, the href takes priority. + Optional: + data-merge-params: A whitelist of parameter names, separated by commas + or spaces. Only these parameters will be merged from the page URL. + + data-merge-params-except: A blacklist of parameter names, separated by + commas or spaces. All parameters except these will be merged from + the page URL. + Example: URL: ?filter=hello&orderby=score href: "?orderby=date" Result: "?filter=hello&orderby=date" */ - page_params = new URLSearchParams(window.location.search); + function ingest(params, new_params) + { + for (const [key, value] of params) { new_params.set(key, value); } + } + const page_params = Array.from(new URLSearchParams(window.location.search)); let as = Array.from(document.getElementsByClassName("merge_params")); for (const a of as) { - let a_params = new URLSearchParams(a.search); - let new_params = new URLSearchParams(); - page_params.forEach(function(value, key) {new_params.set(key, value); }); - a_params.forEach(function(value, key) {new_params.set(key, value); }); + const a_params = new URLSearchParams(a.search); + const new_params = new URLSearchParams(); + + if (a.dataset.mergeParams) + { + let keep = new Set(a.dataset.mergeParams.split(/[\s,]+/)); + ingest(page_params.filter(key_value => keep.has(key_value[0])), new_params); + delete a.dataset.mergeParams; + } + else if (a.dataset.mergeParamsExcept) + { + let remove = new Set(a.dataset.mergeParamsExcept.split(/[\s,]+/)); + ingest(page_params.filter(key_value => (! remove.has(key_value[0]))), new_params); + delete a.dataset.mergeParamsExcept; + } + else + { + ingest(page_params, new_params); + } + + ingest(a_params, new_params); a.search = new_params.toString(); a.classList.remove("merge_params"); }