mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
Email feedback: "If i write the expiration date with the keyboard it turns red, if i choose it with the date picker it is yellow. Can you please tell me the difference?" It is a bug, and the colour was never it. `<input type="date">` reports its value as YYYY-MM-DD, but a browser lets the YEAR sub-field be typed as two digits and reports exactly that: entering 31-12-26 gives "0026-12-31", the year 26 AD. That is a perfectly valid Date, so nothing refused it, and the card was saved with a due date two thousand years in the past. Red is what an overdue date is supposed to look like. The same day chosen from the calendar always fills four digits, so it is 2026 and yellow, because it is only due soon. The attached screenshot shows it rather than the colours: the yellow badges read "31-12-2026 17:00" and the red ones "31-12-26 17:00" - two different years. Saving now refuses a year outside 1000-9999 with "Invalid year. Please type all four digits, for example 2026." and puts the cursor back in the date field. The window is deliberately wide: WeKan holds real historical received dates and long-dated deadlines, and this is only meant to catch a year that cannot have been intended. REFUSED, NOT CORRECTED. 0026 was almost certainly meant as 2026, but silently rewriting a date somebody typed - one that other people's due-date reminders hang off - is a guess in the wrong place. The message says which digits are missing, which is the thing the reporter could not see. tests/datePickerYear.test.cjs pins the reported value the browser really hands over, the date that was meant, the width of the window either side, that the refusal happens before storeDate, and that nothing rewrites the year. Thanks to xet7 !
205 lines
7.2 KiB
JavaScript
205 lines
7.2 KiB
JavaScript
import { Template } from 'meteor/templating';
|
|
import { ReactiveVar } from 'meteor/reactive-var';
|
|
import { ReactiveCache } from '/imports/reactiveCache';
|
|
import { getCurrentCardFromContext } from '/client/lib/currentCard';
|
|
import { normalizeDigits } from '/imports/lib/dateUtils';
|
|
|
|
// The window a typed year has to fall in. Wide on purpose: WeKan holds real
|
|
// historical received dates and long-dated deadlines, so this is only meant to
|
|
// catch a year that cannot have been intended - the two-digit one a browser
|
|
// reports verbatim (0026), and the five-digit slip in the other direction.
|
|
const MIN_PLAUSIBLE_YEAR = 1000;
|
|
const MAX_PLAUSIBLE_YEAR = 9999;
|
|
import {
|
|
isValidDate,
|
|
formatTime,
|
|
initialTimeValue,
|
|
fallbackSubmitTime,
|
|
} from '/imports/lib/datePickerTime';
|
|
|
|
// Format date as YYYY-MM-DD
|
|
function formatDate(date) {
|
|
if (!isValidDate(date)) return '';
|
|
const year = date.getFullYear();
|
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
const day = String(date.getDate()).padStart(2, '0');
|
|
return `${year}-${month}-${day}`;
|
|
}
|
|
|
|
/**
|
|
* Sets up datepicker state on a template instance.
|
|
* Call from onCreated. Stores state on tpl.datePicker.
|
|
*
|
|
* @param {TemplateInstance} tpl - The Blaze template instance
|
|
* @param {Object} options
|
|
* @param {string} [options.defaultTime='1970-01-01 08:00:00'] - Default time string
|
|
* @param {Date} [options.initialDate] - Initial date to set (if valid)
|
|
*/
|
|
export function setupDatePicker(tpl, { defaultTime = '1970-01-01 08:00:00', initialDate } = {}) {
|
|
const card = getCurrentCardFromContext() || Template.currentData();
|
|
tpl.datePicker = {
|
|
error: new ReactiveVar(''),
|
|
card,
|
|
date: new ReactiveVar(initialDate && isValidDate(new Date(initialDate)) ? new Date(initialDate) : new Date('invalid')),
|
|
defaultTime,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* onRendered logic for datepicker templates.
|
|
* Sets initial input values from the datePicker state.
|
|
*
|
|
* @param {TemplateInstance} tpl - The Blaze template instance
|
|
*/
|
|
export function datePickerRendered(tpl) {
|
|
const dp = tpl.datePicker;
|
|
const dateInput = tpl.find('#date');
|
|
const timeInput = tpl.find('#time');
|
|
|
|
if (dateInput && isValidDate(dp.date.get())) {
|
|
dateInput.value = formatDate(dp.date.get());
|
|
}
|
|
// Pre-fill the time input: an existing card date keeps its own time; a
|
|
// card without this date gets the configured default (e.g. 17:00 for due
|
|
// dates) instead of an empty field that would silently save as 12:00
|
|
// (#1502). The template already fills the input for existing dates, so
|
|
// only ever write into an empty input.
|
|
if (timeInput && !timeInput.value) {
|
|
const initial = initialTimeValue(dp.date.get(), dp.defaultTime);
|
|
if (initial) {
|
|
timeInput.value = initial;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns helpers object for datepicker templates.
|
|
* All helpers read from Template.instance().datePicker.
|
|
*/
|
|
export function datePickerHelpers() {
|
|
return {
|
|
error() {
|
|
return Template.instance().datePicker.error;
|
|
},
|
|
showDate() {
|
|
const dp = Template.instance().datePicker;
|
|
if (isValidDate(dp.date.get())) return formatDate(dp.date.get());
|
|
return '';
|
|
},
|
|
showTime() {
|
|
const dp = Template.instance().datePicker;
|
|
if (isValidDate(dp.date.get())) return formatTime(dp.date.get());
|
|
return '';
|
|
},
|
|
dateFormat() {
|
|
return 'YYYY-MM-DD';
|
|
},
|
|
timeFormat() {
|
|
return 'HH:mm';
|
|
},
|
|
startDayOfWeek() {
|
|
const currentUser = ReactiveCache.getCurrentUser();
|
|
if (currentUser) {
|
|
return currentUser.getStartDayOfWeek();
|
|
} else {
|
|
return 1;
|
|
}
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Returns events object for datepicker templates.
|
|
*
|
|
* @param {Object} callbacks
|
|
* @param {Function} callbacks.storeDate - Called with (date) when form is submitted
|
|
* @param {Function} callbacks.deleteDate - Called when delete button is clicked
|
|
*/
|
|
export function datePickerEvents({ storeDate, deleteDate }) {
|
|
return {
|
|
'change .js-date-field'(evt, tpl) {
|
|
// Native HTML date input validation. Normalize any non-Latin digits
|
|
// (e.g. Persian/Arabic-Indic) so parsing works in those locales (#5752).
|
|
const dateValue = normalizeDigits(tpl.find('#date').value);
|
|
if (dateValue) {
|
|
// HTML date input format is always YYYY-MM-DD
|
|
const dateObj = new Date(dateValue + 'T12:00:00');
|
|
if (isValidDate(dateObj)) {
|
|
tpl.datePicker.error.set('');
|
|
} else {
|
|
tpl.datePicker.error.set('invalid-date');
|
|
}
|
|
}
|
|
},
|
|
'change .js-time-field'(evt, tpl) {
|
|
// Native HTML time input validation. Normalize any non-Latin digits
|
|
// (e.g. Persian/Arabic-Indic) so parsing works in those locales (#5752).
|
|
const timeValue = normalizeDigits(tpl.find('#time').value);
|
|
if (timeValue) {
|
|
// HTML time input format is always HH:mm
|
|
const timeObj = new Date(`1970-01-01T${timeValue}:00`);
|
|
if (isValidDate(timeObj)) {
|
|
tpl.datePicker.error.set('');
|
|
} else {
|
|
tpl.datePicker.error.set('invalid-time');
|
|
}
|
|
}
|
|
},
|
|
'submit .edit-date'(evt, tpl) {
|
|
evt.preventDefault();
|
|
|
|
// Normalize any non-Latin digits (e.g. Persian/Arabic-Indic) before
|
|
// parsing so due/start/end dates work in those locales (#5752).
|
|
// An empty time falls back to the popup's configured default time
|
|
// (e.g. 17:00 for due dates), then 12:00 (#1502).
|
|
const dateValue = normalizeDigits(evt.target.date.value);
|
|
const timeValue =
|
|
normalizeDigits(evt.target.time.value) ||
|
|
fallbackSubmitTime(tpl.datePicker.defaultTime);
|
|
|
|
if (!dateValue) {
|
|
tpl.datePicker.error.set('invalid-date');
|
|
evt.target.date.focus();
|
|
return;
|
|
}
|
|
|
|
// Combine date and time: HTML date input is YYYY-MM-DD, time input is HH:mm
|
|
const dateTimeString = `${dateValue}T${timeValue}:00`;
|
|
const newCompleteDate = new Date(dateTimeString);
|
|
|
|
if (!isValidDate(newCompleteDate)) {
|
|
tpl.datePicker.error.set('invalid');
|
|
return;
|
|
}
|
|
|
|
// A TWO-DIGIT YEAR is a real date that is not the one anybody meant.
|
|
// `<input type="date">` reports YYYY-MM-DD, but the browser lets the year
|
|
// sub-field be typed as two digits and reports that literally: typing
|
|
// 31-12-26 gives "0026-12-31", the year 26 AD. Nothing above rejects it -
|
|
// it is a perfectly valid Date - so the card got a due date two thousand
|
|
// years in the past, which is why the reporter saw a date they had TYPED
|
|
// come out red (overdue) while the same date picked from the calendar,
|
|
// which always fills four digits, came out yellow (due soon). The colour
|
|
// was right; the year was wrong.
|
|
//
|
|
// Refused rather than corrected: 0026 could be meant as 2026, but guessing
|
|
// silently rewrites what somebody typed, and this is a date other people's
|
|
// reminders hang off. The error names the year so the fix is obvious.
|
|
const year = newCompleteDate.getFullYear();
|
|
if (year < MIN_PLAUSIBLE_YEAR || year > MAX_PLAUSIBLE_YEAR) {
|
|
tpl.datePicker.error.set('invalid-year');
|
|
evt.target.date.focus();
|
|
return;
|
|
}
|
|
|
|
storeDate.call(tpl, newCompleteDate);
|
|
Popup.back();
|
|
},
|
|
'click .js-delete-date'(evt, tpl) {
|
|
evt.preventDefault();
|
|
deleteDate.call(tpl);
|
|
Popup.back();
|
|
},
|
|
};
|
|
}
|