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

164 lines
5.4 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
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
2018-03-17 18:26:18 +05:30
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
def sprite_icon_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('icons.svg', host: sprite_base_url)
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
def sprite_icon(icon_name, size: nil, css_class: nil)
2019-02-15 15:39:39 +05:30
if Gitlab::Sentry.should_raise_for_dev?
2018-11-18 11:00:15 +05:30
unless known_sprites.include?(icon_name)
exception = ArgumentError.new("#{icon_name} is not a known icon in @gitlab-org/gitlab-svg")
raise exception
end
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?
content_tag(:svg, content_tag(:use, "", { "xlink:href" => "#{sprite_icon_path}##{icon_name}" } ), class: css_classes.empty? ? nil : css_classes.join(' '))
2018-03-17 18:26:18 +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
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
2015-04-26 12:48:37 +05:30
icon('circle', class: 'cgreen')
2014-09-02 18:07:02 +05:30
else
2015-04-26 12:48:37 +05:30
icon('power-off', class: 'clgray')
2014-09-02 18:07:02 +05:30
end
end
2018-11-20 20:47:30 +05:30
def visibility_level_icon(level, fw: true, 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
'globe'
end
2016-06-02 11:05:42 +05:30
2018-12-05 23:21:45 +05:30
name = [name]
name << "fw" if fw
2015-12-23 02:04:40 +05:30
2018-12-05 23:21:45 +05:30
icon(name.join(' '), options)
2015-04-26 12:48:37 +05:30
end
def file_type_icon_class(type, mode, name)
if type == 'folder'
icon_class = 'folder'
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'
icon_class = 'file-pdf-o'
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'
2015-04-26 12:48:37 +05:30
icon_class = 'file-image-o'
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'
2015-04-26 12:48:37 +05:30
icon_class = 'file-archive-o'
2019-09-30 21:07:59 +05:30
when '.mp3', '.wma', '.ogg', '.oga', '.wav', '.flac', '.aac', '.3ga',
'.ac3', '.midi', '.m4a', '.ape', '.mpa'
2015-04-26 12:48:37 +05:30
icon_class = 'file-audio-o'
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'
icon_class = 'file-video-o'
2019-09-30 21:07:59 +05:30
when '.doc', '.dot', '.docx', '.docm', '.dotx', '.dotm', '.docb',
'.odt', '.ott', '.uot', '.rtf'
2015-04-26 12:48:37 +05:30
icon_class = 'file-word-o'
when '.xls', '.xlt', '.xlm', '.xlsx', '.xlsm', '.xltx', '.xltm',
2019-09-30 21:07:59 +05:30
'.xlsb', '.xla', '.xlam', '.xll', '.xlw', '.ots', '.ods', '.uos'
2015-04-26 12:48:37 +05:30
icon_class = 'file-excel-o'
when '.ppt', '.pot', '.pps', '.pptx', '.pptm', '.potx', '.potm',
2019-09-30 21:07:59 +05:30
'.ppam', '.ppsx', '.ppsm', '.sldx', '.sldm', '.odp', '.otp', '.uop'
2015-04-26 12:48:37 +05:30
icon_class = 'file-powerpoint-o'
else
icon_class = 'file-text-o'
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
2018-12-13 13:39:08 +05:30
@known_sprites ||= 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