108 lines
3.0 KiB
Python
108 lines
3.0 KiB
Python
from fasthtml.components import *
|
|
|
|
from core.utils import merge_classes
|
|
|
|
|
|
def mk_icon(icon, size=20, can_select=True, cls='', tooltip=None, **kwargs):
|
|
merged_cls = merge_classes(f"icon-{size}",
|
|
'icon-btn' if can_select else '',
|
|
cls,
|
|
kwargs)
|
|
return mk_tooltip(icon, tooltip, cls=merged_cls, **kwargs) if tooltip else Div(icon, cls=merged_cls, **kwargs)
|
|
|
|
|
|
def mk_ellipsis(txt: str, cls='', **kwargs):
|
|
merged_cls = merge_classes("truncate",
|
|
cls,
|
|
kwargs)
|
|
return Div(txt, cls=merged_cls, data_tooltip=txt, **kwargs)
|
|
|
|
|
|
def mk_tooltip(element, tooltip: str, cls='', **kwargs):
|
|
merged_cls = merge_classes("mmt-tooltip",
|
|
cls,
|
|
kwargs)
|
|
return Div(element, cls=merged_cls, data_tooltip=tooltip, **kwargs)
|
|
|
|
|
|
def mk_tooltip_container(component_id):
|
|
return Div(id=f"tt_{component_id}", style="position: fixed; z-index: 1000;", cls="mmt-tooltip-container")
|
|
|
|
|
|
def mk_dialog_buttons(ok_title: str = "OK",
|
|
cancel_title: str = "Cancel",
|
|
on_ok: dict = None,
|
|
on_cancel: dict = None,
|
|
cls=None):
|
|
if on_ok is None:
|
|
on_ok = {}
|
|
if on_cancel is None:
|
|
on_cancel = {}
|
|
|
|
return Div(
|
|
Div(
|
|
Button(ok_title, cls="btn btn-primary btn-sm mr-2", **on_ok),
|
|
Button(cancel_title, cls="btn btn-ghost btn-sm", **on_cancel),
|
|
cls="flex justify-end"
|
|
),
|
|
cls=merge_classes("flex justify-end w-full", cls)
|
|
)
|
|
|
|
|
|
def mk_select_option(option: str, value=None, selected_value: str = None, selected=False, enabled=True):
|
|
attrs = {}
|
|
if value is not None:
|
|
attrs["value"] = value
|
|
if selected_value == option or selected is True:
|
|
attrs["selected"] = True
|
|
if not enabled:
|
|
attrs["disabled"] = True
|
|
|
|
return Option(option, **attrs)
|
|
|
|
|
|
def mk_accordion_section(component_id, title, icon, content, selected=False):
|
|
return Div(
|
|
Input(type="radio",
|
|
name=f"debugger-accordion-{component_id}",
|
|
checked="checked" if selected else None,
|
|
cls="p-0! min-h-0!",
|
|
),
|
|
Div(
|
|
mk_icon(icon, can_select=False), mk_ellipsis(title, cls="text-sm"),
|
|
cls="collapse-title p-0 min-h-0 flex truncate",
|
|
),
|
|
Div(
|
|
*content,
|
|
cls="collapse-content pr-0! truncate",
|
|
),
|
|
cls="collapse mb-2",
|
|
id=f"{component_id}",
|
|
)
|
|
|
|
|
|
def set_boundaries(boundaries, remove_margin=True, other=0):
|
|
if isinstance(boundaries, int):
|
|
max_height = boundaries
|
|
else:
|
|
max_height = int(boundaries["height"])
|
|
|
|
if remove_margin:
|
|
max_height -= 8
|
|
|
|
max_height -= other
|
|
|
|
return {"style": f"max-height:{max_height}px;"}
|
|
|
|
|
|
def safe_get_dialog_buttons_parameters(buttons_dict):
|
|
res = {}
|
|
if buttons_dict is None:
|
|
return res
|
|
|
|
for param in ["ok_title", "cancel_title", "on_ok", "on_cancel"]:
|
|
if param in buttons_dict:
|
|
res[param] = buttons_dict[param]
|
|
|
|
return res
|