Class: Trestle::Toolbar::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/trestle/toolbar/item.rb

Direct Known Subclasses

Button, Link

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, label, icon: nil, style: nil, **options, &block) ⇒ Item

Returns a new instance of Item.



8
9
10
11
12
13
14
15
16
# File 'lib/trestle/toolbar/item.rb', line 8

def initialize(template, label, icon: nil, style: nil, **options, &block)
  @template = template
  @label, @options, @block = label, options

  @menu = Menu.new(template)
  @menu.build(&block) if block_given?

  @icon, @style = icon, style
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



4
5
6
# File 'lib/trestle/toolbar/item.rb', line 4

def label
  @label
end

Returns the value of attribute menu.



4
5
6
# File 'lib/trestle/toolbar/item.rb', line 4

def menu
  @menu
end

Instance Method Details

#==(other) ⇒ Object



18
19
20
# File 'lib/trestle/toolbar/item.rb', line 18

def ==(other)
  to_s == other.to_s
end

#button_classesObject



47
48
49
50
51
52
# File 'lib/trestle/toolbar/item.rb', line 47

def button_classes
  classes = (@options[:class] || "").split(/\s/)
  classes.push(*button_style_classes)
  classes.push("has-icon") if @icon
  classes.uniq
end

#button_label(content, options) ⇒ Object



54
55
56
57
58
59
# File 'lib/trestle/toolbar/item.rb', line 54

def button_label(content, options)
  icon = icon(@icon) if @icon
  label = tag.span(content, class: "btn-label")

  safe_join([icon, label].compact, " ")
end

#button_styleObject



61
62
63
# File 'lib/trestle/toolbar/item.rb', line 61

def button_style
  @style || "default"
end

#button_style_classesObject



65
66
67
# File 'lib/trestle/toolbar/item.rb', line 65

def button_style_classes
  ["btn", "btn-#{button_style}"]
end

#optionsObject



43
44
45
# File 'lib/trestle/toolbar/item.rb', line 43

def options
  @options.merge(class: button_classes)
end

#renderObject

Raises:

  • (NotImplementedError)


32
33
34
# File 'lib/trestle/toolbar/item.rb', line 32

def render
  raise NotImplementedError
end

#render_menuObject



36
37
38
39
40
41
# File 'lib/trestle/toolbar/item.rb', line 36

def render_menu
  [
    menu.render_toggle(class: button_style_classes),
    menu.render_items
  ]
end

#to_sObject



22
23
24
25
26
27
28
29
30
# File 'lib/trestle/toolbar/item.rb', line 22

def to_s
  if menu.items.any?
    tag.div(class: "btn-group", role: "group") do
      safe_join([render, render_menu], "\n")
    end
  else
    render
  end
end