Project

General

Profile

Feature #43797 » 0002-Update-attachments-list-to-show-MIME-type-icons-inst.patch

Go MAEDA, 2026-02-06 10:45

View differences:

app/assets/javascripts/attachments.js
80 80
        ajaxUpload.uploading--;
81 81
        fileSpan.removeClass('ajax-loading');
82 82
        attachmentIcon.removeClass('svg-loader');
83
        updateSVGIcon(attachmentIcon[0], 'attachment');
83
        updateSVGIcon(attachmentIcon[0], 'file');
84 84
        var form = fileSpan.parents('form');
85 85
        if (form.queue('upload').length == 0 && ajaxUpload.uploading == 0) {
86 86
          $('input:submit', form).removeAttr('disabled');
app/helpers/icons_helper.rb
90 90
    sprite_icon(icon_name, **)
91 91
  end
92 92

  
93
  def mime_type_icon(mime_type, ...)
94
    icon_name = icon_for_mime_type(mime_type)
95
    sprite_icon(icon_name, ...)
96
  end
97

  
98
  def icon_for_mime_type(mime)
99
    if %w(text/plain text/x-c text/x-csharp text/x-java text/x-php
100
          text/x-ruby text/xml text/css text/html text/css text/html
101
          image/gif image/jpeg image/png image/tiff
102
          application/pdf application/zip application/gzip application/javascript).include?(mime)
103
      mime.tr('/', '-')
104
    else
105
      "file"
106
    end
107
  end
108

  
93 109
  private
94 110

  
95 111
  def svg_sprite_icon(icon_name, size: DEFAULT_ICON_SIZE, style: :outline, sprite: DEFAULT_SPRITE, css_class: nil, rtl: false)
......
107 123
      }
108 124
    )
109 125
  end
110

  
111
  def icon_for_mime_type(mime)
112
    if %w(text/plain text/x-c text/x-csharp text/x-java text/x-php
113
          text/x-ruby text/xml text/css text/html text/css text/html
114
          image/gif image/jpeg image/png image/tiff
115
          application/pdf application/zip application/gzip application/javascript).include?(mime)
116
      mime.tr('/', '-')
117
    else
118
      "file"
119
    end
120
  end
121 126
end
app/views/attachments/_form.html.erb
9 9
<span class="attachments_form">
10 10
  <span class="attachments_icons hidden">
11 11
    <%= sprite_icon('del', icon_only: true, css_class: 'svg-del') %>
12
    <%= sprite_icon('attachment', icon_only: true, size: 16, css_class: 'svg-attachment') %>
12
    <%= sprite_icon('file', icon_only: true, size: 16, css_class: 'svg-attachment') %>
13 13
    <%= sprite_icon('loader', icon_only: true, size: 16, css_class: 'svg-loader') %>
14 14
    <%= sprite_icon('hourglass', icon_only: true, size: 16, css_class: 'svg-hourglass') %>
15 15
  </span>
......
17 17
  <% if saved_attachments.present? %>
18 18
    <% saved_attachments.each_with_index do |attachment, i| %>
19 19
      <span id="attachments_p<%= i %>">
20
        <%= sprite_icon('attachment', icon_only: true, size: 16, css_class: 'svg-attachment') %>
20
        <%= sprite_icon('file', icon_only: true, size: 16, css_class: 'svg-attachment') %>
21 21
        <%= text_field_tag("#{attachment_param}[p#{i}][filename]", attachment.filename, :class => 'filename') %>
22 22
        <% if attachment.container_id.present? %>
23 23
          <%= link_to sprite_icon('del', l(:button_delete), icon_only: true), "#", :onclick => "$(this).closest('.attachments_form').find('.add_attachment').show(); $(this).parent().remove(); return false;", :class => 'icon-only icon-del' %>
app/views/attachments/_links.html.erb
15 15
<% for attachment in attachments %>
16 16
<tr>
17 17
  <td>
18
    <%= link_to_attachment attachment, class: 'icon icon-attachment ', icon: 'attachment' -%>
18
    <%= link_to_attachment attachment, class: 'icon icon-attachment ', icon: icon_for_mime_type(attachment.content_type) -%>
19 19
    <span class="size">(<%= number_to_human_size attachment.filesize %>)</span>
20 20
    <%= link_to_attachment attachment, class: 'icon-only icon-download ', title: l(:button_download), download: true, icon: 'download' -%>
21 21
  </td>
app/views/attachments/edit_all.html.erb
11 11
  <% @attachments.each do |attachment| %>
12 12
    <tr>
13 13
      <td colspan="2">
14
        <span class="icon icon-attachment"><%= sprite_icon('attachment', attachment.filename_was) %></span>
14
        <span class="icon icon-attachment"><%= mime_type_icon(attachment.content_type_was, attachment.filename_was) %></span>
15 15
        <span class="size">(<%= number_to_human_size attachment.filesize %>)</span>
16 16
        <span class="author"><%= attachment.author %>, <%= format_time(attachment.created_on) %></span>
17 17
      </td>
app/views/issues/_edit.html.erb
54 54
        <div id="existing-attachments" style="<%= @issue.deleted_attachment_ids.blank? ? 'display:none;' : '' %>">
55 55
          <% @issue.attachments.each do |attachment| %>
56 56
          <span class="existing-attachment">
57
            <%= sprite_icon('attachment', size: 12) %>
57
            <%= mime_type_icon(attachment.content_type, size: 12) %>
58 58
            <%= text_field_tag '', attachment.filename, :class => "icon icon-attachment filename", :disabled => true %>
59 59
            <label>
60 60
              <%= check_box_tag 'issue[deleted_attachment_ids][]',
app/views/wiki/edit.html.erb
41 41
<div id="existing-attachments" style="<%= @page.deleted_attachment_ids.blank? ? 'display:none;' : '' %>">
42 42
  <% @page.attachments.each do |attachment| %>
43 43
  <span class="existing-attachment">
44
    <%= sprite_icon('attachment', size: 12) %>
44
    <%= mime_type_icon(attachment.content_type, size: 12) %>
45 45
    <%= text_field_tag '', attachment.filename, :class => "icon icon-attachment filename", :disabled => true %>
46 46
    <label class='inline'>
47 47
      <%= check_box_tag 'wiki_page[deleted_attachment_ids][]',
test/functional/issues_controller_test.rb
2253 2253
    end
2254 2254
  end
2255 2255

  
2256
  def test_show_should_display_attachment_icons_by_mime_type
2257
    @request.session[:user_id] = 2
2258
    get(:show, :params => {:id => 3})
2259

  
2260
    assert_response :success
2261
    assert_select 'div.attachments' do
2262
      assert_select 'a.icon-attachment[href=?]', '/attachments/1' do
2263
        assert_select "svg.icon-svg use:match('href', ?)", /assets\/icons-\w+.svg#icon--text-plain/
2264
      end
2265
      assert_select 'a.icon-attachment[href=?]', '/attachments/6' do
2266
        assert_select "svg.icon-svg use:match('href', ?)", /assets\/icons-\w+.svg#icon--application-zip/
2267
      end
2268
    end
2269
  end
2270

  
2256 2271
  def test_show_should_display_update_form
2257 2272
    @request.session[:user_id] = 2
2258 2273
    get(:show, :params => {:id => 1})
test/helpers/icons_helper_test.rb
124 124
    assert_match expected, activity_event_type_icon('time-entry')
125 125
  end
126 126

  
127
  def test_mime_type_icon_should_return_specific_icon_for_known_mime_types
128
    expected = %r{<svg class="s18 icon-svg" aria-hidden="true"><use href="/assets/icons-\w+.svg#icon--text-plain"></use></svg>}
129
    assert_match expected, mime_type_icon('text/plain')
130
  end
131

  
132
  def test_mime_type_icon_should_return_generic_file_icon_for_unknown_mime_types
133
    expected = %r{<svg class="s18 icon-svg" aria-hidden="true"><use href="/assets/icons-\w+.svg#icon--file"></use></svg>}
134
    assert_match expected, mime_type_icon('unknown-type/unknown-subtype')
135
  end
136

  
127 137
  def test_icon_for_mime_type_should_return_specific_icon_for_known_mime_types
128 138
    assert_equal 'text-plain', icon_for_mime_type('text/plain')
129 139
    assert_equal 'application-pdf', icon_for_mime_type('application/pdf')
(4-4/4)