@fancyapps/ui
Version:
Robust JavaScript UI Component Library
2 lines • 5.17 kB
JavaScript
/*! License details at fancyapps.com/license */
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.window=e.window||{}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let t=e=>typeof e==`object`&&!!e&&e.constructor===Object&&Object.prototype.toString.call(e)===`[object Object]`,n=e=>typeof e==`string`,r=(e,...n)=>{let i=n.length;for(let a=0;a<i;a++){let i=n[a]||{};Object.entries(i).forEach(([n,i])=>{let a=Array.isArray(i)?[]:{};e[n]||Object.assign(e,{[n]:a}),t(i)?Object.assign(e[n],r(e[n],i)):Array.isArray(i)?Object.assign(e,{[n]:[...i]}):Object.assign(e,{[n]:i})})}return e},i=(e,t=``)=>{e&&e.classList&&t.split(` `).forEach(t=>{t&&e.classList.add(t)})},a=(e,t=``)=>{e&&e.classList&&t.split(` `).forEach(t=>{t&&e.classList.remove(t)})},o=function(e){return e[e.Loading=0]=`Loading`,e[e.Loaded=1]=`Loaded`,e[e.Error=2]=`Error`,e}({}),s={autosize:!1,iframeAttr:{allow:`autoplay; fullscreen`,scrolling:`auto`},preload:!1};e.Html=()=>{let e;function c(){let n=e?.getOptions().Html;return t(n)?r({},s,n):s}function l(){return e?.getOptions().Html!==!1}function u(e){return e.type===`iframe`||e.type===`pdf`||e.type===`gmap`}function d(e){let t=`${e}`;return t.match(/^\d+$/)?`${t}px`:t}function f(e){if(e===void 0||e===``)return;let t=`${e}`.trim();if(!t.match(/^\d+(\.\d+)?(px)?$/))return;let n=parseFloat(t);return Number.isFinite(n)?n:void 0}function p(e,t){let n=e[t]??c()[t];return n===`true`?!0:n===`false`?!1:n}function m(e,t){if(!l())return;let r=t.type,i=t.src;if(!(r||!n(i))){if(i.charAt(0)===`#`?r=`inline`:i.match(/(^data:image\/[a-z0-9+\/=]*,)|(\.((a)?png|avif|gif|jp(g|eg)|pjp(eg)?|jfif|svg|webp|bmp|ico|tif(f)?)((\?|#).*)?$)/i)?r=`image`:i.match(/\.(pdf)((\?|#).*)?$/i)?r=`pdf`:i.match(/\.(html|php)((\?|#).*)?$/i)&&(r=`iframe`),!r){let e=i.match(/(?:maps\.)?google\.([a-z]{2,3}(?:\.[a-z]{2})?)\/(?:(?:(?:maps\/(?:place\/(?:.*)\/)?\@(.*),(\d+\.?\d+?)z))|(?:\?ll=))(.*)?/i);e&&(i=`https://maps.google.${e[1]}/?ll=${(e[2]?e[2]+`&z=`+Math.floor(parseFloat(e[3]))+(e[4]?e[4].replace(/^\//,`&`):``):e[4]+``).replace(/\?/,`&`)}&output=${e[4]&&e[4].indexOf(`layer=c`)>0?`svembed`:`embed`}`,r=`gmap`)}if(!r){let e=i.match(/(?:maps\.)?google\.([a-z]{2,3}(?:\.[a-z]{2})?)\/(?:maps\/search\/)(.*)/i);e&&(i=`https://maps.google.${e[1]}/maps?q=${e[2].replace(`query=`,`q=`).replace(`api=1`,``)}&output=embed`,r=`gmap`)}r&&(t.src=i,t.type=r)}}function h(e,t){l()&&u(t)&&y(t)}function g(t,n){if(u(n)){let t=n.el;e?.hideError(n),n.contentEl?.remove(),n.contentEl=void 0,n.htmlEl?.remove(),n.htmlEl=void 0,t&&(a(t,`has-html has-iframe has-pdf has-gmap`),a(t,`is-loading`))}}function _(){for(let t of e?.getSlides()||[])u(t)&&(v(t),t.state===o.Loaded&&b(t))}function v(e){let t=e.htmlEl;if(!(!t||!u(e))&&(t.style.aspectRatio=``,t.style.width=``,t.style.height=``,t.style.maxWidth=``,t.style.maxHeight=``,e.width&&(t.style.maxWidth=d(e.width)),e.height&&(t.style.maxHeight=d(e.height)),e.aspectRatio)){let n=e.aspectRatio.split(`/`),r=parseFloat(n[0].trim()),i=n[1]?parseFloat(n[1].trim()):0,a=r&&i?r/i:r;t.offsetHeight;let o=t.getBoundingClientRect(),s=a<(o.width||1)/(o.height||1);t.style.aspectRatio=`${e.aspectRatio}`,t.style.width=s?`auto`:``,t.style.height=s?``:`auto`}}function y(t){let n=t.el,r=t.src;if(!e||!n||!r)return;let s=document.createElement(`iframe`);i(s,`f-iframe`);for(let[e,t]of Object.entries(c().iframeAttr||{}))s.setAttribute(e,t);s.onerror=()=>{t.state=o.Error,e?.showError(t,`{{IFRAME_ERROR}}`)};let l=document.createElement(`div`);i(l,`f-html`),l.append(s),t.htmlEl=l,t.contentEl=s,i(n,`has-html has-iframe has-${t.type}`),n.prepend(l),v(t);let u=p(t,`preload`),d=p(t,`autosize`);t.type===`iframe`&&(u||d)?(t.state=o.Loading,e.showLoading(t),i(n,`is-loading`),s.onload=()=>{if(!e||e.getState()===2||!s.src.length||t.contentEl!==s||!s.isConnected)return;t.state=o.Loaded;let r=s.dataset.ready!==`true`;s.dataset.ready=`true`,b(t),e.hideLoading(t),r&&e.emit(`contentReady`,t),a(n,`is-loading`)},s.src=`${r}`):(s.src=`${r}`,e.emit(`contentReady`,t))}function b(e){let t=e.contentEl,n=t?.parentElement,r=n?.style,i=p(e,`autosize`),a=f(e.width)||0,o=f(e.height)||0;if(a&&o&&(i=!1),!(!t||!n||!r||!i)){try{let e=window.getComputedStyle(n),i=parseFloat(e.paddingLeft)+parseFloat(e.paddingRight),s=parseFloat(e.paddingTop)+parseFloat(e.paddingBottom),c=t.contentWindow;if(c){let e=c.document,t=e.getElementsByTagName(`html`)[0],n=e.body;r.width=``;let l=window.getComputedStyle(n),u=parseFloat(l.marginLeft)+parseFloat(l.marginRight),d=n.style.overflow||``;n.style.overflow=`hidden`,a||=n.scrollWidth+u+i,r.flex=`0 0 auto`,r.width=`${a}px`,r.height=`${n.scrollHeight}px`,n.style.overflow=d,o=Math.max(t.scrollHeight,Math.ceil(t.getBoundingClientRect().height))+s}}catch{}(a||o)&&Object.assign(r,{flex:`0 1 auto`,width:a?`${a}px`:``,height:o?`${o}px`:``})}}return{init:function(t){e=t,e.on(`addSlide`,m),e.on(`attachSlideEl`,h),e.on(`detachSlideEl`,g),e.on(`refresh`,_)},destroy:function(){e?.off(`addSlide`,m),e?.off(`attachSlideEl`,h),e?.off(`detachSlideEl`,g),e?.off(`refresh`,_),e=void 0}}}});