fwebc.js

Toy framework for learning web components
git clone git://git.finwo.net/lib/fwebc.js
Log | Files | Refs | README

commit 84b9a3a17772b4a018c615f1f57bfe5c9e352ef7
parent 901fc49d1f2c544bba28893133c9b1ac40c78834
Author: finwo <finwo@pm.me>
Date:   Thu,  5 Nov 2020 00:03:51 +0100

Dashes are now replaced by slashes during load for organization

Diffstat:
Mfwebc.js | 2+-
Mfwebc.min.js | 2+-
Mtest/index.html | 4++--
Rtest/partial/my-app.tag -> test/partial/app/core.tag | 0
4 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/fwebc.js b/fwebc.js @@ -149,7 +149,7 @@ // Load a component fwebc.load = name => { - fetch(`${config.base}/${name}.${config.ext}`) + fetch(`${config.base}/${name.replace(/-/g,'/')}.${config.ext}`) .then(res => res.text()) .then(source => { fwebc.register(name, source); 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={},this.root=this.attachShadow({mode:"open"});for(const e of n)e(this);new Function(s).call(this),this.dependencies&&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.root.innerHTML=e(...Object.values(this.state))}catch(e){console.error(e)}}})}),t.load=(n=>{e(`${o.base}/${n}.${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,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={},this.root=this.attachShadow({mode:"open"});for(const e of n)e(this);new Function(s).call(this),this.dependencies&&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.root.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}); diff --git a/test/index.html b/test/index.html @@ -5,7 +5,7 @@ <link rel="stylesheet" href="/assets/style.css"> </head> <body> - <my-app></my-app> + <app-core></app-core> <script src="/assets/fwebc.js"></script> <script> (async () => { @@ -13,7 +13,7 @@ base: '/partial', ext : 'tag', }); - fwebc.load('my-app'); + fwebc.load('app-core'); })(); </script> </body> diff --git a/test/partial/my-app.tag b/test/partial/app/core.tag