Fixed multi instace date/time Userfields (fixes #1520)

This commit is contained in:
Bernd Bestel 2021-06-23 22:13:54 +02:00
parent 9e3c68982b
commit 5e189c8a4a
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300
3 changed files with 130 additions and 122 deletions

View File

@ -45,6 +45,7 @@
### Userfield fixes ### Userfield fixes
- Fixed that numeric Userfields were initialised with `1.0` - Fixed that numeric Userfields were initialised with `1.0`
- Fixed that shortcuts (up/down key) and the format did not work correctly when using multiple date/time Userfields per object
### General & other improvements/fixes ### General & other improvements/fixes
- LDAP authentication improvements / OpenLDAP support (thanks @tank0226) - LDAP authentication improvements / OpenLDAP support (thanks @tank0226)

View File

@ -10,7 +10,7 @@ Grocy.Components.DateTimePicker.GetValue = function()
return Grocy.Components.DateTimePicker.GetInputElement().val(); return Grocy.Components.DateTimePicker.GetInputElement().val();
} }
Grocy.Components.DateTimePicker.SetValue = function(value) Grocy.Components.DateTimePicker.SetValue = function(value, inputElement = Grocy.Components.DateTimePicker.GetInputElement())
{ {
// "Click" the shortcut checkbox when the desired value is // "Click" the shortcut checkbox when the desired value is
// not the shortcut value and it is currently set // not the shortcut value and it is currently set
@ -19,10 +19,10 @@ Grocy.Components.DateTimePicker.SetValue = function(value)
{ {
$("#datetimepicker-shortcut").click(); $("#datetimepicker-shortcut").click();
} }
Grocy.Components.DateTimePicker.GetInputElement().val(value); inputElement.val(value);
Grocy.Components.DateTimePicker.GetInputElement().trigger('change'); inputElement.trigger('change');
Grocy.Components.DateTimePicker.GetInputElement().keyup(); inputElement.keyup();
} }
Grocy.Components.DateTimePicker.Clear = function() Grocy.Components.DateTimePicker.Clear = function()
@ -78,46 +78,49 @@ if (Grocy.Components.DateTimePicker.GetInputElement().data('limit-end-to-now') =
Grocy.Components.DateTimePicker.Init = function() Grocy.Components.DateTimePicker.Init = function()
{ {
$('.datetimepicker').datetimepicker( $(".datetimepicker").each(function()
{ {
format: Grocy.Components.DateTimePicker.GetInputElement().data('format'), $(this).datetimepicker(
buttons: { {
showToday: true, format: $(this).find("input").data('format'),
showClose: true buttons: {
}, showToday: true,
calendarWeeks: Grocy.CalendarShowWeekNumbers, showClose: true
maxDate: limitDate, },
locale: moment.locale(), calendarWeeks: Grocy.CalendarShowWeekNumbers,
defaultDate: startDate, maxDate: limitDate,
useCurrent: false, locale: moment.locale(),
icons: { defaultDate: startDate,
time: 'far fa-clock', useCurrent: false,
date: 'far fa-calendar', icons: {
up: 'fas fa-arrow-up', time: 'far fa-clock',
down: 'fas fa-arrow-down', date: 'far fa-calendar',
previous: 'fas fa-chevron-left', up: 'fas fa-arrow-up',
next: 'fas fa-chevron-right', down: 'fas fa-arrow-down',
today: 'fas fa-calendar-check', previous: 'fas fa-chevron-left',
clear: 'far fa-trash-alt', next: 'fas fa-chevron-right',
close: 'far fa-times-circle' today: 'fas fa-calendar-check',
}, clear: 'far fa-trash-alt',
sideBySide: true, close: 'far fa-times-circle'
keyBinds: { },
up: function(widget) { }, sideBySide: true,
down: function(widget) { }, keyBinds: {
'control up': function(widget) { }, up: function(widget) { },
'control down': function(widget) { }, down: function(widget) { },
left: function(widget) { }, 'control up': function(widget) { },
right: function(widget) { }, 'control down': function(widget) { },
pageUp: function(widget) { }, left: function(widget) { },
pageDown: function(widget) { }, right: function(widget) { },
enter: function(widget) { }, pageUp: function(widget) { },
escape: function(widget) { }, pageDown: function(widget) { },
'control space': function(widget) { }, enter: function(widget) { },
t: function(widget) { }, escape: function(widget) { },
'delete': function(widget) { } 'control space': function(widget) { },
} t: function(widget) { },
}); 'delete': function(widget) { }
}
});
});
} }
Grocy.Components.DateTimePicker.Init(); Grocy.Components.DateTimePicker.Init();
@ -125,22 +128,23 @@ Grocy.Components.DateTimePicker.GetInputElement().on('keyup', function(e)
{ {
$('.datetimepicker').datetimepicker('hide'); $('.datetimepicker').datetimepicker('hide');
var value = Grocy.Components.DateTimePicker.GetValue(); var inputElement = $(e.currentTarget)
var value = inputElement.val();
var now = new Date(); var now = new Date();
var centuryStart = Number.parseInt(now.getFullYear().toString().substring(0, 2) + '00'); var centuryStart = Number.parseInt(now.getFullYear().toString().substring(0, 2) + '00');
var centuryEnd = Number.parseInt(now.getFullYear().toString().substring(0, 2) + '99'); var centuryEnd = Number.parseInt(now.getFullYear().toString().substring(0, 2) + '99');
var format = Grocy.Components.DateTimePicker.GetInputElement().data('format'); var format = inputElement.data('format');
var nextInputElement = $(Grocy.Components.DateTimePicker.GetInputElement().data('next-input-selector')); 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)) if (value.length === 0 && (e.keyCode === 38 || e.keyCode === 40 || e.keyCode === 37 || e.keyCode === 39))
{ {
Grocy.Components.DateTimePicker.SetValue(moment(new Date(), format, true).format(format)); Grocy.Components.DateTimePicker.SetValue(moment(new Date(), format, true).format(format), inputElement);
nextInputElement.focus(); nextInputElement.focus();
} }
else if (value === 'x' || value === 'X') else if (value === 'x' || value === 'X')
{ {
Grocy.Components.DateTimePicker.SetValue(moment('2999-12-31 23:59:59').format(format)); Grocy.Components.DateTimePicker.SetValue(moment('2999-12-31 23:59:59').format(format), inputElement);
nextInputElement.focus(); 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))
@ -150,18 +154,18 @@ Grocy.Components.DateTimePicker.GetInputElement().on('keyup', function(e)
{ {
date.add(1, "year"); date.add(1, "year");
} }
Grocy.Components.DateTimePicker.SetValue(date.format(format)); Grocy.Components.DateTimePicker.SetValue(date.format(format), inputElement);
nextInputElement.focus(); nextInputElement.focus();
} }
else if (value.length === 8 && $.isNumeric(value)) else if (value.length === 8 && $.isNumeric(value))
{ {
Grocy.Components.DateTimePicker.SetValue(value.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3')); Grocy.Components.DateTimePicker.SetValue(value.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3'), inputElement);
nextInputElement.focus(); 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() === "+"))
{ {
var date = moment(value.substring(0, 4) + "-" + value.substring(4, 6) + "-01").endOf("month"); var date = moment(value.substring(0, 4) + "-" + value.substring(4, 6) + "-01").endOf("month");
Grocy.Components.DateTimePicker.SetValue(date.format(format)); Grocy.Components.DateTimePicker.SetValue(date.format(format), inputElement);
nextInputElement.focus(); nextInputElement.focus();
} }
else else
@ -175,19 +179,19 @@ Grocy.Components.DateTimePicker.GetInputElement().on('keyup', function(e)
if (e.keyCode === 38) // Up if (e.keyCode === 38) // Up
{ {
Grocy.Components.DateTimePicker.SetValue(dateObj.add(-1, 'months').format(format)); Grocy.Components.DateTimePicker.SetValue(dateObj.add(-1, 'months').format(format), inputElement);
} }
else if (e.keyCode === 40) // Down else if (e.keyCode === 40) // Down
{ {
Grocy.Components.DateTimePicker.SetValue(dateObj.add(1, 'months').format(format)); Grocy.Components.DateTimePicker.SetValue(dateObj.add(1, 'months').format(format), inputElement);
} }
else if (e.keyCode === 37) // Left else if (e.keyCode === 37) // Left
{ {
Grocy.Components.DateTimePicker.SetValue(dateObj.add(-1, 'years').format(format)); Grocy.Components.DateTimePicker.SetValue(dateObj.add(-1, 'years').format(format), inputElement);
} }
else if (e.keyCode === 39) // Right else if (e.keyCode === 39) // Right
{ {
Grocy.Components.DateTimePicker.SetValue(dateObj.add(1, 'years').format(format)); Grocy.Components.DateTimePicker.SetValue(dateObj.add(1, 'years').format(format), inputElement);
} }
} }
else else
@ -196,19 +200,19 @@ Grocy.Components.DateTimePicker.GetInputElement().on('keyup', function(e)
if (e.keyCode === 38) // Up if (e.keyCode === 38) // Up
{ {
Grocy.Components.DateTimePicker.SetValue(dateObj.add(-1, 'days').format(format)); Grocy.Components.DateTimePicker.SetValue(dateObj.add(-1, 'days').format(format), inputElement);
} }
else if (e.keyCode === 40) // Down else if (e.keyCode === 40) // Down
{ {
Grocy.Components.DateTimePicker.SetValue(dateObj.add(1, 'days').format(format)); Grocy.Components.DateTimePicker.SetValue(dateObj.add(1, 'days').format(format), inputElement);
} }
else if (e.keyCode === 37) // Left else if (e.keyCode === 37) // Left
{ {
Grocy.Components.DateTimePicker.SetValue(dateObj.add(-1, 'weeks').format(format)); Grocy.Components.DateTimePicker.SetValue(dateObj.add(-1, 'weeks').format(format), inputElement);
} }
else if (e.keyCode === 39) // Right else if (e.keyCode === 39) // Right
{ {
Grocy.Components.DateTimePicker.SetValue(dateObj.add(1, 'weeks').format(format)); Grocy.Components.DateTimePicker.SetValue(dateObj.add(1, 'weeks').format(format), inputElement);
} }
} }
} }

View File

@ -10,20 +10,19 @@ Grocy.Components.DateTimePicker2.GetValue = function()
return Grocy.Components.DateTimePicker2.GetInputElement().val(); return Grocy.Components.DateTimePicker2.GetInputElement().val();
} }
Grocy.Components.DateTimePicker2.SetValue = function(value) Grocy.Components.DateTimePicker2.SetValue = function(value, inputElement = Grocy.Components.DateTimePicker2.GetInputElement())
{ {
// "Click" the shortcut checkbox when the desired value is // "Click" the shortcut checkbox when the desired value is
// not the shortcut value and it is currently set // not the shortcut value and it is currently set
var shortcutValue = $("#datetimepicker2-shortcut").data("datetimepicker2-shortcut-value"); var shortcutValue = $("#datetimepicker2-shortcut").data("datetimepicker-shortcut-value");
if (value != shortcutValue && $("#datetimepicker2-shortcut").is(":checked")) if (value != shortcutValue && $("#datetimepicker2-shortcut").is(":checked"))
{ {
$("#datetimepicker2-shortcut").click(); $("#datetimepicker2-shortcut").click();
} }
inputElement.val(value);
inputElement.trigger('change');
Grocy.Components.DateTimePicker2.GetInputElement().val(value); inputElement.keyup();
Grocy.Components.DateTimePicker2.GetInputElement().trigger('change');
Grocy.Components.DateTimePicker2.GetInputElement().keyup();
} }
Grocy.Components.DateTimePicker2.Clear = function() Grocy.Components.DateTimePicker2.Clear = function()
@ -79,46 +78,49 @@ if (Grocy.Components.DateTimePicker2.GetInputElement().data('limit-end-to-now')
Grocy.Components.DateTimePicker2.Init = function() Grocy.Components.DateTimePicker2.Init = function()
{ {
$('.datetimepicker2').datetimepicker( $(".datetimepicker2").each(function()
{ {
format: Grocy.Components.DateTimePicker2.GetInputElement().data('format'), $(this).datetimepicker(
buttons: { {
showToday: true, format: $(this).find("input").data('format'),
showClose: true buttons: {
}, showToday: true,
calendarWeeks: Grocy.CalendarShowWeekNumbers, showClose: true
maxDate: limitDate, },
locale: moment.locale(), calendarWeeks: Grocy.CalendarShowWeekNumbers,
defaultDate: startDate, maxDate: limitDate,
useCurrent: false, locale: moment.locale(),
icons: { defaultDate: startDate,
time: 'far fa-clock', useCurrent: false,
date: 'far fa-calendar', icons: {
up: 'fas fa-arrow-up', time: 'far fa-clock',
down: 'fas fa-arrow-down', date: 'far fa-calendar',
previous: 'fas fa-chevron-left', up: 'fas fa-arrow-up',
next: 'fas fa-chevron-right', down: 'fas fa-arrow-down',
today: 'fas fa-calendar-check', previous: 'fas fa-chevron-left',
clear: 'far fa-trash-alt', next: 'fas fa-chevron-right',
close: 'far fa-times-circle' today: 'fas fa-calendar-check',
}, clear: 'far fa-trash-alt',
sideBySide: true, close: 'far fa-times-circle'
keyBinds: { },
up: function(widget) { }, sideBySide: true,
down: function(widget) { }, keyBinds: {
'control up': function(widget) { }, up: function(widget) { },
'control down': function(widget) { }, down: function(widget) { },
left: function(widget) { }, 'control up': function(widget) { },
right: function(widget) { }, 'control down': function(widget) { },
pageUp: function(widget) { }, left: function(widget) { },
pageDown: function(widget) { }, right: function(widget) { },
enter: function(widget) { }, pageUp: function(widget) { },
escape: function(widget) { }, pageDown: function(widget) { },
'control space': function(widget) { }, enter: function(widget) { },
t: function(widget) { }, escape: function(widget) { },
'delete': function(widget) { } 'control space': function(widget) { },
} t: function(widget) { },
}); 'delete': function(widget) { }
}
});
});
} }
Grocy.Components.DateTimePicker2.Init(); Grocy.Components.DateTimePicker2.Init();
@ -126,22 +128,23 @@ Grocy.Components.DateTimePicker2.GetInputElement().on('keyup', function(e)
{ {
$('.datetimepicker2').datetimepicker('hide'); $('.datetimepicker2').datetimepicker('hide');
var value = Grocy.Components.DateTimePicker2.GetValue(); var inputElement = $(e.currentTarget)
var value = inputElement.val();
var now = new Date(); var now = new Date();
var centuryStart = Number.parseInt(now.getFullYear().toString().substring(0, 2) + '00'); var centuryStart = Number.parseInt(now.getFullYear().toString().substring(0, 2) + '00');
var centuryEnd = Number.parseInt(now.getFullYear().toString().substring(0, 2) + '99'); var centuryEnd = Number.parseInt(now.getFullYear().toString().substring(0, 2) + '99');
var format = Grocy.Components.DateTimePicker2.GetInputElement().data('format'); var format = inputElement.data('format');
var nextInputElement = $(Grocy.Components.DateTimePicker2.GetInputElement().data('next-input-selector')); 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)) 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)); Grocy.Components.DateTimePicker2.SetValue(moment(new Date(), format, true).format(format), inputElement);
nextInputElement.focus(); nextInputElement.focus();
} }
else if (value === 'x' || value === 'X') else if (value === 'x' || value === 'X')
{ {
Grocy.Components.DateTimePicker2.SetValue(moment('2999-12-31 23:59:59').format(format)); Grocy.Components.DateTimePicker2.SetValue(moment('2999-12-31 23:59:59').format(format), inputElement);
nextInputElement.focus(); 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))
@ -151,18 +154,18 @@ Grocy.Components.DateTimePicker2.GetInputElement().on('keyup', function(e)
{ {
date.add(1, "year"); date.add(1, "year");
} }
Grocy.Components.DateTimePicker2.SetValue(date.format(format)); Grocy.Components.DateTimePicker2.SetValue(date.format(format), inputElement);
nextInputElement.focus(); nextInputElement.focus();
} }
else if (value.length === 8 && $.isNumeric(value)) else if (value.length === 8 && $.isNumeric(value))
{ {
Grocy.Components.DateTimePicker2.SetValue(value.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3')); Grocy.Components.DateTimePicker2.SetValue(value.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3'), inputElement);
nextInputElement.focus(); 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() === "+"))
{ {
var date = moment(value.substring(0, 4) + "-" + value.substring(4, 6) + "-01").endOf("month"); var date = moment(value.substring(0, 4) + "-" + value.substring(4, 6) + "-01").endOf("month");
Grocy.Components.DateTimePicker2.SetValue(date.format(format)); Grocy.Components.DateTimePicker2.SetValue(date.format(format), inputElement);
nextInputElement.focus(); nextInputElement.focus();
} }
else else
@ -176,19 +179,19 @@ Grocy.Components.DateTimePicker2.GetInputElement().on('keyup', function(e)
if (e.keyCode === 38) // Up if (e.keyCode === 38) // Up
{ {
Grocy.Components.DateTimePicker2.SetValue(dateObj.add(-1, 'months').format(format)); Grocy.Components.DateTimePicker2.SetValue(dateObj.add(-1, 'months').format(format), inputElement);
} }
else if (e.keyCode === 40) // Down else if (e.keyCode === 40) // Down
{ {
Grocy.Components.DateTimePicker2.SetValue(dateObj.add(1, 'months').format(format)); Grocy.Components.DateTimePicker2.SetValue(dateObj.add(1, 'months').format(format), inputElement);
} }
else if (e.keyCode === 37) // Left else if (e.keyCode === 37) // Left
{ {
Grocy.Components.DateTimePicker2.SetValue(dateObj.add(-1, 'years').format(format)); Grocy.Components.DateTimePicker2.SetValue(dateObj.add(-1, 'years').format(format), inputElement);
} }
else if (e.keyCode === 39) // Right else if (e.keyCode === 39) // Right
{ {
Grocy.Components.DateTimePicker2.SetValue(dateObj.add(1, 'years').format(format)); Grocy.Components.DateTimePicker2.SetValue(dateObj.add(1, 'years').format(format), inputElement);
} }
} }
else else
@ -197,19 +200,19 @@ Grocy.Components.DateTimePicker2.GetInputElement().on('keyup', function(e)
if (e.keyCode === 38) // Up if (e.keyCode === 38) // Up
{ {
Grocy.Components.DateTimePicker2.SetValue(dateObj.add(-1, 'days').format(format)); Grocy.Components.DateTimePicker2.SetValue(dateObj.add(-1, 'days').format(format), inputElement);
} }
else if (e.keyCode === 40) // Down else if (e.keyCode === 40) // Down
{ {
Grocy.Components.DateTimePicker2.SetValue(dateObj.add(1, 'days').format(format)); Grocy.Components.DateTimePicker2.SetValue(dateObj.add(1, 'days').format(format), inputElement);
} }
else if (e.keyCode === 37) // Left else if (e.keyCode === 37) // Left
{ {
Grocy.Components.DateTimePicker2.SetValue(dateObj.add(-1, 'weeks').format(format)); Grocy.Components.DateTimePicker2.SetValue(dateObj.add(-1, 'weeks').format(format), inputElement);
} }
else if (e.keyCode === 39) // Right else if (e.keyCode === 39) // Right
{ {
Grocy.Components.DateTimePicker2.SetValue(dateObj.add(1, 'weeks').format(format)); Grocy.Components.DateTimePicker2.SetValue(dateObj.add(1, 'weeks').format(format), inputElement);
} }
} }
} }