From 1cfba5efeedec43af6701d39709b3e0d9c0d4198 Mon Sep 17 00:00:00 2001 From: Antonin Date: Thu, 6 Oct 2016 21:07:02 +0200 Subject: [PATCH] Updated buttons state and refactored a little --- js/app/date_poll.js | 78 +++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 35 deletions(-) diff --git a/js/app/date_poll.js b/js/app/date_poll.js index 43601da..8542c82 100644 --- a/js/app/date_poll.js +++ b/js/app/date_poll.js @@ -21,28 +21,45 @@ $(document).ready(function () { // Global variables var $selected_days = $('#selected-days'); - var $removeaday_and_copyhours = $('#remove-a-day, #copyhours'); + var $removeaday = $('#remove-a-day'); + var $copyhours = $('#copyhours'); + var $next = $('button[name="choixheures"]'); + + + var updateButtonState = function () { + $removeaday.toggleClass('disabled', $selected_days.find('fieldset').length <= 1); + $copyhours.toggleClass('disabled', !hasFirstDayFilledHours()); + $next.toggleClass('disabled', countFilledDays() < 1) + }; // at least 1 day filled and you can submit + var isSubmitDaysAvaillable = function() { + return (countFilledDays() >= 1); + }; - var submitDaysAvalaible = function () { + var countFilledDays = function () { var nb_filled_days = 0; - $selected_days.find('fieldset legend input').each(function () { if ($(this).val() != '') { nb_filled_days++; } }); - - if (nb_filled_days >= 1) { - $('button[name="choixheures"]').removeClass('disabled'); - return true; - } else { - $('button[name="choixheures"]').addClass('disabled'); - return false; - } + return nb_filled_days; }; + + var hasFirstDayFilledHours = function () { + var hasFilledHours = false; + $selected_days.find('fieldset').first().find('.hours').each(function () { + if ($(this).val() != '') { + hasFilledHours = true; + } + }); + return hasFilledHours; + }; + + + /** * Parse a string date * @param dateStr The string date @@ -99,7 +116,7 @@ $(document).ready(function () { .after('
' + new_day_html + '
') .next().find('legend input').val(dateStr); $('#day' + (new_day_number)).focus(); - $removeaday_and_copyhours.removeClass('disabled'); + updateButtonState(); } var useFirstEmptyDateField = function (dateStr) { @@ -118,7 +135,7 @@ $(document).ready(function () { // Handle form submission $(document.formulaire).on('submit', function (e) { - if (!submitDaysAvalaible()) { + if (!isSubmitDaysAvaillable()) { e.preventDefault(); e.stopPropagation(); } @@ -135,15 +152,16 @@ $(document).ready(function () { }); // Button "Remove all days" + $('#resetdays').on('click', function () { $selected_days.find('fieldset:gt(0)').remove(); $('#day0').focus(); - $removeaday_and_copyhours.addClass('disabled'); + updateButtonState(); }); // Button "Copy hours of the first day" - $('#copyhours').on('click', function () { + $copyhours.on('click', function () { var first_day_hours = $selected_days.find('fieldset:eq(0) .hours').map(function () { return $(this).val(); }); @@ -211,7 +229,7 @@ $(document).ready(function () { $(this).addClass('disabled'); } } - submitDaysAvalaible(); + updateButtonState(); }); // Button "Add a day" @@ -222,16 +240,12 @@ $(document).ready(function () { // Button "Remove a day" - $('#remove-a-day').on('click', function () { + $removeaday.on('click', function () { $selected_days.find('fieldset:last').remove(); - var nb_days = $selected_days.find('fieldset').length; - $('#day' + (getLastDayNumber() - 1)).focus(); - if (nb_days == 1) { - $removeaday_and_copyhours.addClass('disabled'); - } - submitDaysAvalaible(); + + updateButtonState(); }); // Button "Remove the current day" @@ -240,7 +254,7 @@ $(document).ready(function () { if ($('#days_container').find('fieldset').length > 1) { $(this).parents('fieldset').remove(); } - submitDaysAvalaible(); + updateButtonState(); }); // Add an range of dates @@ -272,7 +286,7 @@ $(document).ready(function () { startDateField.val(''); endDateField.val(''); $('#add_days').modal('hide'); - submitDaysAvalaible(); + updateButtonState(); } else { setTimeout(function () { @@ -313,14 +327,8 @@ $(document).ready(function () { }); }); - $(document).on('keyup, change', '.hours, #selected-days fieldset legend input', function () { - submitDaysAvalaible(); + $(document).on('keyup change', '.hours, #selected-days fieldset legend input', function () { + updateButtonState(); }); - submitDaysAvalaible(); - - // 2 days and you can remove a day or copy hours - - if ($selected_days.find('fieldset').length > 1) { - $removeaday_and_copyhours.removeClass('disabled'); - } -}); \ No newline at end of file + updateButtonState(); +});