UNPKG

@fancyapps/ui

Version:

Robust JavaScript UI Component Library

2 lines • 5.03 kB
/*! 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={autosize:!1,iframeAttr:{allow:`autoplay; fullscreen`,scrolling:`auto`},preload:!1};e.Html=()=>{let e;function s(){let n=e?.getOptions().Html;return t(n)?r({},o,n):o}function c(){return e?.getOptions().Html!==!1}function l(e){return e.type===`iframe`||e.type===`pdf`||e.type===`gmap`}function u(e){let t=`${e}`;return t.match(/^\d+$/)?`${t}px`:t}function d(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 f(e,t){let n=e[t]??s()[t];return n===`true`||n!==`false`&&n}function p(e,t){if(!c())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 m(e,t){c()&&l(t)&&v(t)}function h(t,n){if(l(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 g(){for(let t of e?.getSlides()||[])l(t)&&(_(t),t.state===1&&y(t))}function _(e){let t=e.htmlEl;if(t&&l(e)&&(t.style.aspectRatio=``,t.style.width=``,t.style.height=``,t.style.maxWidth=``,t.style.maxHeight=``,e.width&&(t.style.maxWidth=u(e.width)),e.height&&(t.style.maxHeight=u(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 v(t){let n=t.el,r=t.src;if(!e||!n||!r)return;let o=document.createElement(`iframe`);i(o,`f-iframe`);for(let[e,t]of Object.entries(s().iframeAttr||{}))o.setAttribute(e,t);o.onerror=()=>{t.state=2,e?.showError(t,`{{IFRAME_ERROR}}`)};let c=document.createElement(`div`);i(c,`f-html`),c.append(o),t.htmlEl=c,t.contentEl=o,i(n,`has-html has-iframe has-${t.type}`),n.prepend(c),_(t);let l=f(t,`preload`),u=f(t,`autosize`);t.type===`iframe`&&(l||u)?(t.state=0,e.showLoading(t),i(n,`is-loading`),o.onload=()=>{if(!e||e.getState()===2||!o.src.length||t.contentEl!==o||!o.isConnected)return;t.state=1;let r=o.dataset.ready!==`true`;o.dataset.ready=`true`,y(t),e.hideLoading(t),r&&e.emit(`contentReady`,t),a(n,`is-loading`)},o.src=`${r}`):(o.src=`${r}`,e.emit(`contentReady`,t))}function y(e){let t=e.contentEl,n=t?.parentElement,r=n?.style,i=f(e,`autosize`),a=d(e.width)||0,o=d(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`,p),e.on(`attachSlideEl`,m),e.on(`detachSlideEl`,h),e.on(`refresh`,g)},destroy:function(){e?.off(`addSlide`,p),e?.off(`attachSlideEl`,m),e?.off(`detachSlideEl`,h),e?.off(`refresh`,g),e=void 0}}}});