Module: Megatron::IconHelper

Defined in:
app/helpers/megatron/icon_helper.rb

Constant Summary collapse

ICON_OPTIONS =
{
  config_file: File.expand_path('../../../config/esvg.yml', File.dirname(__FILE__)),
  path: File.expand_path('../../assets/esvg/megatron', File.dirname(__FILE__))
}

Instance Method Summary collapse

Instance Method Details

#dasherize(input) ⇒ Object



79
80
81
# File 'app/helpers/megatron/icon_helper.rb', line 79

def dasherize(input)
  input.gsub(/[\W,_]/, '-').gsub(/-{2,}/, '-')
end

#default_class(classnames, default) ⇒ Object



83
84
85
86
87
88
# File 'app/helpers/megatron/icon_helper.rb', line 83

def default_class(classnames, default)
  if classnames.nil? || !classnames.is_a?(String)
    classnames = ''
  end
  classnames.to_s << " #{default}"
end

#deployment_text_icon(type, options = {}, &block) ⇒ Object



90
91
92
93
94
# File 'app/helpers/megatron/icon_helper.rb', line 90

def deployment_text_icon(type, options={}, &block)
  options[:color] ||= type
  options[:fallback] ||= 'deployment'
  text_icon("deployment-#{type}", options, &block)
end

#font_icon(name, options = {}) ⇒ Object



32
33
34
35
# File 'app/helpers/megatron/icon_helper.rb', line 32

def font_icon(name, options={})
  options[:class] = default_class(options[:class], "#{name}_icon")
  (:span, class: options[:class], 'aria-hidden' => true) {  }
end

#icon(name, options = {}, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/megatron/icon_helper.rb', line 12

def icon(name, options={}, &block)
  i = iconset.svg_icon(name, options).html_safe

  if options[:wrapper]
    i = (:span, class: options[:wrapper].strip) do
      i
    end
  end

  if options[:label]
    i += %Q{<span class="icon-label align-middle">#{options[:label]}</span>}.html_safe
  end

  if block
    i += ('span', class: 'align-middle', &block)
  end

  i
end

#icon_label(name, options = {}, &block) ⇒ Object



47
48
49
# File 'app/helpers/megatron/icon_helper.rb', line 47

def icon_label(name, options={}, &block)
  text_icon(name, options) + ('span', class: 'align-middle', &block)
end

#iconsetObject



8
9
10
# File 'app/helpers/megatron/icon_helper.rb', line 8

def iconset
  svg_icons(ICON_OPTIONS)
end


42
43
44
45
# File 'app/helpers/megatron/icon_helper.rb', line 42

def nav_icon(name, options={}, &block)
  options[:wrapper] = "nav-icon"
  text_icon(name, options, &block)
end

#set_icon_classes(options, defaults = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/helpers/megatron/icon_helper.rb', line 51

def set_icon_classes(options, defaults={})
  options[:class] = (options[:class] || '') << " #{defaults[:class]}"

  if options[:wrapper]
    options[:wrapper] = '' unless options[:wrapper].is_a?(String)
    options[:wrapper] = (options[:wrapper] || '') << " #{defaults[:wrapper]}"
  end

  # Automate adding color classes, blue becomes blue-text
  # This should allow us to change it later more easily if necessary
  if options[:color]
    if options[:color].to_s.start_with?('#')
      options[:style] ||= ''
      options[:style] << " color: #{options[:color].strip}"
    else
      options[:class] << " #{options[:color].to_s.strip}-text"
    end
  end

  # Automate adding size classes, x-small becomes x-small-text
  # This should allow us to change it later more easily if necessary
  if options[:size]
    options[:class] << " #{dasherize(options[:size].to_s.strip)}-text"
  end

  options
end

#text_icon(name, options = {}, &block) ⇒ Object



37
38
39
40
# File 'app/helpers/megatron/icon_helper.rb', line 37

def text_icon(name, options={}, &block)
  options = set_icon_classes(options, class: 'text-icon', wrapper: 'icon-wrapper')
  icon(name.to_s, options, &block)
end