Module: Ratchet::IconHelper

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

Instance Method Summary collapse

Instance Method Details

#deployment_graphicObject



18
19
20
# File 'app/helpers/ratchet/icon_helper.rb', line 18

def deployment_graphic
  Site.deployment_icon
end

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



107
108
109
110
111
# File 'app/helpers/ratchet/icon_helper.rb', line 107

def deployment_icon(name, options={})
  options[:stroke_width] ||= 8
  options[:text] ||= { "font-weight" => 600, "font-size" => "76px" }
  deployment_svg name, options
end

#deployment_info(name) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'app/helpers/ratchet/icon_helper.rb', line 4

def deployment_info(name)
  deployment = deployments[name.downcase]

  if deployment.nil? && !Rails.env.production?
    raise %Q{No deployment type "#{name}" exists.}
  end
    
  deployment
end

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



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/helpers/ratchet/icon_helper.rb', line 93

def deployment_svg(name, options={})
  data = deployment_info(name)
  (options[:use] ||= {})
  options[:use][:stroke] ||= options.delete(:stroke) || "currentColor"
  options[:use][:stroke_width] ||= options.delete(:stroke_width) || 4
  options[:use][:class] ||= "deployment-icon-hex #{options[:use][:class]}".strip

  options[:fill]    ||= "url(##{data[:gradient]})"
  options[:color]   ||= "#fff"
  options[:content] ||= deployment_text(data[:short_name], options.delete(:text) || {})

  deployment_graphic.use(options).html_safe
end

#deployment_text(text, options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/helpers/ratchet/icon_helper.rb', line 41

def deployment_text(text, options={})
  options = {
    x: deployment_graphic.width.to_f / 2,
    y: deployment_graphic.height.to_f / 2,
    fill: "currentColor",
    "font-weight" => 700,
    "font-size" => "57px",
    "text-anchor" => "middle",
    "dominant-baseline" =>"central",
    "alignment-baseline" => "central"
  }.merge(options)

  (:text, text, options).html_safe
end

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/helpers/ratchet/icon_helper.rb', line 56

def deployment_topology(name, options={})
  data = deployment_info(name)

  components = data[:topology].map do |item|
    Esvg.find_symbol("topology/#{item}")
  end

  base = 153 # height of top plane
  options[:spacing] ||= 10  # space between

  # Find height of each section
  heights = components.map(&:height).map { |h| h - base + options[:spacing] }

  # Start coordinates at 0
  y = [0].concat heights

  # Add previous height total to create next coordinate
  y = y.map.with_index { |h, i| y[0..i].reduce(:+) }

  # Full height = last calculated coordinate + base (minus the additional space)
  height = base + y.pop - options[:spacing]

  use = components.map.with_index do |c, i|
    part_name = c.name.sub('topology-','')
    c.use_tag(y: y[i], width: c.width, height: c.height, class:'capsule', "aria-label" => topology_desc(part_name, name.downcase))
  end.reverse.join

  attr = {
    version: "1.1",
    preserveAspectRatio: "xMidYMid meet",
    viewBox: "0 0 #{components.last.width} #{height}",
    'aria-label' => "Topology of a #{data[:name]} deployment"
  }.merge(options)

  (:svg, attr) { use.html_safe }
end

#deploymentsObject



14
15
16
# File 'app/helpers/ratchet/icon_helper.rb', line 14

def deployments
  Site.deployment_data['deployments']
end

#grid_bg(options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'app/helpers/ratchet/icon_helper.rb', line 22

def grid_bg(options={})
  options = {
    scale: true,
    class: "svg-bg bottom centered",
    "aria-hidden" => true
  }.merge(options)

  use_svg "graphics/grid", options
end

#topology_desc(part, deployment_name) ⇒ Object



32
33
34
35
36
37
38
39
# File 'app/helpers/ratchet/icon_helper.rb', line 32

def topology_desc(part, deployment_name)
  parts = Site.deployment_data['parts']
  if parts[deployment_name] && parts[deployment_name][part]
    parts[deployment_name][part]
  else
    parts[part]
  end
end