2023-11-08 18:53:22 +01:00
|
|
|
import { el, on, 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
|
|
|
|
2023-11-08 18:53:22 +01:00
|
|
|
/**
|
|
|
|
* @typedef CustomHTMLTestElementInterface
|
|
|
|
* @type {object}
|
|
|
|
* @property {string} name
|
|
|
|
* */
|
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-11-08 18:53:22 +01:00
|
|
|
static tagName= "custom-test";
|
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);
|
2023-11-17 14:48:11 +01:00
|
|
|
scope.host(
|
|
|
|
on.connected(()=> console.log(CustomHTMLTestElement)),
|
|
|
|
on.attributeChanged(e=> console.log(e)),
|
|
|
|
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(
|
2023-11-17 14:48:11 +01:00
|
|
|
el("#text", name),
|
|
|
|
el("#text", test),
|
|
|
|
el("#text", preName),
|
2023-11-08 18:53:22 +01:00
|
|
|
el("button", { type: "button", textContent: "pre-name", onclick: ()=> preName("Ahoj") })
|
2023-09-12 15:26:06 +02:00
|
|
|
);
|
2023-09-11 18:31:23 +02:00
|
|
|
}
|
2023-11-08 18:53:22 +01:00
|
|
|
|
|
|
|
get name(){ return this.getAttribute("name"); }
|
|
|
|
set name(value){ this.setAttribute("name", value); }
|
|
|
|
get preName(){ return this.getAttribute("pre-name"); }
|
|
|
|
set preName(value){ this.setAttribute("pre-name", value); }
|
2023-09-09 21:15:43 +02:00
|
|
|
}
|
2023-09-13 13:02:17 +02:00
|
|
|
// https://gist.github.com/WebReflection/ec9f6687842aa385477c4afca625bbf4
|
2023-11-08 18:53:22 +01:00
|
|
|
lifecycleToEvents(CustomHTMLTestElement)
|
|
|
|
customElements.define(CustomHTMLTestElement.tagName, CustomHTMLTestElement);
|
2023-09-11 18:31:23 +02:00
|
|
|
|
2023-10-13 16:37:04 +02:00
|
|
|
function customElementRender(_this, render){
|
2023-11-17 14:48:11 +01:00
|
|
|
scope.push({ scope: _this, host: (...c)=> c.length ? c.forEach(c=> c(_this)) : _this, custom_element: _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-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-10-13 16:51:43 +02:00
|
|
|
class_declaration.prototype.__dde_lifecycleToEvents= true;
|
2023-09-11 18:31:23 +02:00
|
|
|
}
|
|
|
|
function wrapMethod(obj, method, apply){
|
|
|
|
obj[method]= new Proxy(obj[method] || (()=> {}), { apply });
|
|
|
|
}
|