Class: HakumiComponents::Pagination::Component

Inherits:
BaseComponent
  • Object
show all
Extended by:
T::Sig
Defined in:
app/components/hakumi_components/pagination/component.rb

Constant Summary collapse

SIZES =
%i[default small].freeze
ALIGNS =
%i[start center end].freeze
DEFAULT_PAGE_SIZE_OPTIONS =
[ 10, 20, 50, 100 ].freeze
ShowTotalProc =
T.type_alias { T.proc.params(total: Integer, range: T::Array[Integer]).returns(String) }
ShowTotal =
T.type_alias { T.nilable(T.any(T::Boolean, String, ShowTotalProc)) }

Constants inherited from BaseComponent

BaseComponent::ControllerOptions, BaseComponent::DateInput, BaseComponent::DateLikeValue, BaseComponent::DimensionInput, BaseComponent::HtmlPayloadInput, BaseComponent::I18nOptionValue, BaseComponent::PresenceArray, BaseComponent::PresenceScalar, BaseComponent::PresenceValue, BaseComponent::RawHtmlInput, BaseComponent::SizeValue, BaseComponent::SymbolInput

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseComponent

#append_data_token, boolean_html_param, #build_inline_style, cast_boolean, #cast_boolean, #class_names, #component_classes, #data_attributes_from, #dimension_to_css, #ensure_dom_id!, float_html_param, #generate_id, #html_classes, html_param, html_primitive_param, #html_style, #i18n_scope, integer_html_param, #merge_attributes, #render_value, #size_to_pixels, #stimulus_attrs, string_html_param, string_or_symbol_array_html_param, symbol_html_param, #t_default, #translate_with_default, #validate_inclusion!, #validate_required!, #value_present?

Constructor Details

#initialize(total:, current: 1, page_size: 10, size: :default, align: :start, disabled: false, simple: false, show_size_changer: nil, show_quick_jumper: false, show_total: nil, page_size_options: DEFAULT_PAGE_SIZE_OPTIONS, hide_on_single_page: false, prev_text: nil, next_text: nil, href: nil, page_param: :page, per_page_param: :per_page, turbo_frame: nil, turbo_action: nil, preserve_params: nil, **html_options) ⇒ Component

Returns a new instance of Component.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/components/hakumi_components/pagination/component.rb', line 40

def initialize(
  total:,
  current: 1,
  page_size: 10,
  size: :default,
  align: :start,
  disabled: false,
  simple: false,
  show_size_changer: nil,
  show_quick_jumper: false,
  show_total: nil,
  page_size_options: DEFAULT_PAGE_SIZE_OPTIONS,
  hide_on_single_page: false,
  prev_text: nil,
  next_text: nil,
  href: nil,
  page_param: :page,
  per_page_param: :per_page,
  turbo_frame: nil,
  turbo_action: nil,
  preserve_params: nil,
  **html_options
)
  @total = T.let(total, Integer)
  @current = T.let(current, Integer)
  @page_size = T.let(page_size, Integer)
  @size = T.let(size, Symbol)
  @align = T.let(align, Symbol)
  @disabled = T.let(disabled, T::Boolean)
  @simple = T.let(simple, T::Boolean)
  @show_size_changer = T.let(show_size_changer, T.nilable(T::Boolean))
  @show_quick_jumper = T.let(show_quick_jumper, T::Boolean)
  @show_total = T.let(show_total, ShowTotal)
  @page_size_options = T.let(page_size_options, T::Array[Integer])
  @hide_on_single_page = T.let(hide_on_single_page, T::Boolean)
  @prev_text = T.let(prev_text, T.nilable(String))
  @next_text = T.let(next_text, T.nilable(String))
  @href = T.let(href, T.nilable(String))
  @page_param = T.let(page_param, Symbol)
  @per_page_param = T.let(per_page_param, Symbol)
  @turbo_frame = T.let(turbo_frame, T.nilable(String))
  @turbo_action = T.let(turbo_action, T.nilable(String))
  @preserve_params = T.let(preserve_params, T.nilable(String))
  @html_options = T.let(html_options, Types::HtmlAttributes)

  validate_props!
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



218
219
220
# File 'app/components/hakumi_components/pagination/component.rb', line 218

def current
  @current
end

#disabledObject (readonly)

Returns the value of attribute disabled.



221
222
223
# File 'app/components/hakumi_components/pagination/component.rb', line 221

def disabled
  @disabled
end

#hrefObject (readonly)

Returns the value of attribute href.



227
228
229
# File 'app/components/hakumi_components/pagination/component.rb', line 227

def href
  @href
end

#next_textObject (readonly)

Returns the value of attribute next_text.



227
228
229
# File 'app/components/hakumi_components/pagination/component.rb', line 227

def next_text
  @next_text
end

#page_sizeObject (readonly)

Returns the value of attribute page_size.



218
219
220
# File 'app/components/hakumi_components/pagination/component.rb', line 218

def page_size
  @page_size
end

#page_size_optionsObject (readonly)

Returns the value of attribute page_size_options.



224
225
226
# File 'app/components/hakumi_components/pagination/component.rb', line 224

def page_size_options
  @page_size_options
end

#prev_textObject (readonly)

Returns the value of attribute prev_text.



227
228
229
# File 'app/components/hakumi_components/pagination/component.rb', line 227

def prev_text
  @prev_text
end

#show_quick_jumperObject (readonly)

Returns the value of attribute show_quick_jumper.



221
222
223
# File 'app/components/hakumi_components/pagination/component.rb', line 221

def show_quick_jumper
  @show_quick_jumper
end

#simpleObject (readonly)

Returns the value of attribute simple.



221
222
223
# File 'app/components/hakumi_components/pagination/component.rb', line 221

def simple
  @simple
end

#totalObject (readonly)

Returns the value of attribute total.



218
219
220
# File 'app/components/hakumi_components/pagination/component.rb', line 218

def total
  @total
end

#turbo_actionObject (readonly)

Returns the value of attribute turbo_action.



227
228
229
# File 'app/components/hakumi_components/pagination/component.rb', line 227

def turbo_action
  @turbo_action
end

#turbo_frameObject (readonly)

Returns the value of attribute turbo_frame.



227
228
229
# File 'app/components/hakumi_components/pagination/component.rb', line 227

def turbo_frame
  @turbo_frame
end

Instance Method Details

#ellipsis_iconObject



197
198
199
# File 'app/components/hakumi_components/pagination/component.rb', line 197

def ellipsis_icon
  HakumiComponents::Icon::Component.new(name: :ellipsis)
end

#jump_next_iconObject



192
193
194
# File 'app/components/hakumi_components/pagination/component.rb', line 192

def jump_next_icon
  HakumiComponents::Icon::Component.new(name: :double_right)
end

#jump_prev_iconObject



187
188
189
# File 'app/components/hakumi_components/pagination/component.rb', line 187

def jump_prev_icon
  HakumiComponents::Icon::Component.new(name: :double_left)
end

#next_disabled?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'app/components/hakumi_components/pagination/component.rb', line 136

def next_disabled?
  @current >= total_pages
end

#next_iconObject



182
183
184
# File 'app/components/hakumi_components/pagination/component.rb', line 182

def next_icon
  HakumiComponents::Icon::Component.new(name: :right)
end

#page_itemsObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/components/hakumi_components/pagination/component.rb', line 148

def page_items
  return [] if @simple

  items = T.let([], T::Array[HakumiComponents::Pagination::PageItem])
  total = total_pages
  current = @current

  items << HakumiComponents::Pagination::PageItem.new(kind: :page, number: 1)

  if total <= 7
    (2..total).each { |n| items << HakumiComponents::Pagination::PageItem.new(kind: :page, number: n) }
  elsif current <= 4
    (2..5).each { |n| items << HakumiComponents::Pagination::PageItem.new(kind: :page, number: n) }
    items << HakumiComponents::Pagination::PageItem.new(kind: :jump_next)
    items << HakumiComponents::Pagination::PageItem.new(kind: :page, number: total)
  elsif current >= total - 3
    items << HakumiComponents::Pagination::PageItem.new(kind: :jump_prev)
    ((total - 4)..total).each { |n| items << HakumiComponents::Pagination::PageItem.new(kind: :page, number: n) }
  else
    items << HakumiComponents::Pagination::PageItem.new(kind: :jump_prev)
    ((current - 1)..(current + 1)).each { |n| items << HakumiComponents::Pagination::PageItem.new(kind: :page, number: n) }
    items << HakumiComponents::Pagination::PageItem.new(kind: :jump_next)
    items << HakumiComponents::Pagination::PageItem.new(kind: :page, number: total)
  end

  items
end

#pagination_classesObject



99
100
101
102
103
104
105
106
# File 'app/components/hakumi_components/pagination/component.rb', line 99

def pagination_classes
  classes = [ "hakumi-pagination" ]
  classes << "hakumi-pagination-mini" if @size == :small
  classes << "hakumi-pagination-simple" if @simple
  classes << "hakumi-pagination-disabled" if @disabled
  classes << "hakumi-pagination-align-#{@align}" if @align != :start
  classes.join(" ")
end

#prev_disabled?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'app/components/hakumi_components/pagination/component.rb', line 131

def prev_disabled?
  @current <= 1
end

#prev_iconObject



177
178
179
# File 'app/components/hakumi_components/pagination/component.rb', line 177

def prev_icon
  HakumiComponents::Icon::Component.new(name: :left)
end

#render?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'app/components/hakumi_components/pagination/component.rb', line 89

def render?
  !(@hide_on_single_page && total_pages <= 1)
end

#show_size_changer?Boolean

Returns:

  • (Boolean)


141
142
143
144
145
# File 'app/components/hakumi_components/pagination/component.rb', line 141

def show_size_changer?
  return @show_size_changer unless @show_size_changer.nil?

  @total > 50
end

#total_pagesObject



94
95
96
# File 'app/components/hakumi_components/pagination/component.rb', line 94

def total_pages
  (@total.to_f / @page_size).ceil
end

#total_textObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/components/hakumi_components/pagination/component.rb', line 202

def total_text
  return nil unless @show_total

  case @show_total
  when true
    t_default(:total, default: "Total %{count} items", count: @total)
  when String
    @show_total
  when Proc
    start_item = (@current - 1) * @page_size + 1
    end_item = [ @current * @page_size, @total ].min
    @show_total.call(@total, [ start_item, end_item ])
  end
end

#wrapper_attributesObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/components/hakumi_components/pagination/component.rb', line 109

def wrapper_attributes
  values = T.let({
    current: @current,
    page_size: @page_size,
    total: @total,
    disabled: @disabled,
    simple: @simple
  }, Types::StimulusValues)
  values[:href] = @href if @href.present?
  values[:page_param] = @page_param.to_s if @href.present?
  values[:per_page_param] = @per_page_param.to_s if @href.present?
  values[:turbo_frame] = @turbo_frame if @turbo_frame.present?
  values[:turbo_action] = @turbo_action if @turbo_action.present?
  values[:preserve_params] = @preserve_params if @preserve_params.present?

  merge_attributes(
    { class: pagination_classes, data: stimulus_attrs("hakumi--pagination", values) },
    @html_options
  )
end