mirror of
https://github.com/grocy/grocy.git
synced 2025-08-18 03:25:50 +00:00
Added new relative date input shorthand (closes #1773)
This commit is contained in:
@@ -130,24 +130,25 @@ Grocy.Components.DateTimePicker2.GetInputElement().on('keyup', function(e)
|
||||
|
||||
var inputElement = $(e.currentTarget)
|
||||
var value = inputElement.val();
|
||||
var lastCharacter = value.slice(-1).toLowerCase();
|
||||
var now = new Date();
|
||||
var centuryStart = Number.parseInt(now.getFullYear().toString().substring(0, 2) + '00');
|
||||
var centuryEnd = Number.parseInt(now.getFullYear().toString().substring(0, 2) + '99');
|
||||
var format = inputElement.data('format');
|
||||
var nextInputElement = $(inputElement.data('next-input-selector'));
|
||||
|
||||
//If input is empty and any arrow key is pressed, set date to today
|
||||
// If input is empty and any arrow key is pressed, set date to today
|
||||
if (value.length === 0 && (e.keyCode === 38 || e.keyCode === 40 || e.keyCode === 37 || e.keyCode === 39))
|
||||
{
|
||||
Grocy.Components.DateTimePicker2.SetValue(moment(new Date(), format, true).format(format), inputElement);
|
||||
nextInputElement.focus();
|
||||
}
|
||||
else if (value === 'x' || value === 'X')
|
||||
else if (value === 'x' || value === 'X') // Shorthand for never overdue
|
||||
{
|
||||
Grocy.Components.DateTimePicker2.SetValue(moment('2999-12-31 23:59:59').format(format), inputElement);
|
||||
nextInputElement.focus();
|
||||
}
|
||||
else if (value.length === 4 && !(Number.parseInt(value) > centuryStart && Number.parseInt(value) < centuryEnd))
|
||||
else if (value.length === 4 && !(Number.parseInt(value) > centuryStart && Number.parseInt(value) < centuryEnd)) // Shorthand for MMDD
|
||||
{
|
||||
var date = moment((new Date()).getFullYear().toString() + value);
|
||||
if (date.isBefore(moment()))
|
||||
@@ -157,17 +158,38 @@ Grocy.Components.DateTimePicker2.GetInputElement().on('keyup', function(e)
|
||||
Grocy.Components.DateTimePicker2.SetValue(date.format(format), inputElement);
|
||||
nextInputElement.focus();
|
||||
}
|
||||
else if (value.length === 8 && $.isNumeric(value))
|
||||
else if (value.length === 8 && $.isNumeric(value)) // Shorthand for YYYYMMDD
|
||||
{
|
||||
Grocy.Components.DateTimePicker2.SetValue(value.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3'), inputElement);
|
||||
nextInputElement.focus();
|
||||
}
|
||||
else if (value.length === 7 && $.isNumeric(value.substring(0, 6)) && (value.substring(6, 7).toLowerCase() === "e" || value.substring(6, 7).toLowerCase() === "+"))
|
||||
else if (value.length === 7 && $.isNumeric(value.substring(0, 6)) && (value.substring(6, 7).toLowerCase() === "e" || value.substring(6, 7).toLowerCase() === "+")) // Shorthand for YYYYMM[e/+]
|
||||
{
|
||||
var date = moment(value.substring(0, 4) + "-" + value.substring(4, 6) + "-01").endOf("month");
|
||||
Grocy.Components.DateTimePicker2.SetValue(date.format(format), inputElement);
|
||||
nextInputElement.focus();
|
||||
}
|
||||
else if ((value.startsWith("+") || value.startsWith("-")) && (lastCharacter == "d" || lastCharacter == "m" || lastCharacter == "y")) // Shorthand for [+/-]n[d/m/y]
|
||||
{
|
||||
var n = parseInt(value.substring(1, value.length - 1));
|
||||
if (value.startsWith("-"))
|
||||
{
|
||||
n = n * -1;
|
||||
}
|
||||
|
||||
if (lastCharacter == "d")
|
||||
{
|
||||
Grocy.Components.DateTimePicker2.SetValue(moment().add(n, "days").format(format));
|
||||
}
|
||||
else if (lastCharacter == "m")
|
||||
{
|
||||
Grocy.Components.DateTimePicker2.SetValue(moment().add(n, "months").format(format));
|
||||
}
|
||||
else if (lastCharacter == "y")
|
||||
{
|
||||
Grocy.Components.DateTimePicker2.SetValue(moment().add(n, "years").format(format));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var dateObj = moment(value, format, true);
|
||||
|
Reference in New Issue
Block a user