Module: Pagy::Frontend
- Includes:
- UseI18nGem, UseItemsExtra, Helpers, UseTrimExtra
- Defined in:
- lib/pagy/frontend.rb,
lib/pagy/extras/i18n.rb,
lib/pagy/extras/navs.rb,
lib/pagy/extras/bulma.rb,
lib/pagy/extras/items.rb,
lib/pagy/extras/uikit.rb,
lib/pagy/extras/shared.rb,
lib/pagy/extras/support.rb,
lib/pagy/extras/semantic.rb,
lib/pagy/extras/bootstrap.rb,
lib/pagy/extras/foundation.rb,
lib/pagy/extras/materialize.rb
Overview
Use ::I18n gem
Defined Under Namespace
Modules: UseI18nGem, UseItemsExtra
Instance Method Summary collapse
-
#pagy_bootstrap_combo_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript combo pagination for bootstrap: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript.
-
#pagy_bootstrap_nav(pagy) ⇒ Object
Pagination for bootstrap: it returns the html with the series of links to the pages.
-
#pagy_bootstrap_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript pagination for bootstrap: it returns a nav and a JSON tag used by the Pagy.nav javascript.
-
#pagy_bulma_combo_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript combo pagination for Bulma: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript.
-
#pagy_bulma_nav(pagy) ⇒ Object
Pagination for Bulma: it returns the html with the series of links to the pages.
- #pagy_bulma_nav_js(pagy, id = pagy_id) ⇒ Object
-
#pagy_combo_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript combo pagination: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript.
-
#pagy_foundation_combo_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript combo pagination for Foundation: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript.
-
#pagy_foundation_nav(pagy) ⇒ Object
Pagination for Foundation: it returns the html with the series of links to the pages.
-
#pagy_foundation_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript pagination for foundation: it returns a nav and a JSON tag used by the Pagy.nav javascript.
-
#pagy_id ⇒ Object
it returns the SHA1 (fastest on modern ruby) string used as default ‘id` attribute by all the `*_js` tags.
-
#pagy_info(pagy, item_name = nil) ⇒ Object
Return examples: “Displaying items 41-60 of 324 in total” of “Displaying Products 41-60 of 324 in total”.
-
#pagy_items_selector_js(pagy, id = pagy_id) ⇒ Object
Return the items selector HTML.
-
#pagy_json_tag(pagy, *args) ⇒ Object
it returns a script tag with the JSON-serialized args generated with the slower to_json.
-
#pagy_link_proc(pagy, link_extra = '') ⇒ Object
Returns a performance optimized proc to generate the HTML links Benchmarked on a 20 link nav: it is ~22x faster and uses ~18x less memory than rails’ link_to.
-
#pagy_marked_link(link) ⇒ Object
it returns the marked link to used by pagy.js.
-
#pagy_materialize_combo_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript combo pagination for materialize: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript.
-
#pagy_materialize_nav(pagy) ⇒ Object
Pagination for materialize: it returns the html with the series of links to the pages.
-
#pagy_materialize_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript pagination for materialize: it returns a nav and a JSON tag used by the Pagy.nav javascript.
-
#pagy_nav(pagy) ⇒ Object
Generic pagination: it returns the html with the series of links to the pages.
-
#pagy_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript pagination: it returns a nav and a JSON tag used by the Pagy.nav javascript.
- #pagy_next_link(pagy, text = pagy_t('pagy.nav.next'), link_extra = '') ⇒ Object
- #pagy_next_link_tag(pagy) ⇒ Object
- #pagy_next_url(pagy) ⇒ Object
- #pagy_prev_link(pagy, text = pagy_t('pagy.nav.prev'), link_extra = '') ⇒ Object
- #pagy_prev_link_tag(pagy) ⇒ Object
- #pagy_prev_url(pagy) ⇒ Object
-
#pagy_semantic_combo_nav_js(pagy, id = pagy_id) ⇒ Object
Combo pagination for semantic: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript.
-
#pagy_semantic_nav(pagy) ⇒ Object
Pagination for semantic: it returns the html with the series of links to the pages.
-
#pagy_semantic_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript pagination for semantic: it returns a nav and a JSON tag used by the Pagy.nav javascript.
-
#pagy_t(key, **opts) ⇒ Object
Similar to I18n.t: just ~18x faster using ~10x less memory (@pagy_locale explicitly initialized in order to avoid warning).
-
#pagy_uikit_combo_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript combo pagination for uikit: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript.
-
#pagy_uikit_nav(pagy) ⇒ Object
Pagination for uikit: it returns the html with the series of links to the pages.
-
#pagy_uikit_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript pagination for uikit: it returns a nav and a JSON tag used by the Pagy.nav javascript.
Methods included from Helpers
#pagy_get_params, #pagy_url_for
Methods included from UseItemsExtra
Instance Method Details
#pagy_bootstrap_combo_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript combo pagination for bootstrap: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/pagy/extras/bootstrap.rb', line 40 def pagy_bootstrap_combo_nav_js(pagy, id=pagy_id) link = pagy_link_proc(pagy) p_page = pagy.page p_pages = pagy.pages input = %(<input type="number" min="1" max="#{p_pages}" value="#{p_page}" class="text-primary" style="padding: 0; border: none; text-align: center; width: #{p_pages.to_s.length+1}rem;">) %(<nav id="#{id}" class="pagy-bootstrap-combo-nav-js pagination" role="navigation" aria-label="pager"><div class="btn-group" role="group">#{ if (p_prev = pagy.prev) link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous" class="prev btn btn-primary"' else %(<a class="prev btn btn-primary disabled" href="#">#{pagy_t('pagy.nav.prev')}</a>) end }<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>#{ if (p_next = pagy.next) link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next" class="next btn btn-primary"' else %(<a class="next btn btn-primary disabled" href="#">#{pagy_t 'pagy.nav.next' }</a>) end }</div></nav>#{ pagy_json_tag pagy, :combo_nav, id, p_page, pagy_marked_link(link) }) end |
#pagy_bootstrap_nav(pagy) ⇒ Object
Pagination for bootstrap: it returns the html with the series of links to the pages
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/pagy/extras/bootstrap.rb', line 10 def pagy_bootstrap_nav(pagy) link = pagy_link_proc(pagy, 'class="page-link"') html = +%(<nav class="pagy-bootstrap-nav" role="navigation" aria-label="pager"><ul class="pagination">) html << pagy_bootstrap_prev_html(pagy, link) pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36] html << case item when Integer then %(<li class="page-item">#{link.call item}</li>) when String then %(<li class="page-item active">#{link.call item}</li>) when :gap then %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t 'pagy.nav.gap'}</a></li>) end end html << pagy_bootstrap_next_html(pagy, link) html << %(</ul></nav>) end |
#pagy_bootstrap_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript pagination for bootstrap: it returns a nav and a JSON tag used by the Pagy.nav javascript
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/pagy/extras/bootstrap.rb', line 27 def pagy_bootstrap_nav_js(pagy, id=pagy_id) link = pagy_link_proc(pagy, 'class="page-link"') = { 'before' => %(<ul class="pagination">#{pagy_bootstrap_prev_html pagy, link}), 'link' => %(<li class="page-item">#{mark = link.call(PAGE_PLACEHOLDER)}</li>), 'active' => %(<li class="page-item active">#{mark}</li>), 'gap' => %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t 'pagy.nav.gap'}</a></li>), 'after' => %(#{pagy_bootstrap_next_html pagy, link}</ul>) } html = %(<nav id="#{id}" class="pagy-bootstrap-nav-js" role="navigation" aria-label="pager"></nav>) html << pagy_json_tag(pagy, :nav, id, , pagy.sequels) end |
#pagy_bulma_combo_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript combo pagination for Bulma: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/pagy/extras/bulma.rb', line 39 def pagy_bulma_combo_nav_js(pagy, id=pagy_id) link = pagy_link_proc(pagy) p_page = pagy.page p_pages = pagy.pages input = %(<input class="input" type="number" min="1" max="#{p_pages}" value="#{p_page}" style="padding: 0; text-align: center; width: #{p_pages.to_s.length+1}rem; margin:0 0.3rem;">) %(<nav id="#{id}" class="pagy-bulma-combo-nav-js" role="navigation" aria-label="pagination"><div class="field is-grouped is-grouped-centered" role="group">#{ if (p_prev = pagy.prev) %(<p class="control">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'class="button" aria-label="previous page"'}</p>) else %(<p class="control"><a class="button" disabled>#{pagy_t 'pagy.nav.prev'}</a></p>) end }<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>#{ if (p_next = pagy.next) %(<p class="control">#{link.call p_next, pagy_t('pagy.nav.next'), 'class="button" aria-label="next page"'}</p>) else %(<p class="control"><a class="button" disabled>#{pagy_t 'pagy.nav.next'}</a></p>) end }</div></nav>#{ pagy_json_tag pagy, :combo_nav, id, p_page, pagy_marked_link(link) }) end |
#pagy_bulma_nav(pagy) ⇒ Object
Pagination for Bulma: it returns the html with the series of links to the pages
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/pagy/extras/bulma.rb', line 10 def pagy_bulma_nav(pagy) link = pagy_link_proc(pagy) html = +%(<nav class="pagy-bulma-nav pagination is-centered" role="navigation" aria-label="pagination">) html << pagy_bulma_prev_next_html(pagy, link) html << %(<ul class="pagination-list">) pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36] html << case item when Integer then %(<li>#{link.call item, item, %(class="pagination-link" aria-label="goto page #{item}") }</li>) # page link when String then %(<li>#{link.call item, item, %(class="pagination-link is-current" aria-label="page #{item}" aria-current="page")}</li>) # active page when :gap then %(<li><span class="pagination-ellipsis">#{pagy_t 'pagy.nav.gap'}</span></li>) # page gap end end html << %(</ul></nav>) end |
#pagy_bulma_nav_js(pagy, id = pagy_id) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/pagy/extras/bulma.rb', line 26 def pagy_bulma_nav_js(pagy, id=pagy_id) link = pagy_link_proc(pagy) = { 'before' => %(#{pagy_bulma_prev_next_html(pagy, link)}<ul class="pagination-list">), 'link' => %(<li>#{link.call PAGE_PLACEHOLDER, PAGE_PLACEHOLDER, %(class="pagination-link" aria-label="goto page #{PAGE_PLACEHOLDER}")}</li>), '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>' } html = %(<nav id="#{id}" class="pagy-bulma-nav-js pagination is-centered" role="navigation" aria-label="pagination"></nav>) html << pagy_json_tag(pagy, :nav, id, , pagy.sequels) end |
#pagy_combo_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript combo pagination: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/pagy/extras/navs.rb', line 23 def pagy_combo_nav_js(pagy, id=pagy_id) link = pagy_link_proc(pagy) p_page = pagy.page p_pages = pagy.pages input = %(<input type="number" min="1" max="#{p_pages}" value="#{p_page}" style="padding: 0; text-align: center; width: #{p_pages.to_s.length+1}rem;">) %(<nav id="#{id}" class="pagy-combo-nav-js pagination" role="navigation" aria-label="pager">#{ pagy_nav_prev_html pagy, link }<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> #{ pagy_nav_next_html pagy, link }</nav>#{ pagy_json_tag pagy, :combo_nav, id, p_page, pagy_marked_link(link) }) end |
#pagy_foundation_combo_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript combo pagination for Foundation: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/pagy/extras/foundation.rb', line 40 def pagy_foundation_combo_nav_js(pagy, id=pagy_id) link = pagy_link_proc(pagy) p_page = pagy.page p_pages = pagy.pages input = %(<input class="input-group-field cell shrink" type="number" min="1" max="#{p_pages}" value="#{p_page}" style="width: #{p_pages.to_s.length+1}rem; padding: 0 0.3rem; margin: 0 0.3rem;">) %(<nav id="#{id}" class="pagy-foundation-combo-nav-js" role="navigation" aria-label="Pagination"><div class="input-group">#{ if (p_prev = pagy.prev) link.call p_prev, pagy_t('pagy.nav.prev'), 'style="margin-bottom: 0px;" aria-label="previous" class="prev button primary"' else %(<a style="margin-bottom: 0px;" class="prev button primary disabled" href="#">#{pagy_t 'pagy.nav.prev'}</a>) end }<span class="input-group-label">#{pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages}</span>#{ if (p_next = pagy.next) link.call p_next, pagy_t('pagy.nav.next'), 'style="margin-bottom: 0px;" aria-label="next" class="next button primary"' else %(<a style="margin-bottom: 0px;" class="next button primary disabled" href="#">#{pagy_t 'pagy.nav.next'}</a>) end }</div></nav>#{ pagy_json_tag pagy, :combo_nav, id, p_page, pagy_marked_link(link) }) end |
#pagy_foundation_nav(pagy) ⇒ Object
Pagination for Foundation: it returns the html with the series of links to the pages
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/pagy/extras/foundation.rb', line 10 def pagy_foundation_nav(pagy) link = pagy_link_proc(pagy) html = +%(<nav class="pagy-foundation-nav" role="navigation" aria-label="Pagination"><ul class="pagination">) html << pagy_foundation_prev_html(pagy, link) pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36] html << case item when Integer then %(<li>#{link.call item}</li>) # page link when String then %(<li class="current">#{item}</li>) # active page when :gap then %(<li class="ellipsis gap" aria-hidden="true"></li>) # page gap end end html << pagy_foundation_next_html(pagy, link) html << %(</ul></nav>) end |
#pagy_foundation_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript pagination for foundation: it returns a nav and a JSON tag used by the Pagy.nav javascript
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/pagy/extras/foundation.rb', line 27 def pagy_foundation_nav_js(pagy, id=pagy_id) link = pagy_link_proc(pagy) = { 'before' => %(<ul class="pagination">#{pagy_foundation_prev_html pagy, link}), 'link' => %(<li>#{link.call PAGE_PLACEHOLDER}</li>), 'active' => %(<li class="current">#{pagy.page}</li>), 'gap' => %(<li class="ellipsis gap" aria-hidden="true"></li>), 'after' => %(#{pagy_foundation_next_html pagy, link}</ul>) } html = %(<nav id="#{id}" class="pagy-foundation-nav-js" role="navigation" aria-label="Pagination"></nav>) html << pagy_json_tag(pagy, :nav, id, , pagy.sequels) end |
#pagy_id ⇒ Object
it returns the SHA1 (fastest on modern ruby) string used as default ‘id` attribute by all the `*_js` tags
46 47 48 |
# File 'lib/pagy/extras/shared.rb', line 46 def pagy_id "pagy-#{Digest::SHA1.hexdigest(caller(2..2)[0].split(':in')[0])}" end |
#pagy_info(pagy, item_name = nil) ⇒ Object
Return examples: “Displaying items 41-60 of 324 in total” of “Displaying Products 41-60 of 324 in total”
61 62 63 64 65 66 67 68 69 |
# File 'lib/pagy/frontend.rb', line 61 def pagy_info(pagy, item_name=nil) count = pagy.count key = if count.zero? then 'pagy.info.no_items' elsif pagy.pages == 1 then 'pagy.info.single_page' else 'pagy.info.multiple_pages' end pagy_t key, item_name: item_name || pagy_t(pagy.vars[:i18n_key], count: count), count: count, from: pagy.from, to: pagy.to end |
#pagy_items_selector_js(pagy, id = pagy_id) ⇒ Object
Return the items selector HTML. For example “Show [20] items per page”
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/pagy/extras/items.rb', line 52 def pagy_items_selector_js(pagy, id=pagy_id) p_vars = pagy.vars p_items = p_vars[:items] p_vars[:items] = ITEMS_PLACEHOLDER link = pagy_marked_link(pagy_link_proc(pagy)) p_vars[:items] = p_items # restore the items 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(pagy, :items_selector, id, pagy.from, link)}) end |
#pagy_json_tag(pagy, *args) ⇒ Object
it returns a script tag with the JSON-serialized args generated with the slower to_json
32 33 34 35 |
# File 'lib/pagy/extras/shared.rb', line 32 def pagy_json_tag(pagy, *args) args << pagy.vars[:page_param] if pagy.vars[:trim] %(<script type="application/json" class="pagy-json">#{Oj.dump(args, mode: :strict)}</script>) end |
#pagy_link_proc(pagy, link_extra = '') ⇒ Object
Returns a performance optimized proc to generate the HTML links Benchmarked on a 20 link nav: it is ~22x faster and uses ~18x less memory than rails’ link_to
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/pagy/frontend.rb', line 73 def pagy_link_proc(pagy, link_extra='') p_prev = pagy.prev p_next = pagy.next left, right = %(<a href="#{pagy_url_for PAGE_PLACEHOLDER, pagy}" #{pagy.vars[:link_extra]} #{link_extra}).split(PAGE_PLACEHOLDER, 2) lambda do |num, text=num, extra=''| %(#{left}#{num}#{right}#{ case num when p_prev then ' rel="prev"' when p_next then ' rel="next"' else '' end } #{extra}>#{text}</a>) end end |
#pagy_marked_link(link) ⇒ Object
it returns the marked link to used by pagy.js
51 52 53 |
# File 'lib/pagy/extras/shared.rb', line 51 def pagy_marked_link(link) link.call PAGE_PLACEHOLDER, '', 'style="display: none;"' end |
#pagy_materialize_combo_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript combo pagination for materialize: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/pagy/extras/materialize.rb', line 40 def pagy_materialize_combo_nav_js(pagy, id=pagy_id) link = pagy_link_proc(pagy) p_page = pagy.page p_pages = pagy.pages style = ' style="vertical-align: middle;"' input = %(<input type="number" class="browser-default" min="1" max="#{p_pages}" value="#{p_page}" style="padding: 2px; border: none; border-radius: 2px; text-align: center; width: #{p_pages.to_s.length+1}rem;">) %(<div id="#{id}" class="pagy-materialize-combo-nav-js pagination" role="navigation" aria-label="pager"><div class="pagy-compact-chip role="group" style="height: 35px; border-radius: 18px; background: #e4e4e4; display: inline-block;"><ul class="pagination" style="margin: 0px;">#{ pagy_materialize_prev_html pagy, link, style }<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>#{ pagy_materialize_next_html pagy, link, style }</ul></div>#{ pagy_json_tag pagy, :combo_nav, id, p_page, pagy_marked_link(link) }) end |
#pagy_materialize_nav(pagy) ⇒ Object
Pagination for materialize: it returns the html with the series of links to the pages
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/pagy/extras/materialize.rb', line 10 def pagy_materialize_nav(pagy) link = pagy_link_proc(pagy) html = +%(<div class="pagy-materialize-nav pagination" role="navigation" aria-label="pager"><ul class="pagination">) html << pagy_materialize_prev_html(pagy, link) pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36] html << case item when Integer then %(<li class="waves-effect">#{link.call item}</li>) # page link when String then %(<li class="active">#{link.call item}</li>) # active page when :gap then %(<li class="gap disabled"><a href="#">#{pagy_t 'pagy.nav.gap'}</a></li>) # page gap end end html << pagy_materialize_next_html(pagy, link) html << %(</ul></div>) end |
#pagy_materialize_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript pagination for materialize: it returns a nav and a JSON tag used by the Pagy.nav javascript
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/pagy/extras/materialize.rb', line 27 def pagy_materialize_nav_js(pagy, id=pagy_id) link = pagy_link_proc(pagy) = { 'before' => %(<ul class="pagination">#{pagy_materialize_prev_html pagy, link}), 'link' => %(<li class="waves-effect">#{mark = link.call(PAGE_PLACEHOLDER)}</li>), 'active' => %(<li class="active">#{mark}</li>), 'gap' => %(<li class="gap disabled"><a href="#">#{pagy_t 'pagy.nav.gap'}</a></li>), 'after' => %(#{pagy_materialize_next_html pagy, link}</ul>) } html = %(<div id="#{id}" class="pagy-materialize-nav-js" role="navigation" aria-label="pager"></div>) html << pagy_json_tag(pagy, :nav, id, , pagy.sequels) end |
#pagy_nav(pagy) ⇒ Object
Generic pagination: it returns the html with the series of links to the pages
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/pagy/frontend.rb', line 34 def pagy_nav(pagy) link = pagy_link_proc(pagy) p_prev = pagy.prev p_next = pagy.next html = +%(<nav class="pagy-nav pagination" role="navigation" aria-label="pager">) html << if p_prev %(<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> ) else %(<span class="page prev disabled">#{pagy_t('pagy.nav.prev')}</span> ) end pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36] html << case item when Integer then %(<span class="page">#{link.call item}</span> ) # page link when String then %(<span class="page active">#{item}</span> ) # current page when :gap then %(<span class="page gap">#{pagy_t('pagy.nav.gap')}</span> ) # page gap end end html << if p_next %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>) else %(<span class="page next disabled">#{pagy_t('pagy.nav.next')}</span>) end html << %(</nav>) end |
#pagy_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript pagination: it returns a nav and a JSON tag used by the Pagy.nav javascript
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/pagy/extras/navs.rb', line 10 def pagy_nav_js(pagy, id=pagy_id) link = pagy_link_proc(pagy) = { 'before' => pagy_nav_prev_html(pagy, link), 'link' => %(<span class="page">#{link.call(PAGE_PLACEHOLDER)}</span> ), 'active' => %(<span class="page active">#{pagy.page}</span> ), 'gap' => %(<span class="page gap">#{pagy_t 'pagy.nav.gap'}</span> ), 'after' => pagy_nav_next_html(pagy, link) } html = %(<nav id="#{id}" class="pagy-nav-js pagination" role="navigation" aria-label="pager"></nav>) html << pagy_json_tag(pagy, :nav, id, , pagy.sequels) end |
#pagy_next_link(pagy, text = pagy_t('pagy.nav.next'), link_extra = '') ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/pagy/extras/support.rb', line 24 def pagy_next_link(pagy, text = pagy_t('pagy.nav.next'), link_extra = '') if pagy.next %(<span class="page next"><a href="#{pagy_url_for(pagy.next, pagy)}" rel="next" aria-label="next" #{pagy.vars[:link_extra]} #{link_extra}>#{text}</a></span>) else %(<span class="page next disabled">#{text}</span>) end end |
#pagy_next_link_tag(pagy) ⇒ Object
36 37 38 |
# File 'lib/pagy/extras/support.rb', line 36 def pagy_next_link_tag(pagy) %(<link href="#{pagy_url_for(pagy.next, pagy)}" rel="next"/>) if pagy.next end |
#pagy_next_url(pagy) ⇒ Object
12 13 14 |
# File 'lib/pagy/extras/support.rb', line 12 def pagy_next_url(pagy) pagy_url_for(pagy.next, pagy) if pagy.next end |
#pagy_prev_link(pagy, text = pagy_t('pagy.nav.prev'), link_extra = '') ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/pagy/extras/support.rb', line 16 def pagy_prev_link(pagy, text = pagy_t('pagy.nav.prev'), link_extra = '') if pagy.prev %(<span class="page prev"><a href="#{pagy_url_for(pagy.prev, pagy)}" rel="prev" aria-label="previous" #{pagy.vars[:link_extra]} #{link_extra}>#{text}</a></span>) else %(<span class="page prev disabled">#{text}</span>) end end |
#pagy_prev_link_tag(pagy) ⇒ Object
32 33 34 |
# File 'lib/pagy/extras/support.rb', line 32 def pagy_prev_link_tag(pagy) %(<link href="#{pagy_url_for(pagy.prev, pagy)}" rel="prev"/>) if pagy.prev end |
#pagy_prev_url(pagy) ⇒ Object
8 9 10 |
# File 'lib/pagy/extras/support.rb', line 8 def pagy_prev_url(pagy) pagy_url_for(pagy.prev, pagy) if pagy.prev end |
#pagy_semantic_combo_nav_js(pagy, id = pagy_id) ⇒ Object
Combo pagination for semantic: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/pagy/extras/semantic.rb', line 40 def pagy_semantic_combo_nav_js(pagy, id=pagy_id) link = pagy_link_proc(pagy, 'class="item"') p_page = pagy.page p_pages = pagy.pages input = %(<input type="number" min="1" max="#{p_pages}" value="#{p_page}" style="padding: 0; text-align: center; width: #{p_pages.to_s.length+1}rem; margin: 0 0.3rem">) %(<div id="#{id}" class="pagy-semantic-combo-nav-js ui compact menu" role="navigation" aria-label="pager">#{ pagy_semantic_prev_html pagy, link }<div class="pagy-combo-input item">#{ pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages }</div> #{ pagy_semantic_next_html pagy, link }</div>#{ pagy_json_tag pagy, :combo_nav, id, p_page, pagy_marked_link(link) }) end |
#pagy_semantic_nav(pagy) ⇒ Object
Pagination for semantic: it returns the html with the series of links to the pages
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/pagy/extras/semantic.rb', line 10 def pagy_semantic_nav(pagy) link = pagy_link_proc(pagy, 'class="item"') html = +%(<div class="pagy-semantic-nav ui pagination menu" aria-label="pager">) html << pagy_semantic_prev_html(pagy, link) pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36] html << case item when Integer then link.call item # page link when String then %(<a class="item active">#{item}</a>) # current page when :gap then %(<div class="disabled item">...</div>) # page gap end end html << pagy_semantic_next_html(pagy, link) html << %(</div>) end |
#pagy_semantic_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript pagination for semantic: it returns a nav and a JSON tag used by the Pagy.nav javascript
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/pagy/extras/semantic.rb', line 27 def pagy_semantic_nav_js(pagy, id=pagy_id) link = pagy_link_proc(pagy, 'class="item"') = { 'before' => pagy_semantic_prev_html(pagy, link), 'link' => link.call(PAGE_PLACEHOLDER), 'active' => %(<a class="item active">#{pagy.page}</a>), 'gap' => %(<div class="disabled item">#{pagy_t('pagy.nav.gap')}</div>), 'after' => pagy_semantic_next_html(pagy, link) } html = %(<div id="#{id}" class="pagy-semantic-nav-js ui pagination menu" role="navigation" aria-label="pager"></div>) html << pagy_json_tag(pagy, :nav, id, , pagy.sequels) end |
#pagy_t(key, **opts) ⇒ Object
Similar to I18n.t: just ~18x faster using ~10x less memory (@pagy_locale explicitly initialized in order to avoid warning)
88 89 90 |
# File 'lib/pagy/frontend.rb', line 88 def pagy_t(key, **opts) Pagy::I18n.t @pagy_locale||=nil, key, **opts end |
#pagy_uikit_combo_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript combo pagination for uikit: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/pagy/extras/uikit.rb', line 39 def pagy_uikit_combo_nav_js(pagy, id=pagy_id) link = pagy_link_proc(pagy) p_page = pagy.page p_pages = pagy.pages input = %(<input type="number" min="1" max="#{p_pages}" value="#{p_page}" class="uk-input" style="padding: 0; text-align: center; width: #{p_pages.to_s.length+1}rem;">) %(<div id="#{id}" class="pagy-uikit-combo-nav-js uk-button-group">#{ if (p_prev = pagy.prev) link.call p_prev, pagy_t('pagy.nav.prev'), 'class="uk-button uk-button-default"' else %(<button class="uk-button uk-button-default" disabled>#{pagy_t 'pagy.nav.prev'}</button>) end }<div class="uk-text-middle uk-margin-left uk-margin-right">#{ pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages }</div>#{ if (p_next = pagy.next) link.call p_next, pagy_t('pagy.nav.next'), 'class="uk-button uk-button-default"' else %(<button class="uk-button uk-button-default" disabled>#{pagy_t 'pagy.nav.next'}</button>) end }</div>#{ pagy_json_tag pagy, :combo_nav, id, p_page, pagy_marked_link(link) }) end |
#pagy_uikit_nav(pagy) ⇒ Object
Pagination for uikit: it returns the html with the series of links to the pages
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/pagy/extras/uikit.rb', line 10 def pagy_uikit_nav(pagy) link = pagy_link_proc(pagy) html = %(<ul class="pagy-uikit-nav uk-pagination uk-flex-center">#{pagy_uikit_prev_html pagy, link}) pagy.series.each do |item| html << case item when Integer then %(<li>#{link.call item}</li>) when String then %(<li class="uk-active"><span>#{item}</span></li>) when :gap then %(<li class="uk-disabled"><span>#{pagy_t 'pagy.nav.gap'}</span></li>) end end html << pagy_uikit_next_html(pagy, link) html << %(</ul>) end |
#pagy_uikit_nav_js(pagy, id = pagy_id) ⇒ Object
Javascript pagination for uikit: it returns a nav and a JSON tag used by the Pagy.nav javascript
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/pagy/extras/uikit.rb', line 26 def pagy_uikit_nav_js(pagy, id=pagy_id) link = pagy_link_proc(pagy) = { 'before' => pagy_uikit_prev_html(pagy, link), 'link' => %(<li>#{link.call(PAGE_PLACEHOLDER)}</li>), 'active' => %(<li class="uk-active"><span>#{PAGE_PLACEHOLDER}</span></li>), 'gap' => %(<li class="uk-disabled"><span>#{pagy_t('pagy.nav.gap')}</span></li>), 'after' => pagy_uikit_next_html(pagy, link) } html = %(<ul id="#{id}" class="pagy-uikit-nav-js uk-pagination uk-flex-center"></ul>) html << pagy_json_tag(pagy, :nav, id, , pagy.sequels) end |