Module: FawIcon

Defined in:
lib/faw_icon/helper.rb,
lib/faw_icon/railtie.rb,
lib/faw_icon/version.rb,
lib/faw_icon/configuration.rb

Defined Under Namespace

Classes: Configuration, Railtie

Constant Summary collapse

VERSION =
"0.6.2"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



5
6
7
# File 'lib/faw_icon/configuration.rb', line 5

def configuration
  @configuration
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



8
9
10
11
# File 'lib/faw_icon/configuration.rb', line 8

def self.configure
  self.configuration ||= Configuration.new
  yield(configuration)
end

Instance Method Details

#by_json(style, name, html_props) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/faw_icon/helper.rb', line 28

def by_json(style, name, html_props)
  icons = JSON.parse(File.read(FawIcon.configuration.icons_path))

  if icons[name].present? && icons[name]['svg'][style].present?
    doc = Nokogiri::HTML(icons[name]['svg'][style]['raw'])
    svg = doc.at_css 'svg'
  end

  fa_tag(svg, html_props)
end

#by_raw(style, name, html_props) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/faw_icon/helper.rb', line 39

def by_raw(style, name, html_props)
  if File.exists? Rails.root.join(FawIcon.configuration.raw_svg_path, style, "#{name}.svg")
    doc = File.open(Rails.root.join(FawIcon.configuration.raw_svg_path, style, "#{name}.svg")) { |f| Nokogiri::HTML(f) }
    svg = doc.at_css 'svg'
  end

  fa_tag(svg, html_props)
end

#by_sprite(style, name, html_props) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/faw_icon/helper.rb', line 48

def by_sprite(style, name, html_props)
  if File.exists? Rails.root.join(FawIcon.configuration.svg_sprites_path, "fa-#{style}.svg")
    doc = Nokogiri::HTML("<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 512 512\"><use xlink:href=\"/fa5/svg-sprites/fa-#{style}.svg##{name}\"></use></svg>")
    svg = doc.at_css 'svg'
  end

  fa_tag(svg, html_props)
end

#fa_style(style) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/faw_icon/helper.rb', line 67

def fa_style(style)
  case style
    when 'solid'
      'fas'
    when 'regular'
      'far'
    when 'light'
      'fal'
    when 'brands'
      'fab'
  end
end

#fa_tag(svg = nil, html_props) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/faw_icon/helper.rb', line 57

def fa_tag(svg = nil, html_props)
  svg = icon_not_found if svg.nil?

  svg['class'] = html_props[:class].join(' ')
  svg['data-fa-transform'] = html_props[:transform] if html_props[:transform]
  svg['data-fa-mask'] = html_props[:mask] if html_props[:mask]

  svg.to_html.html_safe
end

#faw_icon(style, name, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/faw_icon/helper.rb', line 5

def faw_icon(style, name, options = {})
  html_props = {}
  style = 'brands' if style == 'brand'
  fa_prefix = FawIcon.configuration.default_family_prefix

  html_props[:class] = [fa_style(style), "#{fa_prefix}-#{name}", FawIcon.configuration.default_replacement_class]
  html_props[:class] << "#{fa_prefix}-#{options[:size]}" if options[:size]
  html_props[:class] << "#{fa_prefix}-fw" if options[:fixed_width]
  html_props[:class] << "#{fa_prefix}-spin" if options[:spin]
  html_props[:class] << options[:extra_class] if options[:extra_class]
  html_props[:transform] = options[:transform] if options[:transform]
  html_props[:mask] = options[:mask] if options[:mask]

  case FawIcon.configuration.source_type
    when 'json'
      by_json(style, name, html_props)
    when 'raw'
      by_raw(style, name, html_props)
    when 'sprite'
      by_sprite(style, name, html_props)
  end
end

#icon_not_foundObject



80
81
82
83
84
# File 'lib/faw_icon/helper.rb', line 80

def icon_not_found
  doc = Nokogiri::HTML(FawIcon.configuration.icon_not_found)

  doc.at_css 'svg'
end