| 1 |
import { register } from '@elementor/frontend-handlers'; |
| 2 |
|
| 3 |
const getYoutubeVideoIdFromUrl = ( url ) => { |
| 4 |
const regex = /^(?:https?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?vi?=|(?:embed|v|vi|user|shorts)\/))([^?&"'>]+)/; |
| 5 |
const match = url.match( regex ); |
| 6 |
return match ? match[ 1 ] : null; |
| 7 |
}; |
| 8 |
|
| 9 |
const loadYouTubeAPI = () => { |
| 10 |
return new Promise( ( resolve ) => { |
| 11 |
if ( window.YT && window.YT.loaded ) { |
| 12 |
resolve( window.YT ); |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
const YOUTUBE_IFRAME_API_URL = 'https://www.youtube.com/iframe_api'; |
| 17 |
if ( ! document.querySelector( `script[src="${ YOUTUBE_IFRAME_API_URL }"]` ) ) { |
| 18 |
const tag = document.createElement( 'script' ); |
| 19 |
tag.src = YOUTUBE_IFRAME_API_URL; |
| 20 |
const firstScriptTag = document.getElementsByTagName( 'script' )[ 0 ]; |
| 21 |
firstScriptTag.parentNode.insertBefore( tag, firstScriptTag ); |
| 22 |
} |
| 23 |
|
| 24 |
const checkYT = () => { |
| 25 |
if ( window.YT && window.YT.loaded ) { |
| 26 |
resolve( window.YT ); |
| 27 |
} else { |
| 28 |
setTimeout( checkYT, 350 ); |
| 29 |
} |
| 30 |
}; |
| 31 |
checkYT(); |
| 32 |
} ); |
| 33 |
}; |
| 34 |
|
| 35 |
register( { |
| 36 |
elementType: 'e-youtube', |
| 37 |
id: 'e-youtube-handler', |
| 38 |
callback: ( { element } ) => { |
| 39 |
const youtubeElement = document.createElement( 'div' ); |
| 40 |
youtubeElement.style.height = '100%'; |
| 41 |
element.appendChild( youtubeElement ); |
| 42 |
|
| 43 |
const settingsAttr = element.getAttribute( 'data-settings' ); |
| 44 |
const parsedSettings = settingsAttr ? JSON.parse( settingsAttr ) : {}; |
| 45 |
|
| 46 |
const videoId = getYoutubeVideoIdFromUrl( parsedSettings.source ); |
| 47 |
|
| 48 |
if ( ! videoId ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
let player; |
| 53 |
let observer; |
| 54 |
|
| 55 |
const prepareYTVideo = ( YT ) => { |
| 56 |
const playerOptions = { |
| 57 |
videoId, |
| 58 |
events: { |
| 59 |
onReady: () => { |
| 60 |
if ( parsedSettings.mute ) { |
| 61 |
player.mute(); |
| 62 |
} |
| 63 |
|
| 64 |
if ( parsedSettings.autoplay ) { |
| 65 |
player.playVideo(); |
| 66 |
} |
| 67 |
}, |
| 68 |
onStateChange: ( event ) => { |
| 69 |
if ( event.data === YT.PlayerState.ENDED && parsedSettings.loop ) { |
| 70 |
player.seekTo( parsedSettings.start || 0 ); |
| 71 |
} |
| 72 |
}, |
| 73 |
}, |
| 74 |
playerVars: { |
| 75 |
controls: parsedSettings.controls ? 1 : 0, |
| 76 |
rel: parsedSettings.rel ? 0 : 1, |
| 77 |
cc_load_policy: parsedSettings.cc_load_policy ? 1 : 0, |
| 78 |
autoplay: parsedSettings.autoplay ? 1 : 0, |
| 79 |
start: parsedSettings.start, |
| 80 |
end: parsedSettings.end, |
| 81 |
}, |
| 82 |
}; |
| 83 |
|
| 84 |
// To handle CORS issues, when the default host is changed, the origin parameter has to be set. |
| 85 |
if ( parsedSettings.privacy ) { |
| 86 |
playerOptions.host = 'https://www.youtube-nocookie.com'; |
| 87 |
playerOptions.origin = window.location.hostname; |
| 88 |
} |
| 89 |
|
| 90 |
player = new YT.Player( youtubeElement, playerOptions ); |
| 91 |
|
| 92 |
return player; |
| 93 |
}; |
| 94 |
|
| 95 |
if ( parsedSettings.lazyload ) { |
| 96 |
observer = new IntersectionObserver( |
| 97 |
( entries ) => { |
| 98 |
if ( entries[ 0 ].isIntersecting ) { |
| 99 |
loadYouTubeAPI().then( ( apiObject ) => prepareYTVideo( apiObject ) ); |
| 100 |
observer.unobserve( element ); |
| 101 |
} |
| 102 |
}, |
| 103 |
); |
| 104 |
|
| 105 |
observer.observe( element ); |
| 106 |
} else { |
| 107 |
loadYouTubeAPI().then( ( apiObject ) => prepareYTVideo( apiObject ) ); |
| 108 |
} |
| 109 |
|
| 110 |
return () => { |
| 111 |
if ( player && 'function' === typeof player.destroy ) { |
| 112 |
player.destroy(); |
| 113 |
player = null; |
| 114 |
} |
| 115 |
|
| 116 |
if ( element.contains( youtubeElement ) ) { |
| 117 |
element.removeChild( youtubeElement ); |
| 118 |
} |
| 119 |
|
| 120 |
if ( observer && 'function' === typeof observer.disconnect ) { |
| 121 |
observer.disconnect(); |
| 122 |
observer = null; |
| 123 |
} |
| 124 |
}; |
| 125 |
}, |
| 126 |
} ); |
| 127 |
|