Class: Blacklight::Icon

Inherits:
Object
  • Object
show all
Defined in:
app/models/blacklight/icon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(icon_name, classes: '', aria_hidden: false, label: true, role: 'img', additional_options: {}) ⇒ Icon

Returns a new instance of Icon.

Parameters:

  • icon_name (String, Symbol)
  • classes (String) (defaults to: '')

    additional classes separated by a string

  • aria_hidden (Boolean) (defaults to: false)

    include aria_hidden attribute

  • label (Boolean) (defaults to: true)

    include <title> and aria-label as part of svg

  • role (String) (defaults to: 'img')

    role attribute to be included in svg

  • additional_options (Hash) (defaults to: {})

    the way forward instead of named arguments



14
15
16
17
18
19
20
21
# File 'app/models/blacklight/icon.rb', line 14

def initialize(icon_name, classes: '', aria_hidden: false, label: true, role: 'img', additional_options: {})
  @icon_name = icon_name
  @classes = classes
  @aria_hidden = aria_hidden
  @label = label
  @role = role
  @additional_options = additional_options
end

Instance Attribute Details

#additional_optionsObject (readonly)

Returns the value of attribute additional_options.



5
6
7
# File 'app/models/blacklight/icon.rb', line 5

def additional_options
  @additional_options
end

#aria_hiddenObject (readonly)

Returns the value of attribute aria_hidden.



5
6
7
# File 'app/models/blacklight/icon.rb', line 5

def aria_hidden
  @aria_hidden
end

#icon_nameObject (readonly)

Returns the value of attribute icon_name.



5
6
7
# File 'app/models/blacklight/icon.rb', line 5

def icon_name
  @icon_name
end

#labelObject (readonly)

Returns the value of attribute label.



5
6
7
# File 'app/models/blacklight/icon.rb', line 5

def label
  @label
end

#roleObject (readonly)

Returns the value of attribute role.



5
6
7
# File 'app/models/blacklight/icon.rb', line 5

def role
  @role
end

Instance Method Details

#file_sourceString

Returns:

  • (String)

Raises:



55
56
57
58
59
60
61
62
# File 'app/models/blacklight/icon.rb', line 55

def file_source
  raise Blacklight::Exceptions::IconNotFound, "Could not find #{path}" if file.blank?

  # Handle both Sprockets::Asset and Propshaft::Asset
  data = file.respond_to?(:source) ? file.source : file.path.read

  data.force_encoding('UTF-8')
end

#icon_labelObject



34
35
36
# File 'app/models/blacklight/icon.rb', line 34

def icon_label
  I18n.translate("blacklight.icon.#{icon_name_context}", default: icon_name.to_s.titleize)
end

#ng_xmlObject



64
65
66
# File 'app/models/blacklight/icon.rb', line 64

def ng_xml
  @ng_xml ||= Nokogiri::XML(file_source).remove_namespaces!
end

#optionsHash

Returns:

  • (Hash)


40
41
42
43
44
45
# File 'app/models/blacklight/icon.rb', line 40

def options
  {
    class: classes,
    "aria-hidden": (true if aria_hidden)
  }
end

#pathString

Returns:

  • (String)


49
50
51
# File 'app/models/blacklight/icon.rb', line 49

def path
  "blacklight/#{icon_name}.svg"
end

#svgString

Returns an updated version of the svg source

Returns:

  • (String)


26
27
28
29
30
31
32
# File 'app/models/blacklight/icon.rb', line 26

def svg
  svg = ng_xml.at_xpath('svg')
  svg['aria-label'] = icon_label if label
  svg['role'] = role
  svg.prepend_child("<title>#{icon_label}</title>") if label
  ng_xml.to_xml
end