Class: Avo::TabGroupComponent
- Inherits:
-
BaseComponent
- Object
- BaseComponent
- Avo::TabGroupComponent
- Defined in:
- app/components/avo/tab_group_component.rb
Instance Method Summary collapse
- #active_tab ⇒ Object
- #active_tab_title ⇒ Object
- #after_initialize ⇒ Object
- #args(tab) ⇒ Object
- #frame_args(tab) ⇒ Object
- #is_not_loaded?(tab) ⇒ Boolean
-
#manual_frame_url(tab) ⇒ Object
The deferred load URL for a tab's turbo frame.
- #render? ⇒ Boolean
- #tab_active?(tab, current_tab) ⇒ Boolean
- #tab_data(tab, current_tab) ⇒ Object
- #tab_path(tab) ⇒ Object
- #tabs ⇒ Object
- #tabs_have_content? ⇒ Boolean
- #visible_tabs ⇒ Object
Instance Method Details
#active_tab ⇒ Object
81 82 83 84 85 |
# File 'app/components/avo/tab_group_component.rb', line 81 def active_tab visible_tabs.find do |tab| tab.title.to_s == active_tab_title.to_s end end |
#active_tab_title ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'app/components/avo/tab_group_component.rb', line 58 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_initialize ⇒ Object
14 15 16 |
# File 'app/components/avo/tab_group_component.rb', line 14 def after_initialize group.index = index end |
#args(tab) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'app/components/avo/tab_group_component.rb', line 87 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 |
# 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] = manual_frame_url(tab) end args end |
#is_not_loaded?(tab) ⇒ Boolean
50 51 52 |
# File 'app/components/avo/tab_group_component.rb', line 50 def is_not_loaded?(tab) params[:tab_turbo_frame] != tab.turbo_frame_id(parent: group) end |
#manual_frame_url(tab) ⇒ Object
The deferred load URL for a tab's turbo frame. Mirrors the src the lazy
branch builds in frame_args, so a manual tab's Load button fetches the
exact same proven URL — carrying tab_turbo_frame so is_not_loaded? is
false on the framed re-render and the real TabContentComponent renders.
40 41 42 43 44 45 46 47 48 |
# File 'app/components/avo/tab_group_component.rb', line 40 def manual_frame_url(tab) 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 |
#render? ⇒ 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
130 131 132 |
# File 'app/components/avo/tab_group_component.rb', line 130 def tab_active?(tab, current_tab) tab.title == current_tab.title end |
#tab_data(tab, current_tab) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 |
# File 'app/components/avo/tab_group_component.rb', line 118 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
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'app/components/avo/tab_group_component.rb', line 101 def tab_path(tab) = { 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(**, record: resource.record) elsif view.in?(%w[new create]) helpers.new_resource_path(**) else helpers.resource_path(**, record: resource.record) end end |
#tabs ⇒ Object
69 70 71 72 73 |
# File 'app/components/avo/tab_group_component.rb', line 69 def tabs group.visible_items.map do |tab| tab.hydrate(view: view) end end |
#tabs_have_content? ⇒ Boolean
54 55 56 |
# File 'app/components/avo/tab_group_component.rb', line 54 def tabs_have_content? visible_tabs.present? end |
#visible_tabs ⇒ Object
75 76 77 78 79 |
# File 'app/components/avo/tab_group_component.rb', line 75 def visible_tabs tabs.select do |tab| tab.visible? end end |