Module: IconsHelper

Extended by:
IconsHelper
Includes:
FontAwesome::Rails::IconHelper
Included in:
Clusters::ClusterPresenter, DashboardHelper, DiffFileEntity, Gitlab::BlamePresenter, Gitlab::Conflict::File, IconsHelper, NotificationsHelper, ProjectPresenter
Defined in:
app/helpers/icons_helper.rb

Constant Summary collapse

DEFAULT_ICON_SIZE =
16

Instance Method Summary collapse

Instance Method Details

#audit_icon(names, options = {}) ⇒ Object


79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/helpers/icons_helper.rb', line 79

def audit_icon(names, options = {})
  case names
  when "standard"
    names = "key"
  when "two-factor"
    names = "key"
  when "google_oauth2"
    names = "google"
  end

  options.include?(:base) ? fa_stacked_icon(names, options) : fa_icon(names, options)
end

#boolean_to_icon(value) ⇒ Object


101
102
103
104
105
106
107
# File 'app/helpers/icons_helper.rb', line 101

def boolean_to_icon(value)
  if value
    sprite_icon('check', css_class: 'cgreen')
  else
    sprite_icon('power', css_class: 'clgray')
  end
end

#custom_icon(icon_name, size: DEFAULT_ICON_SIZE) ⇒ Object


26
27
28
29
30
# File 'app/helpers/icons_helper.rb', line 26

def custom_icon(icon_name, size: DEFAULT_ICON_SIZE)
  # We can't simply do the below, because there are some .erb SVGs.
  #  File.read(Rails.root.join("app/views/shared/icons/_#{icon_name}.svg")).html_safe
  render "shared/icons/#{icon_name}.svg", size: size
end

#external_snippet_icon(name) ⇒ Object


75
76
77
# File 'app/helpers/icons_helper.rb', line 75

def external_snippet_icon(name)
  (:span, "", class: "gl-snippet-icon gl-snippet-icon-#{name}")
end

#file_type_icon_class(type, mode, name) ⇒ Object


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/helpers/icons_helper.rb', line 125

def file_type_icon_class(type, mode, name)
  if type == 'folder'
    icon_class = 'folder'
  elsif type == 'archive'
    icon_class = 'archive'
  elsif mode == '120000'
    icon_class = 'share'
  else
    # Guess which icon to choose based on file extension.
    # If you think a file extension is missing, feel free to add it on PR

    case File.extname(name).downcase
    when '.pdf'
      icon_class = 'file-pdf-o'
    when '.jpg', '.jpeg', '.jif', '.jfif',
        '.jp2', '.jpx', '.j2k', '.j2c',
        '.apng', '.png', '.gif', '.tif', '.tiff',
        '.svg', '.ico', '.bmp', '.webp'
      icon_class = 'file-image-o'
    when '.zip', '.zipx', '.tar', '.gz', '.gzip', '.tgz', '.bz', '.bzip',
        '.bz2', '.bzip2', '.car', '.tbz', '.xz', 'txz', '.rar', '.7z',
        '.lz', '.lzma', '.tlz'
      icon_class = 'file-archive-o'
    when '.mp3', '.wma', '.ogg', '.oga', '.wav', '.flac', '.aac', '.3ga',
        '.ac3', '.midi', '.m4a', '.ape', '.mpa'
      icon_class = 'file-audio-o'
    when '.mp4', '.m4p', '.m4v',
        '.mpg', '.mp2', '.mpeg', '.mpe', '.mpv',
        '.mpg', '.mpeg', '.m2v', '.m2ts',
        '.avi', '.mkv', '.flv', '.ogv', '.mov',
        '.3gp', '.3g2'
      icon_class = 'file-video-o'
    when '.doc', '.dot', '.docx', '.docm', '.dotx', '.dotm', '.docb',
        '.odt', '.ott', '.uot', '.rtf'
      icon_class = 'file-word-o'
    when '.xls', '.xlt', '.xlm', '.xlsx', '.xlsm', '.xltx', '.xltm',
        '.xlsb', '.xla', '.xlam', '.xll', '.xlw', '.ots', '.ods', '.uos'
      icon_class = 'file-excel-o'
    when '.ppt', '.pot', '.pps', '.pptx', '.pptm', '.potx', '.potm',
        '.ppam', '.ppsx', '.ppsm', '.sldx', '.sldm', '.odp', '.otp', '.uop'
      icon_class = 'file-powerpoint-o'
    else
      icon_class = 'file-text-o'
    end
  end

  icon_class
end

#icon(names, options = {}) ⇒ Object

Creates an icon tag given icon name(s) and possible icon modifiers.

Right now this method simply delegates directly to `fa_icon` from the font-awesome-rails gem, but should we ever use a different icon pack in the future we won't have to change hundreds of method calls.


16
17
18
19
20
21
22
23
24
# File 'app/helpers/icons_helper.rb', line 16

def icon(names, options = {})
  if (options.keys & %w[aria-hidden aria-label data-hidden]).empty?
    # Add 'aria-hidden' and 'data-hidden' if they are not set in options.
    options['aria-hidden'] = true
    options['data-hidden'] = true
  end

  options.include?(:base) ? fa_stacked_icon(names, options) : fa_icon(names, options)
end

#loading_icon(container: false, color: 'orange', size: 'sm', css_class: nil) ⇒ Object


66
67
68
69
70
71
72
73
# File 'app/helpers/icons_helper.rb', line 66

def loading_icon(container: false, color: 'orange', size: 'sm', css_class: nil)
  css_classes = ['gl-spinner', "gl-spinner-#{color}", "gl-spinner-#{size}"]
  css_classes << "#{css_class}" unless css_class.blank?

  spinner = (:span, "", { class: css_classes.join(' '), aria: { label: _('Loading') } })

  container == true ? (:div, spinner, { class: 'gl-spinner-container' }) : spinner
end

#spinner(text = nil, visible = false) ⇒ Object


92
93
94
95
96
97
98
99
# File 'app/helpers/icons_helper.rb', line 92

def spinner(text = nil, visible = false)
  css_class = ['loading']
  css_class << 'hide' unless visible

   :div, class: css_class.join(' ') do
    icon('spinner spin') + text
  end
end

#sprite_file_icons_pathObject


41
42
43
44
45
46
# File 'app/helpers/icons_helper.rb', line 41

def sprite_file_icons_path
  # SVG Sprites currently don't work across domains, so in the case of a CDN
  # we have to set the current path deliberately to prevent addition of asset_host
  sprite_base_url = Gitlab.config.gitlab.url if ActionController::Base.asset_host
  ActionController::Base.helpers.image_path('file_icons.svg', host: sprite_base_url)
end

#sprite_icon(icon_name, size: DEFAULT_ICON_SIZE, css_class: nil) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/helpers/icons_helper.rb', line 48

def sprite_icon(icon_name, size: DEFAULT_ICON_SIZE, css_class: nil)
  if known_sprites&.exclude?(icon_name)
    exception = ArgumentError.new("#{icon_name} is not a known icon in @gitlab-org/gitlab-svg")
    Gitlab::ErrorTracking.track_and_raise_for_dev_exception(exception)
  end

  css_classes = []
  css_classes << "s#{size}" if size
  css_classes << "#{css_class}" unless css_class.blank?

  (
    :svg,
    (:use, '', { 'xlink:href' => "#{sprite_icon_path}##{icon_name}" } ),
    class: css_classes.empty? ? nil : css_classes.join(' '),
    data: { testid: "#{icon_name}-icon" }
  )
end

#sprite_icon_pathObject


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

def sprite_icon_path
  @sprite_icon_path ||= begin
    # SVG Sprites currently don't work across domains, so in the case of a CDN
    # we have to set the current path deliberately to prevent addition of asset_host
    sprite_base_url = Gitlab.config.gitlab.url if ActionController::Base.asset_host
    ActionController::Base.helpers.image_path('icons.svg', host: sprite_base_url)
  end
end

#visibility_level_icon(level, options: {}) ⇒ Object


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/helpers/icons_helper.rb', line 109

def visibility_level_icon(level, options: {})
  name =
    case level
    when Gitlab::VisibilityLevel::PRIVATE
      'lock'
    when Gitlab::VisibilityLevel::INTERNAL
      'shield'
    else # Gitlab::VisibilityLevel::PUBLIC
      'earth'
    end

  css_class = options.delete(:class)

  sprite_icon(name, size: DEFAULT_ICON_SIZE, css_class: css_class)
end