Class: Fluxbit::PaginationComponent

Inherits:
Component
  • Object
show all
Includes:
Config::PaginationComponent
Defined in:
app/components/fluxbit/pagination_component.rb

Overview

The ‘Fluxbit::PaginationComponent` is a component for rendering customizable pagination controls. It extends `Fluxbit::Component` and provides options for configuring the pagination’s appearance, behavior, and content areas. You can control the pagination’s layout, item count, and other interactive elements. The pagination is divided into different sections (previous, next, etc.), each of which can be styled or customized through various properties.

Instance Method Summary collapse

Methods inherited from Component

#add, #add_popover_or_tooltip, #anyicon, #element_name, #fx_id, #icon, #options, #popover?, #random_id, #remove_class, #remove_class_from_props, #render_popover_or_tooltip, #target, #tooltip?

Methods included from IconHelpers

#chevron_double_left, #chevron_double_right, #chevron_down, #chevron_left, #chevron_right, #chevron_up, #close_icon, #ellipsis_horizontal, #eye_icon, #eye_slash_icon, #plus_icon

Constructor Details

#initialize(pagy = nil, **props) ⇒ PaginationComponent

Returns a new instance of PaginationComponent.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/components/fluxbit/pagination_component.rb', line 9

def initialize(pagy = nil, **props)
  @pagy = pagy

  @props = props
  @count = @props.delete(:count) || 0
  @last = @props.delete(:last) || 1
  @next = @props.delete(:next)
  @page = @props.delete(:page) || 1
  @previous = @props.delete(:previous)
  @size = @props.delete(:size) || 3
  @ends = @props.delete(:ends) || true
  @request_path = @props.delete(:request_path) || nil

  if @pagy
    @count = @pagy.count if @pagy.respond_to?(:count)
    @last = @pagy.last if @pagy.respond_to?(:last)
    @next = @pagy.next if @pagy.respond_to?(:next)
    @page = @pagy.page if @pagy.respond_to?(:page)
    @previous = @pagy.prev if @pagy.respond_to?(:prev)
    @previous = @pagy.previous if @pagy.respond_to?(:previous)

    if @pagy.respond_to?(:vars)
      @size = @pagy.vars[:size]
      @ends = @pagy.vars[:ends]
      @request_path = @pagy.vars[:request_path]
    end
  end

  unless @size.is_a?(Integer) && @size >= 0
    raise ArgumentError, "expected :size to be an Integer >= 0, got #{@size.inspect} (#{@size.class})"
  end

  @show_first_last = options @props.delete(:show_first_last), default: @@show_first_last
  @show_prev_next = options @props.delete(:show_prev_next), default: @@show_prev_next
  @show_pages = options @props.delete(:show_pages), default: @@show_pages
  @show_icons = options @props.delete(:show_icons), default: @@show_icons
  @show_texts = options @props.delete(:show_texts), default: @@show_texts
  @sizing = options @props.delete(:sizing), default: @@sizing
  @aria_label = @props.delete(:aria_label) || translate("aria_label.nav", count: @last)
  @show_texts = true if !@show_icons && !@show_texts

  add(class: [ styles[:root], styles[:sizes][@sizing][:root] ], to: @props)
  @page_link_style = [ styles[:page_link], styles[:sizes][@sizing][:page_link] ].join(" ")
  @current_style = [ styles[:current], styles[:sizes][@sizing][:page_link] ].join(" ")
  @props[:aria] ||= {}
  @props[:aria][:label] = @aria_label unless @props[:aria][:label]
end

Instance Method Details

#callObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/components/fluxbit/pagination_component.rb', line 57

def call
  tag.nav(**@props) do
    concat first_button if @show_first_last
    concat prev_button if @show_prev_next

    if @show_pages
      series.each do |item|
        case item
        when Integer
          concat(tag.a(item.to_s, href: url_for(item), role: "link", class: @page_link_style, aria: { label: item.to_s }))
        when String
          concat(tag.a(item.to_s, role: "link", class: @current_style, aria: { disabled: true, current: "page" }))
        when :gap
          concat(tag.a(ellipsis_horizontal, role: "link", class: @page_link_style, aria: { disabled: true }))
        end
      end
    end

    concat next_button if @show_prev_next
    concat last_button if @show_first_last
  end
end