Sha256: 58d841ab35c58a908cfa3061c9ddc383e7753851a35df0ff1f5fb9e14ef47b7f

Contents?: true

Size: 1.62 KB

Versions: 29

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

class Avo::TabSwitcherComponent < Avo::BaseComponent
  include Avo::UrlHelpers
  include Avo::ApplicationHelper

  attr_reader :active_tab_name
  attr_reader :group
  attr_reader :current_tab
  attr_reader :tabs
  attr_reader :view

  delegate :white_panel_classes, to: :helpers

  def initialize(resource:, group:, current_tab:, active_tab_name:, view:)
    @active_tab_name = active_tab_name
    @resource = resource
    @group = group
    @current_tab = current_tab
    @tabs = group.items
    @view = view
  end

  #TOD: helper to record:
  def tab_path(tab)
    if is_edit?
      helpers.edit_resource_path(resource: @resource, record: @resource.record, 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, record: @resource.record, keep_query_params: true, active_tab_name: tab.name, tab_turbo_frame: group.turbo_frame_id)
    end
  end

  def is_edit?
    @view.in?(%w[edit update])
  end

  def is_new?
    @view.in?(%w[new create])
  end

  def is_initial_load?
    params[:active_tab_name].blank?
  end

  # 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.
  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
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
avo-3.0.1.beta23 app/components/avo/tab_switcher_component.rb
avo-3.0.1.beta20 app/components/avo/tab_switcher_component.rb
avo-3.0.1.beta21 app/components/avo/tab_switcher_component.rb
avo-3.0.1.beta17 app/components/avo/tab_switcher_component.rb
avo-3.0.1.beta22 app/components/avo/tab_switcher_component.rb
avo-3.0.1.beta18 app/components/avo/tab_switcher_component.rb
avo-3.0.1.beta19 app/components/avo/tab_switcher_component.rb
avo-3.0.1.beta24 app/components/avo/tab_switcher_component.rb
avo-3.0.1.beta15 app/components/avo/tab_switcher_component.rb
avo-3.0.1.beta16 app/components/avo/tab_switcher_component.rb
avo-3.0.1.beta14 app/components/avo/tab_switcher_component.rb
avo-3.0.1.beta12 app/components/avo/tab_switcher_component.rb
avo-3.0.1.beta13 app/components/avo/tab_switcher_component.rb
avo-3.0.1.beta10 app/components/avo/tab_switcher_component.rb
avo-3.0.1.beta11 app/components/avo/tab_switcher_component.rb
avo-3.0.0.pre19 app/components/avo/tab_switcher_component.rb
avo-3.0.1.beta8 app/components/avo/tab_switcher_component.rb
avo-3.0.1.beta9 app/components/avo/tab_switcher_component.rb
avo-3.1.3 app/components/avo/tab_switcher_component.rb
avo-3.1.2 app/components/avo/tab_switcher_component.rb