1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2024-11-24 09:29:37 +01:00

🔨 O.observedAttributes, observables

This commit is contained in:
Jan Andrle 2024-01-08 22:23:06 +01:00
parent 5e7f7558b5
commit 91f8c96eac
Signed by: jaandrle
GPG Key ID: B3A25AED155AFFAB
7 changed files with 43 additions and 44 deletions

File diff suppressed because one or more lines are too long

2
dist/dde.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

2
dist/esm.js vendored

File diff suppressed because one or more lines are too long

View File

@ -37,7 +37,7 @@ export function todosComponent({ todos= [ "Task A" ] }= {}){
.map((textContent, value)=> .map((textContent, value)=>
el(todoComponent, { textContent, value, className }, onremove)) el(todoComponent, { textContent, value, className }, onremove))
)); ));
const ul_todos_v2= (ts)=> el("ul").append( const ul_todos_v2= ts=> el("ul").append(
...ts.map((textContent, value)=> ...ts.map((textContent, value)=>
el(todoComponent, { textContent, value, className }, onremove)) el(todoComponent, { textContent, value, className }, onremove))
); );
@ -48,7 +48,7 @@ export function todosComponent({ todos= [ "Task A" ] }= {}){
el("h3", "List of todos:"), el("h3", "List of todos:"),
O.el(todosO, ts=> !ts.length O.el(todosO, ts=> !ts.length
? el("p", "No todos yet") ? el("p", "No todos yet")
: ( (ul_todos_version-1) ? ul_todos_v1 : ul_todos_v2(ts) ) : ( !(ul_todos_version-1) ? ul_todos_v1 : ul_todos_v2(ts) )
), ),
el("p", "Click to the text to edit it.") el("p", "Click to the text to edit it.")
), ),

View File

@ -114,7 +114,7 @@ createElement.mark= function(attrs, is_open= false){
attrs= Object.entries(attrs).map(([ n, v ])=> n+`="${v}"`).join(" "); attrs= Object.entries(attrs).map(([ n, v ])=> n+`="${v}"`).join(" ");
const end= is_open ? "" : "/"; const end= is_open ? "" : "/";
const out= env.D.createComment(`<dde:mark ${attrs}${env.ssr}${end}>`); const out= env.D.createComment(`<dde:mark ${attrs}${env.ssr}${end}>`);
if(!is_open) out.end= env.D.createComment("</dde:mark>"); if(is_open) out.end= env.D.createComment("</dde:mark>");
return out; return out;
}; };
export { createElement as el }; export { createElement as el };

View File

@ -90,7 +90,7 @@ import { enviroment as env } from "./dom-common.js";
import { el } from "./dom.js"; import { el } from "./dom.js";
import { scope } from "./dom.js"; import { scope } from "./dom.js";
observable.el= function(o, map){ observable.el= function(o, map){
const mark_start= el.mark({ type: "reactive" }, false); const mark_start= el.mark({ type: "reactive" }, true);
const mark_end= mark_start.end; const mark_end= mark_start.end;
const out= env.D.createDocumentFragment(); const out= env.D.createDocumentFragment();
out.append(mark_start, mark_end); out.append(mark_start, mark_end);
@ -115,35 +115,34 @@ observable.el= function(o, map){
}; };
import { on } from "./events.js"; import { on } from "./events.js";
import { observedAttributes } from "./helpers.js"; import { observedAttributes } from "./helpers.js";
function observedAttribute(instance, name){ const observedAttributeActions= {
const out= (...args)=> !args.length _set(value){ this.value= value; },
};
function observedAttribute(store){
return function(instance, name){
const varO= (...args)=> !args.length
? instance.getAttribute(name) ? instance.getAttribute(name)
: instance.setAttribute(name, ...args); : instance.setAttribute(name, ...args);
out.attribute= name; const out= toObservable(varO, varO(), observedAttributeActions);
store[name]= out;
return out; return out;
};
} }
const key_attributes= "__dde_attributes"; const key_attributes= "__dde_attributes";
observable.observedAttributes= function(element){ observable.observedAttributes= function(element){
const attrs= observedAttributes(element, observedAttribute);
const store= element[key_attributes]= {}; const store= element[key_attributes]= {};
const actions= { const attrs= observedAttributes(element, observedAttribute(store));
_set(value){ this.value= value; },
};
Object.keys(attrs).forEach(name=> {
const attr= attrs[name]= toObservable(attrs[name], attrs[name](), actions);
store[attr.attribute]= attr;
});
on.attributeChanged(function attributeChangeToObservable({ detail }){ on.attributeChanged(function attributeChangeToObservable({ detail }){
/*! This maps attributes to observables (`O.observedAttributes`). /*! This maps attributes to observables (`O.observedAttributes`).
* Investigate `__dde_attributes` key of the element.*/ * Investigate `__dde_attributes` key of the element.*/
const [ name, value ]= detail; const [ name, value ]= detail;
const curr= element[key_attributes][name]; const curr= this[key_attributes][name];
if(curr) return observable.action(curr, "_set", value); if(curr) return observable.action(curr, "_set", value);
})(element); })(element);
on.disconnected(function(){ on.disconnected(function(){
/*! This removes all observables mapped to attributes (`O.observedAttributes`). /*! This removes all observables mapped to attributes (`O.observedAttributes`).
* Investigate `__dde_attributes` key of the element.*/ * Investigate `__dde_attributes` key of the element.*/
observable.clear(...Object.values(element[key_attributes])); observable.clear(...Object.values(this[key_attributes]));
})(element); })(element);
return attrs; return attrs;
}; };
@ -179,10 +178,10 @@ function removeObservablesFromElements(o, listener, ...notes){
} }
function create(is_readonly, value, actions){ function create(is_readonly, value, actions){
const o= is_readonly const varO= is_readonly
? ()=> read(o) ? ()=> read(varO)
: (...value)=> value.length ? write(o, ...value) : read(o); : (...value)=> value.length ? write(varO, ...value) : read(varO);
return toObservable(o, value, actions); return toObservable(varO, value, actions);
} }
const protoSigal= Object.assign(Object.create(null), { const protoSigal= Object.assign(Object.create(null), {
stopPropagation(){ stopPropagation(){