diff --git a/README.md b/README.md index 187ba7c..e649c11 100644 --- a/README.md +++ b/README.md @@ -28,4 +28,24 @@ data.json {"data1": "data11", "data2":"data12"}, {"data2": "data21", "data2":"data22"} ] +``` +### Source can be also an embedded json + +```html + + ``` \ No newline at end of file diff --git a/esm.js b/esm.js index d269276..167e669 100644 --- a/esm.js +++ b/esm.js @@ -203,9 +203,22 @@ async function init(that) { that.data = data; } async function getSourceContent(source) { - try { - return await fetch$1(source).json() - } catch (e) { - return false + const el = document.getElementById(source); + if (el) { + try { + const data = JSON.parse(el.textContent); + console.log('data', data); + return data + } catch (e) { + return false + } + + } else { + try { + return await fetch$1(source).json() + } catch (e) { + return false + } } + } diff --git a/esm.min.js b/esm.min.js index eac7268..449ec42 100644 --- a/esm.min.js +++ b/esm.min.js @@ -1 +1 @@ -const t=("dry-"+Math.random()).replace(".","");let e=1;const n=()=>t+e++,s="#inner#",o=/\{(?t-\w[\-\w]+)\}/;function c(t){const e=document.getElementById(t).content.cloneNode(!0),n=function(t){const e=t.querySelectorAll("*"),n=new Map;for(const t of e){const e=t.attributes;for(let s=0,c=e.length;s"function"==typeof t,d={get:(t,e)=>a.hasOwnProperty(e)?((t,e,{get:n,value:s})=>n||!f(s)?t.then((t=>t[e])):(...n)=>t.then((t=>t[e](...n))))(t,e,a[e]):((t,e)=>f(e)?e.bind(t):e)(t,t[e])};customElements.define("list-m",class extends HTMLElement{constructor(){super()}connectedCallback(){this.style.display="block",async function(t){const e=t.getAttribute("source");if(!e)return;const n=await async function(t){try{return await((t,...e)=>new Proxy(fetch(t,...e),d))(t).json()}catch(t){return!1}}(e);if(!n)return;t.data=n}(this)}set data(t){const e=this.getAttribute("template");try{r(e)}catch(t){}this.innerHTML=this.createList(e,t)}createList(t,e){return e.map((e=>`\n <${t}\n ${function(t){return Object.entries(t).map((([t,e])=>`t-${t}="${e}"`))}(e).join("\n")}\n >\n `)).join("\n")}}); +const t=("dry-"+Math.random()).replace(".","");let e=1;const n=()=>t+e++,s="#inner#",o=/\{(?t-\w[\-\w]+)\}/;function c(t){const e=document.getElementById(t).content.cloneNode(!0),n=function(t){const e=t.querySelectorAll("*"),n=new Map;for(const t of e){const e=t.attributes;for(let s=0,c=e.length;s"function"==typeof t,d={get:(t,e)=>a.hasOwnProperty(e)?((t,e,{get:n,value:s})=>n||!f(s)?t.then((t=>t[e])):(...n)=>t.then((t=>t[e](...n))))(t,e,a[e]):((t,e)=>f(e)?e.bind(t):e)(t,t[e])};customElements.define("list-m",class extends HTMLElement{constructor(){super()}connectedCallback(){this.style.display="block",async function(t){const e=t.getAttribute("source");if(!e)return;const n=await async function(t){const e=document.getElementById(t);if(e)try{const t=JSON.parse(e.textContent);return console.log("data",t),t}catch(t){return!1}else try{return await((t,...e)=>new Proxy(fetch(t,...e),d))(t).json()}catch(t){return!1}}(e);if(!n)return;t.data=n}(this)}set data(t){const e=this.getAttribute("template");try{r(e)}catch(t){}this.innerHTML=this.createList(e,t)}createList(t,e){return e.map((e=>`\n <${t}\n ${function(t){return Object.entries(t).map((([t,e])=>`t-${t}="${e}"`))}(e).join("\n")}\n >\n `)).join("\n")}}); diff --git a/src/index.js b/src/index.js index 53fdd1e..1f9af61 100644 --- a/src/index.js +++ b/src/index.js @@ -40,9 +40,22 @@ async function init(that) { that.data = data } async function getSourceContent(source) { - try { - return await fetch(source).json() - } catch (e) { - return false + const el = document.getElementById(source) + if (el) { + try { + const data = JSON.parse(el.textContent) + console.log('data', data) + return data + } catch (e) { + return false + } + + } else { + try { + return await fetch(source).json() + } catch (e) { + return false + } } + } \ No newline at end of file diff --git a/test/index.html b/test/index.html index 2fd1932..76d3754 100644 --- a/test/index.html +++ b/test/index.html @@ -15,6 +15,18 @@ +

list-m Tests

@@ -35,5 +47,9 @@

list-m Tests

template="info-m" source="./data.txt" > + \ No newline at end of file diff --git a/test/index.js b/test/index.js index 1b2c0da..a2c2b5b 100644 --- a/test/index.js +++ b/test/index.js @@ -4,12 +4,14 @@ const allLists = [...document.getElementsByTagName('list-m')] const descriptions = [ 'test with json file', 'test with non existing file', - 'test with text file' + 'test with text file', + 'test with embedded json' ] const allExpected = [ 'fname1,1,fname2,2,fname3,3', '', - '' + '', + 'ename1,1,ename2,2' ] setTimeout(run, 1000) function run() {