Complete date field on the all the screen that call framadatepicker.js

This commit is contained in:
Olivier PEREZ 2015-03-05 21:01:26 +01:00
parent d6500a77d9
commit f8611c09d6
2 changed files with 64 additions and 58 deletions

View File

@ -174,41 +174,7 @@
// Title update on hours and buttons -/+ hours
var leftPad = function(text, pad) {
return text ? pad.substring(0, pad.length - text.length) + text : text;
};
$(document).on('change', '#selected-days legend input', function () {
// Complete field if needed
var val = $(this).val();
var capture = /([0-9]+)(?:\/([0-9]+))?/.exec(val);
if (capture) {
var inputDay = leftPad(capture[1], "00"); // 5->05, 15->15
var inputMonth = leftPad(capture[2], "00"); // 3->03, 11->11
var inputDate = null;
var now = new Date();
if (inputMonth) {
inputDate = new Date(now.getFullYear() + '-' + inputMonth + '-' + inputDay);
// If new date is before now, add 1 year
if (inputDate < now) {
inputDate.setFullYear(now.getFullYear() + 1);
}
} else {
inputDate = new Date(now.getFullYear() + '-' + leftPad(""+(now.getMonth() + 1), "00") + '-' + inputDay);
// If new date is before now, add 1 month
if (inputDate < now) {
inputDate.setMonth(now.getMonth() + 1);
}
}
$(this).val(inputDate.toLocaleFormat("%d/%m/%Y"));
}
$(document).on('change', '.input-group.date input', function () {
// Define title on hours fields using the value of the new date
$selected_days.find('.hours').each(function () {
$(this).attr('title', $(this).parents('fieldset').find('legend input').val() + ' - ' + $(this).attr('placeholder'));

View File

@ -15,8 +15,8 @@
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/
var init_datepicker = function() {
$(document).ready(function() {
var init_datepicker = function () {
$('.input-group.date').datepicker({
format: "dd/mm/yyyy",
todayBtn: "linked",
@ -24,17 +24,17 @@ var init_datepicker = function() {
autoclose: true,
language: lang,
todayHighlight: true,
beforeShowDay: function (date){
beforeShowDay: function (date) {
var $selected_days = [];
$('#selected-days').find('input[id^="day"]').each(function() {
if($(this).val()!='') {
$('#selected-days').find('input[id^="day"]').each(function () {
if ($(this).val() != '') {
$selected_days.push($(this).val());
}
});
for(var i = 0; i < $selected_days.length; i++){
for (var 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]){
if (date.getFullYear() == $selected_date[2] && (date.getMonth() + 1) == $selected_date[1] && date.getDate() == $selected_date[0]) {
return {
classes: 'disabled selected'
};
@ -42,4 +42,44 @@ var init_datepicker = function() {
}
}
});
};
};
// Complete the date fields when use partialy fill it (eg: 15/01 could become 15/01/2016)
var leftPad = function(text, pad) {
return text ? pad.substring(0, pad.length - text.length) + text : text;
};
$(document).on('change', '.input-group.date input', function () {
// Complete field if needed
var val = $(this).val();
var capture = /([0-9]+)(?:\/([0-9]+))?/.exec(val);
if (capture) {
var inputDay = leftPad(capture[1], "00"); // 5->05, 15->15
var inputMonth = leftPad(capture[2], "00"); // 3->03, 11->11
var inputDate = null;
var now = new Date();
if (inputMonth) {
inputDate = new Date(now.getFullYear() + '-' + inputMonth + '-' + inputDay);
// If new date is before now, add 1 year
if (inputDate < now) {
inputDate.setFullYear(now.getFullYear() + 1);
}
} else {
inputDate = new Date(now.getFullYear() + '-' + leftPad("" + (now.getMonth() + 1), "00") + '-' + inputDay);
// If new date is before now, add 1 month
if (inputDate < now) {
inputDate.setMonth(now.getMonth() + 1);
}
}
$(this).val(inputDate.toLocaleFormat("%d/%m/%Y"));
}
});
});