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