PageRenderTime 29ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/helpers/icons_helper.rb

https://gitlab.com/wolfgang42/gitlab-ce
Ruby | 163 lines | 125 code | 23 blank | 15 comment | 11 complexity | f738c44a1f60bcbfdc4e8dc9293c1c32 MD5 | raw file
  1. # frozen_string_literal: true
  2. require 'json'
  3. module IconsHelper
  4. extend self
  5. include FontAwesome::Rails::IconHelper
  6. # Creates an icon tag given icon name(s) and possible icon modifiers.
  7. #
  8. # Right now this method simply delegates directly to `fa_icon` from the
  9. # font-awesome-rails gem, but should we ever use a different icon pack in the
  10. # future we won't have to change hundreds of method calls.
  11. def icon(names, options = {})
  12. if (options.keys & %w[aria-hidden aria-label data-hidden]).empty?
  13. # Add 'aria-hidden' and 'data-hidden' if they are not set in options.
  14. options['aria-hidden'] = true
  15. options['data-hidden'] = true
  16. end
  17. options.include?(:base) ? fa_stacked_icon(names, options) : fa_icon(names, options)
  18. end
  19. def custom_icon(icon_name, size: 16)
  20. # We can't simply do the below, because there are some .erb SVGs.
  21. # File.read(Rails.root.join("app/views/shared/icons/_#{icon_name}.svg")).html_safe
  22. render "shared/icons/#{icon_name}.svg", size: size
  23. end
  24. def sprite_icon_path
  25. # SVG Sprites currently don't work across domains, so in the case of a CDN
  26. # we have to set the current path deliberately to prevent addition of asset_host
  27. sprite_base_url = Gitlab.config.gitlab.url if ActionController::Base.asset_host
  28. ActionController::Base.helpers.image_path('icons.svg', host: sprite_base_url)
  29. end
  30. def sprite_file_icons_path
  31. # SVG Sprites currently don't work across domains, so in the case of a CDN
  32. # we have to set the current path deliberately to prevent addition of asset_host
  33. sprite_base_url = Gitlab.config.gitlab.url if ActionController::Base.asset_host
  34. ActionController::Base.helpers.image_path('file_icons.svg', host: sprite_base_url)
  35. end
  36. def sprite_icon(icon_name, size: nil, css_class: nil)
  37. if Gitlab::Sentry.should_raise_for_dev?
  38. unless known_sprites.include?(icon_name)
  39. exception = ArgumentError.new("#{icon_name} is not a known icon in @gitlab-org/gitlab-svg")
  40. raise exception
  41. end
  42. end
  43. css_classes = []
  44. css_classes << "s#{size}" if size
  45. css_classes << "#{css_class}" unless css_class.blank?
  46. content_tag(:svg, content_tag(:use, "", { "xlink:href" => "#{sprite_icon_path}##{icon_name}" } ), class: css_classes.empty? ? nil : css_classes.join(' '))
  47. end
  48. def external_snippet_icon(name)
  49. content_tag(:span, "", class: "gl-snippet-icon gl-snippet-icon-#{name}")
  50. end
  51. def audit_icon(names, options = {})
  52. case names
  53. when "standard"
  54. names = "key"
  55. when "two-factor"
  56. names = "key"
  57. when "google_oauth2"
  58. names = "google"
  59. end
  60. options.include?(:base) ? fa_stacked_icon(names, options) : fa_icon(names, options)
  61. end
  62. def spinner(text = nil, visible = false)
  63. css_class = ['loading']
  64. css_class << 'hide' unless visible
  65. content_tag :div, class: css_class.join(' ') do
  66. icon('spinner spin') + text
  67. end
  68. end
  69. def boolean_to_icon(value)
  70. if value
  71. icon('circle', class: 'cgreen')
  72. else
  73. icon('power-off', class: 'clgray')
  74. end
  75. end
  76. def visibility_level_icon(level, fw: true, options: {})
  77. name =
  78. case level
  79. when Gitlab::VisibilityLevel::PRIVATE
  80. 'lock'
  81. when Gitlab::VisibilityLevel::INTERNAL
  82. 'shield'
  83. else # Gitlab::VisibilityLevel::PUBLIC
  84. 'globe'
  85. end
  86. name = [name]
  87. name << "fw" if fw
  88. icon(name.join(' '), options)
  89. end
  90. def file_type_icon_class(type, mode, name)
  91. if type == 'folder'
  92. icon_class = 'folder'
  93. elsif type == 'archive'
  94. icon_class = 'archive'
  95. elsif mode == '120000'
  96. icon_class = 'share'
  97. else
  98. # Guess which icon to choose based on file extension.
  99. # If you think a file extension is missing, feel free to add it on PR
  100. case File.extname(name).downcase
  101. when '.pdf'
  102. icon_class = 'file-pdf-o'
  103. when '.jpg', '.jpeg', '.jif', '.jfif',
  104. '.jp2', '.jpx', '.j2k', '.j2c',
  105. '.apng', '.png', '.gif', '.tif', '.tiff',
  106. '.svg', '.ico', '.bmp', '.webp'
  107. icon_class = 'file-image-o'
  108. when '.zip', '.zipx', '.tar', '.gz', '.gzip', '.tgz', '.bz', '.bzip',
  109. '.bz2', '.bzip2', '.car', '.tbz', '.xz', 'txz', '.rar', '.7z',
  110. '.lz', '.lzma', '.tlz'
  111. icon_class = 'file-archive-o'
  112. when '.mp3', '.wma', '.ogg', '.oga', '.wav', '.flac', '.aac', '.3ga',
  113. '.ac3', '.midi', '.m4a', '.ape', '.mpa'
  114. icon_class = 'file-audio-o'
  115. when '.mp4', '.m4p', '.m4v',
  116. '.mpg', '.mp2', '.mpeg', '.mpe', '.mpv',
  117. '.mpg', '.mpeg', '.m2v', '.m2ts',
  118. '.avi', '.mkv', '.flv', '.ogv', '.mov',
  119. '.3gp', '.3g2'
  120. icon_class = 'file-video-o'
  121. when '.doc', '.dot', '.docx', '.docm', '.dotx', '.dotm', '.docb',
  122. '.odt', '.ott', '.uot', '.rtf'
  123. icon_class = 'file-word-o'
  124. when '.xls', '.xlt', '.xlm', '.xlsx', '.xlsm', '.xltx', '.xltm',
  125. '.xlsb', '.xla', '.xlam', '.xll', '.xlw', '.ots', '.ods', '.uos'
  126. icon_class = 'file-excel-o'
  127. when '.ppt', '.pot', '.pps', '.pptx', '.pptm', '.potx', '.potm',
  128. '.ppam', '.ppsx', '.ppsm', '.sldx', '.sldm', '.odp', '.otp', '.uop'
  129. icon_class = 'file-powerpoint-o'
  130. else
  131. icon_class = 'file-text-o'
  132. end
  133. end
  134. icon_class
  135. end
  136. private
  137. def known_sprites
  138. @known_sprites ||= JSON.parse(File.read(Rails.root.join('node_modules/@gitlab/svgs/dist/icons.json')))['icons']
  139. end
  140. end