Skip to content

Commit

Permalink
Handle Query Params in pagination links (#8)
Browse files Browse the repository at this point in the history
* Docs links don't keep query parameters
Fixes #7
  • Loading branch information
kameshsampath authored Oct 7, 2020
1 parent 36c0559 commit e0a761e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"exports": "always-multiline"
}],
"max-len": [1, 120, 2],
"spaced-comment": "off"
"spaced-comment": "off",
"padded-blocks": "off"
}
}
30 changes: 23 additions & 7 deletions src/js/07-userparams-behaviour.js
Original file line number Diff line number Diff line change
@@ -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/
Expand Down Expand Up @@ -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
}
}
})
4 changes: 2 additions & 2 deletions src/partials/pagination.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
{{#if (or page.previous page.next)}}
<nav class="pagination">
{{#with page.previous}}
<span class="prev"><a href="{{{relativize ./url}}}">{{{./content}}}</a></span>
<span class="prev"><a href="{{{relativize ./url}}}" class="query-params-link">{{{./content}}}</a></span>
{{/with}}
{{#with page.next}}
<span class="next"><a href="{{{relativize ./url}}}">{{{./content}}}</a></span>
<span class="next"><a href="{{{relativize ./url}}}" class="query-params-link">{{{./content}}}</a></span>
{{/with}}
</nav>
{{/if}}
Expand Down

0 comments on commit e0a761e

Please sign in to comment.