Class: Avo::TabSwitcherComponent
- Inherits:
-
BaseComponent
- Object
- ViewComponent::Base
- BaseComponent
- Avo::TabSwitcherComponent
- Includes:
- ApplicationHelper, UrlHelpers
- Defined in:
- app/components/avo/tab_switcher_component.rb
Instance Attribute Summary collapse
-
#active_tab_name ⇒ Object
readonly
Returns the value of attribute active_tab_name.
-
#current_tab ⇒ Object
readonly
Returns the value of attribute current_tab.
-
#group ⇒ Object
readonly
Returns the value of attribute group.
-
#style ⇒ Object
readonly
Returns the value of attribute style.
-
#tabs ⇒ Object
readonly
Returns the value of attribute tabs.
-
#view ⇒ Object
readonly
Returns the value of attribute view.
Instance Method Summary collapse
-
#initialize(resource:, group:, current_tab:, active_tab_name:, view:, style:) ⇒ TabSwitcherComponent
constructor
A new instance of TabSwitcherComponent.
- #is_edit? ⇒ Boolean
- #is_initial_load? ⇒ Boolean
- #is_new? ⇒ Boolean
-
#selected?(tab) ⇒ Boolean
On initial load we want that each tab button to be the selected one.
- #tab_path(tab) ⇒ Object
-
#visible_items ⇒ Object
Goes through all items and removes the ones that are not supposed to be visible.
Methods included from ApplicationHelper
#a_button, #a_link, #button_classes, #empty_state, #get_model_class, #input_classes, #render_license_warning, #render_license_warnings, #root_path_without_url, #svg
Methods included from UrlHelpers
#edit_resource_path, #new_resource_path, #related_resources_path, #resource_attach_path, #resource_detach_path, #resource_path, #resources_path
Methods inherited from BaseComponent
Constructor Details
#initialize(resource:, group:, current_tab:, active_tab_name:, view:, style:) ⇒ TabSwitcherComponent
Returns a new instance of TabSwitcherComponent.
14 15 16 17 18 19 20 21 22 |
# File 'app/components/avo/tab_switcher_component.rb', line 14 def initialize(resource:, group:, current_tab:, active_tab_name:, view:, style:) @active_tab_name = active_tab_name @resource = resource @group = group @current_tab = current_tab @tabs = group.items @view = view @style = style end |
Instance Attribute Details
#active_tab_name ⇒ Object (readonly)
Returns the value of attribute active_tab_name.
7 8 9 |
# File 'app/components/avo/tab_switcher_component.rb', line 7 def active_tab_name @active_tab_name end |
#current_tab ⇒ Object (readonly)
Returns the value of attribute current_tab.
9 10 11 |
# File 'app/components/avo/tab_switcher_component.rb', line 9 def current_tab @current_tab end |
#group ⇒ Object (readonly)
Returns the value of attribute group.
8 9 10 |
# File 'app/components/avo/tab_switcher_component.rb', line 8 def group @group end |
#style ⇒ Object (readonly)
Returns the value of attribute style.
12 13 14 |
# File 'app/components/avo/tab_switcher_component.rb', line 12 def style @style end |
#tabs ⇒ Object (readonly)
Returns the value of attribute tabs.
10 11 12 |
# File 'app/components/avo/tab_switcher_component.rb', line 10 def tabs @tabs end |
#view ⇒ Object (readonly)
Returns the value of attribute view.
11 12 13 |
# File 'app/components/avo/tab_switcher_component.rb', line 11 def view @view end |
Instance Method Details
#is_edit? ⇒ Boolean
34 35 36 |
# File 'app/components/avo/tab_switcher_component.rb', line 34 def is_edit? @view.in?([:edit, :update]) end |
#is_initial_load? ⇒ Boolean
42 43 44 |
# File 'app/components/avo/tab_switcher_component.rb', line 42 def is_initial_load? params[:active_tab_name].blank? end |
#is_new? ⇒ Boolean
38 39 40 |
# File 'app/components/avo/tab_switcher_component.rb', line 38 def is_new? @view.in?([:new, :create]) end |
#selected?(tab) ⇒ Boolean
On initial load we want that each tab button to be the selected one. We do that so we don't get the wrongly selected item for a quick brief when first switching from one panel to another.
48 49 50 51 52 53 54 |
# File 'app/components/avo/tab_switcher_component.rb', line 48 def selected?(tab) if is_initial_load? current_tab.name.to_s == tab.name.to_s else tab.name.to_s == active_tab_name.to_s end end |
#tab_path(tab) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'app/components/avo/tab_switcher_component.rb', line 24 def tab_path(tab) if is_edit? helpers.edit_resource_path(resource: @resource, model: @resource.model, keep_query_params: true, active_tab_name: tab.name, tab_turbo_frame: group.turbo_frame_id) elsif is_new? helpers.new_resource_path(resource: @resource, keep_query_params: true, active_tab_name: tab.name, tab_turbo_frame: group.turbo_frame_id) else helpers.resource_path(resource: @resource, model: @resource.model, keep_query_params: true, active_tab_name: tab.name, tab_turbo_frame: group.turbo_frame_id) end end |
#visible_items ⇒ Object
Goes through all items and removes the ones that are not supposed to be visible. Example below: tabs do
field :comments, as: :has_many
end Because the developer hasn't specified that it should be visible on edit views (with the show_on: :edit option), the field should not be visible in the item switcher either.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'app/components/avo/tab_switcher_component.rb', line 63 def visible_items tabs.select do |item| visible = true if item.items.blank? visible = false end first_item = item.items.first if item.items.count == 1 && first_item.is_field? && first_item.has_own_panel? && !first_item.visible_on?(view) # Return nil if tab contians a has_many type of fields and it's hidden in current view visible = false end if item.respond_to?(:visible_on?) visible = item.visible_on? view end if item.respond_to?(:visible?) visible = item.visible? end if item.respond_to?(:authorized?) visible = item. end visible end end |