UNPKG

@sheet-api/html

Version:
1 lines 2.23 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.VideomatikEditor=e():t.VideomatikEditor=e()}(this,(()=>(()=>{"use strict";var t={};(t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})})(t);const e=(t,e,r)=>{[...t.querySelectorAll(e)].forEach(r)},r=t=>{const r=t.getAttribute("data-sheet-api-url");t.addEventListener("submit",(async a=>{a.preventDefault(),t.setAttribute("action",r),t.setAttribute("method","POST");const i=t.getAttribute("data-loading-msg")||"Loading...",n=t.getAttribute("data-success-msg")||"Your submission was received!",o=t.getAttribute("data-error-msg")||"Your submission has failed, please try again later",s=[...t.querySelectorAll("input[name]")],u=new FormData;s.forEach((t=>{u.append(t.getAttribute("name"),t.value)}));try{if(e(t,"button",(t=>{"submit"===(t.getAttribute("type")||"submit")&&(t.setAttribute("disabled","true"),t.setAttribute("data-previous-inner-text",t.innerText),t.innerText=i)})),(await fetch(r,{method:"POST",body:u})).status>=400)return void e(t,".sheet-api-error-container",(t=>{t.innerText=o}));e(t,".sheet-api-success-container",(t=>{t.innerText=n})),s.forEach((t=>{t.value=""}))}catch(r){throw e(t,".sheet-api-error-container",(t=>{t.innerText=o})),r}finally{e(t,"button[data-previous-inner-text]",(t=>{t.innerText=t.getAttribute("data-previous-inner-text"),t.removeAttribute("disabled")}))}})),t.removeAttribute("data-sheet-api-url")},a=t=>{const e=t.getAttribute("data-sheet-api-url");t.removeAttribute("data-sheet-api-url");const r=t.innerHTML;t.innerHTML="";const a=async()=>{const a=await(async t=>await fetch(t).then((t=>t.json())))(e),i=a.map((t=>(Object.entries(t).forEach((([e,r])=>{t[e.trim()]=r})),r.replace(/{{([^{}]*)}}/g,((e,r)=>t[r.trim()]))))).join("");t.innerHTML=i};a();const i=parseFloat(t.getAttribute("data-refresh-time"))||0;i&&setInterval(a,1e3*i)};return setInterval((()=>{[...document.querySelectorAll("form[data-sheet-api-url]")].forEach(r),[...document.querySelectorAll("[data-sheet-api-url]")].forEach(a)}),200),t})()));