$(document).ready(function() {
var lang = $('html').attr('lang');
// Datepicker
var framadatepicker = function() {
$('.input-group.date').datepicker({
format: "dd/mm/yyyy",
todayBtn: "linked",
orientation: "top left",
autoclose: true,
language: lang,
todayHighlight: true,
beforeShowDay: function (date){
var $selected_days = new Array();
$('#selected-days input[id^="day"]').each(function() {
if($(this).val()!='') {
$selected_days.push($(this).val());
}
});
for(i = 0; i < $selected_days.length; i++){
var $selected_date = $selected_days[i].split('/');
if (date.getFullYear() == $selected_date[2] && (date.getMonth()+1) == $selected_date[1] && date.getDate() == $selected_date[0]){
return {
classes: 'disabled selected'
};
}
}
}
});
};
var datepickerfocus = false; // a11y : datepicker not display on focus until there is one click on the button
var lastDate = new Date();
$(document).on('click','.input-group.date .input-group-addon', function() {
datepickerfocus = true;
// Re-init datepicker config before displaying
$(this).parent().datepicker(framadatepicker());
$(this).parent().datepicker('show');
// Trick to keep the last datepicker view
if ($(this).parent().find('input').val() == '') {
$('.input-group.date input').each(function(){
if($(this).val()!='') {
lastDate = $(this).val();
}
});
$(this).parent().datepicker('setDate', lastDate);
$(this).parent().datepicker('setDate', '');
}
// Trick to refresh calendar
$('.datepicker-days .prev').trigger('click');
$('.datepicker-days .next').trigger('click');
// .active must be clicable in order to unfill the form
$('.datepicker-days .active').removeClass('disabled');
});
$(document).on('focus','.input-group.date input', function() {
if(datepickerfocus) {
// unfocus and click instead (because we are not in a11y mode anymore)
$(this).blur();
$(this).parent().find('.input-group-addon').trigger('click');
}
});
/**
* choix_date.php
**/
// Button "Remove all hours"
$(document).on('click','#resethours', function() {
$('#selected-days fieldset').each(function() {
$(this).find('.hours:gt(2)').parent().remove();
});
$('#d0-h0').focus();
$('#selected-days fieldset .hours').attr('value','');
});
// Button "Remove all days"
$('#resetdays').on('click', function() {
$('#selected-days fieldset:gt(0)').remove();
$('#day0').focus();
$('#remove-a-day, #copyhours').addClass('disabled');
});
// Button "Copy hours of the first day"
$('#copyhours').on('click', function() {
var first_day_hours = $('#selected-days fieldset:eq(0) .hours').map(function() {
return $(this).val();
});
$('#selected-days fieldset:gt(0)').each(function() {
for ($i = 0; $i < first_day_hours.length; $i++) {
$(this).find('.hours:eq('+$i+')').val(first_day_hours[$i]); // fill hours
}
});
$('#d0-h0').focus();
});
// Buttons "Add an hour"
$(document).on('click','.add-an-hour', function() {
var last_hour = $(this).parent('div').parent('div').prev();
// for and id
var di_hj = last_hour.children('.hours').attr('id').split('-');
var di = parseInt(di_hj[0].replace('d','')); var hj = parseInt(di_hj[1].replace('h',''));
// label, title and placeholder
var last_hour_label = last_hour.children('.hours').attr('placeholder');
var hour_text = last_hour_label.substring(0, last_hour_label.indexOf(' '));
// RegEx for multiple replace
var re_label = new RegExp(last_hour_label, 'g');
var re_id = new RegExp('"d'+di+'-h'+hj+'"', 'g');
// HTML code of the new hour
var new_hour_html =
'