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