lib/javascripts/pagy.js in pagy-3.3.1 vs lib/javascripts/pagy.js in pagy-3.3.2
- old
+ new
@@ -3,31 +3,36 @@
function Pagy(){}
Pagy.init = function(arg){
var target = arg instanceof Event || arg === undefined ? document : arg,
jsonTags = target.getElementsByClassName('pagy-json');
+ if (target === document) { // reset resize-listeners on page load (#163)
+ for (var id in Pagy.navResizeListeners) { window.removeEventListener('resize', Pagy.navResizeListeners[id], true) }
+ Pagy.navResizeListeners = {};
+ }
for (var i = 0, len = jsonTags.length; i < len; i++) {
var args = JSON.parse(jsonTags[i].innerHTML);
Pagy[args.shift()].apply(null, args);
}
};
Pagy.nav = function(id, tags, sequels, param){
var pagyEl = document.getElementById(id),
- container = pagyEl.parentElement,
lastWidth = undefined,
timeoutId = 0,
pageREg = new RegExp(/__pagy_page__/g),
- widths = [];
+ widths = [],
+ rendering = function(){ clearTimeout(timeoutId); timeoutId = setTimeout(pagyEl.render, 150) }; // suppress rapid firing rendering
+
for (var width in sequels) { widths.push(parseInt(width)) } // fine with sequels structure
widths.sort(function(a, b){return b-a});
- var render = function(){
- if (container.clientWidth === 0) { rendering() }
+ pagyEl.render = function(){
+ if (this.parentElement.clientWidth === 0) { rendering() }
var width, i, len;
for (i = 0, len = widths.length; i < len; i++) {
- if (container.clientWidth > widths[i]) { width = widths[i]; break }
+ if (this.parentElement.clientWidth > widths[i]) { width = widths[i]; break }
}
if (width !== lastWidth) {
var html = tags.before,
series = sequels[width];
for (i = 0, len = series.length; i < len; i++) {
@@ -36,25 +41,23 @@
else if (typeof(item) === 'number') { html += tags.link.replace(pageREg, item) }
else if (item === 'gap') { html += tags.gap }
else if (typeof(item) === 'string') { html += tags.active.replace(pageREg, item) }
}
html += tags.after;
- pagyEl.innerHTML = '';
- pagyEl.insertAdjacentHTML('afterbegin', html);
+ this.innerHTML = '';
+ this.insertAdjacentHTML('afterbegin', html);
lastWidth = width;
}
- },
- // suppress rapid firing rendering
- rendering = function(){ clearTimeout(timeoutId); timeoutId = setTimeout(render, 150) };
+ }.bind(pagyEl);
if (widths.length > 1) {
// refresh the window resize listener (avoiding rendering multiple times)
- window.removeEventListener('resize', Pagy.windowListeners[id], true);
+ window.removeEventListener('resize', Pagy.navResizeListeners[id], true); // needed for AJAX init
window.addEventListener('resize', rendering, true);
- Pagy.windowListeners[id] = rendering;
+ Pagy.navResizeListeners[id] = rendering;
}
- render();
+ pagyEl.render();
};
Pagy.combo_nav = function(id, page, link, param){
var pagyEl = document.getElementById(id),
input = pagyEl.getElementsByTagName('input')[0],
@@ -83,11 +86,9 @@
pagyEl.getElementsByTagName('a')[0].click();
}
};
Pagy.addInputEventListeners(input, go);
};
-
-Pagy.windowListeners = {};
Pagy.addInputEventListeners = function(input, handler){
// select the content on click: easier for typing a number
input.addEventListener('click', function(){ this.select() });
// go when the input looses focus