Class: Card::Bootstrap::Component::Carousel

Inherits:
Card::Bootstrap::Component show all
Defined in:
lib/card/bootstrap/component/carousel.rb

Instance Method Summary collapse

Methods inherited from Card::Bootstrap::Component

#append, #card, #initialize, #insert, #prepend, #render, #wrap

Methods included from Card::Bootstrap::ComponentKlass

#def_div_method, #def_tag_method, #render

Methods included from Delegate

#method_missing, #respond_to_missing?

Methods included from BasicTags

#html

Constructor Details

This class inherits a constructor from Card::Bootstrap::Component

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Card::Bootstrap::Delegate

Instance Method Details



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/card/bootstrap/component/carousel.rb', line 9

def carousel id, active_index, &block
  @id = id
  @active_item_index = active_index
  @items = []
  instance_exec(&block)

  @html.div class: "carousel slide", id: id, "data-bs-ride" => "true" do
    indicators
    items
    control_prev
    control_next
  end
end


41
42
43
44
45
46
# File 'lib/card/bootstrap/component/carousel.rb', line 41

def carousel_item item, html_opts
  @html.div html_opts do
    item = item.call if item.respond_to?(:call)
    @html << item if item.is_a?(String)
  end
end


35
36
37
38
39
# File 'lib/card/bootstrap/component/carousel.rb', line 35

def carousel_item_opts index
  { class: "carousel-item" }.tap do |opts|
    add_class opts, "active" if index == @active_item_index
  end
end

#control_button(direction, description) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/card/bootstrap/component/carousel.rb', line 56

def control_button direction, description
  @html.button class: "carousel-control-#{direction}",
               "data-bs-target": "##{@id}", type: "button" do
    @html.span class: "carousel-control-#{direction}-icon",
               "aria-hidden": "true" do
      ""
    end
    @html.span description, class: "visually-hidden"
  end
end

#control_nextObject



52
53
54
# File 'lib/card/bootstrap/component/carousel.rb', line 52

def control_next
  control_button :next, "Next"
end

#control_prevObject



48
49
50
# File 'lib/card/bootstrap/component/carousel.rb', line 48

def control_prev
  control_button :prev, "Previous"
end

#indicator(index) ⇒ Object



73
74
75
76
77
78
# File 'lib/card/bootstrap/component/carousel.rb', line 73

def indicator index
  html_opts = { "data-bs-slide-to": index, "data-bs-target": "##{@id}",
                type: "button", "aria-label": "Slide #{index + 1}" }
  add_class html_opts, "active" if index == @active_item_index
  @html.button html_opts
end

#indicatorsObject



67
68
69
70
71
# File 'lib/card/bootstrap/component/carousel.rb', line 67

def indicators
  @html.div class: "carousel-indicators" do
    @items.size.times { |i| indicator i }
  end
end

#item(content = nil, &block) ⇒ Object



23
24
25
# File 'lib/card/bootstrap/component/carousel.rb', line 23

def item content=nil, &block
  @items << (content || block)
end

#itemsObject



27
28
29
30
31
32
33
# File 'lib/card/bootstrap/component/carousel.rb', line 27

def items
  @html.div class: "carousel-inner" do
    @items.each_with_index do |item, index|
      carousel_item item, carousel_item_opts(index)
    end
  end
end

#render_contentObject



5
6
7
# File 'lib/card/bootstrap/component/carousel.rb', line 5

def render_content
  carousel(*@args, &@build_block)
end