Try unifying 3 different element interfaces
This commit is contained in:
@@ -18,7 +18,7 @@ from common.components.core import (
|
|||||||
randomid,
|
randomid,
|
||||||
render,
|
render,
|
||||||
)
|
)
|
||||||
from common.components.custom_elements import custom_element, register_element
|
from common.components.custom_elements import SessionTimestampButtons, register_element
|
||||||
from common.components.date_range_picker import (
|
from common.components.date_range_picker import (
|
||||||
DateRangeCalendar,
|
DateRangeCalendar,
|
||||||
DateRangeField,
|
DateRangeField,
|
||||||
@@ -77,6 +77,7 @@ from common.components.primitives import (
|
|||||||
Tr,
|
Tr,
|
||||||
Ul,
|
Ul,
|
||||||
YearPicker,
|
YearPicker,
|
||||||
|
custom_element_builder,
|
||||||
paginated_table_content,
|
paginated_table_content,
|
||||||
)
|
)
|
||||||
from common.components.search_select import (
|
from common.components.search_select import (
|
||||||
@@ -92,8 +93,9 @@ from common.utils import truncate
|
|||||||
__all__ = [
|
__all__ = [
|
||||||
"truncate",
|
"truncate",
|
||||||
"BaseComponent",
|
"BaseComponent",
|
||||||
"custom_element",
|
|
||||||
"register_element",
|
"register_element",
|
||||||
|
"SessionTimestampButtons",
|
||||||
|
"custom_element_builder",
|
||||||
"Element",
|
"Element",
|
||||||
"Fragment",
|
"Fragment",
|
||||||
"Media",
|
"Media",
|
||||||
|
|||||||
@@ -9,9 +9,10 @@ reader so drift fails ``tsc``.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Mapping, TypedDict, get_type_hints
|
from typing import TypedDict, get_type_hints
|
||||||
|
|
||||||
from common.components.core import Children, Element, HTMLAttribute, Media, Node
|
from common.components.core import Media
|
||||||
|
from common.components.primitives import custom_element_builder
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
@@ -33,22 +34,6 @@ def _kebab(name: str) -> str:
|
|||||||
return name.replace("_", "-")
|
return name.replace("_", "-")
|
||||||
|
|
||||||
|
|
||||||
def custom_element(
|
|
||||||
tag: str, props: Mapping[str, object], *, children: Children = None
|
|
||||||
) -> Node:
|
|
||||||
"""Emit ``<tag kebab-attrs>children</tag>`` and declare its compiled module.
|
|
||||||
|
|
||||||
The module path mirrors the source layout: ``ts/elements/<tag>.ts`` compiles
|
|
||||||
to ``dist/elements/<tag>.js``, which ``Media`` loads via ``ModuleScript``."""
|
|
||||||
attributes: list[HTMLAttribute] = [
|
|
||||||
(_kebab(key), value) # type: ignore[misc]
|
|
||||||
for key, value in props.items()
|
|
||||||
]
|
|
||||||
return Element(tag, attributes, children).with_media(
|
|
||||||
Media(js=(f"dist/elements/{tag}.js",))
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# ── Codegen ──────────────────────────────────────────────────────────────────
|
# ── Codegen ──────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
_TYPE_MAP = {int: "number", float: "number", str: "string", bool: "boolean"}
|
_TYPE_MAP = {int: "number", float: "number", str: "string", bool: "boolean"}
|
||||||
@@ -121,3 +106,22 @@ class PlayEventRowProps(TypedDict):
|
|||||||
|
|
||||||
|
|
||||||
register_element("play-event-row", "PlayEventRow", PlayEventRowProps)
|
register_element("play-event-row", "PlayEventRow", PlayEventRowProps)
|
||||||
|
|
||||||
|
|
||||||
|
class SessionTimestampButtonsProps(TypedDict):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
register_element(
|
||||||
|
"session-timestamp-buttons", "SessionTimestampButtons", SessionTimestampButtonsProps
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# ── Named tag builders (consistent htpy-style with Div/Span) ─────────────────
|
||||||
|
# Underscore-prefixed: used internally by domain wrappers.
|
||||||
|
# Public ones (no domain wrapper): exported directly.
|
||||||
|
|
||||||
|
_GameStatusSelector = custom_element_builder("game-status-selector")
|
||||||
|
_SessionDeviceSelector = custom_element_builder("session-device-selector")
|
||||||
|
_PlayEventRow = custom_element_builder("play-event-row")
|
||||||
|
SessionTimestampButtons = custom_element_builder("session-timestamp-buttons")
|
||||||
|
|||||||
@@ -228,9 +228,8 @@ _SELECTOR_OPTION_CLASS = (
|
|||||||
|
|
||||||
def GameStatusSelector(game, game_statuses, csrf_token: str) -> Node:
|
def GameStatusSelector(game, game_statuses, csrf_token: str) -> Node:
|
||||||
"""Light-DOM custom element; behavior in ts/elements/game-status-selector.ts."""
|
"""Light-DOM custom element; behavior in ts/elements/game-status-selector.ts."""
|
||||||
from common.components import custom_element
|
|
||||||
from common.components.core import Element
|
from common.components.core import Element
|
||||||
from common.components.custom_elements import GameStatusSelectorProps
|
from common.components.custom_elements import _GameStatusSelector, GameStatusSelectorProps
|
||||||
from common.components.primitives import Li, Ul
|
from common.components.primitives import Li, Ul
|
||||||
|
|
||||||
options = [
|
options = [
|
||||||
@@ -266,18 +265,15 @@ def GameStatusSelector(game, game_statuses, csrf_token: str) -> Node:
|
|||||||
dropdown = Div(
|
dropdown = Div(
|
||||||
data_dropdown="", class_="inline-flex rounded-md shadow-2xs relative"
|
data_dropdown="", class_="inline-flex rounded-md shadow-2xs relative"
|
||||||
)[toggle, menu]
|
)[toggle, menu]
|
||||||
return custom_element(
|
return _GameStatusSelector(game_id=game.id, status=game.status, csrf=csrf_token)[
|
||||||
"game-status-selector",
|
Div(class_="flex gap-2 items-center")[dropdown]
|
||||||
GameStatusSelectorProps(game_id=game.id, status=game.status, csrf=csrf_token),
|
]
|
||||||
children=[Div(class_="flex gap-2 items-center")[dropdown]],
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def SessionDeviceSelector(session, session_devices, csrf_token: str) -> Node:
|
def SessionDeviceSelector(session, session_devices, csrf_token: str) -> Node:
|
||||||
"""Light-DOM custom element; behavior in ts/elements/session-device-selector.ts."""
|
"""Light-DOM custom element; behavior in ts/elements/session-device-selector.ts."""
|
||||||
from common.components import custom_element
|
|
||||||
from common.components.core import Element
|
from common.components.core import Element
|
||||||
from common.components.custom_elements import SessionDeviceSelectorProps
|
from common.components.custom_elements import _SessionDeviceSelector, SessionDeviceSelectorProps
|
||||||
from common.components.primitives import Li, Ul
|
from common.components.primitives import Li, Ul
|
||||||
|
|
||||||
current_name = session.device.name if session.device else "Unknown"
|
current_name = session.device.name if session.device else "Unknown"
|
||||||
@@ -307,8 +303,6 @@ def SessionDeviceSelector(session, session_devices, csrf_token: str) -> Node:
|
|||||||
dropdown = Div(
|
dropdown = Div(
|
||||||
data_dropdown="", class_="inline-flex rounded-md shadow-2xs relative"
|
data_dropdown="", class_="inline-flex rounded-md shadow-2xs relative"
|
||||||
)[toggle, menu]
|
)[toggle, menu]
|
||||||
return custom_element(
|
return _SessionDeviceSelector(session_id=session.id, csrf=csrf_token)[
|
||||||
"session-device-selector",
|
Div(class_="flex gap-2 items-center")[dropdown]
|
||||||
SessionDeviceSelectorProps(session_id=session.id, csrf=csrf_token),
|
]
|
||||||
children=[Div(class_="flex gap-2 items-center")[dropdown]],
|
|
||||||
)
|
|
||||||
|
|||||||
@@ -68,8 +68,21 @@ def _attrs_from_kwargs(attrs: dict[str, object]) -> list[HTMLAttribute]:
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def _html_element(tag_name: str):
|
def custom_element_builder(tag_name: str):
|
||||||
"""Build a generic element builder for ``tag_name`` (the whitelist factory)."""
|
"""Create a tag builder for a custom element with auto-attached Media.
|
||||||
|
|
||||||
|
The module path follows the convention ``ts/elements/<tag>.ts`` →
|
||||||
|
``dist/elements/<tag>.js``.
|
||||||
|
"""
|
||||||
|
return _html_element(tag_name, Media(js=(f"dist/elements/{tag_name}.js",)))
|
||||||
|
|
||||||
|
|
||||||
|
def _html_element(tag_name: str, media: Media | None = None):
|
||||||
|
"""Build a generic element builder for ``tag_name`` (the whitelist factory).
|
||||||
|
|
||||||
|
If ``media`` is provided, every node created by the builder will carry it
|
||||||
|
(used for custom elements whose compiled JS must be loaded automatically).
|
||||||
|
"""
|
||||||
|
|
||||||
def element(
|
def element(
|
||||||
attributes: Attributes | None = None,
|
attributes: Attributes | None = None,
|
||||||
@@ -77,7 +90,8 @@ def _html_element(tag_name: str):
|
|||||||
**attrs: object,
|
**attrs: object,
|
||||||
) -> Element:
|
) -> Element:
|
||||||
merged = as_attributes(attributes) + _attrs_from_kwargs(attrs)
|
merged = as_attributes(attributes) + _attrs_from_kwargs(attrs)
|
||||||
return Element(tag_name, merged, children)
|
node = Element(tag_name, merged, children)
|
||||||
|
return node.with_media(media) if media else node
|
||||||
|
|
||||||
element.__name__ = element.__qualname__ = tag_name[:1].upper() + tag_name[1:]
|
element.__name__ = element.__qualname__ = tag_name[:1].upper() + tag_name[1:]
|
||||||
element.__doc__ = f"Builder for the <{tag_name}> element."
|
element.__doc__ = f"Builder for the <{tag_name}> element."
|
||||||
@@ -245,8 +259,9 @@ def Button(
|
|||||||
title: str = "",
|
title: str = "",
|
||||||
onclick: str = "",
|
onclick: str = "",
|
||||||
name: str = "",
|
name: str = "",
|
||||||
|
**attrs: object,
|
||||||
) -> Element:
|
) -> Element:
|
||||||
attributes = as_attributes(attributes)
|
attributes = as_attributes(attributes) + _attrs_from_kwargs(attrs)
|
||||||
children = children or []
|
children = children or []
|
||||||
|
|
||||||
# Separate custom class from other generic attributes
|
# Separate custom class from other generic attributes
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
import { toISOUTCString } from "./utils.js";
|
|
||||||
|
|
||||||
for (let button of document.querySelectorAll("[data-target]")) {
|
|
||||||
let target = button.getAttribute("data-target");
|
|
||||||
let type = button.getAttribute("data-type");
|
|
||||||
let targetElement = document.querySelector(`#id_${target}`);
|
|
||||||
button.addEventListener("click", (event) => {
|
|
||||||
event.preventDefault();
|
|
||||||
if (type == "now") {
|
|
||||||
targetElement.value = toISOUTCString(new Date());
|
|
||||||
} else if (type == "copy") {
|
|
||||||
const oppositeName =
|
|
||||||
targetElement.name == "timestamp_start"
|
|
||||||
? "timestamp_end"
|
|
||||||
: "timestamp_start";
|
|
||||||
document.querySelector(`[name='${oppositeName}']`).value =
|
|
||||||
targetElement.value;
|
|
||||||
} else if (type == "toggle") {
|
|
||||||
if (targetElement.type == "datetime-local") targetElement.type = "text";
|
|
||||||
else targetElement.type = "datetime-local";
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
import { onSwap } from "./utils.js";
|
||||||
|
|
||||||
|
onSwap("#year-picker-input", function(pickerEl) {
|
||||||
|
const selectedYear = pickerEl.dataset.selectedYear;
|
||||||
|
const urlTemplate = pickerEl.dataset.urlTemplate;
|
||||||
|
const currentYear = new Date().getFullYear();
|
||||||
|
const availableYears = new Set(
|
||||||
|
pickerEl.dataset.availableYears
|
||||||
|
.split(",")
|
||||||
|
.map(s => parseInt(s.trim()))
|
||||||
|
.filter(n => !isNaN(n))
|
||||||
|
);
|
||||||
|
|
||||||
|
const picker = new Datepicker(pickerEl, {
|
||||||
|
pickLevel: 2,
|
||||||
|
format: "yyyy",
|
||||||
|
minDate: new Date(1999, 0, 1),
|
||||||
|
maxDate: new Date(currentYear, 11, 31),
|
||||||
|
autohide: false,
|
||||||
|
orientation: "bottom end",
|
||||||
|
showOnClick: false,
|
||||||
|
showOnFocus: false,
|
||||||
|
beforeShowYear: (date) => ({ enabled: availableYears.has(date.getFullYear()) }),
|
||||||
|
});
|
||||||
|
pickerEl._pickerInstance = picker;
|
||||||
|
|
||||||
|
picker.element.addEventListener("changeDate", (event) => {
|
||||||
|
const year = event.detail.date?.getFullYear();
|
||||||
|
if (year && urlTemplate) {
|
||||||
|
window.location.href = urlTemplate.replace("__year__", year);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (selectedYear) {
|
||||||
|
picker.dates = [new Date(parseInt(selectedYear), 0, 1)];
|
||||||
|
picker.update();
|
||||||
|
}
|
||||||
|
});
|
||||||
+8
-14
@@ -354,7 +354,7 @@ _PLAYED_MENU = (
|
|||||||
def _played_row(game: Game, request: HttpRequest) -> Node:
|
def _played_row(game: Game, request: HttpRequest) -> Node:
|
||||||
"""'Played N times' control as a custom element (ts/elements/play-event-row.ts)."""
|
"""'Played N times' control as a custom element (ts/elements/play-event-row.ts)."""
|
||||||
from common.components import Element, custom_element
|
from common.components import Element, custom_element
|
||||||
from common.components.custom_elements import PlayEventRowProps
|
from common.components.custom_elements import PlayEventRowProps, _PlayEventRow
|
||||||
|
|
||||||
played = game.playevents.count()
|
played = game.playevents.count()
|
||||||
|
|
||||||
@@ -397,19 +397,13 @@ def _played_row(game: Game, request: HttpRequest) -> Node:
|
|||||||
group = Div(class_="inline-flex items-stretch rounded-md shadow-2xs")[
|
group = Div(class_="inline-flex items-stretch rounded-md shadow-2xs")[
|
||||||
count_button, toggle_group
|
count_button, toggle_group
|
||||||
]
|
]
|
||||||
return custom_element(
|
return _PlayEventRow(
|
||||||
"play-event-row",
|
game_id=game.id,
|
||||||
PlayEventRowProps(
|
csrf=get_token(request),
|
||||||
game_id=game.id,
|
api_create_url=reverse("api-1.0.0:create_playevent"),
|
||||||
csrf=get_token(request),
|
)[Div(class_="flex gap-2 items-center")[
|
||||||
api_create_url=reverse("api-1.0.0:create_playevent"),
|
Span(class_="uppercase")["Played"], group
|
||||||
),
|
]]
|
||||||
children=[
|
|
||||||
Div(class_="flex gap-2 items-center")[
|
|
||||||
Span(class_="uppercase")["Played"], group
|
|
||||||
]
|
|
||||||
],
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _stat_popover(popover_id: str, tooltip: str, svg_key: str, value: str) -> SafeText:
|
def _stat_popover(popover_id: str, tooltip: str, svg_key: str, value: str) -> SafeText:
|
||||||
|
|||||||
+16
-31
@@ -11,12 +11,12 @@ from django.utils import timezone
|
|||||||
from django.utils.safestring import SafeText, mark_safe
|
from django.utils.safestring import SafeText, mark_safe
|
||||||
|
|
||||||
from common.components import (
|
from common.components import (
|
||||||
Fragment,
|
|
||||||
A,
|
A,
|
||||||
AddForm,
|
AddForm,
|
||||||
Button,
|
Button,
|
||||||
ButtonGroup,
|
ButtonGroup,
|
||||||
Div,
|
Div,
|
||||||
|
Fragment,
|
||||||
Icon,
|
Icon,
|
||||||
ModuleScript,
|
ModuleScript,
|
||||||
NameWithIcon,
|
NameWithIcon,
|
||||||
@@ -27,6 +27,7 @@ from common.components import (
|
|||||||
SessionDeviceSelector,
|
SessionDeviceSelector,
|
||||||
paginated_table_content,
|
paginated_table_content,
|
||||||
)
|
)
|
||||||
|
from common.components import SessionTimestampButtons
|
||||||
from common.components.primitives import Span, Td, Tr
|
from common.components.primitives import Span, Td, Tr
|
||||||
from common.layout import render_page
|
from common.layout import render_page
|
||||||
from common.time import (
|
from common.time import (
|
||||||
@@ -208,32 +209,20 @@ def _session_fields(form) -> Fragment:
|
|||||||
this_side = "start" if field.name == "timestamp_start" else "end"
|
this_side = "start" if field.name == "timestamp_start" else "end"
|
||||||
other_side = "end" if field.name == "timestamp_start" else "start"
|
other_side = "end" if field.name == "timestamp_start" else "start"
|
||||||
children.append(
|
children.append(
|
||||||
Span(
|
SessionTimestampButtons(
|
||||||
attributes=[
|
class_="form-row-button-group flex-row gap-3 justify-start mt-3",
|
||||||
(
|
hx_boost="false",
|
||||||
"class",
|
)[
|
||||||
"form-row-button-group flex-row gap-3 justify-start mt-3",
|
Button(data_target=field.name, data_type="now", size="xs")[
|
||||||
),
|
"Set to now"
|
||||||
("hx-boost", "false"),
|
|
||||||
],
|
],
|
||||||
children=[
|
Button(data_target=field.name, data_type="toggle", size="xs")[
|
||||||
Button(
|
"Toggle text"
|
||||||
[("data-target", field.name), ("data-type", "now")],
|
|
||||||
"Set to now",
|
|
||||||
size="xs",
|
|
||||||
),
|
|
||||||
Button(
|
|
||||||
[("data-target", field.name), ("data-type", "toggle")],
|
|
||||||
"Toggle text",
|
|
||||||
size="xs",
|
|
||||||
),
|
|
||||||
Button(
|
|
||||||
[("data-target", field.name), ("data-type", "copy")],
|
|
||||||
f"Copy {this_side} value to {other_side}",
|
|
||||||
size="xs",
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
)
|
Button(data_target=field.name, data_type="copy", size="xs")[
|
||||||
|
f"Copy {this_side} value to {other_side}"
|
||||||
|
],
|
||||||
|
]
|
||||||
)
|
)
|
||||||
rows.append(Div(children=children))
|
rows.append(Div(children=children))
|
||||||
return Fragment(*rows, separator="\n")
|
return Fragment(*rows, separator="\n")
|
||||||
@@ -265,9 +254,7 @@ def add_session(request: HttpRequest, game_id: int = 0) -> HttpResponse:
|
|||||||
request,
|
request,
|
||||||
AddForm(form, request=request, fields=_session_fields(form), submit_class=""),
|
AddForm(form, request=request, fields=_session_fields(form), submit_class=""),
|
||||||
title="Add New Session",
|
title="Add New Session",
|
||||||
scripts=mark_safe(
|
scripts=mark_safe(ModuleScript("search_select.js")),
|
||||||
ModuleScript("search_select.js") + ModuleScript("add_session.js")
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -282,9 +269,7 @@ def edit_session(request: HttpRequest, session_id: int) -> HttpResponse:
|
|||||||
request,
|
request,
|
||||||
AddForm(form, request=request, fields=_session_fields(form), submit_class=""),
|
AddForm(form, request=request, fields=_session_fields(form), submit_class=""),
|
||||||
title="Edit Session",
|
title="Edit Session",
|
||||||
scripts=mark_safe(
|
scripts=mark_safe(ModuleScript("search_select.js")),
|
||||||
ModuleScript("search_select.js") + ModuleScript("add_session.js")
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import unittest
|
import unittest
|
||||||
from typing import TypedDict
|
from typing import TypedDict
|
||||||
|
|
||||||
from common.components import custom_element, render
|
from common.components import custom_element_builder, render
|
||||||
from common.components.custom_elements import (
|
from common.components.custom_elements import (
|
||||||
ElementSpec,
|
ElementSpec,
|
||||||
_ts_for_spec,
|
_ts_for_spec,
|
||||||
@@ -17,9 +17,8 @@ class SampleProps(TypedDict):
|
|||||||
|
|
||||||
class CustomElementBuilderTest(unittest.TestCase):
|
class CustomElementBuilderTest(unittest.TestCase):
|
||||||
def test_serializes_props_to_kebab_attributes(self):
|
def test_serializes_props_to_kebab_attributes(self):
|
||||||
html = render(
|
x_sample = custom_element_builder("x-sample")
|
||||||
custom_element("x-sample", {"game_id": 3, "status": "f"}, children=["hi"])
|
html = render(x_sample(game_id=3, status="f")["hi"])
|
||||||
)
|
|
||||||
self.assertIn("<x-sample", html)
|
self.assertIn("<x-sample", html)
|
||||||
self.assertIn('game-id="3"', html)
|
self.assertIn('game-id="3"', html)
|
||||||
self.assertIn('status="f"', html)
|
self.assertIn('status="f"', html)
|
||||||
@@ -28,7 +27,8 @@ class CustomElementBuilderTest(unittest.TestCase):
|
|||||||
def test_declares_compiled_module_media(self):
|
def test_declares_compiled_module_media(self):
|
||||||
from common.components import collect_media
|
from common.components import collect_media
|
||||||
|
|
||||||
node = custom_element("x-sample", {"game_id": 3})
|
x_sample = custom_element_builder("x-sample")
|
||||||
|
node = x_sample(game_id=3)
|
||||||
self.assertEqual(collect_media(node).js, ("dist/elements/x-sample.js",))
|
self.assertEqual(collect_media(node).js, ("dist/elements/x-sample.js",))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -139,7 +139,7 @@ class RenderedPagesTest(TestCase):
|
|||||||
|
|
||||||
def test_add_session_form_has_timestamp_helpers(self):
|
def test_add_session_form_has_timestamp_helpers(self):
|
||||||
html = self.get("games:add_session").content.decode()
|
html = self.get("games:add_session").content.decode()
|
||||||
self.assertIn("add_session.js", html)
|
self.assertIn("session-timestamp-buttons", html)
|
||||||
for marker in [
|
for marker in [
|
||||||
"Set to now",
|
"Set to now",
|
||||||
"Toggle text",
|
"Toggle text",
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
// import { toISOUTCString } from "../../games/static/js/utils.js";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Formats Date to a UTC string accepted by the datetime-local input field.
|
||||||
|
* @param {Date} date
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
function toISOUTCString(date: Date): string {
|
||||||
|
function stringAndPad(number: number): string {
|
||||||
|
return number.toString().padStart(2, "0");
|
||||||
|
}
|
||||||
|
const year = date.getFullYear();
|
||||||
|
const month = stringAndPad(date.getMonth() + 1);
|
||||||
|
const day = stringAndPad(date.getDate());
|
||||||
|
const hours = stringAndPad(date.getHours());
|
||||||
|
const minutes = stringAndPad(date.getMinutes());
|
||||||
|
return `${year}-${month}-${day}T${hours}:${minutes}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
class SessionTimestampButtonsElement extends HTMLElement {
|
||||||
|
connectedCallback(): void {
|
||||||
|
for (const button of this.querySelectorAll("[data-target]")) {
|
||||||
|
const target = button.getAttribute("data-target");
|
||||||
|
const type = button.getAttribute("data-type");
|
||||||
|
if (!target || !type) continue;
|
||||||
|
const targetElement = document.querySelector(`#id_${target}`);
|
||||||
|
if (!(targetElement instanceof HTMLInputElement)) return;
|
||||||
|
button.addEventListener("click", (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
if (type == "now") {
|
||||||
|
targetElement.value = toISOUTCString(new Date());
|
||||||
|
} else if (type == "copy") {
|
||||||
|
const oppositeName =
|
||||||
|
targetElement.name == "timestamp_start"
|
||||||
|
? "timestamp_end"
|
||||||
|
: "timestamp_start";
|
||||||
|
const opposite = document.querySelector(`[name='${oppositeName}']`);
|
||||||
|
if (!(opposite instanceof HTMLInputElement)) return;
|
||||||
|
opposite.value = targetElement.value;
|
||||||
|
} else if (type == "toggle") {
|
||||||
|
if (targetElement.type == "datetime-local") targetElement.type = "text";
|
||||||
|
else targetElement.type = "datetime-local";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
customElements.define("session-timestamp-buttons", SessionTimestampButtonsElement);
|
||||||
Reference in New Issue
Block a user