lib/javascripts/pagy.js in pagy-3.1.0 vs lib/javascripts/pagy.js in pagy-3.2.0

- old
+ new

@@ -9,16 +9,16 @@ var args = JSON.parse(jsonTags[i].innerHTML); Pagy[args.shift()].apply(null, args); } }; -Pagy.nav = function(id, tags, sequels){ +Pagy.nav = function(id, tags, sequels, param){ var pagyEl = document.getElementById(id), container = pagyEl.parentElement, lastWidth = undefined, timeoutId = 0, - markRE = new RegExp('--pagy.page--', 'g'), + pageREg = new RegExp(/__pagy_page__/g), widths = []; for (var width in sequels) { widths.push(parseInt(width)) } // fine with sequels structure widths.sort(function(a, b){return b-a}); var render = function(){ @@ -26,21 +26,22 @@ var width, i, len; for (i = 0, len = widths.length; i < len; i++) { if (container.clientWidth > widths[i]) { width = widths[i]; break } } if (width !== lastWidth) { - while (pagyEl.firstChild) { pagyEl.removeChild(pagyEl.firstChild) } var html = tags.before, series = sequels[width]; for (i = 0, len = series.length; i < len; i++) { var item = series[i]; - if (typeof(item) === 'number') { html += tags.link.replace(markRE, item) } - else if (item === 'gap') { html += tags.gap } - else if (typeof(item) === 'string') { html += tags.active.replace(markRE, item) } + if (typeof(param) === 'string' && item === 1) { html += Pagy.trim(tags.link.replace(pageREg, item), param) } + 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.insertAdjacentHTML('beforeend', html); + pagyEl.innerHTML = ''; + pagyEl.insertAdjacentHTML('afterbegin', html); lastWidth = width; } }, // suppress rapid firing rendering rendering = function(){ clearTimeout(timeoutId); timeoutId = setTimeout(render, 150) }; @@ -52,39 +53,35 @@ Pagy.windowListeners[id] = rendering; } render(); }; -Pagy.combo_nav = function(id, page, links){ +Pagy.combo_nav = function(id, page, link, param){ var pagyEl = document.getElementById(id), input = pagyEl.getElementsByTagName('input')[0], go = function(){ if (page !== input.value) { - if (links.hasOwnProperty('trimmed') && input.value === '1') { - pagyEl.insertAdjacentHTML('afterbegin', links.trimmed); - } else { - pagyEl.insertAdjacentHTML('afterbegin', links.standard.replace('--pagy.page--', input.value)); - } + var html = link.replace(/__pagy_page__/, input.value); + if (typeof(param) === 'string' && input.value === '1') { html = Pagy.trim(html, param) } + pagyEl.insertAdjacentHTML('afterbegin', html); pagyEl.getElementsByTagName('a')[0].click(); } }; Pagy.addInputEventListeners(input, go); }; -Pagy.items_selector = function(id, from, links){ +Pagy.items_selector = function(id, from, link, param){ var pagyEl = document.getElementById(id), input = pagyEl.getElementsByTagName('input')[0], current = input.value, go = function(){ var items = input.value; if (current !== items) { - var page = Math.max(Math.ceil(from / items),1); - if (links.hasOwnProperty('trimmed') && page === 1) { - pagyEl.insertAdjacentHTML('afterbegin', links.trimmed.replace('--pagy.items--', items)); - } else { - pagyEl.insertAdjacentHTML('afterbegin', links.standard.replace('--pagy.page--', page).replace('--pagy.items--', 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); pagyEl.getElementsByTagName('a')[0].click(); } }; Pagy.addInputEventListeners(input, go); }; @@ -97,5 +94,10 @@ // go when the input looses focus input.addEventListener('focusout', handler); // … and when pressing enter inside the input input.addEventListener('keyup', function(e){ if (e.which === 13) handler() }.bind(this)); }; + +Pagy.trim = function(html, param){ + var re = new RegExp('[?&]' + param + '=1(?![&])|(?<=[?&])' + param + '=1&'); + return html.replace(re, ''); + };