@fancyapps/ui
Version:
Robust JavaScript UI Component Library
2 lines • 6.65 kB
JavaScript
!/*! License details at fancyapps.com/license */function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).window=e.window||{})}(this,function(e){"use strict";let t=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},o=(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},i={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:"auto",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,n=!1;function l(){let t=e?.getOptions().Video;return"object"==typeof t&&null!==t&&t.constructor===Object&&"[object Object]"===Object.prototype.toString.call(t)?{...i,...t}:i}function r(){return e?.getPage()?.slides[0]}let a=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 s(e,t){let i=t.src;if("string"!=typeof i)return;let n=t.type;if(!n||"html5video"===n){let e=i.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=i.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={...l().youtube,...t.youtube||{}},a=`www.youtube${r.nocookie?"-nocookie":""}.com`,s=o(i,r),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=i.match(/^.+vimeo.com\/(?:\/)?(video\/)?([\d]+)((\/|\?h=)([a-z0-9]+))?(.*)?/);if(e){let r=o(i,{...l().vimeo,...t.vimeo||{}}),a=encodeURIComponent(e[2]),s=e[5]||"";t.videoId=a,t.src=`https://player.vimeo.com/video/${a}?${s?`h=${s}${r?"&":""}`:""}${r}`,n="vimeo"}}t.type=n}function d(o,i){"html5video"===i.type&&function(o){if(!e||!o.el||!o.src)return;let{el:i,src:n}=o;if(!i||!n)return;let r=o.html5videoTpl||l().html5videoTpl,a=o.html5videoFormat||l().html5videoFormat;if(!r)return;let s=o.poster||(o.thumb&&"string"==typeof o.thumb?o.thumb:""),d=t(r.replace(/\{\{src\}\}/gi,n+"").replace(/\{\{format\}\}/gi,a||"").replace(/\{\{poster\}\}/gi,s+""));if(!d)return;let c=document.createElement("div");c.classList.add("f-html"),c.append(d),o.contentEl=d,o.htmlEl=c,i.classList.add(`has-${o.type}`),i.prepend(c),f(o),e.emit("contentReady",o)}(i),("youtube"===i.type||"vimeo"===i.type)&&function(t){if(!e||!t.el||!t.src)return;let o=document.createElement("iframe");for(let[e,i]of(o.classList.add("f-iframe"),o.setAttribute("id",`f-iframe_${t.videoId}`),Object.entries(l().iframeAttr||{})))o.setAttribute(e,i);o.onload=()=>{e&&1===e.getState()&&"youtube"===t.type&&o.contentWindow?.postMessage(JSON.stringify({event:"listening",id:o.getAttribute("id")}),"*")},o.onerror=()=>{e&&1===e.getState()&&e?.showError(t,"{{IFRAME_ERROR}}")},o.src=t.src;let i=document.createElement("div");i.classList.add("f-html"),i.append(o),t.contentEl=o,t.htmlEl=i,t.el.classList.add("has-html"),t.el.classList.add("has-iframe"),t.el.classList.add(`has-${t.type}`),t.el.prepend(i),f(t),e.emit("contentReady",t)}(i)}function c(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 m(){n=!1}function u(){if(n)return;n=!0;let e=r();(e&&void 0!==e.autoplay?e.autoplay:l().autoplay)&&(!function(){let e=r(),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=r(),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 f(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 p(){f(r())}return{init:function(t){(e=t).on("addSlide",s),e.on("attachSlideEl",d),e.on("detachSlideEl",c),e.on("ready",u),e.on("change",m),e.on("settle",u),e.on("refresh",p),window.addEventListener("message",a)},destroy:function(){e?.off("addSlide",s),e?.off("attachSlideEl",d),e?.off("detachSlideEl",c),e?.off("ready",u),e?.off("change",m),e?.off("settle",u),e?.off("refresh",p),window.removeEventListener("message",a),e=void 0}}}});