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 =
"1.4.0"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.configuration ⇒ Object

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_js(html_props) ⇒ Object



68
69
70
71
72
# File 'lib/faw_icon/helper.rb', line 68

def by_js(html_props)
  doc = Nokolexbor::HTML('<i>&nbsp;</i>')

  fa_tag(doc.at_css("i"), html_props)
end

#by_json(style, name, html_props) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/faw_icon/helper.rb', line 33

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 = Nokolexbor::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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/faw_icon/helper.rb', line 44

def by_raw(style, name, html_props)
  image_path = FawIcon.configuration.raw_svg_path

  if (faw_spec = Gem.loaded_specs['faw_files'])
    image_path = "#{faw_spec.full_gem_path}/vendor/assets/images/fa/raw-svg"
  end

  if File.exist? Rails.root.join(image_path, style, "#{name}.svg")
    doc = File.open(Rails.root.join(image_path, style, "#{name}.svg")) { |f| Nokolexbor::HTML(f) }
    svg = doc.at_css('svg')
  end

  fa_tag(svg, html_props)
end

#by_sprite(style, name, html_props) ⇒ Object



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

def by_sprite(style, name, html_props)
  if File.exist? Rails.root.join(FawIcon.configuration.svg_sprites_path, "fa-#{style}.svg")
    doc = Nokolexbor::HTML("<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 512 512\"><use 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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/faw_icon/helper.rb', line 85

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

#fa_tag(svg = nil, html_props) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/faw_icon/helper.rb', line 74

def fa_tag(svg = nil, html_props)
  svg = svg_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['data-source-type'] = html_props[:source_type] if html_props[:source_type]

  svg.to_s.html_safe
end

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



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

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}"]
  html_props[:class] << FawIcon.configuration.default_replacement_class unless FawIcon.configuration.source_type == 'js'
  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]
  html_props[:source_type] = FawIcon.configuration.source_type

  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)
    when 'js'
      by_js(html_props)
  end
end

#svg_not_found ⇒ Object



100
101
102
103
104
# File 'lib/faw_icon/helper.rb', line 100

def svg_not_found
  doc = Nokolexbor::HTML(FawIcon.configuration.icon_not_found)

  doc.at_css("svg")
end