Sha256: b8890c5c48ea1c937e3a0b17805dfac5720d70d2ab5d58b2281bcdcd6c8ae21e

Contents?: true

Size: 1.62 KB

Versions: 26

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?([:edit, :update])
  end

  def is_new?
    @view.in?([: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

26 entries across 26 versions & 1 rubygems

Version Path
avo-3.0.1.beta6 app/components/avo/tab_switcher_component.rb
avo-3.0.1.beta7 app/components/avo/tab_switcher_component.rb
avo-3.0.1.beta5 app/components/avo/tab_switcher_component.rb
avo-3.0.1.beta2 app/components/avo/tab_switcher_component.rb
avo-3.0.1.beta4 app/components/avo/tab_switcher_component.rb
avo-3.0.1.beta3 app/components/avo/tab_switcher_component.rb
avo-3.0.0.beta1 app/components/avo/tab_switcher_component.rb
avo-3.0.1.beta1 app/components/avo/tab_switcher_component.rb
avo-3.0.0.pre17 app/components/avo/tab_switcher_component.rb
avo-3.0.0.pre18 app/components/avo/tab_switcher_component.rb
avo-3.0.0.pre14 app/components/avo/tab_switcher_component.rb
avo-3.0.0.pre16 app/components/avo/tab_switcher_component.rb
avo-3.0.0.pre15 app/components/avo/tab_switcher_component.rb
avo-3.0.0.pre13 app/components/avo/tab_switcher_component.rb
avo-3.0.0.pre12 app/components/avo/tab_switcher_component.rb
avo-3.0.0.pre10 app/components/avo/tab_switcher_component.rb
avo-3.0.0.pre11 app/components/avo/tab_switcher_component.rb
avo-3.0.0.pre8 app/components/avo/tab_switcher_component.rb
avo-3.0.0.pre9 app/components/avo/tab_switcher_component.rb
avo-3.0.0.pre7 app/components/avo/tab_switcher_component.rb