From 6e8cde8bc361ca5238e8b2fcb1c5bc180874b591 Mon Sep 17 00:00:00 2001 From: Domizio Demichelis Date: Sat, 1 Dec 2018 18:50:43 +0700 Subject: [PATCH] refactoring of json tags: - single "pagy-json" class for all json tags - removed prefix used for all pagy element ids - extracted pagy_json_tag from frontend extras --- lib/javascripts/pagy.js | 12 +++++++++--- lib/pagy/extras/bootstrap.rb | 8 ++++---- lib/pagy/extras/bulma.rb | 8 ++++---- lib/pagy/extras/foundation.rb | 8 ++++---- lib/pagy/extras/items.rb | 4 ++-- lib/pagy/extras/materialize.rb | 8 ++++---- lib/pagy/extras/plain.rb | 8 ++++---- lib/pagy/extras/semantic.rb | 8 ++++---- lib/pagy/extras/shared.rb | 8 ++++++++ 9 files changed, 43 insertions(+), 29 deletions(-) diff --git a/lib/javascripts/pagy.js b/lib/javascripts/pagy.js index e0df325b3..b95065c01 100644 --- a/lib/javascripts/pagy.js +++ b/lib/javascripts/pagy.js @@ -14,7 +14,7 @@ Pagy.addInputEventListeners = function(input, handler){ }; Pagy.compact = function(id, marker, page, trim){ - var pagyNav = document.getElementById('pagy-nav-'+id), + var pagyNav = document.getElementById(id), input = pagyNav.getElementsByTagName('input')[0], link = pagyNav.getElementsByTagName('a')[0], linkP1 = pagyNav.getElementsByTagName('a')[1], @@ -32,7 +32,7 @@ Pagy.compact = function(id, marker, page, trim){ }; Pagy.items = function(id, marker, from){ - var pagyNav = document.getElementById('pagy-items-'+id), + var pagyNav = document.getElementById(id), input = pagyNav.getElementsByTagName('input')[0], current = input.value, link = pagyNav.getElementsByTagName('a')[0], @@ -49,7 +49,7 @@ Pagy.items = function(id, marker, from){ }; Pagy.responsive = function(id, tags, widths, series){ - var pagyNav = document.getElementById('pagy-nav-'+id), + var pagyNav = document.getElementById(id), pagyParent = pagyNav.parentElement, lastWidth = undefined, render = function(){ @@ -73,6 +73,12 @@ 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 = []; + var json = document.getElementsByClassName('pagy-json'); + for (var i = 0, len = json.length; i < len; i++) { + var args = JSON.parse(json[i].innerHTML); + Pagy[args.shift()].apply(null, args); + } + // Support for legacy overridden helpers. It will be removed in 2.0 ['compact', 'items', 'responsive'].forEach(function(name){ var json = document.getElementsByClassName("pagy-"+name+"-json"); for (var i = 0, len = json.length; i < len; i++) { diff --git a/lib/pagy/extras/bootstrap.rb b/lib/pagy/extras/bootstrap.rb index c7bf0f212..836581661 100644 --- a/lib/pagy/extras/bootstrap.rb +++ b/lib/pagy/extras/bootstrap.rb @@ -29,7 +29,7 @@ def pagy_bootstrap_nav(pagy) def pagy_bootstrap_compact_nav(pagy, id=caller(1,1)[0].hash.to_s) html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages - html << %(#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))}) end Pagy.deprecate self, :pagy_nav_compact_bootstrap, :pagy_bootstrap_compact_nav @@ -60,8 +60,8 @@ def pagy_bootstrap_responsive_nav(pagy, id=caller(1,1)[0].hash.to_s) tags['after'] = +(p_next ? %() : %()) tags['after'] << '' - script = %() - %(#{script}) + script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series]) + %(#{script}) end Pagy.deprecate self, :pagy_nav_responsive_bootstrap, :pagy_bootstrap_responsive_nav diff --git a/lib/pagy/extras/bulma.rb b/lib/pagy/extras/bulma.rb index 5e58d6554..0981f6ee3 100644 --- a/lib/pagy/extras/bulma.rb +++ b/lib/pagy/extras/bulma.rb @@ -31,7 +31,7 @@ def pagy_bulma_nav(pagy) def pagy_bulma_compact_nav(pagy, id=caller(1,1)[0].hash.to_s) html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages - html << %(#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))}) end Pagy.deprecate self, :pagy_nav_compact_bulma, :pagy_bulma_compact_nav @@ -62,8 +62,8 @@ def pagy_bulma_responsive_nav(pagy, id=caller(1,1)[0].hash.to_s) end end tags['after'] = '' - script = %() - %(#{script}) + script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series]) + %(#{script}) end Pagy.deprecate self, :pagy_nav_responsive_bulma, :pagy_bulma_responsive_nav diff --git a/lib/pagy/extras/foundation.rb b/lib/pagy/extras/foundation.rb index ef880c360..1cbdfb568 100644 --- a/lib/pagy/extras/foundation.rb +++ b/lib/pagy/extras/foundation.rb @@ -29,7 +29,7 @@ def pagy_foundation_nav(pagy) def pagy_foundation_compact_nav(pagy, id=caller(1,1)[0].hash.to_s) html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages - html << %(#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))}) end Pagy.deprecate self, :pagy_nav_compact_foundation, :pagy_foundation_compact_nav @@ -60,8 +60,8 @@ def pagy_foundation_responsive_nav(pagy, id=caller(1,1)[0].hash.to_s) tags['after'] = +(p_next ? %() : %()) tags['after'] << '' - script = %() - %(#{script}) + script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series]) + %(#{script}) end Pagy.deprecate self, :pagy_nav_responsive_foundation, :pagy_foundation_responsive_nav diff --git a/lib/pagy/extras/items.rb b/lib/pagy/extras/items.rb index 4a68dcade..d111fcab3 100644 --- a/lib/pagy/extras/items.rb +++ b/lib/pagy/extras/items.rb @@ -47,11 +47,11 @@ def pagy_url_for_with_items(page, pagy) def pagy_items_selector(pagy, id=caller(1,1)[0].hash.to_s) pagy = pagy.clone; p_vars = pagy.vars; p_items = p_vars[:items]; p_vars[:items] = "#{MARKER}-items-" - html = +%() + html = +%() html << %() input = %() html << %(#{pagy_t('pagy.items', items_input: input, count: p_items)}) - html << %() + html << %() end end diff --git a/lib/pagy/extras/materialize.rb b/lib/pagy/extras/materialize.rb index 26c2f7b29..34e5acbd5 100644 --- a/lib/pagy/extras/materialize.rb +++ b/lib/pagy/extras/materialize.rb @@ -28,7 +28,7 @@ def pagy_materialize_nav(pagy) def pagy_materialize_compact_nav(pagy, id=caller(1,1)[0].hash.to_s) html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages - html << %(