Class: BootstrapBuilders::Tabs

Inherits:
Object
  • Object
show all
Defined in:
lib/bootstrap_builders/tabs.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Tabs

Returns a new instance of Tabs.



2
3
4
5
6
# File 'lib/bootstrap_builders/tabs.rb', line 2

def initialize(args)
  @args = args
  @context = args.fetch(:context)
  @tabs = []
end

Instance Method Details

#tab(*args, &blk) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/bootstrap_builders/tabs.rb', line 8

def tab(*args, &blk)
  tab_args = {}

  tab_args[:label] = args.shift if args.first.is_a?(String)
  tab_args[:container_id] = args.shift if args.first.is_a?(String)
  tab_args.merge!(args.shift) if args.first.is_a?(Hash)

  tab = BootstrapBuilders::Tab.new(tab_args)
  tab.container_html = @context.(:div, nil, class: ["bb-tab-container"], &blk)
  @tabs << tab
  nil
end

#to_htmlObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bootstrap_builders/tabs.rb', line 21

def to_html
  set_default_first_active

  container = HtmlGen::Element.new(:div, classes: ["bb-tabs-container"])
  ul = container.add_ele(:ul, classes: nav_classes)
  container.add_ele(:div, classes: ["clearfix"])

  @tabs.each do |tab|
    li = ul.add_ele(:li, attr: {class: "nav-item"}, data: {specific_id_given: tab.specific_id_given?, tab_container_id: tab.container_id})
    link = li.add_ele(:a, str: tab.label, attr: {class: "nav-link", href: "##{tab.container_id}"}, data: {toggle: "tab"})

    if tab.active?
      link.classes << "show"
      link.classes << "active"
    end

    li.data[:ajax_url] = tab.ajax_url if tab.ajax_url.present?
  end

  tabs_content = container.add_ele(:div, classes: ["tab-content"])

  @tabs.each do |tab|
    tab_content = tabs_content.add_ele(:div, classes: ["tab-pane"], attr: {id: tab.container_id})
    tab_content.add_html(tab.container_html)
    tab_content.classes << "active" if tab.active?
  end

  container.html
end