2023-10-10 10:55:00 +02:00
|
|
|
import { el, scope } from "../../index.js";
|
2023-09-19 09:30:21 +02:00
|
|
|
import { S } from "../../signals.js";
|
2023-09-12 15:26:06 +02:00
|
|
|
const { hasOwnProperty }= Object.prototype;
|
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(
|
2023-10-13 16:37:04 +02:00
|
|
|
customElementRender(this, this.render)
|
2023-09-12 15:26:06 +02:00
|
|
|
);
|
2023-09-09 21:15:43 +02:00
|
|
|
}
|
|
|
|
|
2023-10-13 16:37:04 +02:00
|
|
|
render({ test }){
|
2023-10-10 10:55:00 +02:00
|
|
|
console.log(scope.state);
|
|
|
|
scope.host(on.connected(()=> console.log(CustomHTMLTestElement)));
|
2023-10-13 16:37:04 +02:00
|
|
|
scope.host(on.attributeChanged(e=> console.log(e)));
|
2023-10-10 10:55:00 +02:00
|
|
|
scope.host(on.disconnected(()=> console.log(CustomHTMLTestElement)));
|
2023-10-13 16:37:04 +02:00
|
|
|
|
|
|
|
const name= S.attribute("name");
|
|
|
|
const preName= S.attribute("pre-name");
|
|
|
|
console.log({ name, test, preName});
|
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-13 13:02:17 +02:00
|
|
|
// https://gist.github.com/WebReflection/ec9f6687842aa385477c4afca625bbf4
|
2023-09-11 18:31:23 +02:00
|
|
|
customElementsAssign(
|
|
|
|
CustomHTMLTestElement,
|
|
|
|
reflectObservedAttributes,
|
2023-10-13 16:37:04 +02:00
|
|
|
lifecycleToEvents,
|
2023-09-11 18:31:23 +02:00
|
|
|
);
|
|
|
|
customElements.define("custom-test", CustomHTMLTestElement);
|
|
|
|
|
2023-10-13 16:37:04 +02:00
|
|
|
function customElementRender(_this, render){
|
2023-10-10 10:55:00 +02:00
|
|
|
scope.push({ scope: _this, host: (...a)=> a.length ? a[0](_this) : _this });
|
2023-10-13 16:37:04 +02:00
|
|
|
const out= render(_this);
|
2023-10-10 10:55:00 +02:00
|
|
|
scope.pop();
|
|
|
|
return out;
|
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
|
|
|
}
|
2023-10-13 16:37:04 +02:00
|
|
|
function lifecycleToEvents(class_declaration){
|
|
|
|
for (const name of [ "connected", "disconnected" ])
|
|
|
|
wrapMethod(class_declaration.prototype, name+"Callback", function(target, thisArg, detail){
|
2023-09-11 18:31:23 +02:00
|
|
|
target.apply(thisArg, detail);
|
2023-10-13 16:37:04 +02:00
|
|
|
thisArg.dispatchEvent(new Event("dde:"+name));
|
2023-09-11 18:31:23 +02:00
|
|
|
});
|
2023-10-13 16:37:04 +02:00
|
|
|
const name= "attributeChanged";
|
|
|
|
wrapMethod(class_declaration.prototype, name+"Callback", function(target, thisArg, detail){
|
|
|
|
const [ attribute, , value ]= detail;
|
|
|
|
thisArg.dispatchEvent(new CustomEvent("dde:"+name, {
|
|
|
|
detail: [ attribute, value ]
|
|
|
|
}));
|
|
|
|
target.apply(thisArg, detail);
|
|
|
|
});
|
2023-09-11 18:31:23 +02:00
|
|
|
}
|
|
|
|
function wrapMethod(obj, method, apply){
|
|
|
|
obj[method]= new Proxy(obj[method] || (()=> {}), { apply });
|
|
|
|
}
|