debian-mirror-gitlab/app/helpers/icons_helper.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

189 lines
5.6 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2018-11-18 11:00:15 +05:30
require 'json'
2014-09-02 18:07:02 +05:30
module IconsHelper
2017-09-10 17:25:29 +05:30
extend self
2015-09-11 14:41:01 +05:30
2020-10-24 23:57:45 +05:30
DEFAULT_ICON_SIZE = 16
def custom_icon(icon_name, size: DEFAULT_ICON_SIZE)
2021-01-29 00:20:46 +05:30
memoized_icon("#{icon_name}_#{size}") do
2021-12-11 22:18:48 +05:30
render partial: "shared/icons/#{icon_name}", formats: :svg, locals: { size: size }
2021-01-29 00:20:46 +05:30
end
2018-03-17 18:26:18 +05:30
end
def sprite_icon_path
2020-07-28 23:09:34 +05:30
@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
2018-03-17 18:26:18 +05:30
end
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-10-24 23:57:45 +05:30
def sprite_icon(icon_name, size: DEFAULT_ICON_SIZE, css_class: nil)
2021-01-29 00:20:46 +05:30
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-11-18 11:00:15 +05:30
2021-01-29 00:20:46 +05:30
css_classes = []
css_classes << "s#{size}" if size
css_classes << "#{css_class}" unless css_class.blank?
2020-10-24 23:57:45 +05:30
2021-01-29 00:20:46 +05:30
content_tag(
:svg,
2023-01-13 00:05:48 +05:30
content_tag(:use, '', { 'href' => "#{sprite_icon_path}##{icon_name}" }),
2021-01-29 00:20:46 +05:30
class: css_classes.empty? ? nil : css_classes.join(' '),
data: { testid: "#{icon_name}-icon" }
)
end
2018-03-17 18:26:18 +05:30
end
2022-05-07 20:08:51 +05:30
# Creates a GitLab UI loading icon/spinner.
#
# Examples:
# # Default
# gl_loading_icon
#
# # Sizes
# gl_loading_icon(size: 'md')
# gl_loading_icon(size: 'lg')
# gl_loading_icon(size: 'xl')
#
# # Colors
# gl_loading_icon(color: 'light')
#
# # Block/Inline
# gl_loading_icon(inline: true)
#
# # Custom classes
# gl_loading_icon(css_class: "foo-bar")
#
# See also https://gitlab-org.gitlab.io/gitlab-ui/?path=/story/base-loading-icon--default
2023-01-13 00:05:48 +05:30
def gl_loading_icon(inline: false, color: 'dark', size: 'sm', css_class: nil, data: nil)
render Pajamas::SpinnerComponent.new(
inline: inline,
color: color,
size: size,
class: css_class,
data: data
)
2020-07-28 23:09:34 +05:30
end
2018-10-15 14:42:47 +05:30
def external_snippet_icon(name)
content_tag(:span, "", class: "gl-snippet-icon gl-snippet-icon-#{name}")
end
2021-01-29 00:20:46 +05:30
def audit_icon(name, css_class: nil)
case name
2016-06-02 11:05:42 +05:30
when "standard"
2021-01-29 00:20:46 +05:30
name = "key"
2017-09-10 17:25:29 +05:30
when "two-factor"
2021-01-29 00:20:46 +05:30
name = "key"
2018-11-20 20:47:30 +05:30
when "google_oauth2"
2021-01-29 00:20:46 +05:30
name = "google"
2016-06-02 11:05:42 +05:30
end
2021-01-29 00:20:46 +05:30
sprite_icon(name, css_class: css_class)
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
def boolean_to_icon(value)
2015-09-25 12:07:36 +05:30
if value
2021-02-22 17:27:13 +05:30
sprite_icon('check', css_class: 'gl-text-green-500')
2014-09-02 18:07:02 +05:30
else
2021-02-22 17:27:13 +05:30
sprite_icon('power', css_class: 'gl-text-gray-500')
2014-09-02 18:07:02 +05:30
end
end
2020-10-24 23:57:45 +05:30
def visibility_level_icon(level, options: {})
2015-12-23 02:04:40 +05:30
name =
case level
when Gitlab::VisibilityLevel::PRIVATE
'lock'
when Gitlab::VisibilityLevel::INTERNAL
'shield'
else # Gitlab::VisibilityLevel::PUBLIC
2020-10-24 23:57:45 +05:30
'earth'
2015-12-23 02:04:40 +05:30
end
2016-06-02 11:05:42 +05:30
2020-10-24 23:57:45 +05:30
css_class = options.delete(:class)
2015-12-23 02:04:40 +05:30
2020-10-24 23:57:45 +05:30
sprite_icon(name, size: DEFAULT_ICON_SIZE, css_class: css_class)
2015-04-26 12:48:37 +05:30
end
def file_type_icon_class(type, mode, name)
if type == 'folder'
2021-01-03 14:25:43 +05:30
icon_class = 'folder-o'
2018-12-13 13:39:08 +05:30
elsif type == 'archive'
icon_class = 'archive'
2015-04-26 12:48:37 +05:30
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'
2021-01-03 14:25:43 +05:30
icon_class = 'document'
2015-04-26 12:48:37 +05:30
when '.jpg', '.jpeg', '.jif', '.jfif',
'.jp2', '.jpx', '.j2k', '.j2c',
2019-09-30 21:07:59 +05:30
'.apng', '.png', '.gif', '.tif', '.tiff',
'.svg', '.ico', '.bmp', '.webp'
2021-01-03 14:25:43 +05:30
icon_class = 'doc-image'
2019-09-30 21:07:59 +05:30
when '.zip', '.zipx', '.tar', '.gz', '.gzip', '.tgz', '.bz', '.bzip',
'.bz2', '.bzip2', '.car', '.tbz', '.xz', 'txz', '.rar', '.7z',
'.lz', '.lzma', '.tlz'
2021-01-03 14:25:43 +05:30
icon_class = 'doc-compressed'
2019-09-30 21:07:59 +05:30
when '.mp3', '.wma', '.ogg', '.oga', '.wav', '.flac', '.aac', '.3ga',
'.ac3', '.midi', '.m4a', '.ape', '.mpa'
2021-01-03 14:25:43 +05:30
icon_class = 'volume-up'
2015-04-26 12:48:37 +05:30
when '.mp4', '.m4p', '.m4v',
'.mpg', '.mp2', '.mpeg', '.mpe', '.mpv',
2019-09-30 21:07:59 +05:30
'.mpg', '.mpeg', '.m2v', '.m2ts',
2015-04-26 12:48:37 +05:30
'.avi', '.mkv', '.flv', '.ogv', '.mov',
'.3gp', '.3g2'
2021-01-03 14:25:43 +05:30
icon_class = 'live-preview'
2019-09-30 21:07:59 +05:30
when '.doc', '.dot', '.docx', '.docm', '.dotx', '.dotm', '.docb',
'.odt', '.ott', '.uot', '.rtf'
2021-01-03 14:25:43 +05:30
icon_class = 'doc-text'
2015-04-26 12:48:37 +05:30
when '.xls', '.xlt', '.xlm', '.xlsx', '.xlsm', '.xltx', '.xltm',
2019-09-30 21:07:59 +05:30
'.xlsb', '.xla', '.xlam', '.xll', '.xlw', '.ots', '.ods', '.uos'
2021-01-03 14:25:43 +05:30
icon_class = 'document'
2015-04-26 12:48:37 +05:30
when '.ppt', '.pot', '.pps', '.pptx', '.pptm', '.potx', '.potm',
2019-09-30 21:07:59 +05:30
'.ppam', '.ppsx', '.ppsm', '.sldx', '.sldm', '.odp', '.otp', '.uop'
2021-01-03 14:25:43 +05:30
icon_class = 'doc-chart'
2015-04-26 12:48:37 +05:30
else
2021-01-03 14:25:43 +05:30
icon_class = 'doc-text'
2015-04-26 12:48:37 +05:30
end
end
icon_class
2014-09-02 18:07:02 +05:30
end
2018-11-18 11:00:15 +05:30
private
def known_sprites
2020-01-01 13:55:28 +05:30
return if Rails.env.production?
2020-05-24 23:13:21 +05:30
@known_sprites ||= Gitlab::Json.parse(File.read(Rails.root.join('node_modules/@gitlab/svgs/dist/icons.json')))['icons']
2018-11-18 11:00:15 +05:30
end
2021-01-29 00:20:46 +05:30
def memoized_icon(key)
@rendered_icons ||= {}
@rendered_icons[key] || (
@rendered_icons[key] = yield
)
end
2014-09-02 18:07:02 +05:30
end