diff --git a/lib/pagy/extras/javascripts/pagy.js b/lib/pagy/extras/javascripts/pagy.js index 4627a0849..eaaa39762 100644 --- a/lib/pagy/extras/javascripts/pagy.js +++ b/lib/pagy/extras/javascripts/pagy.js @@ -2,6 +2,8 @@ function Pagy(){} +Pagy.windowListeners = []; + Pagy.addInputEventListeners = function(input, handler){ // select the content on click: easier for typing a number input.addEventListener('click', function(){ this.select() }); @@ -58,13 +60,16 @@ Pagy.responsive = function(id, tags, widths, series){ pagyBox.insertAdjacentHTML('beforeend', html); lastWidth = width; } - }; - if (window.attachEvent) { window.attachEvent('onresize', render) } - else if (window.addEventListener) { window.addEventListener('resize', render, true) } + }.bind(this); + window.addEventListener('resize', render, true); + Pagy.windowListeners.push(render); render(); }; Pagy.init = function(){ + // we need to explicitly remove the window listeners because turbolinks persists the window object + Pagy.windowListeners.forEach(function(l){window.removeEventListener('resize', l, true)}); + Pagy.windowListeners = []; ['compact', 'items', 'responsive'].forEach(function(name){ var json = document.getElementsByClassName("pagy-"+name+"-json"); for (var i = 0, len = json.length; i < len; i++) {