2023-11-08 18:53:22 +01:00
|
|
|
import { el, on, scope } from "../../index.js";
|
2023-11-24 20:41:04 +01:00
|
|
|
import { O } from "../../observables.js";
|
2023-09-09 21:15:43 +02:00
|
|
|
|
2023-11-08 18:53:22 +01:00
|
|
|
/**
|
2023-11-22 17:39:46 +01:00
|
|
|
* Compatible with `npx -y web-component-analyzer examples/components/webComponent.js`
|
|
|
|
* @element custom-test
|
2023-09-09 21:15:43 +02:00
|
|
|
* */
|
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-11-22 17:39:46 +01:00
|
|
|
if(!this.hasAttribute("pre-name")) this.setAttribute("pre-name", "default");
|
2023-09-12 15:26:06 +02:00
|
|
|
this.attachShadow({ mode: "open" }).append(
|
2023-11-22 21:29:40 +01:00
|
|
|
customElementRender(this, this.render)
|
2023-09-12 15:26:06 +02:00
|
|
|
);
|
2023-09-09 21:15:43 +02:00
|
|
|
}
|
|
|
|
|
2023-11-22 21:29:40 +01:00
|
|
|
render({ test }){
|
2023-10-10 10:55:00 +02:00
|
|
|
console.log(scope.state);
|
2023-11-22 21:29:40 +01:00
|
|
|
scope.host(
|
2023-11-17 14:48:11 +01:00
|
|
|
on.connected(()=> console.log(CustomHTMLTestElement)),
|
|
|
|
on.attributeChanged(e=> console.log(e)),
|
|
|
|
on.disconnected(()=> console.log(CustomHTMLTestElement))
|
|
|
|
);
|
2023-11-24 20:41:04 +01:00
|
|
|
const name= O.attribute("name");
|
|
|
|
const preName= O.attribute("pre-name");
|
2023-10-13 16:37:04 +02:00
|
|
|
|
|
|
|
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", 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-22 17:39:46 +01:00
|
|
|
test= "A";
|
2023-11-08 18:53:22 +01:00
|
|
|
|
|
|
|
get name(){ return this.getAttribute("name"); }
|
|
|
|
set name(value){ this.setAttribute("name", value); }
|
2023-11-22 17:39:46 +01:00
|
|
|
/** @attr pre-name */
|
2023-11-08 18:53:22 +01:00
|
|
|
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-11-22 21:29:40 +01:00
|
|
|
function customElementRender(_this, render, props= _this){
|
2023-11-22 17:39:46 +01:00
|
|
|
console.log(_this.shadowRoot, _this.childList);
|
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-11-22 17:39:46 +01:00
|
|
|
if(typeof props==="function") props= props(_this);
|
|
|
|
const out= render(props);
|
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-11-22 17:39:46 +01:00
|
|
|
return class_declaration;
|
2023-09-11 18:31:23 +02:00
|
|
|
}
|
|
|
|
function wrapMethod(obj, method, apply){
|
|
|
|
obj[method]= new Proxy(obj[method] || (()=> {}), { apply });
|
|
|
|
}
|