JavaScript UI component library, includes the latest Fancybox
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2 lines
6.5 KiB

/*! License details at fancyapps.com/license */
import{isPlainObject as t}from"../utils/isPlainObject.js";import{isString as e}from"../utils/isString.js";import{stringToHtml as o}from"../utils/strToHtml.js";const n=(t,e={})=>{const o=new URL(t),n=new URLSearchParams(o.search),i=new URLSearchParams;for(const[t,o]of[...n,...Object.entries(e)]){let e=o+"";if("t"===t){let t=e.match(/((\d*)m)?(\d*)s?/);t&&i.set("start",60*parseInt(t[2]||"0")+parseInt(t[3]||"0")+"")}else i.set(t,e)}let l=i+"",s=t.match(/#t=((.*)?\d+s)/);return s&&(l+=`#t=${s[1]}`),l},i={autoplay:!1,html5videoTpl:'<video class="f-html5video" playsinline controls controlsList="nodownload" poster="{{poster}}">\n <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}},l=()=>{let l,s=!1;function a(){const e=null==l?void 0:l.getOptions().Video;return t(e)?Object.assign(Object.assign({},i),e):i}function r(){var t;return null===(t=null==l?void 0:l.getPage())||void 0===t?void 0:t.slides[0]}const c=t=>{var e;try{let o=JSON.parse(t.data);if("https://player.vimeo.com"===t.origin){if("ready"===o.event)for(let o of Array.from((null===(e=null==l?void 0:l.getContainer())||void 0===e?void 0:e.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){const t=document.getElementById(o.id);t&&(t.dataset.ready="true")}}catch(t){}};function d(t,o){const i=o.src;if(!e(i))return;let l=o.type;if(!l||"html5video"===l){const t=i.match(/\.(mp4|mov|ogv|webm)((\?|#).*)?$/i);t&&(l="html5video",o.html5videoFormat=o.html5videoFormat||"video/"+("ogv"===t[1]?"ogg":t[1]))}if(!l||"youtube"===l){const t=i.match(/(youtube\.com|youtu\.be|youtube\-nocookie\.com)\/(?:watch\?(?:.*&)?v=|v\/|u\/|shorts\/|embed\/?)?(videoseries\?list=(?:.*)|[\w-]{11}|\?listType=(?:.*)&list=(?:.*))(?:.*)/i);if(t){const e=Object.assign(Object.assign({},a().youtube),o.youtube||{}),s=`www.youtube${e.nocookie?"-nocookie":""}.com`,r=n(i,e),c=encodeURIComponent(t[2]);o.videoId=c,o.src=`https://${s}/embed/${c}?${r}`,o.thumb=o.thumb||`https://i.ytimg.com/vi/${c}/mqdefault.jpg`,l="youtube"}}if(!l||"vimeo"===l){const t=i.match(/^.+vimeo.com\/(?:\/)?([\d]+)((\/|\?h=)([a-z0-9]+))?(.*)?/);if(t){const e=Object.assign(Object.assign({},a().vimeo),o.vimeo||{}),s=n(i,e),r=encodeURIComponent(t[1]),c=t[4]||"";o.videoId=r,o.src=`https://player.vimeo.com/video/${r}?${c?`h=${c}${s?"&":""}`:""}${s}`,l="vimeo"}}o.type=l}function u(t,n){"html5video"===n.type&&function(t){if(!l||!t.el||!t.src)return;const{el:n,src:i}=t;if(!n||!i)return;const s=t.html5videoTpl||a().html5videoTpl,r=t.html5videoFormat||a().html5videoFormat;if(!s)return;const c=t.poster||(t.thumb&&e(t.thumb)?t.thumb:""),d=o(s.replace(/\{\{src\}\}/gi,i+"").replace(/\{\{format\}\}/gi,r||"").replace(/\{\{poster\}\}/gi,c+""));if(!d)return;const u=document.createElement("div");u.classList.add("f-html"),u.append(d),t.contentEl=d,t.htmlEl=u,n.classList.add(`has-${t.type}`),n.prepend(u),h(t),l.emit("contentReady",t)}(n),"youtube"!==n.type&&"vimeo"!==n.type||function(t){if(!l||!t.el||!t.src)return;const e=document.createElement("iframe");e.classList.add("f-iframe"),e.setAttribute("id",`f-iframe_${t.videoId}`);for(const[t,o]of Object.entries(a().iframeAttr||{}))e.setAttribute(t,o);e.onload=()=>{var o;l&&1===l.getState()&&"youtube"===t.type&&(null===(o=e.contentWindow)||void 0===o||o.postMessage(JSON.stringify({event:"listening",id:e.getAttribute("id")}),"*"))},e.onerror=()=>{l&&1===l.getState()&&(null==l||l.showError(t,"{{IFRAME_ERROR}}"))},e.src=t.src;const o=document.createElement("div");o.classList.add("f-html"),o.append(e),t.contentEl=e,t.htmlEl=o,t.el.classList.add("has-html"),t.el.classList.add("has-iframe"),t.el.classList.add(`has-${t.type}`),t.el.prepend(o),h(t),l.emit("contentReady",t)}(n)}function m(t,e){var o,n;"html5video"!==e.type&&"youtube"!==e.type&&"vimeo"!==e.type||(null===(o=e.contentEl)||void 0===o||o.remove(),e.contentEl=void 0,null===(n=e.htmlEl)||void 0===n||n.remove(),e.htmlEl=void 0),e.poller&&clearTimeout(e.poller)}function f(){s=!1}function p(){if(s)return;s=!0;const t=r();(t&&void 0!==t.autoplay?t.autoplay:a().autoplay)&&(function(){var t;const e=r(),o=null==e?void 0:e.el;if(o&&"html5video"===(null==e?void 0:e.type))try{const t=o.querySelector("video");if(t){const e=t.play();void 0!==e&&e.then((()=>{})).catch((e=>{t.muted=!0,t.play()}))}}catch(t){}const n=null==e?void 0:e.htmlEl;n instanceof HTMLIFrameElement&&(null===(t=n.contentWindow)||void 0===t||t.postMessage('{"event":"command","func":"stopVideo","args":""}',"*"))}(),function(){const t=r(),e=null==t?void 0:t.type;if(!(null==t?void 0:t.el)||"youtube"!==e&&"vimeo"!==e)return;const o=()=>{if(t.contentEl&&t.contentEl instanceof HTMLIFrameElement&&t.contentEl.contentWindow){let e;if("true"===t.contentEl.dataset.ready)return e="youtube"===t.type?{event:"command",func:"playVideo"}:{method:"play",value:"true"},e&&t.contentEl.contentWindow.postMessage(JSON.stringify(e),"*"),void(t.poller=void 0);"youtube"===t.type&&(e={event:"listening",id:t.contentEl.getAttribute("id")},t.contentEl.contentWindow.postMessage(JSON.stringify(e),"*"))}t.poller=setTimeout(o,250)};o()}())}function h(t){const e=null==t?void 0:t.el,o=null==t?void 0:t.htmlEl;if(!t||!o)return;o.style.aspectRatio="",o.style.width="",o.style.height="",o.style.maxWidth="",o.style.maxHeight="",o.offsetHeight;const n=o.getBoundingClientRect(),i=n.width||1,l=n.height||1;if(t.width){let e=`${t.width}`;e.match(/^\d+$/)&&(e+="px"),o.style.maxWidth=`${e}`}if(t.height){let e=`${t.height}`;e.match(/^\d+$/)&&(e+="px"),o.style.maxHeight=`${e}`}if(!e||!t.aspectRatio)return;const s=t.aspectRatio.split("/"),a=parseFloat(s[0].trim()),r=s[1]?parseFloat(s[1].trim()):0,c=(a&&r?a/r:a)<i/l;o.style.aspectRatio=`${t.aspectRatio}`,o.style.width=c?"auto":"",o.style.height=c?"":"auto"}function v(){h(r())}return{init:function(t){l=t,l.on("addSlide",d),l.on("attachSlideEl",u),l.on("detachSlideEl",m),l.on("ready",p),l.on("change",f),l.on("settle",p),l.on("refresh",v),window.addEventListener("message",c)},destroy:function(){null==l||l.off("addSlide",d),null==l||l.off("attachSlideEl",u),null==l||l.off("detachSlideEl",m),null==l||l.off("ready",p),null==l||l.off("change",f),null==l||l.off("settle",p),null==l||l.off("refresh",v),window.removeEventListener("message",c),l=void 0}}};export{l as Video};