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



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

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:



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

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



36
37
38
# File 'app/models/blacklight/icon.rb', line 36

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

#ng_xmlObject



66
67
68
# File 'app/models/blacklight/icon.rb', line 66

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

#optionsHash

Returns:

  • (Hash)


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

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

#pathString

Returns:

  • (String)


51
52
53
# File 'app/models/blacklight/icon.rb', line 51

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

#svgString

Returns an updated version of the svg source

Returns:

  • (String)


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

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