From e0a761e73c05b9d781be16bb94221c0e075ca019 Mon Sep 17 00:00:00 2001 From: Kamesh Sampath Date: Wed, 7 Oct 2020 11:14:26 +0530 Subject: [PATCH] Handle Query Params in pagination links (#8) * Docs links don't keep query parameters Fixes #7 --- .eslintrc | 3 ++- src/js/07-userparams-behaviour.js | 30 +++++++++++++++++++++++------- src/partials/pagination.hbs | 4 ++-- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/.eslintrc b/.eslintrc index fc50489..df7fe06 100644 --- a/.eslintrc +++ b/.eslintrc @@ -9,6 +9,7 @@ "exports": "always-multiline" }], "max-len": [1, 120, 2], - "spaced-comment": "off" + "spaced-comment": "off", + "padded-blocks": "off" } } diff --git a/src/js/07-userparams-behaviour.js b/src/js/07-userparams-behaviour.js index 0b0553c..fd46f16 100644 --- a/src/js/07-userparams-behaviour.js +++ b/src/js/07-userparams-behaviour.js @@ -1,5 +1,10 @@ document.addEventListener('DOMContentLoaded', function () { - var queryString = window.location.search + + function hasQueryString (url) { + // regex pattern for detecting querystring + var pattern = new RegExp(/\?.+=.*/g) + return pattern.test(url) + } /* * Thanks to https://gomakethings.com/getting-all-query-string-values-from-a-url-with-vanilla-js/ @@ -41,11 +46,22 @@ document.addEventListener('DOMContentLoaded', function () { replaceParamsInNodes(document.body, '(%' + keys[i].toUpperCase() + '%)', allParams[keys[i]]) } - document.querySelectorAll('.userfied-link').forEach(function (el) { - el.href += queryString - }) + //Handle links + var allQueryPramLinks = document.querySelectorAll('.query-params-link') + if (allQueryPramLinks) { + allQueryPramLinks.forEach(appendQueryStringToHref) + } - document.querySelectorAll('.nav-link').forEach(function (el) { - el.href += queryString - }) + var allNavLinks = document.querySelectorAll('.nav-link') + if (allNavLinks) { + allNavLinks.forEach(appendQueryStringToHref) + } + + function appendQueryStringToHref (el) { + var queryString = window.location.search + if (!hasQueryString(el.href) && queryString) { + // console.log('No Query String in %s, adding.', el.href) + el.href += queryString + } + } }) diff --git a/src/partials/pagination.hbs b/src/partials/pagination.hbs index 4f6300f..fb6e17a 100644 --- a/src/partials/pagination.hbs +++ b/src/partials/pagination.hbs @@ -2,10 +2,10 @@ {{#if (or page.previous page.next)}} {{/if}}