implement new "units" parameter to limit used units in date-since widget

eg. <date-since units="d" shows age in days
This commit is contained in:
Ralf Becker 2022-01-12 12:47:28 +02:00
parent 9e655d4de2
commit 26e09680c2
2 changed files with 28 additions and 3 deletions

View File

@ -1060,6 +1060,7 @@ var et2_date_ro = /** @class */ (function (_super) {
return _this;
}
et2_date_ro.prototype.set_value = function (_value) {
var _this = this;
if (typeof _value == 'undefined')
_value = 0;
this.value = _value;
@ -1156,9 +1157,16 @@ var et2_date_ro = /** @class */ (function (_super) {
var d = new Date();
var diff = Math.round(d.valueOf() / 1000) - Math.round(this.date.valueOf() / 1000);
display = '';
// limit units used to display
var smallest = 's';
if (this.options.units) {
var valid = Object.entries(unit2s).filter(function (e) { return _this.options.units.includes(e[0]); });
unit2s = Object.fromEntries(valid);
smallest = (valid.pop() || [])[0];
}
for (var unit in unit2s) {
var unit_s = unit2s[unit];
if (diff >= unit_s || unit == 's') {
if (diff >= unit_s || unit === smallest) {
display = Math.round(diff / unit_s) + ' ' + this.egw().lang(unit2label[unit]);
break;
}
@ -1232,6 +1240,10 @@ var et2_date_ro = /** @class */ (function (_super) {
"ignore": true,
"description": "Format data is in. This is not used client-side because it's always a timestamp client side."
},
units: {
"type": "string",
"descriptions": "Used units for date-since, default 'YmdHis', e.g. 'd' to display a value in days"
},
min: { ignore: true },
max: { ignore: true },
year_range: { ignore: true }

View File

@ -1227,6 +1227,10 @@ export class et2_date_ro extends et2_valueWidget implements et2_IDetachedDOM
"ignore": true,
"description": "Format data is in. This is not used client-side because it's always a timestamp client side."
},
units: {
"type": "string",
"descriptions": "Used units for date-since, default 'YmdHis', e.g. 'd' to display a value in days"
},
min: {ignore: true},
max: {ignore: true},
year_range: {ignore: true}
@ -1360,7 +1364,7 @@ export class et2_date_ro extends et2_valueWidget implements et2_IDetachedDOM
'i': 'minutes',
's': 'seconds'
};
var unit2s = {
var unit2s : Object = {
'Y': 31536000,
'm': 2628000,
'd': 86400,
@ -1372,10 +1376,19 @@ export class et2_date_ro extends et2_valueWidget implements et2_IDetachedDOM
var diff = Math.round(d.valueOf() / 1000) - Math.round(this.date.valueOf()/1000);
display = '';
// limit units used to display
let smallest = 's';
if (this.options.units)
{
const valid = Object.entries(unit2s).filter((e) => (<string>this.options.units).includes(e[0]));
unit2s = Object.fromEntries(valid);
smallest = (valid.pop() || [])[0];
}
for(var unit in unit2s)
{
var unit_s = unit2s[unit];
if (diff >= unit_s || unit == 's')
if (diff >= unit_s || unit === smallest)
{
display = Math.round(diff/unit_s)+' '+this.egw().lang(unit2label[unit]);
break;