2015-03-05 14:53:42 +01:00
|
|
|
/**
|
|
|
|
* This software is governed by the CeCILL-B license. If a copy of this license
|
|
|
|
* is not distributed with this file, you can obtain one at
|
|
|
|
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt
|
|
|
|
*
|
|
|
|
* Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ
|
2016-08-04 21:51:37 +02:00
|
|
|
* Authors of Framadate/OpenSondage: Framasoft (https://github.com/framasoft)
|
2015-03-05 14:53:42 +01:00
|
|
|
*
|
|
|
|
* =============================
|
|
|
|
*
|
|
|
|
* Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence
|
|
|
|
* ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
|
|
|
|
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
|
|
|
|
*
|
|
|
|
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
|
|
|
|
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
|
|
|
|
*/
|
2015-04-16 13:10:28 +02:00
|
|
|
|
|
|
|
$(document).ready(function () {
|
2015-03-05 14:53:42 +01:00
|
|
|
|
|
|
|
// Global variables
|
|
|
|
|
|
|
|
var $selected_days = $('#selected-days');
|
2016-10-06 21:07:02 +02:00
|
|
|
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)
|
|
|
|
};
|
2015-03-05 14:53:42 +01:00
|
|
|
|
2015-11-19 15:38:27 +01:00
|
|
|
// at least 1 day filled and you can submit
|
2016-10-06 21:07:02 +02:00
|
|
|
var isSubmitDaysAvaillable = function() {
|
|
|
|
return (countFilledDays() >= 1);
|
|
|
|
};
|
2015-03-05 14:53:42 +01:00
|
|
|
|
2016-10-06 21:07:02 +02:00
|
|
|
var countFilledDays = function () {
|
2015-03-05 14:53:42 +01:00
|
|
|
var nb_filled_days = 0;
|
|
|
|
$selected_days.find('fieldset legend input').each(function () {
|
|
|
|
if ($(this).val() != '') {
|
|
|
|
nb_filled_days++;
|
|
|
|
}
|
|
|
|
});
|
2016-10-06 21:07:02 +02:00
|
|
|
return nb_filled_days;
|
|
|
|
};
|
2015-03-05 14:53:42 +01:00
|
|
|
|
2016-10-06 21:07:02 +02:00
|
|
|
|
|
|
|
var hasFirstDayFilledHours = function () {
|
|
|
|
var hasFilledHours = false;
|
|
|
|
$selected_days.find('fieldset').first().find('.hours').each(function () {
|
|
|
|
if ($(this).val() != '') {
|
|
|
|
hasFilledHours = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return hasFilledHours;
|
2015-03-05 14:53:42 +01:00
|
|
|
};
|
|
|
|
|
2016-10-06 21:07:02 +02:00
|
|
|
|
|
|
|
|
2015-10-30 22:08:38 +01:00
|
|
|
/**
|
|
|
|
* Parse a string date
|
|
|
|
* @param dateStr The string date
|
|
|
|
* @param format The format PHP style (allowed: %Y, %m and %d)
|
|
|
|
*/
|
|
|
|
var parseDate = function (dateStr, format) {
|
|
|
|
var dtsplit = dateStr.split(/[\/ .:-]/);
|
|
|
|
var dfsplit = format.split(/[\/ .:-]/);
|
|
|
|
|
2015-11-30 22:23:57 +01:00
|
|
|
if (dfsplit.length != dtsplit.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-10-30 22:08:38 +01:00
|
|
|
// creates assoc array for date
|
|
|
|
var df = [];
|
2015-11-30 22:06:07 +01:00
|
|
|
for (var dc = 0; dc < dtsplit.length; dc++) {
|
2015-10-30 22:08:38 +01:00
|
|
|
df[dfsplit[dc]] = dtsplit[dc];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build date
|
|
|
|
return new Date(parseInt(df['%Y']), parseInt(df['%m']) - 1, parseInt(df['%d']), 0, 0, 0, 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
var formatDate = function (date, format) {
|
|
|
|
return format
|
2016-04-26 23:29:22 +02:00
|
|
|
.replace('%d', ("00" +date.getDate()).slice(-2))
|
|
|
|
.replace('%m', ("00" + (date.getMonth() + 1)).slice(-2))
|
|
|
|
.replace('%Y', ("0000" + date.getFullYear()).slice(-4));
|
2015-10-30 22:08:38 +01:00
|
|
|
};
|
|
|
|
|
2016-08-08 07:09:51 +02:00
|
|
|
function getLastDayNumber(last_day) {
|
|
|
|
if (last_day == null)
|
2016-10-06 22:10:51 +02:00
|
|
|
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])
|
2016-08-08 07:09:51 +02:00
|
|
|
}
|
|
|
|
|
2015-10-30 22:08:38 +01:00
|
|
|
function newDateFields(dateStr) {
|
2016-08-07 12:24:41 +02:00
|
|
|
var last_day = $selected_days.find('fieldset').filter(':last');
|
2015-10-30 22:08:38 +01:00
|
|
|
var last_day_title = last_day.find('legend input').attr('title');
|
2016-08-08 07:09:51 +02:00
|
|
|
var new_day_number = getLastDayNumber(last_day) + 1;
|
2015-10-30 22:08:38 +01:00
|
|
|
|
2016-08-07 12:24:41 +02:00
|
|
|
var re_id_hours = new RegExp('"d' + (new_day_number - 1) + '-h', 'g');
|
|
|
|
var re_name_hours = new RegExp('name="horaires' + (new_day_number - 1), 'g');
|
2015-10-30 22:08:38 +01:00
|
|
|
|
2016-08-07 12:24:41 +02:00
|
|
|
var new_day_html = last_day.html().replace(re_id_hours, '"d' + new_day_number + '-h')
|
|
|
|
.replace('id="day' + (new_day_number - 1) + '"', 'id="day' + new_day_number + '"')
|
|
|
|
.replace('for="day' + (new_day_number - 1) + '"', 'for="day' + new_day_number + '"')
|
|
|
|
.replace(re_name_hours, 'name="horaires' + new_day_number)
|
2015-10-30 22:08:38 +01:00
|
|
|
.replace(/value="(.*?)"/g, 'value=""')
|
|
|
|
.replace(/hours" title="(.*?)"/g, 'hours" title="" p')
|
2016-08-07 12:24:41 +02:00
|
|
|
.replace('title="' + last_day_title + '"', 'title="' + last_day_title.substring(0, last_day_title.indexOf(' ')) + ' ' + (new_day_number + 1) + '"');
|
2015-10-30 22:08:38 +01:00
|
|
|
|
|
|
|
last_day
|
|
|
|
.after('<fieldset>' + new_day_html + '</fieldset>')
|
|
|
|
.next().find('legend input').val(dateStr);
|
2016-08-07 12:24:41 +02:00
|
|
|
$('#day' + (new_day_number)).focus();
|
2016-10-06 21:07:02 +02:00
|
|
|
updateButtonState();
|
2015-10-30 22:08:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var useFirstEmptyDateField = function (dateStr) {
|
|
|
|
var used = false;
|
|
|
|
$selected_days.find('fieldset legend input').each(function () {
|
|
|
|
if (!used) {
|
|
|
|
if ($(this).val() == '') {
|
|
|
|
$(this).val(dateStr);
|
|
|
|
used = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return used;
|
|
|
|
};
|
|
|
|
|
2015-05-31 15:45:26 +02:00
|
|
|
// Handle form submission
|
|
|
|
$(document.formulaire).on('submit', function (e) {
|
2016-10-06 21:07:02 +02:00
|
|
|
if (!isSubmitDaysAvaillable()) {
|
2015-05-31 15:45:26 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-03-05 14:53:42 +01:00
|
|
|
// Button "Remove all hours"
|
|
|
|
|
|
|
|
$(document).on('click', '#resethours', function () {
|
|
|
|
$selected_days.find('fieldset').each(function () {
|
|
|
|
$(this).find('.hours:gt(2)').parent().remove();
|
|
|
|
});
|
|
|
|
$('#d0-h0').focus();
|
|
|
|
$selected_days.find('fieldset .hours').attr('value', '');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Button "Remove all days"
|
2016-10-06 21:07:02 +02:00
|
|
|
|
2015-03-05 14:53:42 +01:00
|
|
|
$('#resetdays').on('click', function () {
|
|
|
|
$selected_days.find('fieldset:gt(0)').remove();
|
|
|
|
$('#day0').focus();
|
2016-10-06 21:07:02 +02:00
|
|
|
updateButtonState();
|
2015-03-05 14:53:42 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Button "Copy hours of the first day"
|
|
|
|
|
2016-10-06 21:07:02 +02:00
|
|
|
$copyhours.on('click', function () {
|
2015-03-05 14:53:42 +01:00
|
|
|
var first_day_hours = $selected_days.find('fieldset:eq(0) .hours').map(function () {
|
|
|
|
return $(this).val();
|
|
|
|
});
|
|
|
|
|
|
|
|
$selected_days.find('fieldset:gt(0)').each(function () {
|
|
|
|
for (var i = 0; i < first_day_hours.length; i++) {
|
|
|
|
$(this).find('.hours:eq(' + i + ')').val(first_day_hours[i]); // fill hours
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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 =
|
|
|
|
'<div class="col-sm-2">' +
|
|
|
|
last_hour.html().replace(re_label, hour_text + ' ' + (hj + 2))
|
|
|
|
.replace(re_id, '"d' + di + '-h' + (hj + 1) + '"')
|
|
|
|
.replace(/value="(.*?)"/g, 'value=""') +
|
|
|
|
'</div>';
|
|
|
|
|
|
|
|
// After 11 + button is disable
|
2015-12-06 03:21:04 +01:00
|
|
|
if (hj < 99) {
|
2015-03-05 14:53:42 +01:00
|
|
|
last_hour.after(new_hour_html);
|
|
|
|
$('#d' + di + '-h' + (hj + 1)).focus();
|
|
|
|
$(this).prev('.remove-an-hour').removeClass('disabled');
|
2015-12-06 03:21:04 +01:00
|
|
|
if (hj == 98) {
|
2015-03-05 14:53:42 +01:00
|
|
|
$(this).addClass('disabled');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// Buttons "Remove an hour"
|
|
|
|
|
|
|
|
$(document).on('click', '.remove-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', ''));
|
|
|
|
|
|
|
|
// The first hour must not be removed
|
|
|
|
if (hj > 0) {
|
|
|
|
last_hour.remove();
|
|
|
|
$('#d' + di + '-h' + (hj - 1)).focus();
|
|
|
|
$(this).next('.add-an-hour').removeClass('disabled');
|
|
|
|
if (hj == 1) {
|
|
|
|
$(this).addClass('disabled');
|
|
|
|
}
|
|
|
|
}
|
2016-10-06 21:07:02 +02:00
|
|
|
updateButtonState();
|
2015-03-05 14:53:42 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Button "Add a day"
|
|
|
|
|
|
|
|
$('#add-a-day').on('click', function () {
|
2015-10-30 22:08:38 +01:00
|
|
|
newDateFields();
|
2015-03-05 14:53:42 +01:00
|
|
|
});
|
|
|
|
|
2016-08-08 07:09:51 +02:00
|
|
|
// Button "Remove a day"
|
|
|
|
|
2016-10-06 21:07:02 +02:00
|
|
|
$removeaday.on('click', function () {
|
2016-08-08 07:09:51 +02:00
|
|
|
$selected_days.find('fieldset:last').remove();
|
|
|
|
|
2016-10-06 22:10:51 +02:00
|
|
|
$('#day' + (getLastDayNumber() - 1)).focus();
|
2016-10-06 21:07:02 +02:00
|
|
|
|
|
|
|
updateButtonState();
|
2016-08-08 07:09:51 +02:00
|
|
|
});
|
|
|
|
|
2016-08-07 12:22:58 +02:00
|
|
|
// Button "Remove the current day"
|
2015-03-05 14:53:42 +01:00
|
|
|
|
2016-08-07 12:22:58 +02:00
|
|
|
$(document).on('click', '.remove-day', function () {
|
|
|
|
if ($('#days_container').find('fieldset').length > 1) {
|
|
|
|
$(this).parents('fieldset').remove();
|
2015-03-05 14:53:42 +01:00
|
|
|
}
|
2016-10-06 21:07:02 +02:00
|
|
|
updateButtonState();
|
2015-03-05 14:53:42 +01:00
|
|
|
});
|
|
|
|
|
2015-10-30 22:08:38 +01:00
|
|
|
// Add an range of dates
|
|
|
|
|
|
|
|
$('#interval_add').on('click', function (ev) {
|
|
|
|
var startDateField = $('#range_start');
|
|
|
|
var endDateField = $('#range_end');
|
|
|
|
var startDate = parseDate(startDateField.val(), window.date_formats.DATE);
|
|
|
|
var endDate = parseDate(endDateField.val(), window.date_formats.DATE);
|
|
|
|
|
2015-11-30 22:06:07 +01:00
|
|
|
// Clear error classes
|
|
|
|
startDateField.parent().removeClass('has-error');
|
|
|
|
endDateField.parent().removeClass('has-error');
|
2015-10-30 22:08:38 +01:00
|
|
|
|
2016-04-26 22:45:59 +02:00
|
|
|
var maxDates = 123; // 123 = 4 months
|
|
|
|
var tooMuchDates = endDate - startDate > maxDates * 86400 * 1000;
|
|
|
|
|
|
|
|
if (startDate != null && endDate != null && !tooMuchDates) {
|
2015-11-30 22:23:57 +01:00
|
|
|
if (startDate <= endDate) {
|
|
|
|
while (startDate <= endDate) {
|
|
|
|
var dateStr = formatDate(startDate, window.date_formats.DATE);
|
|
|
|
if (!useFirstEmptyDateField(dateStr)) {
|
|
|
|
newDateFields(dateStr);
|
|
|
|
}
|
|
|
|
startDate.setDate(startDate.getDate() + 1);
|
2015-10-30 22:08:38 +01:00
|
|
|
}
|
|
|
|
|
2015-11-30 22:23:57 +01:00
|
|
|
// Hide modal
|
|
|
|
startDateField.val('');
|
|
|
|
endDateField.val('');
|
|
|
|
$('#add_days').modal('hide');
|
2016-10-06 21:07:02 +02:00
|
|
|
updateButtonState();
|
2015-11-30 22:23:57 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
setTimeout(function () {
|
|
|
|
startDateField.parent().addClass('has-error');
|
|
|
|
endDateField.parent().addClass('has-error');
|
|
|
|
}, 200);
|
2015-11-30 22:06:07 +01:00
|
|
|
|
2015-11-30 22:23:57 +01:00
|
|
|
}
|
2015-10-30 22:08:38 +01:00
|
|
|
} else {
|
2015-11-30 22:12:09 +01:00
|
|
|
setTimeout(function () {
|
2016-04-26 22:45:59 +02:00
|
|
|
if (startDate == null || tooMuchDates) {
|
2015-11-30 22:23:57 +01:00
|
|
|
startDateField.parent().addClass('has-error');
|
|
|
|
}
|
2016-04-26 22:45:59 +02:00
|
|
|
if (endDate == null || tooMuchDates) {
|
2015-11-30 22:23:57 +01:00
|
|
|
endDateField.parent().addClass('has-error');
|
|
|
|
}
|
2015-11-30 22:12:09 +01:00
|
|
|
}, 200);
|
2015-11-30 22:06:07 +01:00
|
|
|
|
2015-10-30 22:08:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-03-05 14:53:42 +01:00
|
|
|
// Title update on hours and buttons -/+ hours
|
|
|
|
|
2015-03-05 21:01:26 +01:00
|
|
|
$(document).on('change', '.input-group.date input', function () {
|
2015-03-05 18:10:41 +01:00
|
|
|
// Define title on hours fields using the value of the new date
|
2015-03-05 14:53:42 +01:00
|
|
|
$selected_days.find('.hours').each(function () {
|
|
|
|
$(this).attr('title', $(this).parents('fieldset').find('legend input').val() + ' - ' + $(this).attr('placeholder'));
|
|
|
|
});
|
2015-03-05 18:10:41 +01:00
|
|
|
// Define title on buttons that add/remove hours using the value of the new date
|
2015-03-05 14:53:42 +01:00
|
|
|
$('#selected-days .add-an-hour, #selected-days .remove-an-hour').each(function () {
|
2015-03-05 18:10:41 +01:00
|
|
|
var title = $(this).attr('title');
|
2015-03-05 14:53:42 +01:00
|
|
|
|
2015-03-05 18:10:41 +01:00
|
|
|
if (title.indexOf('-') > 0) {
|
|
|
|
title = title.substring(title.indexOf('-') + 2, title.length);
|
2015-03-05 14:53:42 +01:00
|
|
|
}
|
2015-03-05 18:10:41 +01:00
|
|
|
$(this).attr('title', $(this).parents('fieldset').find('legend input').val() + ' - ' + title);
|
2015-03-05 14:53:42 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-10-06 21:07:02 +02:00
|
|
|
$(document).on('keyup change', '.hours, #selected-days fieldset legend input', function () {
|
|
|
|
updateButtonState();
|
2015-03-05 14:53:42 +01:00
|
|
|
});
|
2016-10-06 21:07:02 +02:00
|
|
|
updateButtonState();
|
|
|
|
});
|