Class: Avo::TabGroupComponent

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/avo/tab_group_component.rb

Constant Summary

Constants included from Concerns::FindAssociationField

Concerns::FindAssociationField::ASSOCIATIONS

Instance Method Summary collapse

Methods inherited from BaseComponent

#component_name, #hotkey_badge

Methods included from ApplicationHelper

#a_button, #a_link, #body_classes, #button_classes, #chart_color, #container_classes, #d, #decode_filter_params, #e, #editor_file_path, #editor_url, #empty_state, #encode_filter_params, #frame_id, #get_model_class, #input_classes, #mount_path, #number_to_social, #possibly_rails_authentication?, #render_license_warning, #root_path_without_url, #rtl?, #text_direction, #ui, #wrap_in_modal

Methods included from ResourcesHelper

#field_wrapper, #filter_wrapper, #index_field_wrapper, #item_selector_data_attributes, #record_path, #record_title, #resource_for_record, #resource_grid, #resource_show_path, #resource_table

Methods included from Concerns::FindAssociationField

#find_association_field

Instance Method Details

#active_tabObject



73
74
75
76
77
# File 'app/components/avo/tab_group_component.rb', line 73

def active_tab
  visible_tabs.find do |tab|
    tab.title.to_s == active_tab_title.to_s
  end
end

#active_tab_titleObject



50
51
52
53
54
55
56
57
58
59
# File 'app/components/avo/tab_group_component.rb', line 50

def active_tab_title
  requested_tab_title = CGI.unescape(params[group_param].to_s)
  visible_tab_titles = visible_tabs.map { |tab| tab.title.to_s }

  if requested_tab_title.present? && visible_tab_titles.include?(requested_tab_title)
    requested_tab_title
  else
    visible_tab_titles.first
  end
end

#after_initializeObject



14
15
16
# File 'app/components/avo/tab_group_component.rb', line 14

def after_initialize
  group.index = index
end

#args(tab) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/components/avo/tab_group_component.rb', line 79

def args(tab)
  {
    # Hide the turbo frames that aren't in the current tab
    # This way we can lazy load the un-selected tabs on the show view
    class: "block space-y-4 #{"hidden" unless tab.title == active_tab_title}",
    data: {
      # Add a marker to know if we already loaded a turbo frame
      loaded: tab.title == active_tab_title,
      tabs_target: :tabPanel,
      tab_id: tab.title,
    }
  }
end

#frame_args(tab) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/components/avo/tab_group_component.rb', line 22

def frame_args(tab)
  args = {
    target: :_top,
    class: "block"
  }

  if is_not_loaded?(tab)
    args[:loading] = :lazy
    args[:src] = helpers.resource_path(
      resource: resource,
      record: resource.record,
      keep_query_params: true,
      active_tab_title: tab.title,
      tab_turbo_frame: tab.turbo_frame_id(parent: group)
    )
  end

  args
end

#is_not_loaded?(tab) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/components/avo/tab_group_component.rb', line 42

def is_not_loaded?(tab)
  params[:tab_turbo_frame] != tab.turbo_frame_id(parent: group)
end

#render?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'app/components/avo/tab_group_component.rb', line 18

def render?
  tabs_have_content? && visible_tabs.present?
end

#tab_active?(tab, current_tab) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
# File 'app/components/avo/tab_group_component.rb', line 122

def tab_active?(tab, current_tab)
  tab.title == current_tab.title
end

#tab_data(tab, current_tab) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'app/components/avo/tab_group_component.rb', line 110

def tab_data(tab, current_tab)
  data = {
    action: "click->tabs#changeTab",
    tabs_tab_name_param: tab.title,
    tabs_group_id_param: group.to_param,
    tabs_resource_name_param: resource.underscore_name,
    selected: tab_active?(tab, current_tab)
  }
  data[:tippy] = "tooltip" if tab.description.present?
  data
end

#tab_path(tab) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/components/avo/tab_group_component.rb', line 93

def tab_path(tab)
  base_options = {
    resource: resource,
    keep_query_params: true,
    active_tab_title: tab.title,
    tab_turbo_frame: group.turbo_frame_id
  }

  if view.in?(%w[edit update])
    helpers.edit_resource_path(**base_options, record: resource.record)
  elsif view.in?(%w[new create])
    helpers.new_resource_path(**base_options)
  else
    helpers.resource_path(**base_options, record: resource.record)
  end
end

#tabsObject



61
62
63
64
65
# File 'app/components/avo/tab_group_component.rb', line 61

def tabs
  group.visible_items.map do |tab|
    tab.hydrate(view: view)
  end
end

#tabs_have_content?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/components/avo/tab_group_component.rb', line 46

def tabs_have_content?
  visible_tabs.present?
end

#visible_tabsObject



67
68
69
70
71
# File 'app/components/avo/tab_group_component.rb', line 67

def visible_tabs
  tabs.select do |tab|
    tab.visible?
  end
end