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

182 lines
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
include FontAwesome::Rails::IconHelper
2020-10-24 23:57:45 +05:30
DEFAULT_ICON_SIZE = 16
2015-04-26 12:48:37 +05:30
# 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.
def icon(names, options = {})
2017-09-10 17:25:29 +05:30
if (options.keys & %w[aria-hidden aria-label data-hidden]).empty?
# Add 'aria-hidden' and 'data-hidden' if they are not set in options.
2017-08-17 22:00:37 +05:30
options['aria-hidden'] = true
2017-09-10 17:25:29 +05:30
options['data-hidden'] = true
2017-08-17 22:00:37 +05:30
end
2016-04-02 18:10:28 +05:30
options.include?(:base) ? fa_stacked_icon(names, options) : fa_icon(names, options)
2015-04-26 12:48:37 +05:30
end
2020-10-24 23:57:45 +05:30
def custom_icon(icon_name, size: DEFAULT_ICON_SIZE)
2018-03-17 18:26:18 +05:30
# 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
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)
2020-01-01 13:55:28 +05:30
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)
2018-11-18 11:00:15 +05:30
end
2018-12-05 23:21:45 +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
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" }
)
2018-03-17 18:26:18 +05:30
end
2020-07-28 23:09:34 +05:30
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-10-15 14:42:47 +05:30
def external_snippet_icon(name)
content_tag(:span, "", class: "gl-snippet-icon gl-snippet-icon-#{name}")
end
2016-06-02 11:05:42 +05:30
def audit_icon(names, options = {})
case names
when "standard"
names = "key"
2017-09-10 17:25:29 +05:30
when "two-factor"
names = "key"
2018-11-20 20:47:30 +05:30
when "google_oauth2"
names = "google"
2016-06-02 11:05:42 +05:30
end
options.include?(:base) ? fa_stacked_icon(names, options) : fa_icon(names, options)
end
2015-04-26 12:48:37 +05:30
def spinner(text = nil, visible = false)
2018-12-05 23:21:45 +05:30
css_class = ['loading']
css_class << 'hide' unless visible
2015-04-26 12:48:37 +05:30
2018-12-05 23:21:45 +05:30
content_tag :div, class: css_class.join(' ') do
2015-04-26 12:48:37 +05:30
icon('spinner spin') + text
end
end
2014-09-02 18:07:02 +05:30
def boolean_to_icon(value)
2015-09-25 12:07:36 +05:30
if value
2020-11-24 15:15:51 +05:30
sprite_icon('check', css_class: 'cgreen')
2014-09-02 18:07:02 +05:30
else
2020-10-24 23:57:45 +05:30
sprite_icon('power', css_class: 'clgray')
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
2014-09-02 18:07:02 +05:30
end