diff --git a/lib/assets/javascripts/constants.js b/lib/assets/javascripts/constants.js index 25721c0..cd8245f 100644 --- a/lib/assets/javascripts/constants.js +++ b/lib/assets/javascripts/constants.js @@ -7,7 +7,7 @@ export const VALIDATION_MESSAGE_PASSWORD = 'The password must be between 8 and 128 characters.'; export const VALIDATION_MESSAGE_PASSWORDS_MATCH = 'The passwords must match.'; export const VALIDATION_MESSAGE_RADIO = 'Please choose one of the options.'; -export const VALIDATION_MESSAGE_SELECT = "Please select a value from the list."; +export const VALIDATION_MESSAGE_SELECT = 'Please select a value from the list.'; export const VALIDATION_MESSAGE_TEXT = 'This field is required.'; export const SHOW_PASSWORD_MESSAGE = 'Show password'; diff --git a/lib/assets/javascripts/utils/autoComplete.js b/lib/assets/javascripts/utils/autoComplete.js index 3660dba..44b77f1 100644 --- a/lib/assets/javascripts/utils/autoComplete.js +++ b/lib/assets/javascripts/utils/autoComplete.js @@ -1,4 +1,3 @@ -import 'jquery-accessible-autocomplete-list-aria/jquery-accessible-autocomplete-list-aria'; import debounce from '../utils/debounce'; /* @@ -56,4 +55,4 @@ // Show/hide the clear button on page load toggleClearButton(el); }); -}; \ No newline at end of file +}; diff --git a/lib/assets/javascripts/views/plans/new.js b/lib/assets/javascripts/views/plans/new.js index dd7c547..815c832 100644 --- a/lib/assets/javascripts/views/plans/new.js +++ b/lib/assets/javascripts/views/plans/new.js @@ -1,85 +1,67 @@ import ariatiseForm from '../../utils/ariatiseForm'; import initAutoComplete from '../../utils/autoComplete'; +const handleCheckboxClick = (name, checked) => { + $(`#plan_${name}_name`).prop('disabled', checked); + $('#plan_template_id').val('').change(); + $('#available-templates').fadeOut(); + + if (checked) { + $(`#plan_${name}_name`).val(''); + $(`#plan_${name}_id`).val('-1').change(); + $(`#plan_${name}_name`).siblings('.combobox-clear-button').hide(); + } else { + $(`#plan_${name}_id`).val('').change(); + } +}; + +const handleComboboxChange = () => { + const validOrg = ($('#plan_org_id').val().trim().length > 0 || $('#plan_no_org').prop('checked')); + const validFunder = ($('#plan_funder_id').val().trim().length > 0 || $('#plan_no_funder').prop('checked')); + + if (!validOrg || !validFunder) { + $('#available-templates').fadeOut(); + $('#plan_template_id').val(''); + } +}; + $().ready(() => { initAutoComplete(); ariatiseForm({ selector: '#create-plan' }); -}); -/* -$(document).ready(function(){ - $("#available-templates").hide(); + const defaultVisibility = $('#plan_visibility').val(); - var defaultVisibility = $("#plan_visibility").val(); - - // retrieve the template options and toggle the submit button on page reload + // Initialize the form handleComboboxChange(); - handleCheckboxClick("org", $("#plan_no_org").prop("checked")); - handleCheckboxClick("funder", $("#plan_no_funder").prop("checked")); - + handleCheckboxClick('org', $('#plan_no_org').prop('checked')); + handleCheckboxClick('funder', $('#plan_no_funder').prop('checked')); + // When the user checks the 'mock project' box we need to set the // visibility to 'is_test' - $("#is_test").click(function(){ - $("#plan_visibility").val(($(this)[0].checked ? 'is_test' : defaultVisibility)); + $('#is_test').click((e) => { + $('#plan_visibility').val(($(e.currentTarget)[0].checked ? 'is_test' : defaultVisibility)); }); // When the hidden org and funder id fields change toogle the submit button - $("#plan_org_id, #plan_funder_id").change(function(){ + $('#plan_org_id, #plan_funder_id').change(() => { handleComboboxChange(); }); // Make sure the checkbox is unchecked if we're entering text - $(".js-combobox").keyup(function(){ - var whichOne = $(this).prop('id').split('_')[1]; - $("#plan_no_" + whichOne).prop("checked", false); + $('.js-combobox').keyup((e) => { + const whichOne = $(e.currentTarget).prop('id').split('_')[1]; + $(`#plan_no_${whichOne}`).prop('checked', false); }); // If the user clicks the no Org/Funder checkbox disable the dropdown // and hide clear button - $("#plan_no_org, #plan_no_funder").click(function(){ - var whichOne = $(this).prop('id').split('_')[2]; - handleCheckboxClick(whichOne, this.checked); + $('#plan_no_org, #plan_no_funder').click((e) => { + const whichOne = $(e.currentTarget).prop('id').split('_')[2]; + handleCheckboxClick(whichOne, e.currentTarget.checked); }); - + // When the form receives a valid template id enable the button - $("#plan_template_id").change(function(){ - $("#create_plan_submit").attr('aria-disabled', ($(this).val().trim().length <= 0)); + $('#plan_template_id').change((e) => { + $('#create_plan_submit').attr('aria-disabled', ($(e.currentTarget).val().trim().length <= 0)); }); }); - -// Only display the submit button if the user has made each decision -// ------------------------------------------------------------- -function handleComboboxChange(){ - // If the (no_org checkbox is checked OR an org was selected) AND - // (no_funder checkbox is checked OR a funder was selected) AND - // (the template selector is not visible OR a template has been selected) - var retrieve = ($("#plan_no_org").prop("checked") || - ($("#plan_org_id").val() && $("#plan_org_id").val().trim().length > 0)) && - ($("#plan_no_funder").prop("checked") || - ($("#plan_org_id").val() && $("#plan_funder_id").val().trim().length > 0)); - - if(retrieve){ - if($("#plan_template_id").val().trim().length <= 0){ - $("form").submit(); - } - - }else{ - $("#available-templates").fadeOut(); - $("#plan_template_id").val(""); - } -} - -// Clear the combobox and disable it if the box was checked -// ------------------------------------------------------------- -function handleCheckboxClick(name, checked){ - $("#plan_" + name + "_name").prop("disabled", checked); - $("#plan_template_id").val("").change(); - $("#available-templates").fadeOut(); - - if(checked){ - $("#plan_" + name + "_name").val(""); - $("#plan_" + name + "_id").val("").change(); - $("#plan_" + name + "_name").siblings(".combobox-clear-button").hide(); - } -} -*/ diff --git a/lib/assets/webpack.config.js b/lib/assets/webpack.config.js index 6e20570..63fd3eb 100644 --- a/lib/assets/webpack.config.js +++ b/lib/assets/webpack.config.js @@ -15,7 +15,7 @@ context: __dirname, entry: { - vendor: ['jquery', 'timeago/jquery.timeago'], + vendor: ['jquery', 'timeago/jquery.timeago', 'jquery-accessible-autocomplete-list-aria/jquery-accessible-autocomplete-list-aria'], application: ['./javascripts/application.js', './stylesheets/application.scss'], },