2018-08-18 11:19:57 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-07-18 16:56:19 +00:00
|
|
|
require 'json'
|
|
|
|
|
2013-11-20 11:41:41 +00:00
|
|
|
module IconsHelper
|
2017-05-26 18:51:24 +00:00
|
|
|
extend self
|
2015-05-21 21:49:06 +00:00
|
|
|
|
2020-07-23 00:09:43 +00:00
|
|
|
DEFAULT_ICON_SIZE = 16
|
|
|
|
|
|
|
|
def custom_icon(icon_name, size: DEFAULT_ICON_SIZE)
|
2020-11-11 18:09:10 +00:00
|
|
|
memoized_icon("#{icon_name}_#{size}") do
|
|
|
|
# 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
|
2017-09-22 08:39:47 +00:00
|
|
|
end
|
|
|
|
|
2017-11-09 09:38:00 +00:00
|
|
|
def sprite_icon_path
|
2020-07-14 12:09:14 +00:00
|
|
|
@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
|
2017-11-09 09:38:00 +00:00
|
|
|
end
|
|
|
|
|
2018-01-03 10:08:14 +00:00
|
|
|
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
|
|
|
|
|
2020-08-03 15:09:44 +00:00
|
|
|
def sprite_icon(icon_name, size: DEFAULT_ICON_SIZE, css_class: nil)
|
2020-11-11 18:09:10 +00:00
|
|
|
memoized_icon("#{icon_name}_#{size}_#{css_class}") do
|
|
|
|
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
|
2018-07-18 16:56:19 +00:00
|
|
|
|
2020-11-11 18:09:10 +00:00
|
|
|
css_classes = []
|
|
|
|
css_classes << "s#{size}" if size
|
|
|
|
css_classes << "#{css_class}" unless css_class.blank?
|
2020-07-23 00:09:43 +00:00
|
|
|
|
2020-11-11 18:09:10 +00:00
|
|
|
content_tag(
|
|
|
|
:svg,
|
|
|
|
content_tag(:use, '', { 'xlink:href' => "#{sprite_icon_path}##{icon_name}" } ),
|
|
|
|
class: css_classes.empty? ? nil : css_classes.join(' '),
|
|
|
|
data: { testid: "#{icon_name}-icon" }
|
|
|
|
)
|
|
|
|
end
|
2017-09-22 08:39:47 +00:00
|
|
|
end
|
|
|
|
|
2020-07-13 21:09:24 +00:00
|
|
|
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 = content_tag(:span, "", { class: css_classes.join(' '), aria: { label: _('Loading') } })
|
|
|
|
|
|
|
|
container == true ? content_tag(:div, spinner, { class: 'gl-spinner-container' }) : spinner
|
|
|
|
end
|
|
|
|
|
2018-02-06 13:33:18 +00:00
|
|
|
def external_snippet_icon(name)
|
2018-03-03 09:23:42 +00:00
|
|
|
content_tag(:span, "", class: "gl-snippet-icon gl-snippet-icon-#{name}")
|
2018-02-06 13:33:18 +00:00
|
|
|
end
|
|
|
|
|
2020-10-20 18:08:54 +00:00
|
|
|
def audit_icon(name, css_class: nil)
|
|
|
|
case name
|
2016-03-01 09:11:46 +00:00
|
|
|
when "standard"
|
2020-10-20 18:08:54 +00:00
|
|
|
name = "key"
|
2017-05-16 12:54:57 +00:00
|
|
|
when "two-factor"
|
2020-10-20 18:08:54 +00:00
|
|
|
name = "key"
|
2018-08-14 19:12:07 +00:00
|
|
|
when "google_oauth2"
|
2020-10-20 18:08:54 +00:00
|
|
|
name = "google"
|
2016-03-01 09:11:46 +00:00
|
|
|
end
|
|
|
|
|
2020-10-20 18:08:54 +00:00
|
|
|
sprite_icon(name, css_class: css_class)
|
2016-03-01 09:11:46 +00:00
|
|
|
end
|
|
|
|
|
2013-11-20 11:41:41 +00:00
|
|
|
def boolean_to_icon(value)
|
2015-08-20 21:03:09 +00:00
|
|
|
if value
|
2020-12-11 18:09:57 +00:00
|
|
|
sprite_icon('check', css_class: 'gl-text-green-500')
|
2013-11-20 11:41:41 +00:00
|
|
|
else
|
2020-12-11 18:09:57 +00:00
|
|
|
sprite_icon('power', css_class: 'gl-text-gray-500')
|
2013-11-20 11:41:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-23 00:09:43 +00:00
|
|
|
def visibility_level_icon(level, options: {})
|
2015-12-02 13:08:59 +00:00
|
|
|
name =
|
|
|
|
case level
|
|
|
|
when Gitlab::VisibilityLevel::PRIVATE
|
|
|
|
'lock'
|
|
|
|
when Gitlab::VisibilityLevel::INTERNAL
|
|
|
|
'shield'
|
|
|
|
else # Gitlab::VisibilityLevel::PUBLIC
|
2020-07-23 00:09:43 +00:00
|
|
|
'earth'
|
2015-12-02 13:08:59 +00:00
|
|
|
end
|
2016-03-01 09:11:46 +00:00
|
|
|
|
2020-08-03 15:09:44 +00:00
|
|
|
css_class = options.delete(:class)
|
|
|
|
|
|
|
|
sprite_icon(name, size: DEFAULT_ICON_SIZE, css_class: css_class)
|
2013-11-20 11:41:41 +00:00
|
|
|
end
|
2014-10-04 10:29:18 +00:00
|
|
|
|
|
|
|
def file_type_icon_class(type, mode, name)
|
|
|
|
if type == 'folder'
|
2020-10-08 15:08:17 +00:00
|
|
|
icon_class = 'folder-o'
|
2018-10-18 00:03:15 +00:00
|
|
|
elsif type == 'archive'
|
|
|
|
icon_class = 'archive'
|
2015-04-02 03:32:16 +00:00
|
|
|
elsif mode == '120000'
|
2014-10-04 10:29:18 +00:00
|
|
|
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'
|
2020-10-08 15:08:17 +00:00
|
|
|
icon_class = 'document'
|
2014-10-04 10:29:18 +00:00
|
|
|
when '.jpg', '.jpeg', '.jif', '.jfif',
|
|
|
|
'.jp2', '.jpx', '.j2k', '.j2c',
|
2019-07-15 02:53:02 +00:00
|
|
|
'.apng', '.png', '.gif', '.tif', '.tiff',
|
|
|
|
'.svg', '.ico', '.bmp', '.webp'
|
2020-10-08 15:08:17 +00:00
|
|
|
icon_class = 'doc-image'
|
2019-07-15 02:53:02 +00:00
|
|
|
when '.zip', '.zipx', '.tar', '.gz', '.gzip', '.tgz', '.bz', '.bzip',
|
|
|
|
'.bz2', '.bzip2', '.car', '.tbz', '.xz', 'txz', '.rar', '.7z',
|
|
|
|
'.lz', '.lzma', '.tlz'
|
2020-10-08 15:08:17 +00:00
|
|
|
icon_class = 'doc-compressed'
|
2019-07-15 02:53:02 +00:00
|
|
|
when '.mp3', '.wma', '.ogg', '.oga', '.wav', '.flac', '.aac', '.3ga',
|
|
|
|
'.ac3', '.midi', '.m4a', '.ape', '.mpa'
|
2020-10-08 15:08:17 +00:00
|
|
|
icon_class = 'volume-up'
|
2014-10-04 10:29:18 +00:00
|
|
|
when '.mp4', '.m4p', '.m4v',
|
|
|
|
'.mpg', '.mp2', '.mpeg', '.mpe', '.mpv',
|
2019-07-15 02:53:02 +00:00
|
|
|
'.mpg', '.mpeg', '.m2v', '.m2ts',
|
2014-10-04 10:29:18 +00:00
|
|
|
'.avi', '.mkv', '.flv', '.ogv', '.mov',
|
|
|
|
'.3gp', '.3g2'
|
2020-10-08 15:08:17 +00:00
|
|
|
icon_class = 'live-preview'
|
2019-07-15 02:53:02 +00:00
|
|
|
when '.doc', '.dot', '.docx', '.docm', '.dotx', '.dotm', '.docb',
|
|
|
|
'.odt', '.ott', '.uot', '.rtf'
|
2020-10-08 15:08:17 +00:00
|
|
|
icon_class = 'doc-text'
|
2014-10-04 10:29:18 +00:00
|
|
|
when '.xls', '.xlt', '.xlm', '.xlsx', '.xlsm', '.xltx', '.xltm',
|
2019-07-15 02:53:02 +00:00
|
|
|
'.xlsb', '.xla', '.xlam', '.xll', '.xlw', '.ots', '.ods', '.uos'
|
2020-10-08 15:08:17 +00:00
|
|
|
icon_class = 'document'
|
2014-10-04 10:29:18 +00:00
|
|
|
when '.ppt', '.pot', '.pps', '.pptx', '.pptm', '.potx', '.potm',
|
2019-07-15 02:53:02 +00:00
|
|
|
'.ppam', '.ppsx', '.ppsm', '.sldx', '.sldm', '.odp', '.otp', '.uop'
|
2020-10-08 15:08:17 +00:00
|
|
|
icon_class = 'doc-chart'
|
2014-10-04 10:29:18 +00:00
|
|
|
else
|
2020-10-08 15:08:17 +00:00
|
|
|
icon_class = 'doc-text'
|
2014-10-04 10:29:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
icon_class
|
|
|
|
end
|
2018-07-18 16:56:19 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def known_sprites
|
2019-12-14 18:07:40 +00:00
|
|
|
return if Rails.env.production?
|
|
|
|
|
2020-04-30 18:09:38 +00:00
|
|
|
@known_sprites ||= Gitlab::Json.parse(File.read(Rails.root.join('node_modules/@gitlab/svgs/dist/icons.json')))['icons']
|
2018-07-18 16:56:19 +00:00
|
|
|
end
|
2020-11-11 18:09:10 +00:00
|
|
|
|
|
|
|
def memoized_icon(key)
|
|
|
|
@rendered_icons ||= {}
|
|
|
|
|
|
|
|
@rendered_icons[key] || (
|
|
|
|
@rendered_icons[key] = yield
|
|
|
|
)
|
|
|
|
end
|
2013-11-20 11:41:41 +00:00
|
|
|
end
|