Merge branch 'feature/allow-to-remove-date-in-wizard' into 'develop'

Refactoring on button state handling



See merge request !155
This commit is contained in:
Olivier Perez 2016-10-06 23:08:47 +02:00
commit 7c1b3b163d

View File

@ -21,28 +21,45 @@ $(document).ready(function () {
// Global variables // Global variables
var $selected_days = $('#selected-days'); 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 // 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; var nb_filled_days = 0;
$selected_days.find('fieldset legend input').each(function () { $selected_days.find('fieldset legend input').each(function () {
if ($(this).val() != '') { if ($(this).val() != '') {
nb_filled_days++; nb_filled_days++;
} }
}); });
return nb_filled_days;
if (nb_filled_days >= 1) {
$('button[name="choixheures"]').removeClass('disabled');
return true;
} else {
$('button[name="choixheures"]').addClass('disabled');
return false;
}
}; };
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 * Parse a string date
* @param dateStr The string date * @param dateStr The string date
@ -75,7 +92,7 @@ $(document).ready(function () {
function getLastDayNumber(last_day) { function getLastDayNumber(last_day) {
if (last_day == null) if (last_day == null)
last_day = $selected_days.find('fieldset').filter(':last'); last_day = $selected_days.find('fieldset').filter(':last')
return parseInt(/^d([0-9]+)-h[0-9]+$/.exec($(last_day).find('.hours').filter(':first').attr('id'))[1]) return parseInt(/^d([0-9]+)-h[0-9]+$/.exec($(last_day).find('.hours').filter(':first').attr('id'))[1])
} }
@ -99,15 +116,7 @@ $(document).ready(function () {
.after('<fieldset>' + new_day_html + '</fieldset>') .after('<fieldset>' + new_day_html + '</fieldset>')
.next().find('legend input').val(dateStr); .next().find('legend input').val(dateStr);
$('#day' + (new_day_number)).focus(); $('#day' + (new_day_number)).focus();
$removeaday_and_copyhours.removeClass('disabled'); updateButtonState();
}
function manageRemoveadayAndCopyhoursButtons() {
var nb_days = $selected_days.find('fieldset').length;
$('#day' + (getLastDayNumber() - 1)).focus();
if (nb_days == 1) {
$removeaday_and_copyhours.addClass('disabled');
}
} }
var useFirstEmptyDateField = function (dateStr) { var useFirstEmptyDateField = function (dateStr) {
@ -126,7 +135,7 @@ $(document).ready(function () {
// Handle form submission // Handle form submission
$(document.formulaire).on('submit', function (e) { $(document.formulaire).on('submit', function (e) {
if (!submitDaysAvalaible()) { if (!isSubmitDaysAvaillable()) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
} }
@ -139,19 +148,20 @@ $(document).ready(function () {
$(this).find('.hours:gt(2)').parent().remove(); $(this).find('.hours:gt(2)').parent().remove();
}); });
$('#d0-h0').focus(); $('#d0-h0').focus();
$selected_days.find('fieldset .hours').attr('value', ''); $selected_days.find('fieldset .hours').val('');
}); });
// Button "Remove all days" // Button "Remove all days"
$('#resetdays').on('click', function () { $('#resetdays').on('click', function () {
$selected_days.find('fieldset:gt(0)').remove(); $selected_days.find('fieldset:gt(0)').remove();
$('#day0').focus(); $('#day0').focus();
$removeaday_and_copyhours.addClass('disabled'); updateButtonState();
}); });
// Button "Copy hours of the first day" // 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 () { var first_day_hours = $selected_days.find('fieldset:eq(0) .hours').map(function () {
return $(this).val(); return $(this).val();
}); });
@ -219,7 +229,7 @@ $(document).ready(function () {
$(this).addClass('disabled'); $(this).addClass('disabled');
} }
} }
submitDaysAvalaible(); updateButtonState();
}); });
// Button "Add a day" // Button "Add a day"
@ -230,11 +240,12 @@ $(document).ready(function () {
// Button "Remove a day" // Button "Remove a day"
$('#remove-a-day').on('click', function () { $removeaday.on('click', function () {
$selected_days.find('fieldset:last').remove(); $selected_days.find('fieldset:last').remove();
manageRemoveadayAndCopyhoursButtons(); $('#day' + (getLastDayNumber() - 1)).focus();
submitDaysAvalaible();
updateButtonState();
}); });
// Button "Remove the current day" // Button "Remove the current day"
@ -243,8 +254,7 @@ $(document).ready(function () {
if ($('#days_container').find('fieldset').length > 1) { if ($('#days_container').find('fieldset').length > 1) {
$(this).parents('fieldset').remove(); $(this).parents('fieldset').remove();
} }
manageRemoveadayAndCopyhoursButtons(); updateButtonState();
submitDaysAvalaible();
}); });
// Add an range of dates // Add an range of dates
@ -276,7 +286,7 @@ $(document).ready(function () {
startDateField.val(''); startDateField.val('');
endDateField.val(''); endDateField.val('');
$('#add_days').modal('hide'); $('#add_days').modal('hide');
submitDaysAvalaible(); updateButtonState();
} else { } else {
setTimeout(function () { setTimeout(function () {
@ -317,14 +327,8 @@ $(document).ready(function () {
}); });
}); });
$(document).on('keyup, change', '.hours, #selected-days fieldset legend input', function () { $(document).on('keyup change', '.hours, #selected-days fieldset legend input', function () {
submitDaysAvalaible(); updateButtonState();
}); });
submitDaysAvalaible(); updateButtonState();
// 2 days and you can remove a day or copy hours
if ($selected_days.find('fieldset').length > 1) {
$removeaday_and_copyhours.removeClass('disabled');
}
}); });