Class: FoxTail::PaginationComponent

Inherits:
BaseComponent show all
Defined in:
app/components/fox_tail/pagination_component.rb

Instance Attribute Summary collapse

Attributes inherited from BaseComponent

#html_attributes

Instance Method Summary collapse

Methods inherited from BaseComponent

classname_merger, stimulus_merger, use_stimulus?, #with_html_attributes, #with_html_class

Methods inherited from Base

fox_tail_config

Constructor Details

#initialize(current_page, total_pages, html_attributes = {}) ⇒ PaginationComponent

Returns a new instance of PaginationComponent.



49
50
51
52
53
# File 'app/components/fox_tail/pagination_component.rb', line 49

def initialize(current_page, total_pages, html_attributes = {})
  super(html_attributes)
  @current_page = current_page
  @total_pages = total_pages
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



4
5
6
# File 'app/components/fox_tail/pagination_component.rb', line 4

def current_page
  @current_page
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



4
5
6
# File 'app/components/fox_tail/pagination_component.rb', line 4

def total_pages
  @total_pages
end

Instance Method Details

#before_renderObject



132
133
134
135
136
137
138
# File 'app/components/fox_tail/pagination_component.rb', line 132

def before_render
  super

  html_attributes[:class] = classnames theme.apply(:root, self), html_class
  with_previous_button unless previous_button?
  with_next_button unless next_button?
end

#display_rangeObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/components/fox_tail/pagination_component.rb', line 101

def display_range
  right_padding = current_page - [first_page, current_page - padding].max
  left_padding = [current_page + padding, last_page].min - current_page

  start_page = if right_padding <= left_padding
    current_page - right_padding
  else
    [current_page - (right_padding + (padding - left_padding)), 1].max
  end

  end_page = [last_page, start_page + (padding * 2)].min

  (start_page..end_page)
end

#first_page?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'app/components/fox_tail/pagination_component.rb', line 73

def first_page?
  current_page == first_page
end

#last_pageObject



77
78
79
# File 'app/components/fox_tail/pagination_component.rb', line 77

def last_page
  total_pages - 1 + first_page
end

#last_page?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'app/components/fox_tail/pagination_component.rb', line 81

def last_page?
  current_page == last_page
end

#left_page_gap?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'app/components/fox_tail/pagination_component.rb', line 116

def left_page_gap?
  display_range.min > first_page
end

#next_pageObject



97
98
99
# File 'app/components/fox_tail/pagination_component.rb', line 97

def next_page
  current_page + 1 if next_page?
end

#next_page?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'app/components/fox_tail/pagination_component.rb', line 93

def next_page?
  current_page + 1 <= last_page
end

#out_of_range?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'app/components/fox_tail/pagination_component.rb', line 69

def out_of_range?
  current_page >= first_page && current_page <= last_page
end

#page_url(page) ⇒ Object



124
125
126
127
128
129
130
# File 'app/components/fox_tail/pagination_component.rb', line 124

def page_url(page)
  uri = URI(url)
  query = ActiveSupport::HashWithIndifferentAccess.new Rack::Utils.parse_nested_query(uri.query)
  query[page_key] = page
  uri.query = Rack::Utils.build_nested_query query
  uri.to_s
end

#previous_pageObject



89
90
91
# File 'app/components/fox_tail/pagination_component.rb', line 89

def previous_page
  current_page - 1 if previous_page?
end

#previous_page?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'app/components/fox_tail/pagination_component.rb', line 85

def previous_page?
  current_page - 1 >= first_page
end

#right_page_gap?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'app/components/fox_tail/pagination_component.rb', line 120

def right_page_gap?
  display_range.max != last_page
end

#show_icon?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'app/components/fox_tail/pagination_component.rb', line 59

def show_icon?
  %i[with_icons icons].include? variant.to_sym
end

#show_label?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'app/components/fox_tail/pagination_component.rb', line 55

def show_label?
  %i[text with_icons].include? variant.to_sym
end

#urlObject



63
64
65
66
67
# File 'app/components/fox_tail/pagination_component.rb', line 63

def url
  return options[:url] if options.key? :url

  request.original_fullpath
end