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