2023-11-24 20:41:04 +01:00
|
|
|
import { style, el, dispatchEvent, on, O, scope } from '../exports.js';
|
2023-09-05 09:25:47 +02:00
|
|
|
const className= style.host(todosComponent).css`
|
|
|
|
:host{
|
|
|
|
display: flex;
|
|
|
|
flex-flow: column nowrap;
|
|
|
|
}
|
|
|
|
:host input{
|
|
|
|
margin-inline-start: .5em;
|
|
|
|
}
|
|
|
|
:host button{
|
|
|
|
margin-inline-start: 1em;
|
|
|
|
}
|
2023-09-08 20:18:58 +02:00
|
|
|
:host output{
|
|
|
|
white-space: pre;
|
|
|
|
}
|
2023-09-05 09:25:47 +02:00
|
|
|
`;
|
|
|
|
/** @param {{ todos: string[] }} */
|
2023-09-08 20:18:58 +02:00
|
|
|
export function todosComponent({ todos= [ "Task A" ] }= {}){
|
2024-01-07 12:33:56 +01:00
|
|
|
const todosO= O(todos.map(t=> O(t)), {
|
2023-11-24 20:41:04 +01:00
|
|
|
add(v){ this.value.push(O(v)); },
|
|
|
|
remove(i){ O.clear(this.value.splice(i, 1)[0]); }
|
2023-09-05 09:25:47 +02:00
|
|
|
});
|
2023-10-14 16:18:11 +02:00
|
|
|
|
2023-09-05 09:25:47 +02:00
|
|
|
const name= "todoName";
|
|
|
|
const onsubmitAdd= on("submit", event=> {
|
2023-09-09 21:15:43 +02:00
|
|
|
const el= event.target.elements[name];
|
2023-09-05 09:25:47 +02:00
|
|
|
event.preventDefault();
|
2024-01-07 12:33:56 +01:00
|
|
|
O.action(todosO, "add", el.value);
|
2023-09-09 21:15:43 +02:00
|
|
|
el.value= "";
|
2023-09-05 09:25:47 +02:00
|
|
|
});
|
|
|
|
const onremove= on("remove", event=>
|
2024-01-07 12:33:56 +01:00
|
|
|
O.action(todosO, "remove", event.detail));
|
2023-09-05 09:25:47 +02:00
|
|
|
|
2024-01-07 12:33:56 +01:00
|
|
|
const ul_todos_version= 1; // ul_todos_v1/ul_todos_v2
|
|
|
|
const ul_todos_v1= el("ul").append(
|
|
|
|
O.el(todosO, ts=> ts
|
2023-09-08 20:18:58 +02:00
|
|
|
.map((textContent, value)=>
|
|
|
|
el(todoComponent, { textContent, value, className }, onremove))
|
2023-09-05 09:25:47 +02:00
|
|
|
));
|
2024-01-08 22:23:06 +01:00
|
|
|
const ul_todos_v2= ts=> el("ul").append(
|
2024-01-07 12:33:56 +01:00
|
|
|
...ts.map((textContent, value)=>
|
|
|
|
el(todoComponent, { textContent, value, className }, onremove))
|
|
|
|
);
|
|
|
|
|
2023-09-05 09:25:47 +02:00
|
|
|
return el("div", { className }).append(
|
|
|
|
el("div").append(
|
|
|
|
el("h2", "Todos:"),
|
|
|
|
el("h3", "List of todos:"),
|
2024-01-07 12:33:56 +01:00
|
|
|
O.el(todosO, ts=> !ts.length
|
2023-09-05 09:25:47 +02:00
|
|
|
? el("p", "No todos yet")
|
2024-01-08 22:23:06 +01:00
|
|
|
: ( !(ul_todos_version-1) ? ul_todos_v1 : ul_todos_v2(ts) )
|
2024-01-07 12:33:56 +01:00
|
|
|
),
|
2023-09-08 20:18:58 +02:00
|
|
|
el("p", "Click to the text to edit it.")
|
2023-09-05 09:25:47 +02:00
|
|
|
),
|
|
|
|
el("form", null, onsubmitAdd).append(
|
|
|
|
el("h3", "Add a todo:"),
|
|
|
|
el("label", "New todo: ").append(
|
|
|
|
el("input", { name, type: "text", required: true }),
|
|
|
|
),
|
|
|
|
el("button", "+")
|
2023-09-08 20:18:58 +02:00
|
|
|
),
|
|
|
|
el("div").append(
|
|
|
|
el("h3", "Output (JSON):"),
|
2024-01-07 12:33:56 +01:00
|
|
|
el("output", O(()=> JSON.stringify(todosO, null, "\t")))
|
2023-09-05 09:25:47 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
/**
|
2024-01-07 12:33:56 +01:00
|
|
|
* @dispatchs {number} remove
|
2023-09-05 09:25:47 +02:00
|
|
|
* */
|
2023-10-10 10:55:00 +02:00
|
|
|
function todoComponent({ textContent, value }){
|
|
|
|
const { host }= scope;
|
2023-09-05 09:25:47 +02:00
|
|
|
const onclick= on("click", event=> {
|
|
|
|
const value= Number(event.target.value);
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2023-11-21 17:19:59 +01:00
|
|
|
dispatchEvent("remove")(host(), value);
|
2023-09-05 09:25:47 +02:00
|
|
|
});
|
2023-11-24 20:41:04 +01:00
|
|
|
const is_editable= O(false);
|
2023-09-08 20:18:58 +02:00
|
|
|
const onedited= on("change", ev=> {
|
|
|
|
textContent(ev.target.value);
|
|
|
|
is_editable(false);
|
|
|
|
});
|
2024-01-13 09:55:46 +01:00
|
|
|
const memo= initMemo(host);
|
2023-09-09 21:15:43 +02:00
|
|
|
return el("li").append(
|
2023-11-24 20:41:04 +01:00
|
|
|
O.el(is_editable, is=> is
|
2024-01-13 09:55:46 +01:00
|
|
|
? memo("view", ()=> el("input", { value: textContent(), type: "text" }, onedited))
|
|
|
|
: memo("edit", ()=> el("span", { textContent, onclick: is_editable.bind(null, true) }))
|
2023-09-08 20:18:58 +02:00
|
|
|
),
|
2023-09-05 09:25:47 +02:00
|
|
|
el("button", { type: "button", value, textContent: "-" }, onclick)
|
|
|
|
);
|
|
|
|
}
|
2024-01-13 09:55:46 +01:00
|
|
|
function initMemo(host){
|
|
|
|
const memos= new Map();
|
|
|
|
host(on.disconnected(()=> memos.clear()));
|
|
|
|
return function useMemo(key, fn){
|
|
|
|
let memo= memos.get(key);
|
|
|
|
if(!memo){
|
|
|
|
memo= fn();
|
|
|
|
memos.set(key, memo);
|
|
|
|
}
|
|
|
|
return memo;
|
|
|
|
}
|
|
|
|
}
|