@fancyapps/ui
Version:
Robust JavaScript UI Component Library
3 lines • 6.69 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,t=``)=>{e&&e.classList&&t.split(` `).forEach(t=>{t&&e.classList.add(t)})},n=e=>typeof e==`object`&&!!e&&e.constructor===Object&&Object.prototype.toString.call(e)===`[object Object]`,r=e=>typeof e==`string`,i=function(e){var t=new DOMParser().parseFromString(e,`text/html`).body;if(t.childElementCount>1){for(var n=document.createElement(`div`);t.firstChild;)n.appendChild(t.firstChild);return n}let r=t.firstChild;if(r&&!(r instanceof HTMLElement)){var n=document.createElement(`div`);return n.appendChild(r),n}return r},a=(e,t={})=>{let n=new URL(e),r=new URLSearchParams(n.search),i=new URLSearchParams;for(let[e,n]of[...r,...Object.entries(t)]){let t=n+``;if(e===`t`){let e=t.match(/((\d*)m)?(\d*)s?/);e&&i.set(`start`,parseInt(e[2]||`0`)*60+parseInt(e[3]||`0`)+``)}else i.set(e,t)}let a=i+``,o=e.match(/#t=((.*)?\d+s)/);return o&&(a+=`#t=${o[1]}`),a},o={autoplay:!1,html5videoTpl:`<video class="f-html5video" playsinline controls controlsList="nodownload" poster="{{poster}}">
<source src="{{src}}" type="{{format}}" />Sorry, your browser doesn't support embedded videos.</video>`,iframeAttr:{allow:`autoplay; fullscreen`,scrolling:`no`,referrerPolicy:`strict-origin-when-cross-origin`,credentialless:``},vimeo:{byline:1,color:`00adef`,controls:1,dnt:1,muted:0},youtube:{controls:1,enablejsapi:1,nocookie:1,rel:0,fs:1}};e.Video=()=>{let e,s=!1;function c(){let t=e?.getOptions().Video;return n(t)?{...o,...t}:o}function l(){return e?.getPage()?.slides[0]}let u=t=>{try{let n=JSON.parse(t.data);if(t.origin===`https://player.vimeo.com`){if(n.event===`ready`)for(let n of Array.from(e?.getContainer()?.getElementsByClassName(`f-iframe`)||[]))n instanceof HTMLIFrameElement&&n.contentWindow===t.source&&(n.dataset.ready=`true`)}else if(t.origin.match(/^https:\/\/(www.)?youtube(-nocookie)?.com$/)&&n.event===`onReady`){let e=document.getElementById(n.id);e&&(e.dataset.ready=`true`)}}catch{}};function d(){let e=l(),t=e?.el;if(t&&e?.type===`html5video`)try{let e=t.querySelector(`video`);if(e){let t=e.play();t!==void 0&&t.then(()=>{}).catch(t=>{e.muted=!0,e.play()})}}catch{}let n=e?.htmlEl;n instanceof HTMLIFrameElement&&n.contentWindow?.postMessage(`{"event":"command","func":"stopVideo","args":""}`,`*`)}function f(){let e=l(),t=e?.type;if(!(e?.el&&(t===`youtube`||t===`vimeo`)))return;let n=()=>{if(e.contentEl&&e.contentEl instanceof HTMLIFrameElement&&e.contentEl.contentWindow){let t;if(e.contentEl.dataset.ready===`true`){t=e.type===`youtube`?{event:`command`,func:`playVideo`}:{method:`play`,value:`true`},t&&e.contentEl.contentWindow.postMessage(JSON.stringify(t),`*`),e.poller=void 0;return}e.type===`youtube`&&(t={event:`listening`,id:e.contentEl.getAttribute(`id`)},e.contentEl.contentWindow.postMessage(JSON.stringify(t),`*`))}e.poller=setTimeout(n,250)};n()}function p(n){let a=n.el,o=n.src;if(!e||!a||!o)return;let s=n.html5videoTpl||c().html5videoTpl,l=n.html5videoFormat||c().html5videoFormat;if(!s)return;let u=n.poster||(n.thumb&&r(n.thumb)?n.thumb:``),d=i(s.replace(/\{\{src\}\}/gi,o+``).replace(/\{\{format\}\}/gi,l||``).replace(/\{\{poster\}\}/gi,u+``));if(!d)return;let f=document.createElement(`div`);t(f,`f-html`),f.append(d),n.contentEl=d,n.htmlEl=f,t(a,`has-${n.type}`),a.prepend(f),b(n),e.emit(`contentReady`,n)}function m(n){let r=n.el,i=n.src;if(!e||!r||!i)return;let a=document.createElement(`iframe`);t(a,`f-iframe`),a.setAttribute(`id`,`f-iframe_${n.videoId}`);for(let[e,t]of Object.entries(c().iframeAttr||{}))a.setAttribute(e,t);n.type===`youtube`&&(a.onload=()=>{e?.getState()===1&&a.contentWindow?.postMessage(JSON.stringify({event:`listening`,id:a.getAttribute(`id`)}),`*`)}),a.onerror=()=>{e?.showError(n,`{{IFRAME_ERROR}}`)};let o=document.createElement(`div`);t(o,`f-html`),o.append(a),n.contentEl=a,n.htmlEl=o,t(r,`has-html has-iframe has-${n.type}`),a.src=`${n.src}`,r.prepend(o),b(n),e.emit(`contentReady`,n)}function h(e,t){let n=t.src;if(!r(n))return;let i=t.type;if(!i||i===`html5video`){let e=n.match(/\.(mp4|mov|ogv|webm)((\?|#).*)?$/i);e&&(i=`html5video`,t.html5videoFormat=t.html5videoFormat||`video/`+(e[1]===`ogv`?`ogg`:e[1]))}if(!i||i===`youtube`){let e=n.match(/(youtube\.com|youtu\.be|youtube\-nocookie\.com)\/(?:watch\?(?:.*&)?v=|v\/|u\/|shorts\/|embed\/?)?(videoseries\?list=(?:.*)|[\w-]{11}|\?listType=(?:.*)&list=(?:.*))(?:.*)/i);if(e){let r={...c().youtube,...t.youtube||{}},o=`www.youtube${r.nocookie?`-nocookie`:``}.com`,s=a(n,r),l=encodeURIComponent(e[2]);t.videoId=l,t.src=`https://${o}/embed/${l}?${s}`,t.thumb=t.thumb||`https://i.ytimg.com/vi/${l}/mqdefault.jpg`,i=`youtube`}}if(!i||i===`vimeo`){let e=n.match(/^.+vimeo.com\/(?:\/)?(video\/)?([\d]+)((\/|\?h=)([a-z0-9]+))?(.*)?/);if(e){let r=a(n,{...c().vimeo,...t.vimeo||{}}),o=encodeURIComponent(e[2]),s=e[5]||``;t.videoId=o,t.src=`https://player.vimeo.com/video/${o}?${s?`h=${s}${r?`&`:``}`:``}${r}`,i=`vimeo`}}t.type=i}function g(e,t){t.type===`html5video`&&p(t),(t.type===`youtube`||t.type===`vimeo`)&&m(t)}function _(e,t){(t.type===`html5video`||t.type===`youtube`||t.type===`vimeo`)&&(t.contentEl?.remove(),t.contentEl=void 0,t.htmlEl?.remove(),t.htmlEl=void 0),t.poller&&clearTimeout(t.poller)}function v(){s=!1}function y(){if(s)return;s=!0;let e=l();(e&&e.autoplay!==void 0?e.autoplay:c().autoplay)&&(d(),f())}function b(e){let t=e?.htmlEl;if(!(!e||!t||!(e.type===`html5video`||e.type===`youtube`||e.type===`vimeo`))){if(t.style.aspectRatio=``,t.style.width=``,t.style.height=``,t.style.maxWidth=``,t.style.maxHeight=``,e.width){let n=`${e.width}`;n.match(/^\d+$/)&&(n+=`px`),t.style.maxWidth=`${n}`}if(e.height){let n=`${e.height}`;n.match(/^\d+$/)&&(n+=`px`),t.style.maxHeight=`${n}`}if(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 x(){b(l())}return{init:function(t){e=t,e.on(`addSlide`,h),e.on(`attachSlideEl`,g),e.on(`detachSlideEl`,_),e.on(`ready`,y),e.on(`change`,v),e.on(`settle`,y),e.on(`refresh`,x),window.addEventListener(`message`,u)},destroy:function(){e?.off(`addSlide`,h),e?.off(`attachSlideEl`,g),e?.off(`detachSlideEl`,_),e?.off(`ready`,y),e?.off(`change`,v),e?.off(`settle`,y),e?.off(`refresh`,x),window.removeEventListener(`message`,u),e=void 0}}}});