diff --git a/app/assets/javascripts/imageable.js.coffee b/app/assets/javascripts/imageable.js.coffee index 2f4e4c791..dd43ede6d 100644 --- a/app/assets/javascripts/imageable.js.coffee +++ b/app/assets/javascripts/imageable.js.coffee @@ -1,23 +1,22 @@ App.Imageable = initialize: -> - $('#nested-image').on 'cocoon:after-insert', (e, nested_image) -> - input = $(nested_image).find('.js-image-attachment') - App.Imageable.initializeDirectUploadInput(input) - inputFiles = $('.js-image-attachment') $.each inputFiles, (index, input) -> App.Imageable.initializeDirectUploadInput(input) - $("#new_image_link").on 'click', -> - $(this).addClass('hide') + $('#nested-image').on 'cocoon:after-remove', (e, item) -> + $("#new_image_link").removeClass('hide') + + $('#nested-image').on 'cocoon:after-insert', (e, nested_image) -> + $("#new_image_link").addClass('hide') + input = $(nested_image).find('.js-image-attachment') + App.Imageable.initializeDirectUploadInput(input) initializeDirectUploadInput: (input) -> inputData = @buildData([], input) - @initializeRemoveImageLink(input) - @initializeRemoveCachedImageLink(input, inputData) $(input).fileupload @@ -142,18 +141,13 @@ App.Imageable = $('#new_image_link').removeClass('hide') + $(data.wrapper).find(".attachment-actions").addClass('small-12').removeClass('small-6 float-right') + $(data.wrapper).find(".attachment-actions .action-remove").addClass('small-3').removeClass('small-12') + if $(data.input).data('nested-image') == true $(data.wrapper).remove() else - $(data.destroyAttachmentLinkContainer).find('a.delete').remove() - - initializeRemoveImageLink: (input) -> - wrapper = $(input).closest(".direct-upload") - remove_image_link = $(wrapper).find('a.remove-nested-field') - $(remove_image_link).on 'click', (e) -> - e.preventDefault() - $(wrapper).remove() - $('#new_image_link').removeClass('hide') + $(data.wrapper).find('a.remove-cached-attachment').remove() initializeRemoveCachedImageLink: (input, data) -> wrapper = $(input).closest(".direct-upload") @@ -163,6 +157,6 @@ App.Imageable = e.stopPropagation() App.Imageable.doDeleteCachedAttachmentRequest(this.href, data) - destroyNestedImage: (id, notice) -> + removeImage: (id) -> $('#' + id).remove() $("#new_image_link").removeClass('hide') diff --git a/app/controllers/budgets/investments_controller.rb b/app/controllers/budgets/investments_controller.rb index 368cd35a7..884c2fdab 100644 --- a/app/controllers/budgets/investments_controller.rb +++ b/app/controllers/budgets/investments_controller.rb @@ -110,7 +110,7 @@ module Budgets params.require(:budget_investment) .permit(:title, :description, :external_url, :heading_id, :tag_list, :organization_name, :location, :terms_of_service, - image_attributes: [:id, :title, :attachment, :cached_attachment, :user_id], + image_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy], documents_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy]) end diff --git a/app/controllers/proposals_controller.rb b/app/controllers/proposals_controller.rb index 5b9c03d62..5d693e218 100644 --- a/app/controllers/proposals_controller.rb +++ b/app/controllers/proposals_controller.rb @@ -80,7 +80,7 @@ class ProposalsController < ApplicationController def proposal_params params.require(:proposal).permit(:title, :question, :summary, :description, :external_url, :video_url, :responsible_name, :tag_list, :terms_of_service, :geozone_id, - image_attributes: [:id, :title, :attachment, :cached_attachment, :user_id], + image_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy], documents_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy] ) end diff --git a/app/helpers/images_helper.rb b/app/helpers/images_helper.rb index 8b9d15be6..3bebb3957 100644 --- a/app/helpers/images_helper.rb +++ b/app/helpers/images_helper.rb @@ -35,15 +35,8 @@ module ImagesHelper image.persisted? ? "persisted-image" : "cached-image" end - def render_destroy_image_link(image) - if image.persisted? - link_to t('images.form.delete_button'), - image_path(image, nested_image: true), - method: :delete, - remote: true, - data: { confirm: t('images.actions.destroy.confirm') }, - class: "delete remove-image" - elsif !image.persisted? && image.cached_attachment.present? + def render_destroy_image_link(builder, image) + if !image.persisted? && image.cached_attachment.present? link_to t('images.form.delete_button'), direct_upload_destroy_url("direct_upload[resource_type]": image.imageable_type, "direct_upload[resource_id]": image.imageable_id, @@ -53,9 +46,7 @@ module ImagesHelper remote: true, class: "delete remove-cached-attachment" else - link_to t('images.form.delete_button'), - "#", - class: "delete remove-nested-field" + link_to_remove_association t('images.form.delete_button'), builder, class: "delete remove-image" end end diff --git a/app/views/images/_image_fields.html.erb b/app/views/images/_image_fields.html.erb index 43a79a2c2..7a63dab39 100644 --- a/app/views/images/_image_fields.html.erb +++ b/app/views/images/_image_fields.html.erb @@ -1,7 +1,8 @@ -