diff --git a/app/assets/javascripts/tasks_form.coffee b/app/assets/javascripts/tasks_form.coffee index cdf6a7d63..5f8587c11 100644 --- a/app/assets/javascripts/tasks_form.coffee +++ b/app/assets/javascripts/tasks_form.coffee @@ -1,6 +1,7 @@ ready = -> initializeFileTypeSelection() initializeVisibilityWarning() + initializeRadioButtonDeselection() initializeLoadSelect2 = -> $('#task_programming_language_id').select2 @@ -30,6 +31,24 @@ initializeVisibilityWarning = -> $('#task_access_level_public').on 'change', -> warning_message.addClass('d-none') +initializeRadioButtonDeselection = -> + radios = $('.radio-switch input[type="radio"][name*="[usage_by_lms]"]') + hidden_field = $('input[name="file[usage_by_lms]"][type="hidden"]') + + radios.each -> + $radio = $(this) + $radio.data('was-checked', $radio.prop('checked')) + + radios.on 'click', -> + $radio = $(this) + + if $radio.prop('checked') and $radio.data('was-checked') + $radio.prop('checked', false).data('was-checked', false) + hidden_field.val(null) # Reset the value to `nil` + else + radios.data('was-checked', false) # Reset all other radios + $radio.data('was-checked', true) + hidden_field.val('') # Clear hidden field since something is selected $(document).on('turbolinks:load', ready) $(document).on('select2:locales:loaded', initializeLoadSelect2) diff --git a/app/models/task_file.rb b/app/models/task_file.rb index 1c0b87d85..98c74213c 100644 --- a/app/models/task_file.rb +++ b/app/models/task_file.rb @@ -17,6 +17,8 @@ class TaskFile < ApplicationRecord validate :parent_validation_check attr_accessor :use_attached_file, :file_marked_for_deletion, :parent_blob_id + normalizes :usage_by_lms, with: ->(usage_by_lms) { usage_by_lms.presence } + before_validation :attach_parent_blob, if: -> { attachment.blank? && task&.contribution? && parent.present? && parent_blob_id.present? } before_save :remove_attachment diff --git a/app/views/tasks/_file_config.html.slim b/app/views/tasks/_file_config.html.slim index 867d2e6cb..4828ad3bc 100644 --- a/app/views/tasks/_file_config.html.slim +++ b/app/views/tasks/_file_config.html.slim @@ -31,6 +31,7 @@ .form-control.placeholder = file.label :usage_by_lms, TaskFile.human_attribute_name('usage_by_lms'), class: 'form-label w-auto me-2' .radio-switch + = file.hidden_field :usage_by_lms, value: nil, class: 'fallback' = file.radio_button :usage_by_lms, 'edit', value: 'edit' = file.label :usage_by_lms_edit, data: {toggle: 'tooltip', placement: 'bottom'}, title: t('common.button.edit'), class: 'radio-left small-radio radio-third' do span.fa-stack