Keyboard.py : ajout de enabled dans add(), nouveau render() retournant (Script,
control_div), et méthodes mk_enable / mk_disable - keyboard.js : nouvelle signature add_keyboard_support(elementId, controlDivId, combinationsJson), fonction isCombinationEnabled(), vérification avant déclenchement - test_Keyboard.py : 8 tests couvrant les comportements et le rendu
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import json
|
||||
|
||||
from fasthtml.components import Div
|
||||
from fasthtml.xtend import Script
|
||||
|
||||
from myfasthtml.core.commands import Command
|
||||
from myfasthtml.core.instances import MultipleInstance
|
||||
from myfasthtml.core.utils import make_html_id
|
||||
|
||||
|
||||
class Keyboard(MultipleInstance):
|
||||
@@ -16,18 +18,38 @@ class Keyboard(MultipleInstance):
|
||||
def __init__(self, parent, combinations=None, _id=None):
|
||||
super().__init__(parent, _id=_id)
|
||||
self.combinations = combinations or {}
|
||||
|
||||
def add(self, sequence: str, command: Command, require_inside: bool = True):
|
||||
self.combinations[sequence] = {"command": command, "require_inside": require_inside}
|
||||
|
||||
def add(self, sequence: str, command: Command, require_inside: bool = True, enabled: bool = True):
|
||||
self.combinations[sequence] = {"command": command, "require_inside": require_inside, "enabled": enabled}
|
||||
return self
|
||||
|
||||
def render(self):
|
||||
str_combinations = {}
|
||||
control_children = []
|
||||
for sequence, value in self.combinations.items():
|
||||
params = value["command"].get_htmx_params()
|
||||
params["require_inside"] = value.get("require_inside", True)
|
||||
str_combinations[sequence] = params
|
||||
return Script(f"add_keyboard_support('{self._parent.get_id()}', '{json.dumps(str_combinations)}')")
|
||||
control_children.append(
|
||||
Div(id=f"{self.get_id()}-{make_html_id(sequence)}",
|
||||
data_combination=sequence,
|
||||
data_enabled="true" if value.get("enabled", True) else "false")
|
||||
)
|
||||
script = Script(f"add_keyboard_support('{self._parent.get_id()}', '{self.get_id()}', '{json.dumps(str_combinations)}')")
|
||||
control_div = Div(*control_children, id=self.get_id(), name="keyboard")
|
||||
return script, control_div
|
||||
|
||||
def mk_enable(self, sequence: str):
|
||||
return Div(id=f"{self.get_id()}-{make_html_id(sequence)}",
|
||||
data_combination=sequence,
|
||||
data_enabled="true",
|
||||
hx_swap_oob="true")
|
||||
|
||||
def mk_disable(self, sequence: str):
|
||||
return Div(id=f"{self.get_id()}-{make_html_id(sequence)}",
|
||||
data_combination=sequence,
|
||||
data_enabled="false",
|
||||
hx_swap_oob="true")
|
||||
|
||||
def __ft__(self):
|
||||
return self.render()
|
||||
|
||||
Reference in New Issue
Block a user