Time Range Field¶
Two time boxes, a start and an end, each with the time field's clock icon inside. It is built on the Field Adornments time box and checks through the validation core, with its message from the Field Message.
Decision¶
| When | Use |
|---|---|
| A window within a day, such as quiet hours or an access window | Time Range Field |
| A single time | A time input, which Stolon adorns |
| A range of days | Date Range Picker |
| A length of time | Duration Field |
| Several windows on chosen days | Access Schedule |
Usage¶
import { createTimeRangeField, toHours, fromHours } from './configurationpage?name=myplugin_stolon.js';
var quiet = createTimeRangeField({ value: { start: fromHours(config.QuietStart), end: fromHours(config.QuietEnd) }, step: 15 });
view.querySelector('#quietHost').appendChild(quiet.element);
// On save: toHours(quiet.getValue().start), toHours(quiet.getValue().end, true)
Classes: stolon-time-range, stolon-time-range-row, stolon-time-range-start, stolon-time-range-end. The handle is { element, start, end, getValue, setValue, isValid, setSize, destroy }, and getValue() returns { start, end } as 'HH:MM'.
States and Modifiers¶
stepis in minutes (default 30). A typed time moves to the nearest allowed one, andsetValuekeeps a time off the step as it is.- Without
allowOvernight, an end at or before the start shows "End must be after the start.". An end of00:00is midnight at the end of the day, so 22:00 to 00:00 is valid. requiredrequires both times. It andvalidate({ start, end })check through the core, andisValid()checks both times and shows the message.endBeforeStartText,startLabel, andendLabelreplace the defaults.toHours(time, end)reads Jellyfin's decimal hours, with00:00as 24 whenendis true, andfromHours(hours)writes them back.snapTime(time, step)is the snap the field uses.- The Access Schedule builds each window's times on it.
Reference¶
Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.
toHours¶
function toHours(time: string, end?: boolean): number | null
Reads a time as decimal hours, as Jellyfin stores an access schedule, such as '09:30' as 9.5. With end, '00:00' reads as 24, midnight at the end of the day.
fromHours¶
function fromHours(hours: number): string
Writes decimal hours as a time, such as 9.5 as '09:30'. 24 is midnight, '00:00', and an hour outside 0 to 24 is clamped.
snapTime¶
function snapTime(time: string, step: number): string
Moves a time to the nearest one a step of minutes allows, counted from midnight, such as '09:20' to '09:30' with a step of 30.
createTimeRangeField¶
function createTimeRangeField(options?: TimeRangeFieldOptions): TimeRangeFieldHandle
Two time boxes, a start and an end, each with the time field's clock icon inside. A typed time moves to the nearest one the step allows, and without allowOvernight an end at or before the start is an error, through the validation core. An end of '00:00' is midnight at the end of the day, so 22:00 to 00:00 is valid. toHours and fromHours convert to and from Jellyfin's decimal hours.
| Option | Type | What it does |
|---|---|---|
value |
Partial<TimeRangeValue> | null |
{ start, end } as 'HH:MM' [optional] |
step |
number |
minutes between allowed times (default 30) |
allowOvernight |
boolean |
true lets the end come before the start, for a range that runs past midnight (default false) |
required |
boolean |
true requires both times (default false) |
startLabel |
string |
aria-label of the start box (default 'Start') |
endLabel |
string |
aria-label of the end box (default 'End') |
endBeforeStartText |
string |
message for an end at or before the start (default 'The end must be after the start.') |
size |
'small' | 'medium' | 'large' |
'small', 'medium', or 'large' for its fields (default 'medium') |
validate |
(value: TimeRangeValue): string | null | undefined |
function({ start, end }) returning an error sentence, or an empty value when valid [optional] |
onChange |
(value: TimeRangeValue): void |
function({ start, end }) [optional] |
| Handle member | Type |
|---|---|
start |
HTMLInputElement |
end |
HTMLInputElement |
getValue |
(): TimeRangeValue |
setValue |
(next?: Partial<TimeRangeValue> | null): void |
isValid |
(): boolean |
setSize |
(size?: 'small' | 'medium' | 'large'): void |
destroy |
(): void |
| Type | What it is |
|---|---|
TimeRangeValue |
A start and an end time, each as 'HH:MM', or '' while empty. |
| Class | What it looks like |
|---|---|
stolon-time-range |
A start and an end time side by side, each with the clock icon inside its box, and the range's message under them |
Example¶
Example Code¶
<!-- title: Time Range Field -->
<div class="stolon-field"><label>Quiet Hours</label><div id="quiet"></div></div>
<div class="stolon-field" style="margin-top: 16px;"><label>Overnight Window</label><div id="night"></div></div>
<div class="stolon-field" style="margin-top: 16px;"><label>Backwards Range</label><div id="bad"></div></div>
<script type="module">
const base = await window.__baseReady;
document.getElementById('quiet').appendChild(base.createTimeRangeField({ value: { start: '22:00', end: '00:00' }, step: 15 }).element);
document.getElementById('night').appendChild(base.createTimeRangeField({ value: { start: '23:00', end: '06:30' }, allowOvernight: true }).element);
const bad = base.createTimeRangeField({ value: { start: '18:00', end: '09:00' } });
document.getElementById('bad').appendChild(bad.element);
bad.isValid();
</script>