2023-09-09 21:15:43 +02:00
|
|
|
import { el } from "../../index.js";
|
|
|
|
import { S } from "../../src/signals.js";
|
2023-09-12 15:26:06 +02:00
|
|
|
const { hasOwnProperty }= Object.prototype;
|
2023-09-09 21:15:43 +02:00
|
|
|
|
2023-09-12 15:26:06 +02:00
|
|
|
const store= attrsPropsToSignals([ "test" ]);
|
2023-09-09 21:15:43 +02:00
|
|
|
/**
|
|
|
|
* Compatible with `npx-wca test/components/webComponent.js`
|
|
|
|
* */
|
2023-09-11 18:31:23 +02:00
|
|
|
export class CustomHTMLTestElement extends HTMLElement{
|
2023-09-09 21:15:43 +02:00
|
|
|
static get observedAttributes(){
|
2023-09-12 15:26:06 +02:00
|
|
|
return [ "name", "pre-name" ];
|
2023-09-09 21:15:43 +02:00
|
|
|
}
|
|
|
|
connectedCallback(){
|
2023-09-12 15:26:06 +02:00
|
|
|
this.attachShadow({ mode: "open" }).append(
|
|
|
|
customElementRender(this, store.toRender(this), this.render)
|
|
|
|
);
|
2023-09-09 21:15:43 +02:00
|
|
|
}
|
|
|
|
|
2023-09-12 15:26:06 +02:00
|
|
|
render({ name, test, preName }, host){
|
2023-09-11 18:31:23 +02:00
|
|
|
host(on.connected(console.log));
|
2023-09-12 15:26:06 +02:00
|
|
|
return el("p").append(
|
|
|
|
el("#text", { textContent: name }),
|
|
|
|
el("#text", { textContent: test }),
|
|
|
|
el("#text", { textContent: preName }),
|
|
|
|
);
|
2023-09-11 18:31:23 +02:00
|
|
|
}
|
2023-09-09 21:15:43 +02:00
|
|
|
}
|
2023-09-11 18:31:23 +02:00
|
|
|
customElementsAssign(
|
|
|
|
CustomHTMLTestElement,
|
|
|
|
reflectObservedAttributes,
|
2023-09-12 15:26:06 +02:00
|
|
|
lifecycleToEvents(false),
|
|
|
|
store.connect
|
2023-09-11 18:31:23 +02:00
|
|
|
);
|
|
|
|
customElements.define("custom-test", CustomHTMLTestElement);
|
|
|
|
|
2023-09-12 15:26:06 +02:00
|
|
|
function customElementRender(_this, attrs, render){
|
2023-09-09 21:15:43 +02:00
|
|
|
const host= (...a)=> a.length ? a[0](_this) : _this;
|
2023-09-12 15:26:06 +02:00
|
|
|
return render(attrs, host);
|
2023-09-09 21:15:43 +02:00
|
|
|
}
|
2023-09-11 18:31:23 +02:00
|
|
|
/** @returns {HTMLElement} */
|
|
|
|
function customElementsAssign(class_base, ...automatize){
|
2023-09-12 15:26:06 +02:00
|
|
|
automatize.forEach(a=> a(class_base));
|
2023-09-11 18:31:23 +02:00
|
|
|
}
|
|
|
|
function reflectObservedAttributes(c){
|
2023-09-12 15:26:06 +02:00
|
|
|
for(const name of c.observedAttributes){
|
|
|
|
const name_camel= name.replace(/([a-z])-([a-z])/g, (_, l, r)=> l+r.toUpperCase());
|
|
|
|
if(hasOwnProperty.call(c.prototype, name_camel))
|
|
|
|
continue;
|
|
|
|
Reflect.defineProperty(c.prototype, name_camel, {
|
2023-09-11 18:31:23 +02:00
|
|
|
get(){ return this.getAttribute(name); },
|
|
|
|
set(value){ this.setAttribute(name, value); }
|
|
|
|
});
|
2023-09-12 15:26:06 +02:00
|
|
|
}
|
2023-09-11 18:31:23 +02:00
|
|
|
}
|
|
|
|
function lifecycleToEvents(is_attrs){
|
|
|
|
return function(c){
|
|
|
|
wrapMethod(c.prototype, "connectedCallback", function(target, thisArg, detail){
|
|
|
|
target.apply(thisArg, detail);
|
|
|
|
thisArg.dispatchEvent(new Event("dde:connected"));
|
|
|
|
});
|
|
|
|
wrapMethod(c.prototype, "disconnectedCallback", function(target, thisArg, detail){
|
|
|
|
target.apply(thisArg, detail);
|
|
|
|
thisArg.dispatchEvent(new Event("dde:disconnected"));
|
|
|
|
});
|
|
|
|
if(is_attrs)
|
|
|
|
wrapMethod(c.prototype, "attributeChangedCallback", function(target, thisArg, detail){
|
|
|
|
thisArg.dispatchEvent(new CustomEvent("dde:attribute", { detail }));
|
|
|
|
target.apply(thisArg, detail);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
function attrsPropsToSignals(props= []){
|
2023-09-12 15:26:06 +02:00
|
|
|
const store_attrs= new WeakMap();
|
|
|
|
const store_props= new WeakMap();
|
|
|
|
return {
|
|
|
|
toRender(target){
|
|
|
|
const out= {};
|
|
|
|
const sattrs= get(store_attrs, target);
|
|
|
|
target.constructor.observedAttributes.forEach(function(name){
|
|
|
|
const name_camel= name.replace(/([a-z])-([a-z])/g, (_, l, r)=> l+r.toUpperCase());
|
|
|
|
if(!hasOwnProperty.call(sattrs, name)) sattrs[name]= S(undefined);
|
|
|
|
out[name_camel]= sattrs[name];
|
2023-09-11 18:31:23 +02:00
|
|
|
});
|
2023-09-12 15:26:06 +02:00
|
|
|
const sprops= get(store_props, target);
|
|
|
|
props.forEach(p=> !hasOwnProperty.call(sprops, p) && (sprops[p]= S(undefined)));
|
|
|
|
return Object.assign(out, sprops);
|
|
|
|
},
|
|
|
|
connect(c){
|
|
|
|
wrapMethod(c.prototype, "attributeChangedCallback", function(target, thisArg, detail){
|
|
|
|
const [ name, _, value ]= detail;
|
|
|
|
const s= get(store_attrs, thisArg);
|
|
|
|
if(s[name]) s[name](value);
|
|
|
|
else s[name]= S(value);
|
|
|
|
|
|
|
|
target.apply(thisArg, detail);
|
|
|
|
});
|
|
|
|
for(const name of props){
|
|
|
|
Reflect.defineProperty(c.prototype, name, {
|
|
|
|
get(){
|
|
|
|
const s= get(store_props, this);
|
|
|
|
if(s[name]) return s[name]();
|
|
|
|
},
|
|
|
|
set(value){
|
|
|
|
const s= get(store_props, this);
|
|
|
|
if(s[name]) s[name](value);
|
|
|
|
else s[name]= S(value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-09-11 18:31:23 +02:00
|
|
|
}
|
|
|
|
};
|
2023-09-12 15:26:06 +02:00
|
|
|
function get(store, t){
|
|
|
|
if(store.has(t)) return store.get(t);
|
|
|
|
const s= {};
|
|
|
|
store.set(t, s);
|
|
|
|
return s;
|
|
|
|
}
|
2023-09-11 18:31:23 +02:00
|
|
|
}
|
|
|
|
function wrapMethod(obj, method, apply){
|
|
|
|
obj[method]= new Proxy(obj[method] || (()=> {}), { apply });
|
|
|
|
}
|