pagy 4.0.0 → 4.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/pagy.rb CHANGED
@@ -1,52 +1,76 @@
1
1
  # See Pagy API documentation: https://ddnexus.github.io/pagy/api/pagy
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  require 'pathname'
6
5
 
7
- class Pagy ; VERSION = '4.0.0'
6
+ # main class
7
+ class Pagy
8
+ VERSION = '4.5.0'
8
9
 
9
10
  # Root pathname to get the path of Pagy files like templates or dictionaries
10
- def self.root = @root ||= Pathname.new(__FILE__).dirname.freeze
11
+ def self.root
12
+ @root ||= Pathname.new(__dir__).freeze
13
+ end
11
14
 
12
15
  # default vars
13
- VARS = { page:1, items:20, outset:0, size:[1,4,4,1], page_param: :page, params:{}, anchor:'', link_extra:'', i18n_key:'pagy.item_name', cycle:false }
16
+ VARS = { page: 1, items: 20, outset: 0, size: [1, 4, 4, 1], page_param: :page, # rubocop:disable Style/MutableConstant
17
+ params: {}, fragment: '', link_extra: '', i18n_key: 'pagy.item_name', cycle: false }
14
18
 
15
19
  attr_reader :count, :page, :items, :vars, :pages, :last, :offset, :from, :to, :prev, :next
16
20
 
21
+ INSTANCE_VARS_MIN = { count: 0, items: 1, page: 1, outset: 0 }.freeze
22
+
17
23
  # Merge and validate the options, do some simple arithmetic and set the instance variables
18
24
  def initialize(vars)
19
- @vars = VARS.merge(vars.delete_if{|_,v| v.nil? || v == '' }) # default vars + cleaned vars
20
- { count:0, items:1, outset:0, page:1 }.each do |k,min| # validate instance variables
21
- (@vars[k] && instance_variable_set(:"@#{k}", @vars[k].to_i) >= min) \
22
- or raise(VariableError.new(self), "expected :#{k} >= #{min}; got #{@vars[k].inspect}")
25
+ @vars = VARS.merge( vars.delete_if{|k,v| VARS.key?(k) && (v.nil? || v == '') } )
26
+ @vars[:fragment] = deprecated_var(:anchor, @vars[:anchor], :fragment, @vars[:fragment]) if @vars[:anchor]
27
+
28
+ INSTANCE_VARS_MIN.each do |name,min|
29
+ raise VariableError.new(self), "expected :#{name} >= #{min}; got #{@vars[name].inspect}" \
30
+ unless @vars[name] && instance_variable_set(:"@#{name}", @vars[name].to_i) >= min
23
31
  end
24
- @pages = @last = [(@count.to_f / @items).ceil, 1].max # cardinal and ordinal meanings
25
- @page <= @last or raise(OverflowError.new(self), "expected :page in 1..#{@last}; got #{@page.inspect}")
26
- @offset = @items * (@page - 1) + @outset # pagination offset + outset (initial offset)
27
- @items = @count - ((@pages-1) * @items) if @page == @last && @count > 0 # adjust items for last non-empty page
28
- @from = @count == 0 ? 0 : @offset+1 - @outset # page begins from item
29
- @to = @count == 0 ? 0 : @offset + @items - @outset # page ends to item
30
- @prev = (@page-1 unless @page == 1) # nil if no prev page
31
- @next = @page == @last ? (1 if @vars[:cycle]) : @page+1 # nil if no next page, 1 if :cycle
32
+ @pages = @last = [(@count.to_f / @items).ceil, 1].max
33
+ raise OverflowError.new(self), "expected :page in 1..#{@last}; got #{@page.inspect}" \
34
+ if @page > @last
35
+
36
+ @offset = @items * (@page - 1) + @outset
37
+ @items = @count - ((@pages - 1) * @items) if @page == @last && @count.positive?
38
+ @from = @count.zero? ? 0 : @offset + 1 - @outset
39
+ @to = @count.zero? ? 0 : @offset + @items - @outset
40
+ @prev = (@page - 1 unless @page == 1)
41
+ @next = @page == @last ? (1 if @vars[:cycle]) : @page + 1
32
42
  end
33
43
 
34
44
  # Return the array of page numbers and :gap items e.g. [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
35
45
  def series(size=@vars[:size])
36
- (series = []) and size.empty? and return series
37
- 4.times{|i| (size[i]>=0 rescue nil) or raise(VariableError.new(self), "expected 4 items >= 0 in :size; got #{size.inspect}")}
38
- [*0..size[0], *@page-size[1]..@page+size[2], *@last-size[3]+1..@last+1].sort!.each_cons(2) do |a, b|
39
- if a<0 || a==b || a>@last; next # skip out of range and duplicates
40
- elsif a+1 == b; series.push(a) # no gap -> no additions
41
- elsif a+2 == b; series.push(a, a+1) # 1 page gap -> fill with missing page
42
- else series.push(a, :gap) # n page gap -> add gap
43
- end # skip the end boundary (last+1)
44
- end # shift the start boundary (0) and
45
- series.shift; series[series.index(@page)] = @page.to_s; series # convert the current page to String
46
+ return [] if size.empty?
47
+ raise VariableError.new(self), "expected 4 items >= 0 in :size; got #{size.inspect}" \
48
+ unless size.size == 4 && size.all?{ |num| !num.negative? rescue false } # rubocop:disable Style/RescueModifier
49
+ # This algorithm is up to ~5x faster and ~2.3x lighter than the previous one (pagy < 4.3)
50
+ left_gap_start = 1 + size[0]
51
+ left_gap_end = @page - size[1] - 1
52
+ right_gap_start = @page + size[2] + 1
53
+ right_gap_end = @last - size[3]
54
+ left_gap_end = right_gap_end if left_gap_end > right_gap_end
55
+ right_gap_start = left_gap_start if left_gap_start > right_gap_start
56
+ series = []
57
+ start = 1
58
+ if (left_gap_end - left_gap_start).positive?
59
+ series.push(*start..(left_gap_start - 1), :gap)
60
+ start = left_gap_end + 1
61
+ end
62
+ if (right_gap_end - right_gap_start).positive?
63
+ series.push(*start..(right_gap_start - 1), :gap)
64
+ start = right_gap_end + 1
65
+ end
66
+ series.push(*start..@last)
67
+ series[series.index(@page)] = @page.to_s
68
+ series
46
69
  end
47
70
 
48
71
  end
49
72
 
73
+ require 'pagy/deprecation'
50
74
  require 'pagy/backend'
51
75
  require 'pagy/frontend'
52
76
  require 'pagy/exceptions'
data/lib/pagy/backend.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  # See Pagy::Backend API documentation: https://ddnexus.github.io/pagy/api/backend
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  class Pagy
@@ -8,16 +7,19 @@ class Pagy
8
7
 
9
8
  # See also the extras if you need specialized methods to paginate Arrays or other collections
10
9
 
11
- module Backend ; private # the whole module is private so no problem with including it in a controller
10
+
11
+ module Backend
12
+ private # the whole module is private so no problem with including it in a controller
12
13
 
13
14
  # Return Pagy object and items
14
15
  def pagy(collection, vars={})
15
16
  pagy = Pagy.new(pagy_get_vars(collection, vars))
16
- return pagy, pagy_get_items(collection, pagy)
17
+ [ pagy, pagy_get_items(collection, pagy) ]
17
18
  end
18
19
 
19
20
  # Sub-method called only by #pagy: here for easy customization of variables by overriding
20
21
  def pagy_get_vars(collection, vars)
22
+ pagy_set_items_from_params(vars) if defined?(UseItemsExtra)
21
23
  vars[:count] ||= (c = collection.count(:all)).is_a?(Hash) ? c.size : c
22
24
  vars[:page] ||= params[ vars[:page_param] || VARS[:page_param] ]
23
25
  vars
@@ -1,4 +1,3 @@
1
- # encoding: utf-8
2
1
  # frozen_string_literal: true
3
2
 
4
3
  require 'pagy'
@@ -7,25 +6,29 @@ class Pagy
7
6
 
8
7
  class Countless < Pagy
9
8
 
9
+ INSTANCE_VARS_MIN = { items: 1, page: 1, outset: 0 }.freeze
10
+
10
11
  # Merge and validate the options, do some simple arithmetic and set a few instance variables
11
12
  def initialize(vars={}) # rubocop:disable Lint/MissingSuper
12
13
  @vars = VARS.merge(vars.delete_if{|_,v| v.nil? || v == '' }) # default vars + cleaned vars (can be overridden)
13
- { items:1, outset:0, page:1 }.each do |k,min| # validate instance variables
14
- (@vars[k] && instance_variable_set(:"@#{k}", @vars[k].to_i) >= min) \
15
- or raise(VariableError.new(self), "expected :#{k} >= #{min}; got #{@vars[k].inspect}")
14
+ INSTANCE_VARS_MIN.each do |k,min| # validate instance variables
15
+ raise VariableError.new(self), "expected :#{k} >= #{min}; got #{@vars[k].inspect}" \
16
+ unless @vars[k] && instance_variable_set(:"@#{k}", @vars[k].to_i) >= min
16
17
  end
17
18
  @offset = @items * (@page - 1) + @outset # pagination offset + outset (initial offset)
18
19
  end
19
20
 
20
21
  # Finalize the instance variables based on the fetched items
21
22
  def finalize(fetched)
22
- fetched == 0 && @page > 1 and raise(OverflowError.new(self), "page #{@page} got no items")
23
+ raise OverflowError.new(self), "page #{@page} got no items" \
24
+ if fetched.zero? && @page > 1
25
+
23
26
  @pages = @last = (fetched > @items ? @page + 1 : @page) # set the @pages and @last
24
- @items = fetched if fetched < @items && fetched > 0 # adjust items for last non-empty page
25
- @from = fetched == 0 ? 0 : @offset+1 - @outset # page begins from item
26
- @to = fetched == 0 ? 0 : @offset + @items - @outset # page ends to item
27
+ @items = fetched if fetched < @items && fetched.positive? # adjust items for last non-empty page
28
+ @from = fetched.zero? ? 0 : @offset + 1 - @outset # page begins from item
29
+ @to = fetched.zero? ? 0 : @offset + @items - @outset # page ends to item
27
30
  @prev = (@page-1 unless @page == 1) # nil if no prev page
28
- @next = @page == @last ? (1 if @vars[:cycle]) : @page+1 # nil if no next page, 1 if :cycle
31
+ @next = @page == @last ? (1 if @vars[:cycle]) : @page + 1 # nil if no next page, 1 if :cycle
29
32
  self
30
33
  end
31
34
 
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+ class Pagy
3
+ class << self
4
+
5
+ # deprecated variables
6
+ def deprecated_var(var, val, new_var, new_val)
7
+ value = new_val || val
8
+ Warning.warn %([PAGY WARNING] deprecated use of `#{var}` var will not be supported in 5.0! Use `#{new_var}: #{value.inspect}` instead.)
9
+ value
10
+ end
11
+
12
+ # deprecated pagy_url_for argument order
13
+ def deprecated_order(pagy, page)
14
+ Warning.warn '[PAGY WARNING] inverted use of pagy/page in pagy_url_for will not be supported in 5.0! Use pagy_url_for(pagy, page) instead.'
15
+ [page, pagy]
16
+ end
17
+
18
+
19
+ # deprecated posiitioal arguments
20
+ def deprecated_arg(arg, val, new_key, new_val)
21
+ value = new_val || val # we use the new_val if present
22
+ Warning.warn %([PAGY WARNING] deprecated use of positional `#{arg}` arg will not be supported in 5.0! Use only the keyword arg `#{new_key}: #{value.inspect}` instead.)
23
+ value
24
+ end
25
+
26
+ end
27
+ end
@@ -1,5 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pagy
2
4
 
5
+ # generic variable error
3
6
  class VariableError < ArgumentError
4
7
  attr_reader :pagy
5
8
 
@@ -9,15 +12,14 @@ class Pagy
9
12
  end
10
13
 
11
14
  def variable
12
- message =~ /expected :([\w]+)/
13
- $1.to_sym if $1
15
+ message =~ /expected :(\w+)/
16
+ Regexp.last_match(1)&.to_sym
14
17
  end
15
18
 
16
- def value
17
- pagy.vars[variable]
18
- end
19
+ def value = pagy.vars[variable]
19
20
  end
20
21
 
22
+ # specific overflow error
21
23
  class OverflowError < VariableError; end
22
24
 
23
25
  end
@@ -1,16 +1,17 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/arel
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  class Pagy
6
- module Backend ; private
5
+ module Backend
6
+ private
7
7
 
8
8
  def pagy_arel(collection, vars={})
9
9
  pagy = Pagy.new(pagy_arel_get_vars(collection, vars))
10
- return pagy, pagy_get_items(collection, pagy)
10
+ [ pagy, pagy_get_items(collection, pagy) ]
11
11
  end
12
12
 
13
13
  def pagy_arel_get_vars(collection, vars)
14
+ pagy_set_items_from_params(vars) if defined?(UseItemsExtra)
14
15
  vars[:count] ||= pagy_arel_count(collection)
15
16
  vars[:page] ||= params[ vars[:page_param] || VARS[:page_param] ]
16
17
  vars
@@ -1,19 +1,20 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/array
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  class Pagy
6
5
  # Add specialized backend methods to paginate array collections
7
- module Backend ; private
6
+ module Backend
7
+ private
8
8
 
9
9
  # Return Pagy object and items
10
10
  def pagy_array(array, vars={})
11
11
  pagy = Pagy.new(pagy_array_get_vars(array, vars))
12
- return pagy, array[pagy.offset, pagy.items]
12
+ [ pagy, array[pagy.offset, pagy.items] ]
13
13
  end
14
14
 
15
15
  # Sub-method called only by #pagy_array: here for easy customization of variables by overriding
16
16
  def pagy_array_get_vars(array, vars)
17
+ pagy_set_items_from_params(vars) if defined?(UseItemsExtra)
17
18
  vars[:count] ||= array.size
18
19
  vars[:page] ||= params[ vars[:page_param] || VARS[:page_param] ]
19
20
  vars
@@ -1,5 +1,4 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/bootstrap
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  require 'pagy/extras/shared'
@@ -8,48 +7,81 @@ class Pagy
8
7
  module Frontend
9
8
 
10
9
  # Pagination for bootstrap: it returns the html with the series of links to the pages
11
- def pagy_bootstrap_nav(pagy)
12
- link, p_prev, p_next = pagy_link_proc(pagy, 'class="page-link"'), pagy.prev, pagy.next
10
+ def pagy_bootstrap_nav(pagy, pagy_id: nil, link_extra: '')
11
+ p_id = %( id="#{pagy_id}") if pagy_id
12
+ link = pagy_link_proc(pagy, link_extra: %(class="page-link" #{link_extra}))
13
13
 
14
- html = (p_prev ? %(<li class="page-item prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
15
- : %(<li class="page-item prev disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.prev')}</a></li>))
14
+ html = +%(<nav#{p_id} class="pagy-bootstrap-nav" role="navigation" aria-label="pager"><ul class="pagination">)
15
+ html << pagy_bootstrap_prev_html(pagy, link)
16
16
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
17
- html << if item.is_a?(Integer); %(<li class="page-item">#{link.call item}</li>) # page link
18
- elsif item.is_a?(String) ; %(<li class="page-item active">#{link.call item}</li>) # active page
19
- elsif item == :gap ; %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.gap')}</a></li>) # page gap
17
+ html << case item
18
+ when Integer then %(<li class="page-item">#{link.call item}</li>)
19
+ when String then %(<li class="page-item active">#{link.call item}</li>)
20
+ when :gap then %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t 'pagy.nav.gap'}</a></li>)
20
21
  end
21
22
  end
22
- html << (p_next ? %(<li class="page-item next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
23
- : %(<li class="page-item next disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.next')}</a></li>))
24
- %(<nav class="pagy-bootstrap-nav" role="navigation" aria-label="pager"><ul class="pagination">#{html}</ul></nav>)
23
+ html << pagy_bootstrap_next_html(pagy, link)
24
+ html << %(</ul></nav>)
25
25
  end
26
26
 
27
27
  # Javascript pagination for bootstrap: it returns a nav and a JSON tag used by the Pagy.nav javascript
28
- def pagy_bootstrap_nav_js(pagy, id=pagy_id)
29
- link, p_prev, p_next = pagy_link_proc(pagy, 'class="page-link"'), pagy.prev, pagy.next
30
- tags = { 'before' => p_prev ? %(<ul class="pagination"><li class="page-item prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
31
- : %(<ul class="pagination"><li class="page-item prev disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.prev')}</a></li>),
28
+ def pagy_bootstrap_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '', steps: nil)
29
+ pagy_id = Pagy.deprecated_arg(:id, deprecated_id, :pagy_id, pagy_id) if deprecated_id
30
+ p_id = %( id="#{pagy_id}") if pagy_id
31
+ link = pagy_link_proc(pagy, link_extra: %(class="page-link" #{link_extra}))
32
+ tags = { 'before' => %(<ul class="pagination">#{pagy_bootstrap_prev_html pagy, link}),
32
33
  'link' => %(<li class="page-item">#{mark = link.call(PAGE_PLACEHOLDER)}</li>),
33
34
  'active' => %(<li class="page-item active">#{mark}</li>),
34
- 'gap' => %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.gap')}</a></li>),
35
- 'after' => p_next ? %(<li class="page-item next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li></ul>)
36
- : %(<li class="page-item next disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.next')}</a></li></ul>) }
37
- %(<nav id="#{id}" class="pagy-bootstrap-nav-js" role="navigation" aria-label="pager"></nav>#{pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)})
35
+ 'gap' => %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t 'pagy.nav.gap'}</a></li>),
36
+ 'after' => %(#{pagy_bootstrap_next_html pagy, link}</ul>) }
37
+
38
+ html = %(<nav#{p_id} class="pagy-njs pagy-bootstrap-nav-js" role="navigation" aria-label="pager"></nav>)
39
+ html << pagy_json_tag(pagy, :nav, tags, pagy.sequels(steps))
38
40
  end
39
41
 
40
42
  # Javascript combo pagination for bootstrap: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
41
- def pagy_bootstrap_combo_nav_js(pagy, id=pagy_id)
42
- link, p_prev, p_next, p_page, p_pages = pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
43
-
44
- html = %(<nav id="#{id}" class="pagy-bootstrap-combo-nav-js pagination" role="navigation" aria-label="pager">) + %(<div class="btn-group" role="group">)
45
- html << (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous" class="prev btn btn-primary"')
46
- : %(<a class="prev btn btn-primary disabled" href="#">#{pagy_t('pagy.nav.prev')}</a>))
47
- 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;">)
48
- 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>)
49
- html << (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'aria-label="next" class="next btn btn-primary"')
50
- : %(<a class="next btn btn-primary disabled" href="#">#{pagy_t('pagy.nav.next')}</a>))
51
- html << %(</div></nav>#{pagy_json_tag(pagy, :combo_nav, id, p_page, pagy_marked_link(link))})
43
+ def pagy_bootstrap_combo_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '')
44
+ pagy_id = Pagy.deprecated_arg(:id, deprecated_id, :pagy_id, pagy_id) if deprecated_id
45
+ p_id = %( id="#{pagy_id}") if pagy_id
46
+ link = pagy_link_proc(pagy, link_extra: link_extra)
47
+ p_page = pagy.page
48
+ p_pages = pagy.pages
49
+ 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;">)
50
+
51
+ %(<nav#{p_id} class="pagy-bootstrap-combo-nav-js pagination" role="navigation" aria-label="pager"><div class="btn-group" role="group">#{
52
+ if (p_prev = pagy.prev)
53
+ link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous" class="prev btn btn-primary"'
54
+ else
55
+ %(<a class="prev btn btn-primary disabled" href="#">#{pagy_t('pagy.nav.prev')}</a>)
56
+ end
57
+ }<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>#{
58
+ if (p_next = pagy.next)
59
+ link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next" class="next btn btn-primary"'
60
+ else
61
+ %(<a class="next btn btn-primary disabled" href="#">#{pagy_t 'pagy.nav.next' }</a>)
62
+ end
63
+ }</div></nav>#{
64
+ pagy_json_tag pagy, :combo_nav, p_page, pagy_marked_link(link)
65
+ })
52
66
  end
53
67
 
68
+ private
69
+
70
+ def pagy_bootstrap_prev_html(pagy, link)
71
+ if (p_prev = pagy.prev)
72
+ %(<li class="page-item prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
73
+ else
74
+ %(<li class="page-item prev disabled"><a href="#" class="page-link">#{pagy_t 'pagy.nav.prev'}</a></li>)
75
+ end
76
+ end
77
+
78
+ def pagy_bootstrap_next_html(pagy, link)
79
+ if (p_next = pagy.next)
80
+ %(<li class="page-item next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
81
+ else
82
+ %(<li class="page-item next disabled"><a href="#" class="page-link">#{pagy_t 'pagy.nav.next'}</a></li>)
83
+ end
84
+ end
85
+
54
86
  end
55
87
  end
@@ -1,5 +1,4 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/bulma
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  require 'pagy/extras/shared'
@@ -8,53 +7,77 @@ class Pagy
8
7
  module Frontend
9
8
 
10
9
  # Pagination for Bulma: it returns the html with the series of links to the pages
11
- def pagy_bulma_nav(pagy)
12
- link, p_prev, p_next = pagy_link_proc(pagy), pagy.prev, pagy.next
13
-
14
- html = (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"')
15
- : %(<a class="pagination-previous" disabled>#{pagy_t('pagy.nav.prev')}</a>)) \
16
- + (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'class="pagination-next" aria-label="next page"')
17
- : %(<a class="pagination-next" disabled>#{pagy_t('pagy.nav.next')}</a>))
18
- html << '<ul class="pagination-list">'
10
+ def pagy_bulma_nav(pagy, pagy_id: nil, link_extra: '')
11
+ p_id = %( id="#{pagy_id}") if pagy_id
12
+ link = pagy_link_proc(pagy, link_extra: link_extra)
13
+
14
+ html = +%(<nav#{p_id} class="pagy-bulma-nav pagination is-centered" role="navigation" aria-label="pagination">)
15
+ html << pagy_bulma_prev_next_html(pagy, link)
16
+ html << %(<ul class="pagination-list">)
19
17
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
20
- html << if item.is_a?(Integer); %(<li>#{link.call item, item, %(class="pagination-link" aria-label="goto page #{item}") }</li>) # page link
21
- elsif item.is_a?(String) ; %(<li>#{link.call item, item, %(class="pagination-link is-current" aria-label="page #{item}" aria-current="page")}</li>) # active page
22
- elsif item == :gap ; %(<li><span class="pagination-ellipsis">#{pagy_t('pagy.nav.gap')}</span></li>) # page gap
18
+ html << case item
19
+ when Integer then %(<li>#{link.call item, item, %(class="pagination-link" aria-label="goto page #{item}") }</li>) # page link
20
+ when String then %(<li>#{link.call item, item, %(class="pagination-link is-current" aria-label="page #{item}" aria-current="page")}</li>) # active page
21
+ when :gap then %(<li><span class="pagination-ellipsis">#{pagy_t 'pagy.nav.gap'}</span></li>) # page gap
23
22
  end
24
23
  end
25
- html << '</ul>'
26
- %(<nav class="pagy-bulma-nav pagination is-centered" role="navigation" aria-label="pagination">#{html}</nav>)
24
+ html << %(</ul></nav>)
27
25
  end
28
26
 
29
- # Javascript pagination for bulma: it returns a nav and a JSON tag used by the Pagy.nav javascript
30
- def pagy_bulma_nav_js(pagy, id=pagy_id)
31
- link, p_prev, p_next = pagy_link_proc(pagy), pagy.prev, pagy.next
32
- tags = { 'before' => ( (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"')
33
- : %(<a class="pagination-previous" disabled>#{pagy_t('pagy.nav.prev')}</a>)) \
34
- + (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'class="pagination-next" aria-label="next page"')
35
- : %(<a class="pagination-next" disabled>#{pagy_t('pagy.nav.next')}</a>)) \
36
- + '<ul class="pagination-list">' ),
37
- 'link' => %(<li>#{link.call(PAGE_PLACEHOLDER, PAGE_PLACEHOLDER, %(class="pagination-link" aria-label="goto page #{PAGE_PLACEHOLDER}"))}</li>),
38
- 'active' => %(<li>#{link.call(PAGE_PLACEHOLDER, PAGE_PLACEHOLDER, %(class="pagination-link is-current" aria-current="page" aria-label="page #{PAGE_PLACEHOLDER}"))}</li>),
39
- 'gap' => %(<li><span class="pagination-ellipsis">#{pagy_t('pagy.nav.gap')}</span></li>),
27
+ def pagy_bulma_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '', steps: nil)
28
+ pagy_id = Pagy.deprecated_arg(:id, deprecated_id, :pagy_id, pagy_id) if deprecated_id
29
+ p_id = %( id="#{pagy_id}") if pagy_id
30
+ link = pagy_link_proc(pagy, link_extra: link_extra)
31
+ tags = { 'before' => %(#{pagy_bulma_prev_next_html(pagy, link)}<ul class="pagination-list">),
32
+ 'link' => %(<li>#{link.call PAGE_PLACEHOLDER, PAGE_PLACEHOLDER, %(class="pagination-link" aria-label="goto page #{PAGE_PLACEHOLDER}")}</li>),
33
+ 'active' => %(<li>#{link.call PAGE_PLACEHOLDER, PAGE_PLACEHOLDER, %(class="pagination-link is-current" aria-current="page" aria-label="page #{PAGE_PLACEHOLDER}")}</li>),
34
+ 'gap' => %(<li><span class="pagination-ellipsis">#{pagy_t 'pagy.nav.gap' }</span></li>),
40
35
  'after' => '</ul>' }
41
- %(<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)})
36
+
37
+ html = %(<nav#{p_id} class="pagy-njs pagy-bulma-nav-js pagination is-centered" role="navigation" aria-label="pagination"></nav>)
38
+ html << pagy_json_tag(pagy, :nav, tags, pagy.sequels(steps))
42
39
  end
43
40
 
44
41
  # Javascript combo pagination for Bulma: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
45
- def pagy_bulma_combo_nav_js(pagy, id=pagy_id)
46
- link, p_prev, p_next, p_page, p_pages = pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
47
-
48
- html = %(<nav id="#{id}" class="pagy-bulma-combo-nav-js" role="navigation" aria-label="pagination">) \
49
- + %(<div class="field is-grouped is-grouped-centered" role="group">)
50
- html << (p_prev ? %(<p class="control">#{link.call(p_prev, pagy_t('pagy.nav.prev'), 'class="button" aria-label="previous page"')}</p>)
51
- : %(<p class="control"><a class="button" disabled>#{pagy_t('pagy.nav.prev')}</a></p>))
52
- 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;">)
53
- 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>)
54
- html << (p_next ? %(<p class="control">#{link.call(p_next, pagy_t('pagy.nav.next'), 'class="button" aria-label="next page"')}</p>)
55
- : %(<p class="control"><a class="button" disabled>#{pagy_t('pagy.nav.next')}</a></p>))
56
- html << %(</div></nav>#{pagy_json_tag(pagy, :combo_nav, id, p_page, pagy_marked_link(link))})
42
+ def pagy_bulma_combo_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '')
43
+ pagy_id = Pagy.deprecated_arg(:id, deprecated_id, :pagy_id, pagy_id) if deprecated_id
44
+ p_id = %( id="#{pagy_id}") if pagy_id
45
+ link = pagy_link_proc(pagy, link_extra: link_extra)
46
+ p_page = pagy.page
47
+ p_pages = pagy.pages
48
+ 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;">)
49
+
50
+ %(<nav#{p_id} class="pagy-bulma-combo-nav-js" role="navigation" aria-label="pagination"><div class="field is-grouped is-grouped-centered" role="group">#{
51
+ if (p_prev = pagy.prev)
52
+ %(<p class="control">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'class="button" aria-label="previous page"'}</p>)
53
+ else
54
+ %(<p class="control"><a class="button" disabled>#{pagy_t 'pagy.nav.prev'}</a></p>)
55
+ end
56
+ }<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>#{
57
+ if (p_next = pagy.next)
58
+ %(<p class="control">#{link.call p_next, pagy_t('pagy.nav.next'), 'class="button" aria-label="next page"'}</p>)
59
+ else
60
+ %(<p class="control"><a class="button" disabled>#{pagy_t 'pagy.nav.next'}</a></p>)
61
+ end
62
+ }</div></nav>#{
63
+ pagy_json_tag pagy, :combo_nav, p_page, pagy_marked_link(link)
64
+ })
57
65
  end
58
66
 
67
+ private
68
+
69
+ def pagy_bulma_prev_next_html(pagy, link)
70
+ html = if (p_prev = pagy.prev)
71
+ link.call p_prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"'
72
+ else
73
+ %(<a class="pagination-previous" disabled>#{pagy_t 'pagy.nav.prev'}</a>)
74
+ end
75
+ html << if (p_next = pagy.next)
76
+ link.call p_next, pagy_t('pagy.nav.next'), 'class="pagination-next" aria-label="next page"'
77
+ else
78
+ %(<a class="pagination-next" disabled>#{pagy_t 'pagy.nav.next' }</a>)
79
+ end
80
+ end
81
+
59
82
  end
60
83
  end