Module: Megatron::IconHelper
- Defined in:
- app/helpers/megatron/icon_helper.rb
Constant Summary collapse
- @@iconset =
Esvg::SVG.new( config_file: File.('../../../config/esvg.yml', File.dirname(__FILE__)), path: File.('../../assets/esvg/megatron', File.dirname(__FILE__)) )
Instance Method Summary collapse
- #dasherize(input) ⇒ Object
- #default_class(classnames, default) ⇒ Object
- #deployment_text_icon(type, options = {}, &block) ⇒ Object
- #font_icon(name, options = {}) ⇒ Object
- #icon(name, options = {}, &block) ⇒ Object
- #icon_label(name, options = {}, &block) ⇒ Object
- #iconset ⇒ Object
- #nav_icon(name, options = {}, &block) ⇒ Object
- #set_icon_classes(options, defaults = {}) ⇒ Object
- #text_icon(name, options = {}, &block) ⇒ Object
Instance Method Details
#dasherize(input) ⇒ Object
87 88 89 |
# File 'app/helpers/megatron/icon_helper.rb', line 87 def dasherize(input) input.gsub(/[\W,_]/, '-').gsub(/-{2,}/, '-') end |
#default_class(classnames, default) ⇒ Object
91 92 93 94 95 96 |
# File 'app/helpers/megatron/icon_helper.rb', line 91 def default_class(classnames, default) if classnames.nil? || !classnames.is_a?(String) classnames = '' end classnames.to_s << " #{default}" end |
#deployment_text_icon(type, options = {}, &block) ⇒ Object
98 99 100 101 102 |
# File 'app/helpers/megatron/icon_helper.rb', line 98 def deployment_text_icon(type, ={}, &block) [:color] ||= type [:fallback] ||= 'deployment' text_icon("deployment-#{type}-fill", , &block) end |
#font_icon(name, options = {}) ⇒ Object
40 41 42 43 |
# File 'app/helpers/megatron/icon_helper.rb', line 40 def font_icon(name, ={}) [:class] = default_class([:class], "#{name}_icon") content_tag(:span, class: [:class], 'aria-hidden' => true) { } end |
#icon(name, options = {}, &block) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/helpers/megatron/icon_helper.rb', line 14 def icon(name, ={}, &block) name = dasherize(name) begin i = iconset.svg_icon(name, ).html_safe rescue Exception => e raise e if !Rails.env.production? i = '' end if [:wrapper] i = content_tag(:span, class: [:wrapper].strip) do i end end if [:label] i += %Q{<span class="icon-label align-middle">#{[:label]}</span>}.html_safe end if block i += content_tag('span', class: 'align-middle', &block) end i end |
#icon_label(name, options = {}, &block) ⇒ Object
55 56 57 |
# File 'app/helpers/megatron/icon_helper.rb', line 55 def icon_label(name, ={}, &block) text_icon(name, ) + content_tag('span', class: 'align-middle', &block) end |
#iconset ⇒ Object
9 10 11 12 |
# File 'app/helpers/megatron/icon_helper.rb', line 9 def iconset @@iconset.read_files if Rails.env.development? @@iconset end |
#nav_icon(name, options = {}, &block) ⇒ Object
50 51 52 53 |
# File 'app/helpers/megatron/icon_helper.rb', line 50 def nav_icon(name, ={}, &block) [:wrapper] = "nav-icon" text_icon(name, , &block) end |
#set_icon_classes(options, defaults = {}) ⇒ Object
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 |
# File 'app/helpers/megatron/icon_helper.rb', line 59 def set_icon_classes(, defaults={}) [:class] = ([:class] || '') << " #{defaults[:class]}" if [:wrapper] [:wrapper] = '' unless [:wrapper].is_a?(String) [:wrapper] = ([:wrapper] || '') << " #{defaults[:wrapper]}" end # Automate adding color classes, blue becomes blue-text # This should allow us to change it later more easily if necessary if [:color] if [:color].to_s.start_with?('#') [:style] ||= '' [:style] << " color: #{[:color].strip}" else [:class] << " #{[:color].to_s.strip}-text" end end # Automate adding size classes, x-small becomes x-small-text # This should allow us to change it later more easily if necessary if [:size] [:class] << " #{dasherize([:size].to_s.strip)}-text" end end |
#text_icon(name, options = {}, &block) ⇒ Object
45 46 47 48 |
# File 'app/helpers/megatron/icon_helper.rb', line 45 def text_icon(name, ={}, &block) = set_icon_classes(, class: 'text-icon', wrapper: 'icon-wrapper') icon(name.to_s, , &block) end |