Date Duration
<et2-date-duration> | Et2DateDuration
Overview
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.
Examples
et2-date-duration is the odd one in the date family: it holds a length of time, not a
point in time. “2 hours”, not “2 o’clock”. There is no calendar behind it - it is a number with a unit
beside it, and the value it stores is just that number. See
Which date widget for the widgets that do hold a
moment.
Two separate properties decide what it means and what it looks like:
-
dataFormatis the unit the stored number is in -ddays,hhours,mminutes (the default),sseconds. Match it to the column behind the field and then leave it alone. -
displayFormatis the set of units the user may work in, largest first, eg.dhmor justh. Changing it never changes the stored number.
Like the other editable date widgets, value is a property rather than an attribute: in a
template the value arrives from the server, and in plain HTML like the examples below it is set from script.
Minutes in, best unit out
With the defaults - data in minutes, display in days/hours/minutes - the widget picks the unit that suits the number. 90 minutes is shown as 1.5 hours; the value stays 90.
Value: -
<et2-date-duration id="duration-basic" label="Time spent"></et2-date-duration>
<p>Value: <span id="duration-basic-out">-</span></p>
<script>
const durationBasic = document.getElementById("duration-basic");
const durationBasicOut = document.getElementById("duration-basic-out");
const showDurationBasic = () => {durationBasicOut.textContent = JSON.stringify(durationBasic.value);};
durationBasic.addEventListener("change", showDurationBasic);
customElements.whenDefined("et2-date-duration")
.then(() => durationBasic.updateComplete)
.then(() => {durationBasic.value = 90;})
// the unit selector has to catch up before value can be read back
.then(() => durationBasic.updateComplete)
.then(showDurationBasic);
</script>
One unit only
A single letter in displayFormat drops the unit selector - there is nothing to choose - and the
number is always in that unit. Handy when the field has a fixed meaning, like an estimate in hours.
Value: -
<et2-date-duration id="duration-hours" label="Estimate" displayFormat="h"></et2-date-duration>
<p>Value: <span id="duration-hours-out">-</span></p>
<script>
const durationHours = document.getElementById("duration-hours");
const durationHoursOut = document.getElementById("duration-hours-out");
const showDurationHours = () => {durationHoursOut.textContent = JSON.stringify(durationHours.value);};
durationHours.addEventListener("change", showDurationHours);
customElements.whenDefined("et2-date-duration")
.then(() => durationHours.updateComplete)
.then(() => {durationHours.value = 150;})
.then(() => durationHours.updateComplete)
.then(showDurationHours);
</script>
An input per unit
Turning selectUnit off replaces the one number plus a selector with one number per unit in
displayFormat, read like a clock. Overfilling a field rolls over into the one to its left: put
90 in the minutes box and tab away.
In a template you write selectUnit="false" and the template reader turns the text into a
boolean. Plain HTML has no such reader - an attribute that is present means true for a boolean - so
the example sets the property from script instead.
Value: -
<et2-date-duration id="duration-split" label="Duration" displayFormat="hm"></et2-date-duration>
<p>Value: <span id="duration-split-out">-</span></p>
<script>
const durationSplit = document.getElementById("duration-split");
const durationSplitOut = document.getElementById("duration-split-out");
const showDurationSplit = () => {durationSplitOut.textContent = JSON.stringify(durationSplit.value);};
durationSplit.selectUnit = false;
durationSplit.addEventListener("change", showDurationSplit);
customElements.whenDefined("et2-date-duration")
.then(() => durationSplit.updateComplete)
.then(() => {durationSplit.value = 135;})
.then(() => durationSplit.updateComplete)
.then(showDurationSplit);
</script>
Days are working days
A day is hoursPerDay hours, 8 by default, not 24 - this is timesheet arithmetic, not calendar
arithmetic. Both fields below were set to the same 960 minutes and disagree about how many days that is.
<et2-date-duration id="duration-8h" label="8 hour day" displayFormat="dh"></et2-date-duration>
<et2-date-duration id="duration-6h" label="6 hour day" displayFormat="dh" hoursPerDay="6"></et2-date-duration>
<script>
const duration8h = document.getElementById("duration-8h");
const duration6h = document.getElementById("duration-6h");
customElements.whenDefined("et2-date-duration")
.then(() => Promise.all([duration8h.updateComplete, duration6h.updateComplete]))
.then(() => {
duration8h.value = 960;
duration6h.value = 960;
});
</script>
Empty is not zero
By default an empty field submits "0": no duration and a duration of nothing are the same
answer. emptyNot0 keeps them apart, so an untouched field submits an empty value and only a
typed 0 submits zero. Use it where the difference is real - “not estimated yet” against “estimated at
nothing”. Clear both fields below and compare.
Values: -
<et2-date-duration id="duration-zero" label="Empty is 0" displayFormat="h"></et2-date-duration>
<et2-date-duration id="duration-empty" label="Empty is empty" displayFormat="h" emptyNot0></et2-date-duration>
<p>Values: <span id="duration-empty-out">-</span></p>
<script>
const durationZero = document.getElementById("duration-zero");
const durationEmpty = document.getElementById("duration-empty");
const durationEmptyOut = document.getElementById("duration-empty-out");
const showDurationEmpty = () =>
{
durationEmptyOut.textContent = JSON.stringify(durationZero.value) + " / " + JSON.stringify(durationEmpty.value);
};
durationZero.addEventListener("change", showDurationEmpty);
durationEmpty.addEventListener("change", showDurationEmpty);
customElements.whenDefined("et2-date-duration")
.then(() => Promise.all([durationZero.updateComplete, durationEmpty.updateComplete]))
.then(showDurationEmpty);
</script>
Short unit labels
shortLabels swaps “Hours” for “h” in the selector, for the narrow column of a list.
<et2-date-duration id="duration-short" label="Spent" shortLabels></et2-date-duration>
<script>
const durationShort = document.getElementById("duration-short");
customElements.whenDefined("et2-date-duration")
.then(() => durationShort.updateComplete)
.then(() => {durationShort.value = 45;});
</script>
Properties
| Name | Description | Reflects | Type | Default |
|---|---|---|---|---|
dataFormat
|
Data format Units to read/store the data. ‘d’ = days (float), ‘h’ = hours (float), ‘m’ = minutes (int), ‘s’ = seconds (int). |
|
"d" | "h" | "m" | "s"
|
"m"
|
displayFormat
|
Display format Permitted units for displaying the data. ‘d’ = days, ‘h’ = hours, ‘m’ = minutes, ‘s’ = seconds. Use combinations to give a choice. Default is ‘dhm’ = days or hours with selectbox. |
|
string
|
"dhm"
|
emptyNot0
|
0 or empty Should the widget differ between 0 and empty, which get then returned as NULL Default false, empty is considered as 0 |
|
boolean
|
false
|
hoursPerDay
|
Hours per day Number of hours in a day, used for converting between hours and (working) days. Default 8 |
|
number
|
8
|
selectUnit
|
Select unit or input per unit Display a unit-selection for multiple units, or an input field per unit. Default is true |
|
boolean
|
true
|
shortLabels
|
Short labels use d/h/m instead of day/hour/minute |
|
boolean
|
false
|
step
|
Step limit Works with the min and max attributes to limit the increments at which a numeric or date-time value can be set. |
|
number
|
1
|
Learn more about attributes and properties.
Inherited properties (22)
Et2Widget
| Name | Description |
|---|---|
accesskey |
Accesskey provides a hint for generating a keyboard shortcut for the current element. The attribute value must consist of a single printable character. |
actions |
Set Actions on the widget Each action is defined as an object: move: { type: “drop”, acceptedTypes: “mail”, icon: “move”, caption: “Move to” onExecute: javascript:mail_move” } This will turn the widget into a drop target for “mail” drag types. When “mail” drag types are dropped, the global function mail_move(egwAction action, egwActionObject sender) will be called. The ID of the dragged “mail” will be in sender.id, some information about the sender will be in sender.context. The etemplate2 widget involved can typically be found in action.parent.data.widget, so your handler can operate in the widget context easily. The location varies depending on your action though. It might be action.parent.parent.data.widget To customise how the actions are handled for a particular widget, override _link_actions(). It handles the more widget-specific parts. |
align |
Used by Et2Box to determine alignment. Allowed values are left, right |
class |
CSS Class. This class is applied to the outside, on the web component itself. Due to how WebComponents work, this might not change anything inside the component. |
data |
Set the dataset from a CSV |
deferredProperties |
Any attribute that refers to row content cannot be resolved immediately, but some like booleans cannot stay a string because it’s a boolean attribute. We store them for later, and parse when they’re fully in their row. If you are creating a widget that can go in a nextmatch row, and it has boolean attributes that can change for each row, add those attributes into deferredProperties |
disabled |
Defines whether this widget is visibly disabled. The widget is still visible, but clearly cannot be interacted with. Widgets disabled in the template will not return a value to the application code, even if re-enabled via javascript before submitting. To allow a disabled widget to be re-enabled and return a value, disable via javascript in the app’s et2_ready() instead of an attribute in the template file. |
dom_id |
Get the actual DOM ID, which has been prefixed to make sure it’s unique. |
hidden |
The widget is not visible. As far as the user is concerned, the widget does not exist. Widgets hidden with an attribute in the template may not be created in the DOM, and will not return a value. Widgets can be hidden after creation, and they may return a value if hidden this way. |
id |
Get the ID of the widget |
label |
The label of the widget Legacy support for labels with %s that get wrapped around the widget Not the best way go with webComponents - shouldn’t modify their DOM like this |
noLang |
Disable any translations for the widget |
parentId |
Parent is different than what is specified in the template / hierarchy. Widget ID of another node to insert this node into instead of the normal location |
statustext |
Tooltip which is shown for this element on hover |
styles |
WebComponent * |
options |
Get property-values as object |
supportedWidgetClasses |
et2_widget compatability |
Et2InputWidget
| Name | Description |
|---|---|
autofocus |
Have browser focus this input on load. Overrides etemplate2.focusOnFirstInput(), use only once per page https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attributes |
hasFeedbackFor |
Get a list of feedback types |
translate |
List of properties that get translated Done separately to not interfere with properties - if we re-define label property, labels go missing. |
needed |
Compatibility for deprecated name “needed” |
LitElement
| Name | Description |
|---|---|
updateComplete |
A read-only promise that resolves when the component has finished updating. |
Events
| Name | Description | Event Detail |
|---|---|---|
change |
Missing description |
Event
|
Learn more about events.
Inherited methods (29)
Et2Widget
| Name | Description |
|---|---|
checkCreateNamespace() |
Checks whether a namespace exists for this element in the content array. If yes, an own perspective of the content array is created. If not, the parent content manager is used. Constructor attributes are passed in case a child needs to make decisions |
clone() |
Creates a copy of this widget. |
createElementFromNode() |
Create a et2_widget from an XML node. First the type and attributes are read from the node. Then the readonly & modifications arrays are checked for changes specific to the loaded data. Then the appropriate constructor is called. After the constructor returns, the widget has a chance to further initialize itself from the XML node when the widget’s loadFromXML() method is called with the node. |
getArrayMgr() |
Returns the array manager object for the given part |
getArrayMgrs() |
Returns an associative array containing the top-most array managers. |
getChildren() |
Get child widgets Use |
getInstanceManager() |
Returns the instance manager |
getPath() |
Returns the path into the data array. By default, array manager takes care of this, but some extensions need to override this |
getRoot() |
Returns the base widget Usually this is the same as getInstanceManager().widgetContainer |
loadFromXML() |
Loads the widget tree from an XML node |
loadingFinished() |
Needed for legacy compatability. |
parseXMLAttrs() |
The parseXMLAttrs function takes an XML DOM attributes object and adds the given attributes to the _target associative array. This function also parses the legacyOptions. N.B. This is only used for legacy widgets. WebComponents use transformAttributes() and do their own handling of attributes. |
set_label() |
NOT the setter, since we cannot add to the DOM before connectedCallback() TODO: This is not best practice. Should just set property, DOM modification should be done in render https://lit-element.polymer-project.org/guide/templates#design-a-performant-template |
setArrayMgr() |
Sets the array manager for the given part |
setArrayMgrs() |
Sets all array manager objects - this function can be used to set the root array managers of the container object. |
setInstanceManager() |
Set the instance manager Normally this is not needed as it’s set on the top-level container, and we just return that reference |
_handleClick() |
Click handler calling custom handler set via onclick attribute to this.onclick |
destroy() |
et2_widget compatability |
set_class() |
Set the widget class |
set_disabled() |
Wrapper on this.disabled because legacy had it. |
set_statustext() |
supports legacy set_statustext |
Et2InputWidget
| Name | Description |
|---|---|
et2HandleBlur() |
If the value is unchanged, put any held validation messages back Named et2HandleBlur to avoid overwriting handleBlur() in Shoelace components |
et2HandleFocus() |
When input receives focus, clear any validation errors. If the value is the same on blur, we’ll put them back The ones from the server (ManualMessage) can interfere with submitting. Named et2HandleFocus to avoid overwriting handleFocus() in Shoelace components |
getInputNode() |
Get input to e.g. set aria-attributes |
handleSlChange() |
Handle sl-change event from Shoelace components and dispatch a change event so anything listening for change events can react to it instead of having to listen for both sl-change and change. |
isValid() |
Used by etemplate2 to determine if we can submit or not |
submit() |
Called whenever the template gets submitted. We return false if the widget is not valid, which cancels the submission. |
validate() |
Massively simplified validate, as compared to what ValidatorMixin gives us, since ValidatorMixin extends FormControlMixin which breaks SlSelect’s render() We take all validators for the widget, and if there’s a value (or field is required) we check the value with each validator. For array values we check each element with each validator. If the value does not pass the validator, we collect the message and display feedback to the user. We handle validation errors from the server with ManualMessages, which always “fail”. If the value is empty, we only validate if the field is required. |
_oldChange() |
Change handler calling custom handler set via onchange attribute |
Related
These widgets are related by naming but not by inheritance or sub-widget composition.