2022-01-21 21:45:12 +01:00
|
|
|
/**
|
|
|
|
* EGroupware eTemplate2 - Duration date widget (WebComponent)
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link https://www.egroupware.org
|
|
|
|
* @author Nathan Gray
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2022-02-17 00:44:57 +01:00
|
|
|
import {css, html, LitElement} from "@lion/core";
|
2022-01-21 21:45:12 +01:00
|
|
|
import {Et2InputWidget} from "../Et2InputWidget/Et2InputWidget";
|
2022-02-17 00:44:57 +01:00
|
|
|
import {sprintf} from "../../egw_action/egw_action_common";
|
|
|
|
import {dateStyles} from "./DateStyles";
|
2022-02-23 18:43:39 +01:00
|
|
|
import {FormControlMixin} from "@lion/form-core";
|
2022-01-21 21:45:12 +01:00
|
|
|
|
|
|
|
export interface formatOptions
|
|
|
|
{
|
2022-07-21 17:57:50 +02:00
|
|
|
selectUnit : string;
|
|
|
|
displayFormat : string;
|
|
|
|
dataFormat : string;
|
|
|
|
hoursPerDay : number;
|
|
|
|
emptyNot0 : boolean;
|
2022-01-21 21:45:12 +01:00
|
|
|
number_format? : string;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a number as a time duration
|
|
|
|
*
|
|
|
|
* @param {number} value
|
|
|
|
* @param {object} options
|
|
|
|
* set 'timeFormat': "12" to specify a particular format
|
|
|
|
* @returns {value: string, unit: string}
|
|
|
|
*/
|
|
|
|
export function formatDuration(value : number | string, options : formatOptions) : { value : string, unit : string }
|
|
|
|
{
|
|
|
|
// Handle empty / 0 / no value
|
|
|
|
if(value === "" || value == "0" || !value)
|
|
|
|
{
|
2022-07-21 17:57:50 +02:00
|
|
|
return {value: options.emptyNot0 ? "0" : "", unit: ""};
|
2022-01-21 21:45:12 +01:00
|
|
|
}
|
|
|
|
// Make sure it's a number now
|
|
|
|
value = typeof value == "string" ? parseInt(value) : value;
|
|
|
|
|
2022-07-21 17:57:50 +02:00
|
|
|
if(!options.selectUnit)
|
2022-01-21 21:45:12 +01:00
|
|
|
{
|
|
|
|
let vals = [];
|
2022-07-21 17:57:50 +02:00
|
|
|
for(let i = 0; i < options.displayFormat.length; ++i)
|
2022-01-21 21:45:12 +01:00
|
|
|
{
|
2022-07-21 17:57:50 +02:00
|
|
|
let unit = options.displayFormat[i];
|
2022-01-21 21:45:12 +01:00
|
|
|
let val = this._unit_from_value(value, unit, i === 0);
|
2022-07-21 17:57:50 +02:00
|
|
|
if(unit === 's' || unit === 'm' || unit === 'h' && options.displayFormat[0] === 'd')
|
2022-01-21 21:45:12 +01:00
|
|
|
{
|
|
|
|
vals.push(sprintf('%02d', val));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vals.push(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {value: vals.join(':'), unit: ''};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put value into minutes for further processing
|
2022-07-21 17:57:50 +02:00
|
|
|
switch(options.dataFormat)
|
2022-01-21 21:45:12 +01:00
|
|
|
{
|
|
|
|
case 'd':
|
2022-07-21 17:57:50 +02:00
|
|
|
value *= options.hoursPerDay;
|
2022-01-21 21:45:12 +01:00
|
|
|
// fall-through
|
|
|
|
case 'h':
|
|
|
|
value *= 60;
|
|
|
|
break;
|
2022-02-25 08:45:35 +01:00
|
|
|
case 's': // round to full minutes, unless this would give 0, use 1 digit instead
|
|
|
|
value = value < 30 ? Math.round(value / 6.0)/10.0 : Math.round(value / 60.0);
|
2022-01-21 21:45:12 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out the best unit for display
|
2022-07-21 17:57:50 +02:00
|
|
|
let _unit = options.displayFormat == "d" ? "d" : "h";
|
|
|
|
if(options.displayFormat.indexOf('m') > -1 && value < 60)
|
2022-01-21 21:45:12 +01:00
|
|
|
{
|
|
|
|
_unit = 'm';
|
|
|
|
}
|
2022-07-21 17:57:50 +02:00
|
|
|
else if(options.displayFormat.indexOf('d') > -1 && value >= (60 * options.hoursPerDay))
|
2022-01-21 21:45:12 +01:00
|
|
|
{
|
|
|
|
_unit = 'd';
|
|
|
|
}
|
2022-07-21 17:57:50 +02:00
|
|
|
let out_value = "" + (_unit == 'm' ? value : (Math.round((value / 60.0 / (_unit == 'd' ? options.hoursPerDay : 1)) * 100) / 100));
|
2022-01-21 21:45:12 +01:00
|
|
|
|
|
|
|
if(out_value === '')
|
|
|
|
{
|
|
|
|
_unit = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// use decimal separator from user prefs
|
|
|
|
var format = options.number_format || this.egw().preference('number_format');
|
|
|
|
var sep = format ? format[0] : '.';
|
|
|
|
if(format && sep && sep != '.')
|
|
|
|
{
|
|
|
|
out_value = out_value.replace('.', sep);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {value: out_value, unit: _unit};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a time duration (eg: 3 days, 6 hours)
|
|
|
|
*
|
|
|
|
* If not specified, the time is in assumed to be minutes and will be displayed with a calculated unit
|
|
|
|
* but this can be specified with the properties.
|
|
|
|
*/
|
2022-02-23 18:43:39 +01:00
|
|
|
export class Et2DateDuration extends Et2InputWidget(FormControlMixin(LitElement))
|
2022-01-21 21:45:12 +01:00
|
|
|
{
|
|
|
|
static get styles()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
...super.styles,
|
2022-02-17 00:44:57 +01:00
|
|
|
...dateStyles,
|
2022-01-21 21:45:12 +01:00
|
|
|
css`
|
2022-02-23 18:43:39 +01:00
|
|
|
.form-field__group-two {
|
|
|
|
width: 100%;
|
2022-02-17 00:44:57 +01:00
|
|
|
}
|
2022-07-21 21:32:06 +02:00
|
|
|
et2-select {
|
2022-02-17 00:44:57 +01:00
|
|
|
color: var(--input-text-color);
|
2022-07-21 21:32:06 +02:00
|
|
|
border-left: 1px solid var(--input-border-color);
|
2022-02-17 00:44:57 +01:00
|
|
|
flex: 2 1 auto;
|
|
|
|
}
|
2022-07-22 18:43:30 +02:00
|
|
|
et2-select::part(control) {
|
|
|
|
border-top-left-radius: 0px;
|
|
|
|
border-bottom-left-radius: 0px;
|
2022-07-21 21:32:06 +02:00
|
|
|
}
|
|
|
|
et2-textbox {
|
2022-02-17 00:44:57 +01:00
|
|
|
flex: 1 1 auto;
|
|
|
|
max-width: 4.5em;
|
2022-07-22 18:43:30 +02:00
|
|
|
margin-right: -2px;
|
2022-01-21 21:45:12 +01:00
|
|
|
}
|
2022-07-27 21:36:04 +02:00
|
|
|
et2-textbox::part(input) {
|
|
|
|
padding-right: 0px;
|
|
|
|
}
|
2022-07-22 18:43:30 +02:00
|
|
|
et2-textbox:not(:last-child)::part(base) {
|
|
|
|
border-right: none;
|
|
|
|
border-top-right-radius: 0px;
|
|
|
|
border-bottom-right-radius: 0px;
|
2022-02-23 19:15:55 +01:00
|
|
|
}
|
|
|
|
|
2022-01-21 21:45:12 +01:00
|
|
|
`,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
static get properties()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
...super.properties,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data format
|
|
|
|
*
|
|
|
|
* Units to read/store the data. 'd' = days (float), 'h' = hours (float), 'm' = minutes (int), 's' = seconds (int).
|
|
|
|
*/
|
2022-07-21 17:57:50 +02:00
|
|
|
dataFormat: {
|
2022-02-25 08:45:35 +01:00
|
|
|
reflect: true,
|
2022-01-21 21:45:12 +01:00
|
|
|
type: String
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Display format
|
|
|
|
*
|
|
|
|
* Permitted units for displaying the data.
|
|
|
|
* 'd' = days, 'h' = hours, 'm' = minutes, 's' = seconds. Use combinations to give a choice.
|
|
|
|
* Default is 'dh' = days or hours with selectbox.
|
|
|
|
*/
|
2022-07-21 17:57:50 +02:00
|
|
|
displayFormat: {
|
2022-02-25 08:45:35 +01:00
|
|
|
reflect: true,
|
2022-01-21 21:45:12 +01:00
|
|
|
type: String
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Select unit or input per unit
|
|
|
|
*
|
|
|
|
* Display a unit-selection for multiple units, or an input field per unit.
|
|
|
|
* Default is true
|
|
|
|
*/
|
2022-07-21 17:57:50 +02:00
|
|
|
selectUnit: {
|
2022-03-02 10:02:24 +01:00
|
|
|
reflect: true,
|
2022-01-21 21:45:12 +01:00
|
|
|
type: Boolean
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Percent allowed
|
|
|
|
*
|
|
|
|
* Allows to enter a percentage instead of numbers
|
|
|
|
*/
|
2022-07-21 17:57:50 +02:00
|
|
|
percentAllowed: {
|
2022-01-21 21:45:12 +01:00
|
|
|
type: Boolean
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hours per day
|
|
|
|
*
|
|
|
|
* Number of hours in a day, used for converting between hours and (working) days.
|
|
|
|
* Default 8
|
|
|
|
*/
|
2022-07-21 17:57:50 +02:00
|
|
|
hoursPerDay: {
|
2022-02-25 08:45:35 +01:00
|
|
|
reflect: true,
|
|
|
|
type: Number
|
|
|
|
},
|
2022-01-21 21:45:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 0 or empty
|
|
|
|
*
|
|
|
|
* Should the widget differ between 0 and empty, which get then returned as NULL
|
|
|
|
* Default false, empty is considered as 0
|
|
|
|
*/
|
2022-07-21 17:57:50 +02:00
|
|
|
emptyNot0: {
|
2022-02-25 08:45:35 +01:00
|
|
|
reflect: true,
|
|
|
|
type: Boolean
|
|
|
|
},
|
2022-01-21 21:45:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Short labels
|
|
|
|
*
|
|
|
|
* use d/h/m instead of day/hour/minute
|
|
|
|
*/
|
2022-07-21 17:57:50 +02:00
|
|
|
shortLabels: {
|
2022-02-25 08:45:35 +01:00
|
|
|
reflect: true,
|
2022-01-21 21:45:12 +01:00
|
|
|
type: Boolean
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Step limit
|
|
|
|
*
|
|
|
|
* Works with the min and max attributes to limit the increments at which a numeric or date-time value can be set.
|
|
|
|
*/
|
|
|
|
step: {
|
|
|
|
type: String
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 00:44:57 +01:00
|
|
|
protected static time_formats = {d: "d", h: "h", m: "m", s: "s"};
|
|
|
|
protected _display = {value: "", unit: ""};
|
|
|
|
|
2022-01-21 21:45:12 +01:00
|
|
|
constructor()
|
|
|
|
{
|
|
|
|
super();
|
|
|
|
|
|
|
|
// Property defaults
|
2022-07-21 17:57:50 +02:00
|
|
|
this.dataFormat = "m";
|
|
|
|
this.displayFormat = "dhm";
|
|
|
|
this.selectUnit = true;
|
|
|
|
this.percentAllowed = false;
|
|
|
|
this.hoursPerDay = 8;
|
|
|
|
this.emptyNot0 = false;
|
|
|
|
this.shortLabels = false;
|
2022-01-21 21:45:12 +01:00
|
|
|
|
|
|
|
this.formatter = formatDuration;
|
|
|
|
}
|
|
|
|
|
2022-02-25 09:16:28 +01:00
|
|
|
transformAttributes(attrs)
|
|
|
|
{
|
2022-07-21 17:57:50 +02:00
|
|
|
// Clean formats, but avoid things that need to be expanded like $cont[displayFormat]
|
2022-03-03 22:14:26 +01:00
|
|
|
const check = new RegExp('[\$\@' + Object.keys(Et2DateDuration.time_formats).join('') + ']');
|
2022-07-21 17:57:50 +02:00
|
|
|
for(let attr in ["displayFormat", "dataFormat"])
|
2022-02-25 09:16:28 +01:00
|
|
|
{
|
2022-03-03 22:14:26 +01:00
|
|
|
if(typeof attrs[attrs] === 'string' && !check.test(attrs[attr]))
|
|
|
|
{
|
|
|
|
console.warn("Invalid format for " + attr + "'" + attrs[attr] + "'", this);
|
|
|
|
attrs[attr] = attrs[attr].replace(/[^dhms]/g, '');
|
|
|
|
}
|
2022-02-25 09:16:28 +01:00
|
|
|
}
|
2022-03-03 22:14:26 +01:00
|
|
|
|
2022-02-25 09:16:28 +01:00
|
|
|
super.transformAttributes(attrs);
|
|
|
|
}
|
|
|
|
|
2022-02-17 00:44:57 +01:00
|
|
|
get value() : string
|
|
|
|
{
|
|
|
|
let value = 0;
|
|
|
|
|
2022-07-21 17:57:50 +02:00
|
|
|
if(!this.selectUnit)
|
2022-01-21 21:45:12 +01:00
|
|
|
{
|
2022-02-17 00:44:57 +01:00
|
|
|
for(let i = this._durationNode.length; --i >= 0;)
|
|
|
|
{
|
|
|
|
value += parseInt(<string>this._durationNode[i].value) * this._unit2seconds(this._durationNode[i].name);
|
|
|
|
}
|
2022-07-21 17:57:50 +02:00
|
|
|
if(this.dataFormat !== 's')
|
2022-02-17 00:44:57 +01:00
|
|
|
{
|
2022-07-21 17:57:50 +02:00
|
|
|
value /= this._unit2seconds(this.dataFormat);
|
2022-02-17 00:44:57 +01:00
|
|
|
}
|
2022-07-21 17:57:50 +02:00
|
|
|
return "" + (this.dataFormat === 'm' ? Math.round(value) : value);
|
2022-02-17 00:44:57 +01:00
|
|
|
}
|
|
|
|
|
2022-07-26 21:31:20 +02:00
|
|
|
let val = this._durationNode.length ? this._durationNode[0].value : '';
|
2022-09-13 21:00:25 +02:00
|
|
|
if(val === '' || isNaN(val))
|
2022-02-17 00:44:57 +01:00
|
|
|
{
|
2022-07-21 17:57:50 +02:00
|
|
|
return this.emptyNot0 ? '' : "0";
|
2022-02-17 00:44:57 +01:00
|
|
|
}
|
|
|
|
value = parseFloat(val);
|
|
|
|
|
|
|
|
// Put value into minutes for further processing
|
2022-07-21 17:57:50 +02:00
|
|
|
switch(this._formatNode && this._formatNode.value ? this._formatNode.value : this.displayFormat)
|
2022-02-17 00:44:57 +01:00
|
|
|
{
|
|
|
|
case 'd':
|
2022-07-21 17:57:50 +02:00
|
|
|
value *= this.hoursPerDay;
|
2022-02-17 00:44:57 +01:00
|
|
|
// fall-through
|
|
|
|
case 'h':
|
|
|
|
value *= 60;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Minutes should be an integer. Floating point math.
|
2022-07-21 17:57:50 +02:00
|
|
|
if(this.dataFormat !== 's')
|
2022-02-17 00:44:57 +01:00
|
|
|
{
|
|
|
|
value = Math.round(value);
|
|
|
|
}
|
2022-07-21 17:57:50 +02:00
|
|
|
switch(this.dataFormat)
|
2022-02-17 00:44:57 +01:00
|
|
|
{
|
|
|
|
case 'd':
|
2022-07-21 17:57:50 +02:00
|
|
|
value /= this.hoursPerDay;
|
2022-02-17 00:44:57 +01:00
|
|
|
// fall-through
|
|
|
|
case 'h':
|
|
|
|
value /= 60.0;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
value = Math.round(value * 60.0);
|
|
|
|
break;
|
2022-01-21 21:45:12 +01:00
|
|
|
}
|
|
|
|
|
2022-02-17 00:44:57 +01:00
|
|
|
return "" + value;
|
|
|
|
}
|
|
|
|
|
|
|
|
set value(_value)
|
|
|
|
{
|
|
|
|
this._display = this._convert_to_display(parseFloat(_value));
|
|
|
|
this.requestUpdate();
|
2022-01-21 21:45:12 +01:00
|
|
|
}
|
|
|
|
|
2022-03-29 19:00:09 +02:00
|
|
|
/**
|
|
|
|
* Set the format for displaying the duration
|
|
|
|
*
|
|
|
|
* @param {string} value
|
|
|
|
*/
|
2022-07-21 17:57:50 +02:00
|
|
|
set displayFormat(value : string)
|
2022-03-29 19:00:09 +02:00
|
|
|
{
|
2022-07-21 17:57:50 +02:00
|
|
|
this.__displayFormat = "";
|
2022-03-29 19:00:09 +02:00
|
|
|
// Display format must be in decreasing size order (dhms) or the calculations
|
|
|
|
// don't work out nicely
|
|
|
|
for(let f of Object.keys(Et2DateDuration.time_formats))
|
|
|
|
{
|
|
|
|
if(value.indexOf(f) !== -1)
|
|
|
|
{
|
2022-07-21 17:57:50 +02:00
|
|
|
this.__displayFormat += f;
|
2022-03-29 19:00:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 17:57:50 +02:00
|
|
|
get displayFormat()
|
2022-03-29 19:00:09 +02:00
|
|
|
{
|
2022-07-21 17:57:50 +02:00
|
|
|
return this.__displayFormat;
|
2022-03-29 19:00:09 +02:00
|
|
|
}
|
|
|
|
|
2022-02-23 18:43:39 +01:00
|
|
|
/**
|
|
|
|
* @return {TemplateResult}
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
_inputGroupInputTemplate()
|
2022-01-21 21:45:12 +01:00
|
|
|
{
|
2022-02-17 00:44:57 +01:00
|
|
|
return html`
|
2022-02-23 18:43:39 +01:00
|
|
|
<div class="input-group__input">
|
|
|
|
<slot name="input">
|
|
|
|
${this._inputTemplate()}
|
|
|
|
${this._formatTemplate()}
|
|
|
|
</slot>
|
|
|
|
</div>
|
|
|
|
`;
|
2022-02-17 00:44:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the value in data format into value in display format.
|
|
|
|
*
|
|
|
|
* @param _value int/float Data in data format
|
|
|
|
*
|
|
|
|
* @return Object {value: Value in display format, unit: unit for display}
|
|
|
|
*/
|
|
|
|
protected _convert_to_display(_value)
|
|
|
|
{
|
2022-07-21 17:57:50 +02:00
|
|
|
if(!this.selectUnit)
|
2022-02-17 00:44:57 +01:00
|
|
|
{
|
|
|
|
let vals = [];
|
2022-07-21 17:57:50 +02:00
|
|
|
for(let i = 0; i < this.displayFormat.length; ++i)
|
2022-02-17 00:44:57 +01:00
|
|
|
{
|
2022-07-21 17:57:50 +02:00
|
|
|
let unit = this.displayFormat[i];
|
2022-02-17 00:44:57 +01:00
|
|
|
let val = this._unit_from_value(_value, unit, i === 0);
|
2022-07-21 17:57:50 +02:00
|
|
|
if(unit === 's' || unit === 'm' || unit === 'h' && this.displayFormat[0] === 'd')
|
2022-02-17 00:44:57 +01:00
|
|
|
{
|
|
|
|
vals.push(sprintf('%02d', val));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vals.push(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {value: vals.join(':'), unit: ''};
|
|
|
|
}
|
|
|
|
if(_value)
|
|
|
|
{
|
|
|
|
// Put value into minutes for further processing
|
2022-07-21 17:57:50 +02:00
|
|
|
switch(this.dataFormat)
|
2022-02-17 00:44:57 +01:00
|
|
|
{
|
|
|
|
case 'd':
|
2022-07-21 17:57:50 +02:00
|
|
|
_value *= this.hoursPerDay;
|
2022-02-17 00:44:57 +01:00
|
|
|
// fall-through
|
|
|
|
case 'h':
|
|
|
|
_value *= 60;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
_value /= 60.0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out best unit for display
|
2022-07-21 17:57:50 +02:00
|
|
|
var _unit = this.displayFormat == "d" ? "d" : "h";
|
|
|
|
if(this.displayFormat.indexOf('m') > -1 && _value && _value < 60)
|
2022-02-17 00:44:57 +01:00
|
|
|
{
|
|
|
|
_unit = 'm';
|
|
|
|
}
|
2022-07-21 17:57:50 +02:00
|
|
|
else if(this.displayFormat.indexOf('d') > -1 && _value >= 60 * this.hoursPerDay)
|
2022-02-17 00:44:57 +01:00
|
|
|
{
|
|
|
|
_unit = 'd';
|
|
|
|
}
|
2022-07-21 17:57:50 +02:00
|
|
|
_value = this.emptyNot0 && _value === '' || !this.emptyNot0 && !_value ? '' :
|
|
|
|
(_unit == 'm' ? parseInt(_value) : (Math.round((_value / 60.0 / (_unit == 'd' ? this.hoursPerDay : 1)) * 100) / 100));
|
2022-02-17 00:44:57 +01:00
|
|
|
|
|
|
|
if(_value === '')
|
|
|
|
{
|
|
|
|
_unit = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// use decimal separator from user prefs
|
|
|
|
var format = this.egw().preference('number_format');
|
|
|
|
var sep = format ? format[0] : '.';
|
|
|
|
if(typeof _value == 'string' && format && sep && sep != '.')
|
|
|
|
{
|
|
|
|
_value = _value.replace('.', sep);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {value: _value, unit: _unit};
|
|
|
|
}
|
|
|
|
|
|
|
|
private _unit2seconds(_unit)
|
|
|
|
{
|
|
|
|
switch(_unit)
|
|
|
|
{
|
|
|
|
case 's':
|
|
|
|
return 1;
|
|
|
|
case 'm':
|
|
|
|
return 60;
|
|
|
|
case 'h':
|
|
|
|
return 3600;
|
|
|
|
case 'd':
|
2022-07-21 17:57:50 +02:00
|
|
|
return 3600 * this.hoursPerDay;
|
2022-02-17 00:44:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private _unit_from_value(_value, _unit, _highest)
|
|
|
|
{
|
2022-07-21 17:57:50 +02:00
|
|
|
_value *= this._unit2seconds(this.dataFormat);
|
2022-02-17 00:44:57 +01:00
|
|
|
// get value for given _unit
|
|
|
|
switch(_unit)
|
|
|
|
{
|
|
|
|
case 's':
|
|
|
|
return _highest ? _value : _value % 60;
|
|
|
|
case 'm':
|
|
|
|
_value = Math.floor(_value / 60);
|
|
|
|
return _highest ? _value : _value % 60;
|
|
|
|
case 'h':
|
|
|
|
_value = Math.floor(_value / 3600);
|
2022-07-21 17:57:50 +02:00
|
|
|
return _highest ? _value : _value % this.hoursPerDay;
|
2022-02-17 00:44:57 +01:00
|
|
|
case 'd':
|
2022-07-21 17:57:50 +02:00
|
|
|
return Math.floor(_value / 3600 / this.hoursPerDay);
|
2022-02-17 00:44:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-21 17:57:50 +02:00
|
|
|
* Render the needed number inputs according to selectUnit & displayFormat properties
|
2022-02-17 00:44:57 +01:00
|
|
|
*
|
|
|
|
* @returns {any}
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _inputTemplate()
|
|
|
|
{
|
|
|
|
let inputs = [];
|
|
|
|
let value = typeof this._display.value === "number" ? this._display.value : (this._display.value.split(":") || []);
|
2022-07-21 17:57:50 +02:00
|
|
|
for(let i = this.selectUnit ? 1 : this.displayFormat.length; i > 0; --i)
|
2022-02-17 00:44:57 +01:00
|
|
|
{
|
|
|
|
let input = {
|
|
|
|
name: "",
|
|
|
|
title: "",
|
2022-09-13 21:00:25 +02:00
|
|
|
value: typeof value == "number" ? value : ((this.selectUnit ? value.pop() : value[i]) || ""),
|
2022-02-17 00:44:57 +01:00
|
|
|
min: undefined,
|
|
|
|
max: undefined
|
|
|
|
};
|
2022-07-21 17:57:50 +02:00
|
|
|
if(!this.selectUnit)
|
2022-02-17 00:44:57 +01:00
|
|
|
{
|
|
|
|
input.min = 0;
|
2022-07-21 17:57:50 +02:00
|
|
|
input.name = this.displayFormat[this.displayFormat.length - i];
|
2022-02-18 20:02:27 +01:00
|
|
|
// @ts-ignore - it doesn't like that it may have been an array
|
2022-07-21 17:57:50 +02:00
|
|
|
input.value = <string>(value[this.displayFormat.length - i]);
|
|
|
|
switch(this.displayFormat[this.displayFormat.length - i])
|
2022-02-17 00:44:57 +01:00
|
|
|
{
|
|
|
|
case 's':
|
|
|
|
input.max = 60;
|
|
|
|
input.title = this.egw().lang('Seconds');
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
input.max = 60;
|
|
|
|
input.title = this.egw().lang('Minutes');
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
input.max = 24;
|
|
|
|
input.title = this.egw().lang('Hours');
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
input.title = this.egw().lang('Days');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inputs.push(input);
|
|
|
|
}
|
|
|
|
return html`${inputs.map((input : any) =>
|
2022-07-21 21:32:06 +02:00
|
|
|
html`
|
|
|
|
<et2-textbox part="duration__${input.name}" type="number" class="duration__input" name=${input.name}
|
|
|
|
min=${input.min} max=${input.max} title=${input.title}
|
|
|
|
value=${input.value}></et2-textbox>`
|
2022-02-17 00:44:57 +01:00
|
|
|
)}
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-21 17:57:50 +02:00
|
|
|
* Generate the format selector according to the selectUnit and displayFormat properties
|
2022-02-17 00:44:57 +01:00
|
|
|
*
|
|
|
|
* @returns {any}
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _formatTemplate()
|
|
|
|
{
|
|
|
|
// If no formats or only 1 format, no need for a selector
|
2022-07-21 17:57:50 +02:00
|
|
|
if(!this.displayFormat || this.displayFormat.length < 1 ||
|
|
|
|
(!this.selectUnit && this.displayFormat.length > 1))
|
2022-02-17 00:44:57 +01:00
|
|
|
{
|
|
|
|
return html``;
|
|
|
|
}
|
|
|
|
// Get translations
|
|
|
|
this.time_formats = this.time_formats || {
|
2022-07-21 17:57:50 +02:00
|
|
|
d: this.shortLabels ? this.egw().lang("d") : this.egw().lang("Days"),
|
|
|
|
h: this.shortLabels ? this.egw().lang("h") : this.egw().lang("Hours"),
|
|
|
|
m: this.shortLabels ? this.egw().lang("m") : this.egw().lang("Minutes"),
|
|
|
|
s: this.shortLabels ? this.egw().lang("s") : this.egw().lang("Seconds")
|
2022-02-17 00:44:57 +01:00
|
|
|
};
|
2022-02-18 20:02:27 +01:00
|
|
|
// It would be nice to use an et2-select here, but something goes weird with the styling
|
2022-02-17 00:44:57 +01:00
|
|
|
return html`
|
2022-07-27 21:36:04 +02:00
|
|
|
<et2-select value="${this._display.unit || this.displayFormat[0]}">
|
2022-07-21 17:57:50 +02:00
|
|
|
${[...this.displayFormat].map((format : string) =>
|
2022-02-17 00:44:57 +01:00
|
|
|
html`
|
2022-07-21 21:32:06 +02:00
|
|
|
<sl-menu-item value=${format} ?checked=${this._display.unit === format}>
|
2022-02-17 00:44:57 +01:00
|
|
|
${this.time_formats[format]}
|
2022-07-21 21:32:06 +02:00
|
|
|
</sl-menu-item>`
|
2022-02-17 00:44:57 +01:00
|
|
|
)}
|
2022-07-21 21:32:06 +02:00
|
|
|
</et2-select>
|
2022-02-17 00:44:57 +01:00
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {HTMLInputElement}
|
|
|
|
*/
|
|
|
|
get _durationNode() : HTMLInputElement[]
|
|
|
|
{
|
2022-07-26 21:31:20 +02:00
|
|
|
return this.shadowRoot ? this.shadowRoot.querySelectorAll("et2-textbox") || [] : [];
|
2022-02-17 00:44:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {HTMLSelectElement}
|
|
|
|
*/
|
|
|
|
get _formatNode() : HTMLSelectElement
|
|
|
|
{
|
2022-07-26 21:31:20 +02:00
|
|
|
return this.shadowRoot ? this.shadowRoot.querySelector("et2-select") : null;
|
2022-01-21 21:45:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// @ts-ignore TypeScript is not recognizing that this is a LitElement
|
2022-02-21 19:58:56 +01:00
|
|
|
customElements.define("et2-date-duration", Et2DateDuration);
|