diff --git a/README.md b/README.md index 9eeb6d8c4..111d4355b 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,10 @@ Paginate arrays efficiently avoiding expensive array-wrapping and without any ov Nav helper and templates for Bootstrap pagination. _(see [more...](http://ddnexus.github.io/pagy/extras/bootstrap))_ +### Bulma Extra + +Nav helper and templates for Bulma CSS framework pagination. _(see [more...](http://ddnexus.github.io/pagy/extras/bulma))_ + ### Compact Extra An alternative UI that combines the pagination feature with the navigation info in one compact element. _(see [more...](http://ddnexus.github.io/pagy/extras/compact))_ diff --git a/docs/assets/images/pagy_compact_bulma_g.png b/docs/assets/images/pagy_compact_bulma_g.png new file mode 100644 index 000000000..58c95432c Binary files /dev/null and b/docs/assets/images/pagy_compact_bulma_g.png differ diff --git a/docs/extras.md b/docs/extras.md index ab60c66e0..df6082e03 100644 --- a/docs/extras.md +++ b/docs/extras.md @@ -9,6 +9,7 @@ Pagy comes with a few optional extensions/extras: | ------------ | ------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------- | | `array` | Paginate arrays efficiently avoiding expensive array-wrapping and without overriding | [array.rb](https://github.com/ddnexus/pagy/blob/master/lib/pagy/extras/array.rb), [documentation](extras/array.md) | | `bootstrap` | Nav helper and templates for Bootstrap pagination | [bootstrap.rb](https://github.com/ddnexus/pagy/blob/master/lib/pagy/extras/bootstrap.rb), [documentation](extras/bootstrap.md) | +| `bulma` | Nav helper and templates for [Bulma](https://bulma.io) pagination component | [bulma.rb](https://github.com/ddnexus/pagy/blob/master/lib/pagy/extras/bulma.rb), [documentation](extras/bulma.md) | | `compact` | An alternative UI that combines the pagination with the nav info in a single compact element | [compact.rb](https://github.com/ddnexus/pagy/blob/master/lib/pagy/extras/compact.rb), [documentation](extras/compact.md) | | `i18n` | Use the `I18n` gem instead of the pagy implementation | [i18n.rb](https://github.com/ddnexus/pagy/blob/master/lib/pagy/extras/i81n.rb), [documentation](extras/i18n.md) | | `items` | Allow the client to request a custom number of items per page with a ready to use selector UI | [items.rb](https://github.com/ddnexus/pagy/blob/master/lib/pagy/extras/items.rb), [documentation](extras/items.md) | diff --git a/docs/extras/bulma.md b/docs/extras/bulma.md new file mode 100644 index 000000000..c3b15d045 --- /dev/null +++ b/docs/extras/bulma.md @@ -0,0 +1,46 @@ +--- +title: Bulma +--- +# Bulma Extra + +This extra adds nav helper and templates for Bulma CSS framework [pagination component](https://bulma.io/documentation/components/pagination). + +## Synopsys + +See [extras](../extras.md) for general usage info. + +Render the navigation links in some view... +with a fast helper: + +```erb +<%== pagy_nav_bulma(@pagy) %> +``` + +or with a template: + +```erb +<%== render 'pagy/nav_bulma', locals: {pagy: @pagy} %> +``` + +## Files + +This extra is composed of 4 files: + +- [bulma.rb](https://github.com/ddnexus/pagy/blob/master/lib/pagy/extras/bulma.rb) +- [nav_bulma.html.erb](https://github.com/ddnexus/pagy/blob/master/lib/pagy/extras/templates/nav_bulma.html.erb) (optional template) +- [nav_bulma.html.haml](https://github.com/ddnexus/pagy/blob/master/lib/pagy/extras/templates/nav_bulma.html.haml) (optional template) +- [nav_bulma.html.slim](https://github.com/ddnexus/pagy/blob/master/lib/pagy/extras/templates/nav_bulma.html.slim) (optional template) + +## Methods + +This extra adds one nav helpers to the `Pagy::Frontend` module. You can customize it by overriding it directly in your own view helper. + +### pagy_nav_bulma(pagy) + +This method is the same as the `pagy_nav`, but customized for Bulma. + +The `nav_bulma.*` templates produce the same output, and can be used as an easier (but slower) starting point to override it. + +### Optional Template Files + +See [Using Templates](../how-to.md#using-templates). diff --git a/docs/extras/compact.md b/docs/extras/compact.md index b2b36992b..13ce8c3b3 100644 --- a/docs/extras/compact.md +++ b/docs/extras/compact.md @@ -22,6 +22,7 @@ Then use the responsive helper(s) in any view: ```erb <%== pagy_nav_compact(@pagy) %> <%== pagy_nav_compact_bootstrap(@pagy) %> +<%== pagy_nav_compact_bulma(@pagy) %> ``` ## Files @@ -41,3 +42,10 @@ It can take an extra `id` argument, which is used to build the `id` attribute of ### pagy_nav_compact_bootstrap(pagy, ...) This method is the same as the `pagy_nav_compact`, but customized for Bootstrap. + +### pagy_nav_compact_bulma(pagy, ...) + +This method is the same as the `pagy_nav_compact`, but customized for Bulma CSS framework. + +Generated pagination preview: +![pagy-compact-bulma](../assets/images/pagy-compact-bulma-g.png) diff --git a/docs/extras/responsive.md b/docs/extras/responsive.md index 1ef2272b1..710bc4067 100644 --- a/docs/extras/responsive.md +++ b/docs/extras/responsive.md @@ -28,6 +28,7 @@ Then use the responsive helper(s) in any view: ```erb <%== pagy_nav_responsive(@pagy) %> <%== pagy_nav_responsive_bootstrap(@pagy) %> +<%== pagy_nav_responsive_bulma(@pagy) %> ``` ## Files @@ -57,9 +58,9 @@ The `reponsive` extra adds an instance method to the `Pagy` class and couple of ### responsive -**Notice**: Unless you are going to override a `pagy_nav_responsive` or `pagy_nav_responsive_bootstrap` helper you can ignore this method. +**Notice**: Unless you are going to override a `pagy_nav_responsive*` helpers you can ignore this method. -This is a `Pagy` instance method that returns the data structure used by the `pagy_nav_responsive` and `pagy_nav_responsive_bootstrap` helpers in order to build the html and the javascript codes needed to make Pagy responsive to different widths. +This is a `Pagy` instance method that returns the data structure used by the `pagy_nav_responsive*` helpers in order to build the html and the javascript codes needed to make Pagy responsive to different widths. ### pagy_nav_responsive(pagy, ...) @@ -70,3 +71,7 @@ It can take an extra `id` argument, which is used to build the `id` attribute of ### pagy_nav_responsive_bootstrap(pagy, ...) This method is the same as the `pagy_nav_responsive`, but customized for Bootstrap. + +### pagy_nav_responsive_bulma(pagy, ...) + +This method is the same as the `pagy_nav_responsive`, but customized for Bulma CSS framework. diff --git a/lib/pagy/extras/bulma.rb b/lib/pagy/extras/bulma.rb new file mode 100644 index 000000000..67eb930a9 --- /dev/null +++ b/lib/pagy/extras/bulma.rb @@ -0,0 +1,37 @@ +# See the Pagy documentation: https://ddnexus.github.io/pagy/extras/bulma +# frozen_string_literal: true + +class Pagy + # Add nav helper for Bulma pagination + module Frontend + + def pagy_nav_bulma(pagy) + html, link, p_prev, p_next = +'', pagy_link_proc(pagy), pagy.prev, pagy.next + + html << (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"') + : %(#{pagy_t('pagy.nav.prev')})) + html << (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'class="pagination-next" aria-label="next page"') + : %(#{pagy_t('pagy.nav.next')})) + html << '' + %() + end + + private + + def pagy_nav_bulma_item(link, item) + if item.is_a?(Integer) # page link + aria = %( area-label="goto page #{item}") + %(
  • #{link.call item, item, 'class="pagination-link"' + aria}
  • ) + elsif item.is_a?(String) # active page + aria = %( area-label="page #{item}" area-current="page") + %(
  • #{link.call item, item, 'class="pagination-link is-current"' + aria}
  • ) + elsif item == :gap # page gap + %(
  • #{pagy_t('pagy.nav.gap')}
  • ) + end + end + end +end diff --git a/lib/pagy/extras/compact.rb b/lib/pagy/extras/compact.rb index b551fac4f..59e0291b0 100644 --- a/lib/pagy/extras/compact.rb +++ b/lib/pagy/extras/compact.rb @@ -38,5 +38,22 @@ def pagy_nav_compact_bootstrap(pagy, id=caller(1,1)[0].hash) html << %() end + # Compact pagination for Bulma: it returns the html with the series of links to the pages + # we use a numeric input tag to set the page and the Pagy.compact javascript to navigate + def pagy_nav_compact_bulma(pagy, id=caller(1,1)[0].hash) + html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages + + html << %() + end + end end diff --git a/lib/pagy/extras/responsive.rb b/lib/pagy/extras/responsive.rb index 106070e53..298a1d747 100644 --- a/lib/pagy/extras/responsive.rb +++ b/lib/pagy/extras/responsive.rb @@ -69,5 +69,24 @@ def pagy_nav_responsive_bootstrap(pagy, id=caller(1,1)[0].hash) %(#{script}) end + # Responsive pagination for Bulma: it returns the html with the series of links to the pages + # rendered by the Pagy.responsive javascript + def pagy_nav_responsive_bulma(pagy, id=caller(1,1)[0].hash) + tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.responsive + + tags['prev'] = (p_prev ? %(#{link.call(p_prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"')}#{pagy_t('pagy.nav.next')})) + script = %() + %(#{script}) + end + end end diff --git a/lib/pagy/extras/templates/nav_bulma.html.erb b/lib/pagy/extras/templates/nav_bulma.html.erb new file mode 100644 index 000000000..6c9a57eeb --- /dev/null +++ b/lib/pagy/extras/templates/nav_bulma.html.erb @@ -0,0 +1,24 @@ +<%# + This template is i18n-ready: if you don't use i18n, then you can replace the pagy_t + calls with the actual strings ("‹ Prev", "Next ›", "…"). + + The link variable is set to a proc that returns the link tag. + Usage: link.call( page_number [, text [, extra_attributes_string ]]) +-%> +<% link = pagy_link_proc(pagy) -%> +<%# -%> diff --git a/lib/pagy/extras/templates/nav_bulma.html.haml b/lib/pagy/extras/templates/nav_bulma.html.haml new file mode 100644 index 000000000..61d73e529 --- /dev/null +++ b/lib/pagy/extras/templates/nav_bulma.html.haml @@ -0,0 +1,32 @@ +-# This template is i18n-ready: if you don't use i18n, then you can replace the pagy_t +-# calls with the actual strings ("‹ Prev", "Next ›", "…"). + +-# The link variable is set to a proc that returns the link tag. +-# Usage: link.call( page_number [, text [, extra_attributes_string ]]) + +- link = pagy_link_proc(pagy) + +%nav.pagy-nav-bulma.pagination.is-centered{:role => "navigation", "aria-label" => "pagination"} + + - if pagy.prev + != link.call(pagy.prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"') + - else + %a.pagination-previous{:disabled => "disabled"}!= pagy_t('pagy.nav.prev') + + - if pagy.next + != link.call(pagy.next, pagy_t('pagy.nav.next'), 'class="pagination-next" aria-label="next page"') + - else + %a.pagination-next{:disabled => "disabled"}!= pagy_t('pagy.nav.next') + + %ul.pagination-list + + - pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36] + - if item.is_a?(Integer) # page link + %li!= link.call item, item, %(class="pagination-link" area-label="goto page #{item}") + + - elsif item.is_a?(String) # current page + %li!= link.call item, item, %(class="pagination-link is-current" area-label="page #{item}" area-current="page") + + - elsif item == :gap # page gap + %li + %span.pagination-ellipsis!= pagy_t('pagy.nav.gap') diff --git a/lib/pagy/extras/templates/nav_bulma.html.slim b/lib/pagy/extras/templates/nav_bulma.html.slim new file mode 100644 index 000000000..ddaaaead1 --- /dev/null +++ b/lib/pagy/extras/templates/nav_bulma.html.slim @@ -0,0 +1,32 @@ +/ This template is i18n-ready: if you don't use i18n, then you can replace the pagy_t +/ calls with the actual strings ("‹ Prev", "Next ›", "…"). + +/ The link variable is set to a proc that returns the link tag. +/ Usage: link.call( page_number [, text [, extra_attributes_string ]]) + +- link = pagy_link_proc(pagy) + +nav.pagy-nav-bulma.pagination.is-centered role="navigation" aria-label="pagination" + + - if pagy.prev + == link.call(pagy.prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"') + - else + a.pagination-previous disabled="disabled" == pagy_t('pagy.nav.prev') + + - if pagy.next + == link.call(pagy.next, pagy_t('pagy.nav.next'), 'class="pagination-next" aria-label="next page"') + - else + a.pagination-next disabled="disabled" == pagy_t('pagy.nav.next') + + ul.pagination-list + + - pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36] + - if item.is_a?(Integer) # page link + li == link.call item, item, %(class="pagination-link" area-label="goto page #{item}") + + - elsif item.is_a?(String) # current page + li == link.call item, item, %(class="pagination-link is-current" area-label="page #{item}" area-current="page") + + - elsif item == :gap # page gap + li + span.pagination-ellipsis == pagy_t('pagy.nav.gap') diff --git a/test/pagy/extras/bulma_test.rb b/test/pagy/extras/bulma_test.rb new file mode 100644 index 000000000..9b1e5096d --- /dev/null +++ b/test/pagy/extras/bulma_test.rb @@ -0,0 +1,95 @@ +require_relative '../../test_helper' +require 'pagy/extras/bulma' + +SingleCov.covered! + +describe Pagy::Frontend do + + let(:frontend) { TestView.new } + + describe '#pagy_nav_bulma' do + + before do + @array = (1..103).to_a.extend(Pagy::Array::PageMethod) + end + + it 'renders page 1' do + pagy, _ = @array.pagy(1) + frontend.pagy_nav_bulma(pagy).must_equal \ + '' + end + + it 'renders page 3' do + pagy, _ = @array.pagy(3) + frontend.pagy_nav_bulma(pagy).must_equal \ + '' + end + + + it 'renders page 6' do + pagy, _ = @array.pagy(6) + frontend.pagy_nav_bulma(pagy).must_equal \ + '' + end + + it 'renders page 10' do + @array = (1..1000).to_a.extend(Pagy::Array::PageMethod) + pagy, _ = @array.pagy(10) + frontend.pagy_nav_bulma(pagy).must_equal \ + '' + end + + end + +end diff --git a/test/pagy/extras/compact_test.rb b/test/pagy/extras/compact_test.rb index 72d9f85ce..08b83f1d1 100644 --- a/test/pagy/extras/compact_test.rb +++ b/test/pagy/extras/compact_test.rb @@ -119,4 +119,69 @@ end + describe "#pagy_nav_compact_bulma" do + + before do + @array = (1..103).to_a.extend(Pagy::Array::PageMethod) + end + + it 'renders page 1 for bulma' do + pagy, _ = @array.pagy(1) + html, id = frontend.pagy_nav_compact_bulma(pagy), caller(0,1)[0].hash + html.must_equal \ + %() + + %() + end + + it 'renders page 3 for bulma' do + pagy, _ = @array.pagy(3) + html, id = frontend.pagy_nav_compact_bulma(pagy), caller(0,1)[0].hash + html.must_equal \ + %() + + %() + end + + it 'renders page 6 for bulma' do + pagy, _ = @array.pagy(6) + html, id = frontend.pagy_nav_compact_bulma(pagy), caller(0,1)[0].hash + html.must_equal \ + %() + + %() + end + + end + end diff --git a/test/pagy/extras/responsive_test.rb b/test/pagy/extras/responsive_test.rb index 792823e58..33f60f952 100644 --- a/test/pagy/extras/responsive_test.rb +++ b/test/pagy/extras/responsive_test.rb @@ -81,4 +81,41 @@ end + describe "#pagy_nav_responsive_bulma" do + + before do + @array = (1..103).to_a.extend(Pagy::Array::PageMethod) + end + + it 'renders page 1 for bulma' do + pagy, _ = @array.pagy(1) + html, id = frontend.pagy_nav_responsive_bulma(pagy), caller(0,1)[0].hash + html.must_equal \ + "" + end + + it 'renders page 3 for bulma' do + pagy, _ = @array.pagy(3) + html, id = frontend.pagy_nav_responsive_bulma(pagy), caller(0,1)[0].hash + html.must_equal \ + "" + end + + it 'renders page 6 for bulma' do + pagy, _ = @array.pagy(6) + html, id = frontend.pagy_nav_responsive_bulma(pagy), caller(0,1)[0].hash + html.must_equal \ + "" + end + + it 'renders page 10 for bulma' do + @array = (1..1000).to_a.extend(Pagy::Array::PageMethod) + pagy, _ = @array.pagy(10) + html, id = frontend.pagy_nav_responsive_bulma(pagy), caller(0,1)[0].hash + html.must_equal \ + "" + end + + end + end