/
thymeleaf-layout-dialect.min.js
1 lines (1 loc) · 2.15 KB
/
thymeleaf-layout-dialect.min.js
1
!function(){"use strict";function t(t,e,n){return t.getAttribute(`${e}:${n}`)||t.getAttribute(`data-${e}-${n}`)}function e(t,e){let n=t.parentNode;n.insertBefore(e,t),n.removeChild(t)}function n(t={},...e){return e.forEach(e=>{Object.keys(e).forEach(o=>{let r=t[o];let u=e[o];t[o]=r instanceof Object&&u instanceof Object?n(r,u):u})}),t}function o(t){if(t.status>=200&&t.status<300)return t;{let e=new Error(`${t.status} - ${t.statusText}`);throw e.response=t,e}}function r(t,e={}){return fetch(t,n({headers:{Accept:"text/html"}},e)).then(o).then(t=>t.text())}function u(t,e={}){return r(t,e).then(t=>(new DOMParser).parseFromString(t,"text/html"))}class c{process(n,o){let{fragments:fragments}=n;if(fragments){let n=t(o,"layout","fragment"),r=fragments[n];r&&e(o,r)}}}c.PROCESSOR_NAME="fragment";var f="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},a=function(t,e){return e={exports:{}},t(e,e.exports),e.exports}(function(t){!function(e,n){if(t.exports)t.exports=n();else{var o=n();Object.keys(o).forEach(function(t){e[t]=o[t]})}}(f,function(){return{$:function(t,e){return(e||document).querySelector(t)},$$:function(t,e){return Array.prototype.slice.call((e||document).querySelectorAll(t))}}})}),i=a.$$;class s{findFragments(e){let n={};return i("[layout\\:fragment], [data-layout-fragment]",e).map(e=>{let o=t(e,"layout",c.PROCESSOR_NAME);n[o]=e}),n}}class d{process(e,n){return new Promise(function(o,r){let c=t(n,"layout","decorate");c||(console.warn("No layout:decorate or data-layout-decorate attribute found on the <html> element"),o()),u(c.match(/~\{(.+)\}/)[1]).then(function(t){e.fragments=(new s).findFragments(n),document.open("text/html","replace"),document.write(t.firstElementChild.outerHTML),document.close(),document.addEventListener("DOMContentLoaded",o)}).catch(function(t){console.warn(`Unable to retrieve layout at ${t.response.url}`),o()})})}}d.PROCESSOR_NAME="decorate";document.addEventListener("DOMContentLoaded",function(){let t={};(new d).process(t,document.documentElement).then(function(){let e=`[layout\\:${c.PROCESSOR_NAME}], `+`[data-layout-${c.PROCESSOR_NAME}]`;i(e).forEach(e=>{(new c).process(t,e)})})})}();