Class: Bootstrap::Component::Carousel

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

Instance Method Summary collapse

Methods inherited from Bootstrap::Component

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

Methods included from ComponentClass

#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 Bootstrap::Component

Dynamic Method Handling

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

Instance Method Details



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

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-ride" => "carousel" do
    indicators
    items
    control_prev
    control_next
  end
end


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

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


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

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

#control_nextObject



55
56
57
58
59
60
61
# File 'card-mod-bootstrap/lib/bootstrap/component/carousel.rb', line 55

def control_next
  @html.a class: "carousel-control-next", href: "##{@id}", role: "button",
          "data-slide": "next"  do
    @html.span class: "carousel-control-next-icon", "aria-hidden" => "true"
    @html.span "Next", class: "sr-only"
  end
end

#control_prevObject



47
48
49
50
51
52
53
# File 'card-mod-bootstrap/lib/bootstrap/component/carousel.rb', line 47

def control_prev
  @html.a class: "carousel-control-prev", href: "##{@id}", role: "button",
          "data-slide" => "prev" do
    @html.span class: "carousel-control-prev-icon", "aria-hidden" => "true"
    @html.span "Previous", class: "sr-only"
  end
end

#indicator(index) ⇒ Object



69
70
71
72
73
# File 'card-mod-bootstrap/lib/bootstrap/component/carousel.rb', line 69

def indicator index
  html_opts = { "data-slide-to" => index, "data-target": "##{@id}" }
  add_class html_opts, "active" if index == @active_item_index
  @html.li html_opts
end

#indicatorsObject



63
64
65
66
67
# File 'card-mod-bootstrap/lib/bootstrap/component/carousel.rb', line 63

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

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



22
23
24
# File 'card-mod-bootstrap/lib/bootstrap/component/carousel.rb', line 22

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

#itemsObject



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

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

#render_contentObject



4
5
6
# File 'card-mod-bootstrap/lib/bootstrap/component/carousel.rb', line 4

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