Skip to content

Commit aff8cbb

Browse files
committed
better handling of resize listeners; added render funcition to the pagy element
1 parent c699fe5 commit aff8cbb

2 files changed

Lines changed: 24 additions & 16 deletions

File tree

β€Ždocs/extras/navs.mdβ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ Here is what you should consider/ensure:
101101

102102
5. If the container width snaps to specific widths in discrete steps, you should sync the quantity and widths of the pagy `:steps` to the quantity and internal widths for each discrete step of the container.
103103

104+
#### Javascript Caveats
105+
106+
In case of a window resize, the `pagy_*nav_js` elements on the page are re-rendered (when the container width changes), however if the container width changes in any other way that does not involve a window resize, then you should re-render the pagy element explicitly. For example:
107+
108+
```js
109+
document.getElementById('my-pagy-nav-js').render();
110+
```
111+
104112
## Methods
105113

106114
### pagy_nav_js(pagy, ...)

β€Žlib/javascripts/pagy.jsβ€Ž

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ function Pagy(){}
55
Pagy.init = function(arg){
66
var target = arg instanceof Event || arg === undefined ? document : arg,
77
jsonTags = target.getElementsByClassName('pagy-json');
8+
if (target === document) { // reset resize-listeners on page load (#163)
9+
for (var id in Pagy.navResizeListeners) { window.removeEventListener('resize', Pagy.navResizeListeners[id], true) }
10+
Pagy.navResizeListeners = {};
11+
}
812
for (var i = 0, len = jsonTags.length; i < len; i++) {
913
var args = JSON.parse(jsonTags[i].innerHTML);
1014
Pagy[args.shift()].apply(null, args);
@@ -13,20 +17,20 @@ Pagy.init = function(arg){
1317

1418
Pagy.nav = function(id, tags, sequels, param){
1519
var pagyEl = document.getElementById(id),
16-
container = pagyEl.parentElement,
1720
lastWidth = undefined,
1821
timeoutId = 0,
1922
pageREg = new RegExp(/__pagy_page__/g),
20-
widths = [];
23+
widths = [],
24+
rendering = function(){ clearTimeout(timeoutId); timeoutId = setTimeout(pagyEl.render, 150) }; // suppress rapid firing rendering
25+
2126
for (var width in sequels) { widths.push(parseInt(width)) } // fine with sequels structure
2227
widths.sort(function(a, b){return b-a});
2328

24-
var render = function(){
25-
if (document.getElementById(id) === null){ return }
26-
if (container.clientWidth === 0) { rendering() }
29+
pagyEl.render = function(){
30+
if (this.parentElement.clientWidth === 0) { rendering() }
2731
var width, i, len;
2832
for (i = 0, len = widths.length; i < len; i++) {
29-
if (container.clientWidth > widths[i]) { width = widths[i]; break }
33+
if (this.parentElement.clientWidth > widths[i]) { width = widths[i]; break }
3034
}
3135
if (width !== lastWidth) {
3236
var html = tags.before,
@@ -39,21 +43,19 @@ Pagy.nav = function(id, tags, sequels, param){
3943
else if (typeof(item) === 'string') { html += tags.active.replace(pageREg, item) }
4044
}
4145
html += tags.after;
42-
pagyEl.innerHTML = '';
43-
pagyEl.insertAdjacentHTML('afterbegin', html);
46+
this.innerHTML = '';
47+
this.insertAdjacentHTML('afterbegin', html);
4448
lastWidth = width;
4549
}
46-
},
47-
// suppress rapid firing rendering
48-
rendering = function(){ clearTimeout(timeoutId); timeoutId = setTimeout(render, 150) };
50+
}.bind(pagyEl);
4951

5052
if (widths.length > 1) {
5153
// refresh the window resize listener (avoiding rendering multiple times)
52-
window.removeEventListener('resize', Pagy.windowListeners[id], true);
54+
window.removeEventListener('resize', Pagy.navResizeListeners[id], true); // needed for AJAX init
5355
window.addEventListener('resize', rendering, true);
54-
Pagy.windowListeners[id] = rendering;
56+
Pagy.navResizeListeners[id] = rendering;
5557
}
56-
render();
58+
pagyEl.render();
5759
};
5860

5961
Pagy.combo_nav = function(id, page, link, param){
@@ -87,8 +89,6 @@ Pagy.items_selector = function(id, from, link, param){
8789
Pagy.addInputEventListeners(input, go);
8890
};
8991

90-
Pagy.windowListeners = {};
91-
9292
Pagy.addInputEventListeners = function(input, handler){
9393
// select the content on click: easier for typing a number
9494
input.addEventListener('click', function(){ this.select() });

0 commit comments

Comments
 (0)