forked from pyscript/pyscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.py
46 lines (35 loc) · 1.3 KB
/
todo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from datetime import datetime as dt
from js import console
from utils import add_class, remove_class
tasks = []
# define the task template that will be use to render new templates to the page
task_template = Element("task-template").select(".task", from_content=True)
task_list = Element("list-tasks-container")
new_task_content = Element("new-task-content")
def add_task(*ags, **kws):
# create task
task_id = f"task-{len(tasks)}"
task = {
"id": task_id,
"content": new_task_content.element.value,
"done": False,
"created_at": dt.now(),
}
tasks.append(task)
# add the task element to the page as new node in the list by cloning from a template
taskHtml = task_template.clone(task_id, to=task_list)
taskHtmlContent = taskHtml.select("p")
taskHtmlContent.element.innerText = task["content"]
taskHtmlCheck = taskHtml.select("input")
task_list.element.appendChild(taskHtml.element)
def check_task(evt=None):
task["done"] = not task["done"]
if task["done"]:
add_class(taskHtmlContent, "line-through")
else:
remove_class(taskHtmlContent, "line-through")
new_task_content.clear()
taskHtmlCheck.element.onclick = check_task
def add_task_event(e):
if e.key == "Enter":
add_task()