Class: HakumiComponents::Timeline::Component

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

Constant Summary collapse

TitleSpan =
T.type_alias { T.nilable(BaseComponent::DimensionInput) }
ItemPayload =
T.type_alias { T::Hash[String, T.nilable(String)] }
ParsedItems =
T.type_alias { T::Array[ItemPayload] }
ControllerOptions =
T.type_alias { T.any(ActionController::Parameters, Types::HtmlAttributes) }
ControllerValue =
T.type_alias { T.any(Types::HtmlAttributeValue, ParsedItems) }
TimelineItem =
T.type_alias { T.any(HakumiComponents::Timeline::Item::Component, ViewComponent::Slot) }
ItemInput =
T.type_alias { T.untyped }
DIRECTIONS =
T.let(%i[vertical horizontal].freeze, T::Array[Symbol])
MODES =
T.let(%i[left right alternate].freeze, T::Array[Symbol])
VARIANTS =
T.let(%i[filled outlined].freeze, T::Array[Symbol])

Constants inherited from BaseComponent

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

Instance Attribute Summary collapse

Class Method 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(direction: :vertical, mode: :left, reverse: false, pending: nil, pending_dot: nil, title_span: nil, variant: :filled, items: [], **html_options) ⇒ Component

Returns a new instance of Component.



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

def initialize(
  direction: :vertical,
  mode: :left,
  reverse: false,
  pending: nil,
  pending_dot: nil,
  title_span: nil,
  variant: :filled,
  items: [],
  **html_options
)
  @direction = T.let(direction.to_sym, Symbol)
  @mode = T.let(mode.to_sym, Symbol)
  @reverse = T.let(cast_boolean(reverse) ? true : false, T::Boolean)
  @pending = T.let(pending, Types::Renderable)
  @pending_dot = T.let(pending_dot, Types::Renderable)
  @title_span = T.let(title_span, TitleSpan)
  @variant = T.let(variant.to_sym, Symbol)
  @item_definitions = T.let(normalize_item_definitions(items), ParsedItems)
  @html_options = T.let(html_options, Types::HtmlAttributes)

  validate_props!
end

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction.



100
101
102
# File 'app/components/hakumi_components/timeline/component.rb', line 100

def direction
  @direction
end

#modeObject (readonly)

Returns the value of attribute mode.



100
101
102
# File 'app/components/hakumi_components/timeline/component.rb', line 100

def mode
  @mode
end

#pendingObject (readonly)

Returns the value of attribute pending.



106
107
108
# File 'app/components/hakumi_components/timeline/component.rb', line 106

def pending
  @pending
end

#pending_dotObject (readonly)

Returns the value of attribute pending_dot.



106
107
108
# File 'app/components/hakumi_components/timeline/component.rb', line 106

def pending_dot
  @pending_dot
end

#reverseObject (readonly)

Returns the value of attribute reverse.



103
104
105
# File 'app/components/hakumi_components/timeline/component.rb', line 103

def reverse
  @reverse
end

#title_spanObject (readonly)

Returns the value of attribute title_span.



109
110
111
# File 'app/components/hakumi_components/timeline/component.rb', line 109

def title_span
  @title_span
end

#variantObject (readonly)

Returns the value of attribute variant.



100
101
102
# File 'app/components/hakumi_components/timeline/component.rb', line 100

def variant
  @variant
end

Class Method Details

.extract_controller_locals(params) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/components/hakumi_components/timeline/component.rb', line 86

def self.extract_controller_locals(params)
  {
    direction: symbol_html_param(html_param(params, :direction)),
    mode: symbol_html_param(html_param(params, :mode)),
    reverse: boolean_html_param(html_param(params, :reverse)),
    pending: string_html_param(html_param(params, :pending)),
    pending_dot: string_html_param(html_param(params, :pending_dot)),
    title_span: html_primitive_param(html_param(params, :title_span)),
    variant: symbol_html_param(html_param(params, :variant)),
    items: parse_json_param(html_param(params, :items)) || []
  }
end

.parse_json_param(value) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'app/components/hakumi_components/timeline/component.rb', line 237

def self.parse_json_param(value)
  return nil unless value.is_a?(String) && value.present?

  parsed = JSON.parse(value)
  return nil unless parsed.is_a?(Array)

  items = T.let([], ParsedItems)
  parsed.each do |entry|
    next unless entry.is_a?(Hash)

    item = T.let({}, T::Hash[String, T.nilable(String)])
    entry.each do |key, raw_value|
      item[key.to_s] = raw_value&.to_s
    end
    items << item
  end
  items
rescue JSON::ParserError
  nil
end

Instance Method Details

#contextualized_item(item, index, total) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'app/components/hakumi_components/timeline/component.rb', line 154

def contextualized_item(item, index, total)
  if item.is_a?(HakumiComponents::Timeline::Item::Component)
    return item.with_context(index: index, total: total, position: item_position(index, item), reverse: reverse)
  end

  raw_item = item.public_send(
    :with_context,
    index: index,
    total: total,
    position: item_position(index, item),
    reverse: reverse
  )

  return raw_item if raw_item.is_a?(ViewComponent::Slot)
  return raw_item if raw_item.is_a?(HakumiComponents::Timeline::Item::Component)

  item
end

#item_position(index, item) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/components/hakumi_components/timeline/component.rb', line 133

def item_position(index, item)
  existing_position = slot_position(item)
  return existing_position unless existing_position.nil?

  case mode
  when :right
    :right
  when :alternate
    index.even? ? :left : :right
  else
    :left
  end
end

#pending_itemObject



174
175
176
177
178
179
180
181
182
183
184
185
# File 'app/components/hakumi_components/timeline/component.rb', line 174

def pending_item
  return nil if pending.nil?

  HakumiComponents::Timeline::Item::Component.new(
    title: nil,
    body: pending,
    loading: true,
    dot: pending_dot,
    parent: self,
    pending: true
  )
end

#timeline_itemsObject



124
125
126
127
128
129
130
# File 'app/components/hakumi_components/timeline/component.rb', line 124

def timeline_items
  base_items = T.let(programmatic_items + items.dup, T::Array[TimelineItem])
  pending = pending_item
  base_items << pending if pending

  reverse ? base_items.reverse : base_items
end

#wrapper_attributesObject



112
113
114
115
116
117
118
119
120
121
# File 'app/components/hakumi_components/timeline/component.rb', line 112

def wrapper_attributes
  merge_attributes(
    {
      class: timeline_classes,
      style: timeline_style,
      data: stimulus_attrs("hakumi--timeline", { reverse: reverse })
    },
    @html_options.except(:class)
  )
end