Updated buttons state and refactored a little
This commit is contained in:
parent
4881b3c8b2
commit
1cfba5efee
@ -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
|
||||||
@ -99,7 +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();
|
||||||
}
|
}
|
||||||
|
|
||||||
var useFirstEmptyDateField = function (dateStr) {
|
var useFirstEmptyDateField = function (dateStr) {
|
||||||
@ -118,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();
|
||||||
}
|
}
|
||||||
@ -135,15 +152,16 @@ $(document).ready(function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// 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();
|
||||||
});
|
});
|
||||||
@ -211,7 +229,7 @@ $(document).ready(function () {
|
|||||||
$(this).addClass('disabled');
|
$(this).addClass('disabled');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
submitDaysAvalaible();
|
updateButtonState();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Button "Add a day"
|
// Button "Add a day"
|
||||||
@ -222,16 +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();
|
||||||
|
|
||||||
var nb_days = $selected_days.find('fieldset').length;
|
|
||||||
|
|
||||||
$('#day' + (getLastDayNumber() - 1)).focus();
|
$('#day' + (getLastDayNumber() - 1)).focus();
|
||||||
if (nb_days == 1) {
|
|
||||||
$removeaday_and_copyhours.addClass('disabled');
|
updateButtonState();
|
||||||
}
|
|
||||||
submitDaysAvalaible();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Button "Remove the current day"
|
// Button "Remove the current day"
|
||||||
@ -240,7 +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();
|
||||||
}
|
}
|
||||||
submitDaysAvalaible();
|
updateButtonState();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add an range of dates
|
// Add an range of dates
|
||||||
@ -272,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 () {
|
||||||
@ -313,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');
|
|
||||||
}
|
|
||||||
});
|
});
|
Loading…
Reference in New Issue
Block a user