Project

General

Profile

Feature #43087 » icons_helper.rb

Catirau Mihail, 2025-08-01 14:59

 
1
# frozen_string_literal: true
2

    
3
# Redmine - project management software
4
# Copyright (C) 2006-  Jean-Philippe Lang
5
#
6
# This program is free software; you can redistribute it and/or
7
# modify it under the terms of the GNU General Public License
8
# as published by the Free Software Foundation; either version 2
9
# of the License, or (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19

    
20
module IconsHelper
21
  DEFAULT_ICON_SIZE = "18"
22
  DEFAULT_SPRITE = "icons"
23

    
24
  include Redmine::Themes::Helper
25

    
26
  def sprite_source(sprite: DEFAULT_SPRITE, plugin: nil)
27
    if plugin
28
      "plugin_assets/#{plugin}/#{sprite}.svg"
29
    elsif current_theme && current_theme.images.include?("#{sprite}.svg")
30
      current_theme.image_path("#{sprite}.svg")
31
    else
32
      "#{sprite}.svg"
33
    end
34
  end
35

    
36
  def sprite_icon(icon_name, label = nil, icon_only: false, size: DEFAULT_ICON_SIZE, style: :outline, css_class: nil, sprite: DEFAULT_SPRITE, plugin: nil, rtl: false)
37
    sprite = sprite_source(sprite: sprite, plugin: plugin)
38

    
39
    svg_icon = svg_sprite_icon(icon_name, size: size, style: style, css_class: css_class, sprite: sprite, rtl: rtl)
40

    
41
    if label
42
      label_classes = ["icon-label"]
43
      label_classes << "hidden" if icon_only
44

    
45
      svg_icon + content_tag(:span, label, class: label_classes.join(' '))
46
    else
47
      svg_icon
48
    end
49
  end
50

    
51
  def file_icon(entry, name, **)
52
    if entry.is_dir?
53
      sprite_icon("folder", name, **)
54
    else
55
      icon_name = icon_for_mime_type(Redmine::MimeType.css_class_of(name))
56
      sprite_icon(icon_name, name, **)
57
    end
58
  end
59

    
60
  def principal_icon(principal, **)
61
    raise ArgumentError, "First argument has to be a Principal, was #{principal.inspect}" unless principal.is_a?(Principal)
62

    
63
    principal_class = principal.class.name.downcase
64
    sprite_icon('group', **) if ['groupanonymous', 'groupnonmember', 'group'].include?(principal_class)
65
  end
66

    
67
  def activity_event_type_icon(event_type, **)
68
    icon_name = case event_type
69
                when 'reply'
70
                  'comments'
71
                when 'time-entry'
72
                  'time'
73
                when 'message'
74
                  'comment'
75
                else
76
                  event_type
77
                end
78

    
79
    sprite_icon(icon_name, **)
80
  end
81

    
82
  def scm_change_icon(action, name, **options)
83
    icon_name = case action
84
                when 'A'
85
                  "add"
86
                when 'D'
87
                  "circle-minus"
88
                else
89
                  "circle-dot-filled"
90
                end
91
    sprite_icon(icon_name, name, size: 14)
92
  end
93

    
94
  def notice_icon(type, **)
95
    icon_name = case type
96
                when 'notice'
97
                  'checked'
98
                when 'warning', 'error'
99
                  'warning'
100
                end
101

    
102
    sprite_icon(icon_name, **)
103
  end
104

    
105
  private
106

    
107
  def svg_sprite_icon(icon_name, size: DEFAULT_ICON_SIZE, style: :outline, sprite: DEFAULT_SPRITE, css_class: nil, rtl: false)
108
    css_classes = "s#{size} icon-svg"
109
    css_classes += " icon-svg-filled" if style == :filled
110
    css_classes += " #{css_class}" unless css_class.nil?
111
    css_classes += " icon-rtl" if rtl
112

    
113
    content_tag(
114
      :svg,
115
      content_tag(:use, '', { 'href' => "#{asset_path(sprite)}#icon--#{icon_name}" }),
116
      class: css_classes,
117
      aria: {
118
        hidden: true
119
      }
120
    )
121
  end
122

    
123
  def icon_for_mime_type(mime)
124
    if %w(text-plain text-x-c text-x-csharp text-x-java text-x-php
125
          text-x-ruby text-xml text-css text-html text-css text-html
126
          image-gif image-jpeg image-png image-tiff
127
          application-pdf application-zip application-gzip application-javascript).include?(mime)
128
      mime
129
    else
130
      "file"
131
    end
132
  end
133
end
(1-1/2)