Skip to content

Add functionality to deselect radio buttons for usage_by_lms #1882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/assets/javascripts/tasks_form.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ready = ->
initializeFileTypeSelection()
initializeVisibilityWarning()
initializeRadioButtonDeselection()

initializeLoadSelect2 = ->
$('#task_programming_language_id').select2
Expand Down Expand Up @@ -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)
2 changes: 2 additions & 0 deletions app/models/task_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions app/views/tasks/_file_config.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading