lib/javascripts/pagy.js in pagy-4.11.0 vs lib/javascripts/pagy.js in pagy-5.0.0
- old
+ new
@@ -1,13 +1,16 @@
// See the Pagy documentation: https://ddnexus.github.io/pagy/extras#javascript
+// Container of the whole pagy stuff
function Pagy(){}
-Pagy.version = '4.11.0'
+Pagy.version = '5.0.0'
+// Used by the waitForMe function
Pagy.delay = 100
+// Scan the target for data-pagy-json elements and apply their functions
Pagy.init =
function(arg) {
var target = arg instanceof Event || arg === undefined ? document : arg,
elements = target.querySelectorAll('[data-pagy-json]')
for (var i = 0, len = elements.length; i < len; i++) {
@@ -16,10 +19,11 @@
args.unshift(elements[i])
Pagy[fname].apply(null, args)
}
}
+// Power the pagy*_nav_js helpers
Pagy.nav =
function(pagyEl, tags, sequels, trimParam) {
var lastWidth,
pageREg = new RegExp(/__pagy_page__/g),
widths = []
@@ -43,18 +47,18 @@
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
- this.innerHTML = ''
this.insertAdjacentHTML('afterbegin', html)
lastWidth = width
}
}.bind(pagyEl)
pagyEl.render()
- }
+ }
+// Power the pagy*_combo_nav_js helpers
Pagy.combo_nav =
function(pagyEl, page, link, trimParam) {
var input = pagyEl.getElementsByTagName('input')[0],
toPage =
function() {
@@ -66,17 +70,19 @@
}
}
Pagy.addInputEventListeners(input, toPage)
}
+// Power the pagy_items_selector_js helper
Pagy.items_selector =
function(pagyEl, from, link, param) {
var input = pagyEl.getElementsByTagName('input')[0],
current = input.value,
toPage =
function() {
var items = input.value
+ if (items === 0 || items === '') { return }
if (current !== items) {
var page = Math.max(Math.ceil(from / items), 1),
html = link.replace(/__pagy_page__/, page).replace(/__pagy_items__/, items)
if (typeof (param) === 'string' && page === 1) { html = Pagy.trim(html, param) }
pagyEl.insertAdjacentHTML('afterbegin', html)
@@ -84,38 +90,41 @@
}
}
Pagy.addInputEventListeners(input, toPage)
}
+// Utility for input fields
Pagy.addInputEventListeners =
function(input, toPage) {
// select the content on click: easier for typing a number
input.addEventListener('click', function() { this.select() })
// toPage when the input looses focus
input.addEventListener('focusout', toPage)
// … and when pressing enter inside the input
input.addEventListener('keyup', function(e) { if (e.which === 13) {toPage()} }.bind(this))
}
+// Power the trim extra for js helpers
Pagy.trim =
function(html, param) {
var re = new RegExp('[?&]' + param + '=1\\b(?!&)|\\b' + param + '=1&')
return html.replace(re, '')
}
+// Render all *nav_js helpers
Pagy.renderNavs =
function() {
var navs = document.getElementsByClassName('pagy-njs') // 'pagy-njs' is common to all *nav_js helpers
for (var i = 0, len = navs.length; i < len; i++) { navs[i].render() }
}
+// Throttle to avoid to fire multiple time the renderNavs on resize
Pagy.waitForMe =
function() {
if (typeof(Pagy.tid) === 'number') { clearTimeout(Pagy.tid) }
Pagy.tid = setTimeout(Pagy.renderNavs, Pagy.delay)
}
-
-if (typeof window !== "undefined") {
+// If there is a window object then add the event listener on resize
+if (typeof window !== 'undefined') {
window.addEventListener('resize', Pagy.waitForMe, true)
}
-