def test_live_element2(): setup() with context(is_test=True): @mock_hypergen_callback def my_callback(): pass with context(is_test=True, hypergen=hypergen_context()): el1 = input_(id_="id_new_password", placeholder="Adgangskode", oninput=cb(my_callback, THIS, "")) el2 = input_(placeholder="Gentag Adgangskode", oninput=cb(my_callback, THIS, el1)) h2(u"Skift Adgangskode") p(u"Rules:") with div(class_="form"): with div(): with ul(id_="password_verification_smartassness"): div("TODO") with div(class_="form"): div(el1, class_="form-field") div(el2, class_="form-field") div(u"Skift adgangskode", class_="button disabled") assert f( ) == """<h2>Skift Adgangskode</h2><p>Rules:</p><div class="form"><div><ul id="password_verification_smartassness"><div>TODO</div></ul><div class="form"><div class="form-field"><input id="id_new_password" oninput="e(event,'__main__',1234)" placeholder="Adgangskode"/></div><div class="form-field"><input id="A" oninput="e(event,'__main__',1234)" placeholder="Gentag Adgangskode"/></div><div class="button disabled">Skift adgangskode</div></div></div></div>"""
def test_callback(): setup() with context(is_test=True, hypergen=hypergen_context()): @mock_hypergen_callback def f1(foo, punk=300): pass element = input_(oninput=cb(f1, THIS, 200, debounce=500)) assert type(cb("foo", 42, debounce=42)(element, "oninput", 92)) is list
def test_live_element(): setup() with context(is_test=True): @mock_hypergen_callback def my_callback(): pass with context(is_test=True, hypergen=hypergen_context()): div("hello world!", onclick=cb("my_url", 42)) assert f( ) == """<div id="A" onclick="e(event,'__main__',1234)">hello world!</div>""" return with context(is_test=True, hypergen=hypergen_context()): div("hello world!", onclick=cb(my_callback, [42])) assert f( ) == """<div id="A" onclick="e(event,'__main__',1234)">hello world!</div>""" with context(is_test=True, hypergen=hypergen_context()): a = input_(name="a") input_(name="b", onclick=cb(my_callback, a)) assert f( ) == """<input name="a"/><input id="A" name="b" onclick="e(event,'__main__',1234)"/>""" with context(is_test=True, hypergen=hypergen_context()): el = textarea(placeholder=u"myplace") with div(class_="message"): with div(class_="action-left"): span(u"Annullér", class_="clickable") with div(class_="action-right"): span(u"Send", class_="clickable", onclick=cb(my_callback, el)) div(el, class_="form form-write") assert f( ) == """<div class="message"><div class="action-left"><span class="clickable">Annull\xe9r</span></div><div class="action-right"><span class="clickable" onclick="e(event,'__main__',1234)" id="A">Send</span></div><div class="form form-write"><textarea placeholder="myplace"></textarea></div></div>""" with context(is_test=True, hypergen=hypergen_context()): input_(autofocus=True) assert join_html(c.hypergen.into) == '<input autofocus/>'
def todo_item(item): from todomvc.views import toggle_is_completed, delete, start_edit, submit_edit is_editing = item.pk == c.appstate["edit_item_pk"] classes = [] if item.is_completed: classes.append("completed") if is_editing: classes.append("editing") with li(class_=classes): if not is_editing: with div(class_="view"): input_(id_=("toggle_is_completed", item.pk), class_="toggle", type_="checkbox", checked=item.is_completed, onclick=cb(toggle_is_completed, item.pk)) label(item.description, id_=("start_edit", item.pk), ondblclick=cb(start_edit, item.pk)) button(class_="destroy", id_=("destroy", item.pk), onclick=cb(delete, item.pk)) else: input_(id_="edit-item", class_="edit", autofocus=True, value=item.description, onkeyup=cb(submit_edit, item.pk, THIS, event_matches={"key": "Enter"}), onblur=cb(submit_edit, item.pk, THIS))
def content(items, filtering, all_completed): from todomvc.views import ALL, ACTIVE, COMPLETED, todomvc, add, clear_completed, toggle_all with section(class_="todoapp"): with header(class_="header"): h1("todos") input_(id_="new-todo", class_="new-todo", placeholder="What needs to be done?", autofocus=not c.appstate["edit_item_pk"], onkeyup=cb(add, THIS, event_matches={"key": "Enter"}, clear=True)) if filtering == ALL and not items: return with section(class_="main"): input_(id_="toggle-all", class_="toggle-all", type_="checkbox", checked=all_completed, onclick=cb(toggle_all, not all_completed)) label("Mark all as complete", for_="toggle-all") ul([todo_item(item) for item in items], class_="todo-list") with footer(class_="footer"): span(strong(len(items), "items" if len(items) > 1 else "item", sep=" "), class_="todo-count") with ul(class_="filters"): li( a("All", class_="selected" if filtering == ALL else "", href=todomvc.reverse(ALL), id_="filter-all")) li( a("Active", class_="selected" if filtering == ACTIVE else "", href=todomvc.reverse(ACTIVE), id_="filter-active")) li( a("Completed", class_="selected" if filtering == COMPLETED else "", href=todomvc.reverse(COMPLETED), id_="filter-completed")) if items.filter(is_completed=True): button("Clear completed", id_="clear-completed", class_="clear-completed", onclick=cb(clear_completed))
def test_eventhandler_cache(): with context(is_test=True, hypergen=hypergen_context()): @mock_hypergen_callback def f1(): pass input_(onclick=cb(f1, THIS)) ehc = { i: v for i, v in enumerate(context.hypergen.client_state.values()) } assert dumps( ehc ) == '{"0":["hypergen.callback","/path/to/cb/",[["_","element_value",["hypergen.read.value",null,"A"]]],{"blocks":false,"confirm_":false,"debounce":0,"clear":false,"elementId":"A","uploadFiles":false}]}'
def inputs(request): INPUT_TYPES = [ ("button", d(value="clicked")), ("checkbox", d(checked=True)), ("color", d(value="#bb7777")), ("date", d(value=datetime.date(2021, 4, 16))), ("datetime-local", d(value=datetime.datetime(1941, 5, 5, 5, 23))), ("email", d(value="*****@*****.**")), ("file", d()), ("hidden", d(value="hidden")), ("image", d(src="https://picsum.photos/80/40", value="clicked")), ("month", d(value=d(year=2099, month=9))), ("number", d(title="number (int)", value=99)), ("number", d(title="number (float)", value=12.12, coerce_to=float)), ("password", d(value="1234")), ("radio", d(name="myradio", value=20, checked=True, coerce_to=int)), ("radio", d(name="myradio", value=21, coerce_to=int)), ("range", d()), ("reset", d(value="clicked")), ("search", d(value="Who is Rune Kaagaard?")), ("submit", d(value="clicked")), ("tel", d(value="12345678")), ("text", d(value="This is text!")), ("time", d(value=datetime.time(7, 42))), ("url", d(value="https://github.com/runekaagaard/django-hypergen/")), ("week", d(value=d(year=1999, week=42))),] h1("Showing all input types.") with table(): tr(th(x) for x in ["Input type", "Input attributes", "Element", "Server callback value"]) for i, pair in enumerate(INPUT_TYPES): type_, attrs = pair title = attrs.pop("title", type_) id_ = "server-value-{}".format(i) with tr(): td(title) td(code(attrs)) submit_cb = cb(submit, THIS, id_) td(input_(id_=("element", i), class_="input", type_=type_, oninput=submit_cb, **attrs)) td(id_=id_) with tr(): i += 1 id_ = "server-value-{}".format(i) attrs = d() td("textarea") td(code(attrs)) submit_cb = cb(submit, THIS, id_) td( textarea("Who is Jeppe Tuxen?", id_=("element", i), class_="input", type_=type_, oninput=submit_cb, **attrs)) td(id_=id_) with tr(): i += 1 id_ = "server-value-{}".format(i) attrs = d(coerce_to=int) td("select") td(code(attrs)) submit_cb = cb(submit, THIS, id_) td( select([option(x, value=x, selected=x == 3) for x in range(5)], id_=("element", i), class_="input", type_=type_, onclick=submit_cb, oninput=submit_cb, **attrs)) td(id_=id_) script(""" ready(function() { document.querySelectorAll(".input").forEach(el => (el.oninput(new Event("input")))); }) """)
def callback(id_, event_name, url_or_view, *cb_args, **kwargs): # Fake element to use with the callback func. el = div(id_=id_) return mark_safe("".join(cb(url_or_view, *cb_args, **kwargs)(el, event_name, None)))