Skip to content

Commit f3403d4

Browse files
committed
[Fixes browsermedia#656] Multiple Attachments widget
* Upload Attachment now opens a modal dialog. * Show attachments in properly styled table. * Constraint attachment thumbnails size in preview table. * Add Delete button for each row. * Show message if there are no attachments. * Delete attachment no longer uses deprecated link_to_function from delete (deprecated) * Preview image opens a new tab (allows New blocks to continue safely) * Removed custom 'loading' image (should have a CMS wide consistent one if needed) * Add Never Mind button for modal. * Fixed Jquery missing errors for inline page editing. Catalog Updates: * Catalog - Slug is now correctly based of the name field.
1 parent 5297d22 commit f3403d4

File tree

13 files changed

+93
-56
lines changed

13 files changed

+93
-56
lines changed

app/assets/javascripts/cms/attachment_manager.js.erb renamed to app/assets/javascripts/cms/attachment_manager.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
$(function () {
44
$.cms.AttachmentManager = {
55
upload:function (file) {
6-
var assetName = $('#asset_types').val()
6+
var assetName = $('#attachment_attachable_type').val()
77
, attachableClass = $('#asset_attachable_class').val()
8-
, attachableIDField = $('#asset_attachable_id')
9-
, attachableID = attachableIDField.val()
8+
// , attachableIDField = $('#asset_attachable_id')
9+
, attachableID = $('#asset_attachable_id').val()
1010
, file = $('#asset_add_file')
11-
, clone = file.clone()
1211
, form = $('<form target="asset_add_uploader" method="post" action="/cms/attachments" enctype="multipart/form-data" style="visibility:hidden">')
1312
, fields = '';
1413

@@ -21,21 +20,24 @@ $(function () {
2120
$('body').append(form);
2221
form.append(fields);
2322
form.append(file);
24-
attachableIDField.after(clone);
23+
$('label[for="asset_add_file"]').after(file.clone());
2524

2625
var inp = $('<input type="file" name="attachment[data]" id="asset_add_file" onchange="$.cms.AttachmentManager.upload(this)" />');
2726

28-
clone.after($('<img>')
29-
.css({position:'static', margin:'5px 0 0 5px', 'verticalAlign':'middle'})
30-
.attr({'id':'file-asset-uploader', 'src':'<%= asset_path 'cms/file-uploading.gif' %>'}));
31-
clone.replaceWith(inp);
32-
3327
form.submit();
3428

35-
$('div.buttons').hide();
36-
29+
$('#upload-attachment').modal('hide');
30+
},
31+
enableDeleteButtons:function(){
32+
// Handle delete attachment button
33+
var delete_attachments_btns = $("a[data-purpose='delete-attachment']");
34+
if(delete_attachments_btns.exists()){
35+
delete_attachments_btns.off('click').on('click', function(){
36+
var id = $(this).data('id');
37+
$.cms.AttachmentManager.destroy(id);
38+
});
39+
}
3740
},
38-
3941
// @param [Integer] id The id of the attachment to delete.
4042
destroy:function (id) {
4143
if (confirm("Are you sure want to delete this attachment?")) {
@@ -78,5 +80,9 @@ $(function () {
7880
$('#assets_table > table').append(asset).show();
7981
$('div.buttons').show();
8082
}
83+
$('.empty-row').hide();
84+
$.cms.AttachmentManager.enableDeleteButtons();
8185
});
86+
87+
$.cms.AttachmentManager.enableDeleteButtons();
8288
});

app/assets/javascripts/cms/page_editor.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//= require 'jquery'
12
//= require 'jquery_ujs'
23
//= require 'cms/core_library'
34
//= require 'bootstrap/modal'

app/assets/stylesheets/cms/_assets.css.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,9 @@
5252
}
5353
}
5454
}
55+
}
56+
57+
// Iframe for attachments
58+
#asset_add_uploader {
59+
display: none;
5560
}

app/assets/stylesheets/cms/_main-area.css.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* MAIN FORM AREA */
2-
.main-content {
2+
.main-content, .modal-body {
33
@include rem(margin-top,1rem);
44

55
.add-link {

app/assets/stylesheets/cms/styles/_images.css.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ img[data-type="image_block"] {
1111
max-width: 100%;
1212
}
1313

14+
// For multiple attachments in the table
15+
img[data-purpose='attachment'] {
16+
width:60px;
17+
}
1418
.img-rounded {
1519
@include border-radius(6px);
1620
}
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<%
2-
show_delete = true
3-
show_delete = can_delete if defined?(can_delete)
2+
show_delete = true
3+
show_delete = can_delete if defined?(can_delete)
44
%>
5-
<tr id="<%= dom_id(attachment)%>">
5+
<tr id="<%= dom_id(attachment) %>">
66
<% if attachment.is_image? %>
7-
<td><%= link_to image_tag(attachment_path_for(attachment), :size => '60x60', :data=>{:purpose=>'attachment'}), attachment_path_for(attachment) %></td>
7+
<td><%= link_to(image_tag(attachment_path_for(attachment), :size => '60x60', :data => {:purpose => 'attachment'}), attachment_path_for(attachment), target: "_blank") %></td>
88
<% else %>
9-
<td><%= image_tag "cms/icons/file_types/#{attachment.icon}.png" %>
9+
<td><%= image_tag "cms/icons/file_types/#{attachment.icon}.png" %>
1010
<% end %>
1111
<td><%= attachment.attachment_name.singularize.capitalize %></td>
1212
<td><%= number_to_human_size(attachment.size) %></td>
13-
<td><%= link_to_function "Delete", "$.cms.AttachmentManager.destroy(#{attachment.id})" if show_delete %></td>
13+
<td><%= link_to("Delete", '#',
14+
data: {purpose: 'delete-attachment', id: attachment.id},
15+
class: 'btn btn-mini btn-danger') if show_delete %></td>
1416
</tr>

app/views/cms/attachments/_attachment_manager.html.erb

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
11
<%= render :partial => 'cms/attachments/attachment_table', :locals => {:block => @block, :can_delete => true} %>
2-
<div class="fields">
3-
<p>Upload a new attachment:</p>
4-
</div>
2+
<%= link_to 'Upload a New Attachment', "#", class: 'btn btn-primary', data: {toggle: "modal", target: "#upload-attachment"} %>
53

6-
<div class="fields select_fields">
7-
<%= label_tag "Attachment Type" %>
8-
<%= select_tag :asset_types, options_for_select(asset_types) %>
9-
</div>
10-
<%= f.hidden_field :attachment_id_list, :id => "attachment_manager_ids_list" %>
11-
<%= f.hidden_field :attachments_changed, :id => "attachments_manager_changed" %>
4+
<% content_for :before_main_content do
5+
# Must be outside main form, since we can't have a form within a form.
6+
%>
7+
<div class="modal fade" id="upload-attachment">
8+
<div class="modal-dialog">
9+
<div class="modal-content">
10+
<div class="modal-header">
11+
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
12+
<h4 class="modal-title">Upload a New Attachment</h4>
13+
</div>
14+
<div class="modal-body">
15+
<%= simple_form_for Cms::Attachment.new do |af| %>
16+
<%= af.input :attachable_type, collection: asset_types, label: 'Type', include_blank: false %>
17+
<%= hidden_field_tag :attachment_id_list, "", :id => "attachment_manager_ids_list" %>
18+
<%= hidden_field_tag :attachments_changed, "", :id => "attachments_manager_changed" %>
1219

13-
<div id="asset_add" class="fields file_fields" style="display:<%= asset_types.size > 1 ? "none" : "block" %>">
14-
<label for="asset_add">Choose file</label>
20+
<%= af.input :data, label: 'File', input_html: {id: 'asset_add_file', onchange: "$.cms.AttachmentManager.upload(this)"} %>
21+
<input type="hidden" id="asset_attachable_id" name="asset[attachable_id]" value="<%= object.id %>"/>
22+
<input type="hidden" id="asset_attachable_class" value="<%= object.class.name %>"/>
23+
24+
<div id="asset_add" class="fields file_fields" style="display:<%= asset_types.size > 1 ? "none" : "block" %>">
25+
26+
<div id="asset_add_div">
27+
<iframe src="javascript:false" name="asset_add_uploader" id="asset_add_uploader"></iframe>
28+
</div>
29+
</div>
30+
<% end %>
31+
</div>
32+
<div class="modal-footer">
33+
<button type="button" class="btn btn-small" data-dismiss="modal">Never Mind</button>
34+
</div>
35+
</div>
36+
</div>
37+
</div>
38+
<% end %>
1539

16-
<div id="asset_add_div">
17-
<input type="file" name="attachment[data]" id="asset_add_file" onchange="$.cms.AttachmentManager.upload(this)"/>
18-
<input type="hidden" id="asset_attachable_id" name="asset[attachable_id]" value="<%= object.id %>"/>
19-
<input type="hidden" id="asset_attachable_class" value="<%= object.class.name %>"/>
20-
<iframe src="javascript:false" name="asset_add_uploader" id="asset_add_uploader"></iframe>
21-
</div>
22-
</div>
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
<div class="fields" id="assets_table">
2-
<label><span id="asset-name"><%= block.class.name %></span> Attachments</label>
3-
<br /><br />
4-
<table style="display:<%= block.multiple_attachments.empty? ? "none" : "block"%>">
2+
<table class="table table-bordered assets">
3+
<thead>
54
<tr>
65
<th>Preview</th>
76
<th>Type</th>
87
<th>Size</th>
9-
<th></th>
8+
<th>Actions</th>
109
<tr>
10+
</thead>
11+
<tbody>
12+
<% if @block.multiple_attachments.empty? %>
13+
<tr class='empty-row'><td colspan="4" >There are no attachments yet.</td></tr>
14+
<% end %>
1115
<% for attachment in block.multiple_attachments %>
12-
<% unless attachment.new_record? %>
13-
<%= render :partial => "cms/attachments/attachment", :locals => { :attachment => attachment, :can_delete => can_delete } %>
14-
<% end %>
16+
<% unless attachment.new_record? %>
17+
<%= render :partial => "cms/attachments/attachment", :locals => {:attachment => attachment, :can_delete => can_delete} %>
18+
<% end %>
1519
<% end %>
20+
</tbody>
1621
</table>
1722
</div>

app/views/layouts/cms/application.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
</nav>
1212
<%= render 'cms/sites/flash' %>
1313
<section class="container center-column main-container bottom-form-btn clearfix">
14+
<% if content_for?(:before_main_content) %>
15+
<%= yield :before_main_content %>
16+
<% end %>
1417
<%= yield %>
1518
<div class="padded row-fluid">
1619
<div class="span12"><%= render :partial => 'layouts/cms/footer' %></div>

features/ckeditor.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ Feature: CKEditor
77
When I request /cms/content_library
88
And I click on "create_new_html_block"
99
Then I should see a widget to select which editor to use
10-
And I should see the CKEditor

0 commit comments

Comments
 (0)