-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrag.min.js
More file actions
1 lines (1 loc) · 3.1 KB
/
frag.min.js
File metadata and controls
1 lines (1 loc) · 3.1 KB
1
export const frag=function(){let e={};const t=[];function n(e,t){return t.split(".").reduce(((e,t)=>e&&e[t]),e)}function r(e,...t){let n;if("string"==typeof e||Array.isArray(e)){const r=e.map(((e,n)=>e+(t[n]??""))).join(""),s=document.createElement("template");s.innerHTML=r.trim(),n=s.content.cloneNode(!0)}else{const r=e,s=t[0]||{},o=t.slice(1);n=document.createElement(r);for(const[e,t]of Object.entries(s))e.startsWith("on")&&"function"==typeof t?n.addEventListener(e.slice(2).toLowerCase(),t):"style"===e&&"object"==typeof t?Object.assign(n.style,t):n[e]=t;o.flat().forEach((e=>{e instanceof Node?n.appendChild(e):n.appendChild(document.createTextNode(e))}))}return n.placeOn=function(e,t=-1){const n=document.querySelectorAll(e);return n.length?(n.forEach((e=>{const n=Array.from(e.children),r=n.length;let s;s=t<0?Math.max(r+t,0):Math.min(t,r),n[s]?e.insertBefore(this.cloneNode(!0),n[s]):e.appendChild(this.cloneNode(!0))})),this):(console.warn(`Nenhum elemento encontrado com o seletor "${e}".`),this)},n}return r.extend=function(e,t){return Object.assign(r.prototype,t),this},r.get=function(e){const t=document.querySelectorAll(e);return t.length?{elements:t,add(e,t){return this.elements.forEach((n=>{"class"===e||"className"===e?n.classList.add(t):n[e]instanceof DOMTokenList?n[e].add(t):e.startsWith("data-")?n.setAttribute(e,t):n[e]=t})),this},set(e,t){return this.elements.forEach((n=>{"html"===e?n.innerHTML=t:"text"===e?n.textContent=t:n[e]=t})),this},on(e,t){return this.elements.forEach((n=>n.addEventListener(e,t))),this},off(e,t){return this.elements.forEach((n=>n.removeEventListener(e,t))),this},placeOn(e,t=-1){return document.querySelectorAll(e).forEach((e=>{const n=Array.from(e.children),r=n.length;let s;s=t<0?Math.max(r+t,0):Math.min(t,r),this.elements.forEach((t=>{const r=t.cloneNode(!0);n[s]?e.insertBefore(r,n[s]):e.appendChild(r)}))})),this},then(e){return e?.(),this},catch(){return this}}:(console.warn(`Nenhum elemento encontrado com o seletor "${e}".`),{then:()=>this,catch:()=>this})},r.createStore=function(n){return e=JSON.parse(JSON.stringify(n)),new Proxy(e,{set(e,n,r){var s;return e[n]=r,s=n,t.forEach((e=>e(s))),!0}})},r.watch=function(e){t.push(e)},r.http={get(e,t){fetch(e).then((e=>e.json())).then((e=>t(null,e))).catch((e=>t(e)))},post(e,t,n){fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)}).then((e=>e.json())).then((e=>n(null,e))).catch((e=>n(e)))}},r.form={serialize(e){const t=new FormData(e),n={};return t.forEach(((e,t)=>{n[t]=e})),n}},r.cache={templates:{},set(e,t){this.templates[e]=t},get(e,t){const r=this.templates[e];return r?function(e,t){return e.replace(/{{\s*(.*?)\s*}}/g,((r,s)=>{if(s.startsWith("each ")){const r=s.match(/each (\w+)(?: as (\w+))?/);if(!r)return"";const o=r[1],c=r[2]||"item",a=n(t,o);return Array.isArray(a)?a.map((t=>e.split(`{{ each ${o} as ${c} }}`)[1].split("{{ end }}")[0].replace(new RegExp(`{{\\s*${c}\\.`),((e,t)=>`{{ ${t}`)).replace(new RegExp(`{{\\s*${c}\\b`,"g"),((e,t)=>"{{ ")).replace(/\.\b/g,"").replace(/(\w+)\./g,"$1_").replace(/_/g,"."))).join(""):""}return s.startsWith("this.")?t[s.replace("this.","")]:n(t,s)}))}(r,t):""}},r}();