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