From f8611c09d694a144f79b770dfcdcec07c9009ff1 Mon Sep 17 00:00:00 2001 From: Olivier PEREZ Date: Thu, 5 Mar 2015 21:01:26 +0100 Subject: [PATCH] Complete date field on the all the screen that call framadatepicker.js --- js/app/date_poll.js | 36 +--------------- js/app/framadatepicker.js | 86 ++++++++++++++++++++++++++++----------- 2 files changed, 64 insertions(+), 58 deletions(-) diff --git a/js/app/date_poll.js b/js/app/date_poll.js index 635b610..7faf044 100644 --- a/js/app/date_poll.js +++ b/js/app/date_poll.js @@ -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')); diff --git a/js/app/framadatepicker.js b/js/app/framadatepicker.js index 9f3667b..72b5aa6 100644 --- a/js/app/framadatepicker.js +++ b/js/app/framadatepicker.js @@ -15,31 +15,71 @@ * Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaƫl DROZ * Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft) */ +$(document).ready(function() { + var init_datepicker = 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 = []; + $('#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++) { + var $selected_date = $selected_days[i].split('/'); -var init_datepicker = 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 = []; - $('#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++){ - 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' - }; + if (date.getFullYear() == $selected_date[2] && (date.getMonth() + 1) == $selected_date[1] && date.getDate() == $selected_date[0]) { + return { + classes: 'disabled selected' + }; + } } } + }); + }; + + // 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")); } }); -}; \ No newline at end of file + +}); \ No newline at end of file