commit 356c9b6b1c4852ecc2a8c2d19bbb03a9f1aefe3e
parent d75a1b79302a37a7f46b0e0c850f91415049edfc
Author: finwo <finwo@pm.me>
Date: Wed, 25 Nov 2020 21:41:34 +0100
Add easy event handling in tags & emit 'update' on state update
Diffstat:
2 files changed, 13 insertions(+), 3 deletions(-)
diff --git a/fwebc.js b/fwebc.js
@@ -132,14 +132,24 @@
}
// Start observing state & initial rendering
- this.state = util.observable(this.state, this.render.bind(this));
+ this.state = util.observable(this.state, () => this.emit('update'));
+ this.on('update', this.render.bind(this));
this.render();
}
render() {
const fn = new Function(...Object.keys(this.state), 'return `'+template+'`;');
try { this.innerHTML = fn(...Object.values(this.state)); } catch(e) { console.error(e); }
- };
+ }
+
+ emit(event, data = {}) {
+ const ev = new CustomEvent(event, data);
+ this.dispatchEvent(ev);
+ }
+
+ on(event, handler) {
+ this.addEventListener(event, handler);
+ }
});
};
diff --git a/fwebc.min.js b/fwebc.min.js
@@ -1 +1 @@
-(e=>{"object"==typeof module&&"exports"in module?module.exports=e({fetch:require("node-fetch")}):"object"==typeof window&&(window.fwebc=e({fetch:window.fetch}))})(({fetch:e})=>{const t={},n=[],o={ext:"tag",base:"/partial"},r=t.util={isObject:e=>null!==e&&("object"==typeof e&&!Array.isArray(e)),unescape(e){const t=document.createElement("textarea");return t.innerHTML=e,0===t.childNodes.length?"":t.childNodes[0].nodeValue},observable(e,t,n=""){if(Object(e)!==e)throw new Error(`Object is not an object, got: ${e}`);if("function"!=typeof t)throw new Error(`Callback is not a function, got: ${t}`);for(const o of Object.keys(e))Object(e[o])===e[o]&&(e[o]=r.observable(e[o],t,`${n}${o}.`));return new Proxy(e,{set(e,o,c,s){if(e[o]===c)return;let i=o in e?"update":"add";const l={name:o,type:i,object:e};return"update"==i&&(l.oldValue=e[o]),e[o]=Object(c)===c?r.observable(c,t,`${n}${o}.`):c,t([l]),!0},deleteProperty(e,n,o){if(!(n in e))return;const r={name:n,type:"delete",object:e,oldValue:e[n]};return delete e[n],t([r]),!0}})}};return t.cfg=(e=>{Object.assign(o,e)}),t.install=(e=>{"function"==typeof e&&n.push(e)}),t.uninstall=(e=>{const t=n.indexOf(e);~t&&n.splice(t,1)}),t.register=((e,o)=>{if(window.customElements.get(e))return;let c=document.createElement("template");for(c.innerHTML=o;1==c.content.children.length&&c.content.firstChild instanceof HTMLTemplateElement;)c=c.content.firstChild;if(c.content.firstChild instanceof HTMLTemplateElement){const e=c.content.firstChild;c.content.prepend(...e.content.children),c.content.removeChild(e)}let s="";for(const e of[...c.content.children])e instanceof HTMLScriptElement&&(e.getAttribute("src")||(s+=e.innerHTML,c.content.removeChild(e)));c=r.unescape(c.innerHTML),window.customElements.define(e,class extends HTMLElement{constructor(){super(),this.state={};for(const e of n)e(this);new Function(s).call(this),this.dependencies&&this.dependencies.forEach(t.load),this.state=r.observable(this.state,this.render.bind(this)),this.render()}render(){const e=new Function(...Object.keys(this.state),"return `"+c+"`;");try{this.innerHTML=e(...Object.values(this.state))}catch(e){console.error(e)}}})}),t.load=(n=>{e(`${o.base}/${n.replace(/-/g,"/")}.${o.ext}`).then(e=>e.text()).then(e=>{t.register(n,e)})}),t});
+(e=>{"object"==typeof module&&"exports"in module?module.exports=e({fetch:require("node-fetch")}):"object"==typeof window&&(window.fwebc=e({fetch:window.fetch}))})(({fetch:e})=>{const t={},n=[],o={ext:"tag",base:"/partial"},r=t.util={isObject:e=>null!==e&&("object"==typeof e&&!Array.isArray(e)),unescape(e){const t=document.createElement("textarea");return t.innerHTML=e,0===t.childNodes.length?"":t.childNodes[0].nodeValue},observable(e,t,n=""){if(Object(e)!==e)throw new Error(`Object is not an object, got: ${e}`);if("function"!=typeof t)throw new Error(`Callback is not a function, got: ${t}`);for(const o of Object.keys(e))Object(e[o])===e[o]&&(e[o]=r.observable(e[o],t,`${n}${o}.`));return new Proxy(e,{set(e,o,s,i){if(e[o]===s)return;let c=o in e?"update":"add";const l={name:o,type:c,object:e};return"update"==c&&(l.oldValue=e[o]),e[o]=Object(s)===s?r.observable(s,t,`${n}${o}.`):s,t([l]),!0},deleteProperty(e,n,o){if(!(n in e))return;const r={name:n,type:"delete",object:e,oldValue:e[n]};return delete e[n],t([r]),!0}})}};return t.cfg=(e=>{Object.assign(o,e)}),t.install=(e=>{"function"==typeof e&&n.push(e)}),t.uninstall=(e=>{const t=n.indexOf(e);~t&&n.splice(t,1)}),t.register=((e,o)=>{if(window.customElements.get(e))return;let s=document.createElement("template");for(s.innerHTML=o;1==s.content.children.length&&s.content.firstChild instanceof HTMLTemplateElement;)s=s.content.firstChild;if(s.content.firstChild instanceof HTMLTemplateElement){const e=s.content.firstChild;s.content.prepend(...e.content.children),s.content.removeChild(e)}let i="";for(const e of[...s.content.children])e instanceof HTMLScriptElement&&(e.getAttribute("src")||(i+=e.innerHTML,s.content.removeChild(e)));s=r.unescape(s.innerHTML),window.customElements.define(e,class extends HTMLElement{constructor(){super(),this.state={};for(const e of n)e(this);new Function(i).call(this),this.dependencies&&this.dependencies.forEach(t.load),this.state=r.observable(this.state,()=>this.emit("update")),this.on("update",this.render.bind(this)),this.render()}render(){const e=new Function(...Object.keys(this.state),"return `"+s+"`;");try{this.innerHTML=e(...Object.values(this.state))}catch(e){console.error(e)}}emit(e,t={}){const n=new CustomEvent(e,t);this.dispatchEvent(n)}on(e,t){this.addEventListener(e,t)}})}),t.load=(n=>{e(`${o.base}/${n.replace(/-/g,"/")}.${o.ext}`).then(e=>e.text()).then(e=>{t.register(n,e)})}),t});