Skip to content

Commit

Permalink
refactoring of pagy_json_tag
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Mar 10, 2021
1 parent 745e68f commit 4d48d33
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lib/pagy/extras/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def pagy_bootstrap_nav_js(pagy, id=pagy_id)
'gap' => %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.gap')}</a></li>),
'after' => p_next ? %(<li class="page-item next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li></ul>)
: %(<li class="page-item next disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.next')}</a></li></ul>) }
%(<nav id="#{id}" class="pagy-bootstrap-nav-js" role="navigation" aria-label="pager"></nav>#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(Trim) && pagy.vars[:page_param])})
%(<nav id="#{id}" class="pagy-bootstrap-nav-js" role="navigation" aria-label="pager"></nav>#{pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)})
end

# Javascript combo pagination for bootstrap: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
Expand All @@ -48,7 +48,7 @@ def pagy_bootstrap_combo_nav_js(pagy, id=pagy_id)
html << %(<div class="pagy-combo-input btn btn-primary disabled" style="white-space: nowrap;">#{pagy_t('pagy.combo_nav_js', 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>#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(Trim) && pagy.vars[:page_param])})
html << %(</div></nav>#{pagy_json_tag(pagy, :combo_nav, id, p_page, pagy_marked_link(link))})
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/pagy/extras/bulma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def pagy_bulma_nav_js(pagy, id=pagy_id)
'active' => %(<li>#{link.call(PAGE_PLACEHOLDER, PAGE_PLACEHOLDER, %(class="pagination-link is-current" aria-current="page" aria-label="page #{PAGE_PLACEHOLDER}"))}</li>),
'gap' => %(<li><span class="pagination-ellipsis">#{pagy_t('pagy.nav.gap')}</span></li>),
'after' => '</ul>' }
%(<nav id="#{id}" class="pagy-bulma-nav-js pagination is-centered" role="navigation" aria-label="pagination"></nav>#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(Trim) && pagy.vars[:page_param])})
%(<nav id="#{id}" class="pagy-bulma-nav-js pagination is-centered" role="navigation" aria-label="pagination"></nav>#{pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)})
end

# Javascript combo pagination for Bulma: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
Expand All @@ -53,7 +53,7 @@ def pagy_bulma_combo_nav_js(pagy, id=pagy_id)
html << %(<div class="pagy-combo-input control level is-mobile">#{pagy_t('pagy.combo_nav_js', 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>#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(Trim) && pagy.vars[:page_param])})
html << %(</div></nav>#{pagy_json_tag(pagy, :combo_nav, id, p_page, pagy_marked_link(link))})
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/pagy/extras/foundation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def pagy_foundation_nav_js(pagy, id=pagy_id)
'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>)) \
+ '</ul>' ) }
%(<nav id="#{id}" class="pagy-foundation-nav-js" role="navigation" aria-label="Pagination"></nav>#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(Trim) && pagy.vars[:page_param])})
%(<nav id="#{id}" class="pagy-foundation-nav-js" role="navigation" aria-label="Pagination"></nav>#{pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)})
end

# Javascript combo pagination for Foundation: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
Expand All @@ -50,7 +50,7 @@ def pagy_foundation_combo_nav_js(pagy, id=pagy_id)
html << %(<span class="input-group-label">#{pagy_t('pagy.combo_nav_js', 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>#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(Trim) && pagy.vars[:page_param])})
html << %(</div></nav>#{pagy_json_tag(pagy, :combo_nav, id, p_page, pagy_marked_link(link))})
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/pagy/extras/items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def pagy_items_selector_js(pagy, id=pagy_id)
html = %(<span id="#{id}">)
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_selector_js', item_name: pagy_t(p_vars[:i18n_key], count: p_items), items_input: input, count: p_items)})
html << %(</span>#{pagy_json_tag(:items_selector, id, pagy.from, link, defined?(Trim) && p_vars[:page_param])})
html << %(</span>#{pagy_json_tag(pagy, :items_selector, id, pagy.from, link)})
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/pagy/extras/materialize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def pagy_materialize_nav_js(pagy, id=pagy_id)
'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>)) \
+ '</ul>' ) }
%(<div id="#{id}" class="pagy-materialize-nav-js" role="navigation" aria-label="pager"></div>#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(Trim) && pagy.vars[:page_param])})
%(<div id="#{id}" class="pagy-materialize-nav-js" role="navigation" aria-label="pager"></div>#{pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)})
end

# Javascript combo pagination for materialize: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
Expand All @@ -52,7 +52,7 @@ def pagy_materialize_combo_nav_js(pagy, id=pagy_id)
html << %(<div class="pagy-combo-input btn-flat" style="cursor: default; padding: 0px">#{pagy_t('pagy.combo_nav_js', 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>#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(Trim) && pagy.vars[:page_param])})
html << %(</ul></div>#{pagy_json_tag(pagy, :combo_nav, id, p_page, pagy_marked_link(link))})
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/pagy/extras/navs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def pagy_nav_js(pagy, id=pagy_id)
'gap' => %(<span class="page gap">#{pagy_t('pagy.nav.gap')}</span> ),
'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>) }
%(<nav id="#{id}" class="pagy-nav-js pagination" role="navigation" aria-label="pager"></nav>#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(Trim) && pagy.vars[:page_param])})
%(<nav id="#{id}" class="pagy-nav-js pagination" role="navigation" aria-label="pager"></nav>#{pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)})
end

# Javascript combo pagination: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
Expand All @@ -31,7 +31,7 @@ def pagy_combo_nav_js(pagy, id=pagy_id)
html << %(<span class="pagy-combo-input" style="margin: 0 0.6rem;">#{pagy_t('pagy.combo_nav_js', 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>#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(Trim) && pagy.vars[:page_param])})
html << %(</nav>#{pagy_json_tag(pagy, :combo_nav, id, p_page, pagy_marked_link(link))})
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/pagy/extras/semantic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def pagy_semantic_nav_js(pagy, id=pagy_id)
'gap' => %(<div class="disabled item">#{pagy_t('pagy.nav.gap')}</div>),
'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>)) }
%(<div id="#{id}" class="pagy-semantic-nav-js ui pagination menu" role="navigation" aria-label="pager"></div>#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(Trim) && pagy.vars[:page_param])})
%(<div id="#{id}" class="pagy-semantic-nav-js ui pagination menu" role="navigation" aria-label="pager"></div>#{pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)})
end

# Combo pagination for semantic: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
Expand All @@ -48,7 +48,7 @@ def pagy_semantic_combo_nav_js(pagy, id=pagy_id)
html << %(<div class="pagy-combo-input item">#{pagy_t('pagy.combo_nav_js', 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>#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(Trim) && pagy.vars[:page_param])})
html << %(</div>#{pagy_json_tag(pagy, :combo_nav, id, p_page, pagy_marked_link(link))})
end

end
Expand Down
10 changes: 8 additions & 2 deletions lib/pagy/extras/shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ module Frontend

if defined?(Oj)
# it returns a script tag with the JSON-serialized args generated with the faster oj gem
def pagy_json_tag(*args) = %(<script type="application/json" class="pagy-json">#{Oj.dump(args, mode: :strict)}</script>)
def pagy_json_tag(pagy, *args)
args << ( defined?(Trim) && pagy.vars[:page_param] )
%(<script type="application/json" class="pagy-json">#{Oj.dump(args, mode: :strict)}</script>)
end
else
require 'json'
# it returns a script tag with the JSON-serialized args generated with the slower to_json
def pagy_json_tag(*args) = %(<script type="application/json" class="pagy-json">#{args.to_json}</script>)
def pagy_json_tag(pagy, *args)
args << ( defined?(Trim) && pagy.vars[:page_param] )
%(<script type="application/json" class="pagy-json">#{args.to_json}</script>)
end
end

# it returns the SHA1 (fastest on modern ruby) string used as default `id` attribute by all the `*_js` tags
Expand Down
4 changes: 2 additions & 2 deletions lib/pagy/extras/uikit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def pagy_uikit_nav_js(pagy, id=pagy_id)
'gap' => %(<li class="uk-disabled"><span>#{pagy_t('pagy.nav.gap')}</span></li>),
'after' => p_next ? %(<li>#{link.call p_next, next_span}</li>)
: %(<li class="uk-disabled"><a href="#">#{next_span}</a></li>) }
%(<ul id="#{id}" class="pagy-uikit-nav-js uk-pagination uk-flex-center"></ul>#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(Trim) && pagy.vars[:page_param])})
%(<ul id="#{id}" class="pagy-uikit-nav-js uk-pagination uk-flex-center"></ul>#{pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)})
end

# Javascript combo pagination for uikit: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
Expand All @@ -56,7 +56,7 @@ def pagy_uikit_combo_nav_js(pagy, id=pagy_id)
html << (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'class="uk-button uk-button-default"')
: %(<button class="uk-button uk-button-default" disabled>#{pagy_t('pagy.nav.next')}</button>))

html << %(</div>#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(Trim) && pagy.vars[:page_param])})
html << %(</div>#{pagy_json_tag(pagy, :combo_nav, id, p_page, pagy_marked_link(link))})
end
end
end
4 changes: 2 additions & 2 deletions test/pagy/extras/shared_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
describe "#pagy_json_tag" do

it 'should use oj' do
_(view.pagy_json_tag(:test_function, 'some-id', 'some-string', 123, true)).must_equal \
"<script type=\"application/json\" class=\"pagy-json\">[\"test_function\",\"some-id\",\"some-string\",123,true]</script>"
_(view.pagy_json_tag(Pagy.new(count: 10), :test_function, 'some-id', 'some-string', 123, true)).must_equal \
"<script type=\"application/json\" class=\"pagy-json\">[\"test_function\",\"some-id\",\"some-string\",123,true,null]</script>"
end

end
Expand Down

0 comments on commit 4d48d33

Please sign in to comment.