Skip to content

Commit

Permalink
refactoring of json tags:
Browse files Browse the repository at this point in the history
- single "pagy-json" class for all json tags
- removed prefix used for all pagy element ids
- extracted pagy_json_tag from frontend extras
  • Loading branch information
ddnexus committed Dec 1, 2018
1 parent 98ce8e7 commit 6e8cde8
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 29 deletions.
12 changes: 9 additions & 3 deletions lib/javascripts/pagy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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],
Expand All @@ -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(){
Expand All @@ -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++) {
Expand Down
8 changes: 4 additions & 4 deletions lib/pagy/extras/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 << %(<nav id="pagy-nav-#{id}" class="pagy-nav-compact-bootstrap pagy-bootstrap-compact-nav pagination" role="navigation" aria-label="pager">)
html << %(<nav id="#{id}" class="pagy-nav-compact-bootstrap pagy-bootstrap-compact-nav pagination" role="navigation" aria-label="pager">)
html << link.call(MARKER, '', %(style="display: none;" ))
(html << link.call(1, '', %(style="display: none;" ))) if defined?(TRIM)
html << %(<div class="btn-group" role="group">)
Expand All @@ -39,7 +39,7 @@ def pagy_bootstrap_compact_nav(pagy, id=caller(1,1)[0].hash.to_s)
html << %(<div class="pagy-compact-input btn btn-primary disabled">#{pagy_t('pagy.compact', page_input: input, count: p_page, pages: p_pages)}</div>)
html << (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'aria-label="next" class="next btn btn-primary"')
: %(<a class="next btn btn-primary disabled" href="#">#{pagy_t('pagy.nav.next')}</a>))
html << %(</div></nav><script type="application/json" class="pagy-compact-json">["#{id}", "#{MARKER}", "#{p_page}", #{!!defined?(TRIM)}]</script>)
html << %(</div></nav>#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))})
end
Pagy.deprecate self, :pagy_nav_compact_bootstrap, :pagy_bootstrap_compact_nav

Expand All @@ -60,8 +60,8 @@ def pagy_bootstrap_responsive_nav(pagy, id=caller(1,1)[0].hash.to_s)
tags['after'] = +(p_next ? %(<li class="page-item next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
: %(<li class="page-item next disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.next')}</a></li>))
tags['after'] << '</ul>'
script = %(<script type="application/json" class="pagy-responsive-json">["#{id}", #{tags.to_json}, #{responsive[:widths].to_json}, #{responsive[:series].to_json}]</script>)
%(<nav id="pagy-nav-#{id}" class="pagy-nav-responsive-bootstrap pagy-bootstrap-responsive-nav pagination" role="navigation" aria-label="pager"></nav>#{script})
script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series])
%(<nav id="#{id}" class="pagy-nav-responsive-bootstrap pagy-bootstrap-responsive-nav pagination" role="navigation" aria-label="pager"></nav>#{script})
end
Pagy.deprecate self, :pagy_nav_responsive_bootstrap, :pagy_bootstrap_responsive_nav

Expand Down
8 changes: 4 additions & 4 deletions lib/pagy/extras/bulma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 << %(<nav id="pagy-nav-#{id}" class="pagy-nav-compact-bulma pagy-bulma-compact-nav" role="navigation" aria-label="pagination">)
html << %(<nav id="#{id}" class="pagy-nav-compact-bulma pagy-bulma-compact-nav" role="navigation" aria-label="pagination">)
html << link.call(MARKER, '', 'style="display: none;"')
(html << link.call(1, '', %(style="display: none;"))) if defined?(TRIM)
html << %(<div class="field is-grouped is-grouped-centered" role="group">)
Expand All @@ -41,7 +41,7 @@ def pagy_bulma_compact_nav(pagy, id=caller(1,1)[0].hash.to_s)
html << %(<div class="pagy-compact-input control level is-mobile">#{pagy_t('pagy.compact', page_input: input, count: p_page, pages: p_pages)}</div>)
html << (p_next ? %(<p class="control">#{link.call(p_next, pagy_t('pagy.nav.next'), 'class="button" aria-label="next page"')}</p>)
: %(<p class="control"><a class="button" disabled>#{pagy_t('pagy.nav.next')}</a></p>))
html << %(</div></nav><script type="application/json" class="pagy-compact-json">["#{id}", "#{MARKER}", "#{p_page}", #{!!defined?(TRIM)}]</script>)
html << %(</div></nav>#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))})
end
Pagy.deprecate self, :pagy_nav_compact_bulma, :pagy_bulma_compact_nav

Expand All @@ -62,8 +62,8 @@ def pagy_bulma_responsive_nav(pagy, id=caller(1,1)[0].hash.to_s)
end
end
tags['after'] = '</ul>'
script = %(<script type="application/json" class="pagy-responsive-json">["#{id}", #{tags.to_json}, #{responsive[:widths].to_json}, #{responsive[:series].to_json}]</script>)
%(<nav id="pagy-nav-#{id}" class="pagy-nav-responsive-bulma pagy-bulma-responsive-nav pagination is-centered" role="navigation" aria-label="pagination"></nav>#{script})
script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series])
%(<nav id="#{id}" class="pagy-nav-responsive-bulma pagy-bulma-responsive-nav pagination is-centered" role="navigation" aria-label="pagination"></nav>#{script})
end
Pagy.deprecate self, :pagy_nav_responsive_bulma, :pagy_bulma_responsive_nav

Expand Down
8 changes: 4 additions & 4 deletions lib/pagy/extras/foundation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 << %(<nav id="pagy-nav-#{id}" class="pagy-nav-compact-foundation pagy-foundation-compact-nav" role="navigation" aria-label="Pagination">)
html << %(<nav id="#{id}" class="pagy-nav-compact-foundation pagy-foundation-compact-nav" role="navigation" aria-label="Pagination">)
html << link.call(MARKER, '', %(style="display: none;" ))
(html << link.call(1, '', %(style="display: none;" ))) if defined?(TRIM)
html << %(<div class="input-group">)
Expand All @@ -39,7 +39,7 @@ def pagy_foundation_compact_nav(pagy, id=caller(1,1)[0].hash.to_s)
html << %(<span class="input-group-label">#{pagy_t('pagy.compact', page_input: input, count: p_page, pages: p_pages)}</span>)
html << (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'style="margin-bottom: 0px;" aria-label="next" class="next button primary"')
: %(<a style="margin-bottom: 0px;" class="next button primary disabled" href="#">#{pagy_t('pagy.nav.next')}</a>))
html << %(</div></nav><script type="application/json" class="pagy-compact-json">["#{id}", "#{MARKER}", "#{p_page}", #{!!defined?(TRIM)}]</script>)
html << %(</div></nav>#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))})
end
Pagy.deprecate self, :pagy_nav_compact_foundation, :pagy_foundation_compact_nav

Expand All @@ -60,8 +60,8 @@ def pagy_foundation_responsive_nav(pagy, id=caller(1,1)[0].hash.to_s)
tags['after'] = +(p_next ? %(<li class="next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
: %(<li class="next disabled">#{pagy_t('pagy.nav.next')}</li>))
tags['after'] << '</ul>'
script = %(<script type="application/json" class="pagy-responsive-json">["#{id}", #{tags.to_json}, #{responsive[:widths].to_json}, #{responsive[:series].to_json}]</script>)
%(<nav id="pagy-nav-#{id}" class="pagy-nav-responsive-foundation pagy-foundation-responsive-nav" aria-label="Pagination"></nav>#{script})
script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series])
%(<nav id="#{id}" class="pagy-nav-responsive-foundation pagy-foundation-responsive-nav" aria-label="Pagination"></nav>#{script})
end
Pagy.deprecate self, :pagy_nav_responsive_foundation, :pagy_foundation_responsive_nav

Expand Down
4 changes: 2 additions & 2 deletions lib/pagy/extras/items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = +%(<span id="pagy-items-#{id}">)
html = +%(<span id="#{id}">)
html << %(<a href="#{pagy_url_for("#{MARKER}-page-", pagy)}"></a>)
input = %(<input type="number" min="1" max="#{p_vars[:max_items]}" value="#{p_items}" style="padding: 0; text-align: center; width: #{p_items.to_s.length+1}rem;">)
html << %(#{pagy_t('pagy.items', items_input: input, count: p_items)})
html << %(</span><script type="application/json" class="pagy-items-json">["#{id}", "#{MARKER}", #{pagy.from}]</script>)
html << %(</span><script type="application/json" class="pagy-json">#{[:items, id, MARKER, pagy.from]}</script>)
end

end
Expand Down
8 changes: 4 additions & 4 deletions lib/pagy/extras/materialize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 << %(<div id="pagy-nav-#{id}" class="pagy-nav-compact-materialize pagy-materialize-compact-nav pagination" role="navigation" aria-label="pager">)
html << %(<div id="#{id}" class="pagy-nav-compact-materialize pagy-materialize-compact-nav pagination" role="navigation" aria-label="pager">)
html << link.call(MARKER, '', %(style="display: none;" ))
(html << link.call(1, '', %(style="display: none;" ))) if defined?(TRIM)
html << %(<div class="pagy-compact-chip role="group" style="height: 35px; border-radius: 18px; background: #e4e4e4; display: inline-block;">)
Expand All @@ -40,7 +40,7 @@ def pagy_materialize_compact_nav(pagy, id=caller(1,1)[0].hash.to_s)
html << %(<div class="pagy-compact-input btn-flat" style="cursor: default; padding: 0px">#{pagy_t('pagy.compact', page_input: input, count: p_page, pages: p_pages)}</div>)
html << (p_next ? %(<li class="waves-effect next" #{li_style}>#{link.call p_next, '<i class="material-icons">chevron_right</i>', 'aria-label="next"'}</li>)
: %(<li class="next disabled" #{li_style}><a href="#"><i class="material-icons">chevron_right</i></a></li>))
html << %(</ul></div><script type="application/json" class="pagy-compact-json">["#{id}", "#{MARKER}", "#{p_page}", #{!!defined?(TRIM)}]</script>)
html << %(</ul></div>#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))})
end
Pagy.deprecate self, :pagy_nav_compact_materialize, :pagy_materialize_compact_nav

Expand All @@ -61,8 +61,8 @@ def pagy_materialize_responsive_nav(pagy, id=caller(1,1)[0].hash.to_s)
tags['after'] = +(p_next ? %(<li class="waves-effect next">#{link.call p_next, '<i class="material-icons">chevron_right</i>', 'aria-label="next"'}</li>)
: %(<li class="next disabled"><a href="#"><i class="material-icons">chevron_right</i></a></li>))
tags['after'] << '</ul>'
script = %(<script type="application/json" class="pagy-responsive-json">["#{id}", #{tags.to_json}, #{responsive[:widths].to_json}, #{responsive[:series].to_json}]</script>)
%(<div id="pagy-nav-#{id}" class="pagy-nav-responsive-materialize pagy-materialize-responsive-nav pagination" role="navigation" aria-label="pager"></div>#{script})
script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series])
%(<div id="#{id}" class="pagy-nav-responsive-materialize pagy-materialize-responsive-nav pagination" role="navigation" aria-label="pager"></div>#{script})
end
Pagy.deprecate self, :pagy_nav_responsive_materialize, :pagy_materialize_responsive_nav

Expand Down
8 changes: 4 additions & 4 deletions lib/pagy/extras/plain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Frontend
def pagy_plain_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 << %(<nav id="pagy-nav-#{id}" class="pagy-nav-compact pagy-plain-compact-nav pagination" role="navigation" aria-label="pager">)
html << %(<nav id="#{id}" class="pagy-nav-compact pagy-plain-compact-nav pagination" role="navigation" aria-label="pager">)
html << link.call(MARKER, '', %(style="display: none;" ))
(html << link.call(1, '', %(style="display: none;" ))) if defined?(TRIM)
html << (p_prev ? %(<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> )
Expand All @@ -23,7 +23,7 @@ def pagy_plain_compact_nav(pagy, id=caller(1,1)[0].hash.to_s)
html << %(<span class="pagy-compact-input" style="margin: 0 0.6rem;">#{pagy_t('pagy.compact', page_input: input, count: p_page, pages: p_pages)}</span> )
html << (p_next ? %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>)
: %(<span class="page next disabled">#{pagy_t('pagy.nav.next')}</span>))
html << %(</nav><script type="application/json" class="pagy-compact-json">["#{id}", "#{MARKER}", "#{p_page}", #{!!defined?(TRIM)}]</script>)
html << %(</nav>#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))})
end
Pagy.deprecate self, :pagy_nav_compact, :pagy_plain_compact_nav

Expand All @@ -42,8 +42,8 @@ def pagy_plain_responsive_nav(pagy, id=caller(1,1)[0].hash.to_s)
end
tags['after'] = (p_next ? %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>)
: %(<span class="page next disabled">#{pagy_t('pagy.nav.next')}</span>))
script = %(<script type="application/json" class="pagy-responsive-json">["#{id}", #{tags.to_json}, #{responsive[:widths].to_json}, #{responsive[:series].to_json}]</script>)
%(<nav id="pagy-nav-#{id}" class="pagy-nav-responsive pagy-plain-responsive-nav pagination" role="navigation" aria-label="pager"></nav>#{script})
script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series])
%(<nav id="#{id}" class="pagy-nav-responsive pagy-plain-responsive-nav pagination" role="navigation" aria-label="pager"></nav>#{script})
end
Pagy.deprecate self, :pagy_nav_responsive, :pagy_plain_responsive_nav

Expand Down
8 changes: 4 additions & 4 deletions lib/pagy/extras/semantic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def pagy_semantic_nav(pagy)
def pagy_semantic_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, 'class="item"'), pagy.prev, pagy.next, pagy.page, pagy.pages

html << %(<div id="pagy-nav-#{id}" class="pagy-nav-compact-semantic pagy-semantic-compact-nav ui compact menu" role="navigation" aria-label="pager">)
html << %(<div id="#{id}" class="pagy-nav-compact-semantic pagy-semantic-compact-nav ui compact menu" role="navigation" aria-label="pager">)
html << link.call(MARKER, '', %(style="display: none;" ))
(html << link.call(1, '', %(style="display: none;" ))) if defined?(TRIM)
html << (p_prev ? %(#{link.call p_prev, '<i class="left small chevron icon"></i>', 'aria-label="previous"'})
Expand All @@ -38,7 +38,7 @@ def pagy_semantic_compact_nav(pagy, id=caller(1,1)[0].hash.to_s)
html << %(<div class="pagy-compact-input item">#{pagy_t('pagy.compact', page_input: input, count: p_page, pages: p_pages)}</div> )
html << (p_next ? %(#{link.call p_next, '<i class="right small chevron icon"></i>', 'aria-label="next"'})
: %(<div class="item disabled"><i class="right small chevron icon"></i></div>))
html << %(</div><script type="application/json" class="pagy-compact-json">["#{id}", "#{MARKER}", "#{p_page}", #{!!defined?(TRIM)}]</script>)
html << %(</div>#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))})
end
Pagy.deprecate self, :pagy_nav_compact_semantic, :pagy_semantic_compact_nav

Expand All @@ -57,8 +57,8 @@ def pagy_semantic_responsive_nav(pagy, id=caller(1,1)[0].hash.to_s)
end
tags['after'] = (p_next ? %(#{link.call p_next, '<i class="right small chevron icon"></i>', 'aria-label="next"'})
: %(<div class="item disabled"><i class="right small chevron icon"></i></div>))
script = %(<script type="application/json" class="pagy-responsive-json">["#{id}", #{tags.to_json}, #{responsive[:widths].to_json}, #{responsive[:series].to_json}]</script>)
%(<div id="pagy-nav-#{id}" class="pagy-nav-responsive-semantic pagy-semantic-responsive-nav ui pagination menu" role="navigation" aria-label="pager"></div>#{script})
script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series])
%(<div id="#{id}" class="pagy-nav-responsive-semantic pagy-semantic-responsive-nav ui pagination menu" role="navigation" aria-label="pager"></div>#{script})
end
Pagy.deprecate self, :pagy_nav_responsive_semantic, :pagy_semantic_responsive_nav

Expand Down
8 changes: 8 additions & 0 deletions lib/pagy/extras/shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@ def self.deprecate(mod, old_meth, new_meth)
end
end

module Frontend

def pagy_json_tag(*args)
%(<script type="application/json" class="pagy-json">#{args.to_json}</script>)
end

end

end

0 comments on commit 6e8cde8

Please sign in to comment.