Skip to content

Commit afe3718

Browse files
committed
Add functionality to deselect radio buttons for usage_by_lms
1 parent 9ccdec1 commit afe3718

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

app/assets/javascripts/tasks_form.coffee

+22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
ready = ->
22
initializeFileTypeSelection()
33
initializeVisibilityWarning()
4+
initializeRadioButtonDeselection()
45

56
initializeLoadSelect2 = ->
67
$('#task_programming_language_id').select2
@@ -30,6 +31,27 @@ initializeVisibilityWarning = ->
3031
$('#task_access_level_public').on 'change', ->
3132
warning_message.addClass('d-none')
3233

34+
initializeRadioButtonDeselection = ->
35+
radios = $('.radio-switch input[type="radio"][name*="[usage_by_lms]"]')
36+
hidden_field = $('input[name="file[usage_by_lms]"][type="hidden"]')
37+
38+
radios.each ->
39+
$radio = $(this)
40+
$radio.data('was-checked', $radio.prop('checked'))
41+
42+
radios.on 'click', ->
43+
$radio = $(this)
44+
45+
if $radio.prop('checked') and $radio.data('was-checked')
46+
$radio.prop('checked', false).data('was-checked', false)
47+
hidden_field.val(null) # Reset the value to `nil`
48+
else
49+
radios.data('was-checked', false) # Reset all other radios
50+
$radio.data('was-checked', true)
51+
hidden_field.val('') # Clear hidden field since something is selected
52+
53+
54+
3355

3456
$(document).on('turbolinks:load', ready)
3557
$(document).on('select2:locales:loaded', initializeLoadSelect2)

app/views/tasks/_file_config.html.slim

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
.form-control.placeholder
3232
= file.label :usage_by_lms, TaskFile.human_attribute_name('usage_by_lms'), class: 'form-label w-auto me-2'
3333
.radio-switch
34+
= file.hidden_field :usage_by_lms, value: nil, class: 'fallback'
3435
= file.radio_button :usage_by_lms, 'edit', value: 'edit'
3536
= file.label :usage_by_lms_edit, data: {toggle: 'tooltip', placement: 'bottom'}, title: t('common.button.edit'), class: 'radio-left small-radio radio-third' do
3637
span.fa-stack
@@ -42,4 +43,4 @@
4243
= file.radio_button :usage_by_lms, 'download', value: 'download'
4344
= file.label :usage_by_lms_download, data: {toggle: 'tooltip', placement: 'bottom'}, title: t('.download'), class: 'radio-right small-radio radio-third' do
4445
span.fa-stack
45-
i.fa-solid.fa-download.fa-stack-1x.fa-2x
46+
i.fa-solid.fa-download.fa-stack-1x.fa-2x

0 commit comments

Comments
 (0)