| 1 |
/** |
| 2 |
* Yatra Video Player Module |
| 3 |
* Reusable video player for YouTube and other video sources |
| 4 |
* |
| 5 |
* @package Yatra |
| 6 |
* @version 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
(function() { |
| 10 |
'use strict'; |
| 11 |
|
| 12 |
class YatraVideoPlayer { |
| 13 |
constructor() { |
| 14 |
this.modal = null; |
| 15 |
this.iframe = null; |
| 16 |
this.currentVideo = null; |
| 17 |
this.init(); |
| 18 |
} |
| 19 |
|
| 20 |
init() { |
| 21 |
this.createModal(); |
| 22 |
this.attachEventListeners(); |
| 23 |
} |
| 24 |
|
| 25 |
createModal() { |
| 26 |
// Check if modal already exists |
| 27 |
if (document.getElementById('yatra-video-player-modal')) { |
| 28 |
this.modal = document.getElementById('yatra-video-player-modal'); |
| 29 |
this.iframe = document.getElementById('yatra-video-player-iframe'); |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
// Create modal HTML |
| 34 |
const modalHTML = ` |
| 35 |
<div id="yatra-video-player-modal" class="yatra-video-player-modal" style="display: none;"> |
| 36 |
<div class="yatra-video-player-overlay"></div> |
| 37 |
<div class="yatra-video-player-content"> |
| 38 |
<button type="button" class="yatra-video-player-close" aria-label="Close Video"> |
| 39 |
<svg width="24" height="24" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 40 |
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/> |
| 41 |
</svg> |
| 42 |
</button> |
| 43 |
<div class="yatra-video-player-wrapper"> |
| 44 |
<iframe id="yatra-video-player-iframe" |
| 45 |
width="100%" |
| 46 |
height="100%" |
| 47 |
frameborder="0" |
| 48 |
allowfullscreen |
| 49 |
allow="autoplay; encrypted-media; picture-in-picture"> |
| 50 |
</iframe> |
| 51 |
</div> |
| 52 |
</div> |
| 53 |
</div> |
| 54 |
`; |
| 55 |
|
| 56 |
// Append to body |
| 57 |
document.body.insertAdjacentHTML('beforeend', modalHTML); |
| 58 |
|
| 59 |
this.modal = document.getElementById('yatra-video-player-modal'); |
| 60 |
this.iframe = document.getElementById('yatra-video-player-iframe'); |
| 61 |
} |
| 62 |
|
| 63 |
attachEventListeners() { |
| 64 |
// Close button |
| 65 |
const closeBtn = this.modal.querySelector('.yatra-video-player-close'); |
| 66 |
if (closeBtn) { |
| 67 |
closeBtn.addEventListener('click', () => this.close()); |
| 68 |
} |
| 69 |
|
| 70 |
// Overlay click to close |
| 71 |
const overlay = this.modal.querySelector('.yatra-video-player-overlay'); |
| 72 |
if (overlay) { |
| 73 |
overlay.addEventListener('click', () => this.close()); |
| 74 |
} |
| 75 |
|
| 76 |
// ESC key to close |
| 77 |
document.addEventListener('keydown', (e) => { |
| 78 |
if (e.key === 'Escape' && this.modal.style.display === 'flex') { |
| 79 |
this.close(); |
| 80 |
} |
| 81 |
}); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Play a video |
| 86 |
* @param {Object} video - Video object with url, embed_url, video_id, title |
| 87 |
*/ |
| 88 |
play(video) { |
| 89 |
if (!video || !video.url) { |
| 90 |
console.error('YatraVideoPlayer: Invalid video object'); |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
this.currentVideo = video; |
| 96 |
|
| 97 |
// Determine the embed URL |
| 98 |
let embedUrl = ''; |
| 99 |
|
| 100 |
if (video.embed_url) { |
| 101 |
// Use provided embed URL |
| 102 |
embedUrl = video.embed_url; |
| 103 |
} else if (video.video_id) { |
| 104 |
// YouTube video ID provided |
| 105 |
embedUrl = `https://www.youtube.com/embed/${video.video_id}?autoplay=1&rel=0`; |
| 106 |
} else if (video.url.includes('youtube.com') || video.url.includes('youtu.be')) { |
| 107 |
// Extract YouTube video ID from URL |
| 108 |
const videoId = this.extractYoutubeVideoId(video.url); |
| 109 |
if (videoId) { |
| 110 |
embedUrl = `https://www.youtube.com/embed/${videoId}?autoplay=1&rel=0`; |
| 111 |
} |
| 112 |
} else if (video.url.includes('vimeo.com')) { |
| 113 |
// Vimeo video |
| 114 |
const vimeoId = video.url.split('/').pop(); |
| 115 |
embedUrl = `https://player.vimeo.com/video/${vimeoId}?autoplay=1`; |
| 116 |
} else { |
| 117 |
// Direct video URL |
| 118 |
embedUrl = video.url; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
// Add load event handlers for debugging |
| 126 |
this.iframe.onload = () => { |
| 127 |
|
| 128 |
}; |
| 129 |
|
| 130 |
this.iframe.onerror = (error) => { |
| 131 |
console.error('YouTube iframe failed to load:', error); |
| 132 |
}; |
| 133 |
|
| 134 |
// Set iframe source |
| 135 |
|
| 136 |
this.iframe.src = embedUrl; |
| 137 |
|
| 138 |
// Verify iframe src was set |
| 139 |
setTimeout(() => { |
| 140 |
|
| 141 |
|
| 142 |
}, 100); |
| 143 |
|
| 144 |
// Show modal |
| 145 |
this.open(); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Extract YouTube video ID from various URL formats |
| 150 |
* @param {string} url - YouTube URL |
| 151 |
* @returns {string|null} Video ID or null |
| 152 |
*/ |
| 153 |
extractYoutubeVideoId(url) { |
| 154 |
const patterns = [ |
| 155 |
/youtube\.com\/watch\?v=([^&]+)/, |
| 156 |
/youtube\.com\/embed\/([^?]+)/, |
| 157 |
/youtu\.be\/([^?]+)/, |
| 158 |
/youtube\.com\/v\/([^?]+)/ |
| 159 |
]; |
| 160 |
|
| 161 |
for (const pattern of patterns) { |
| 162 |
const match = url.match(pattern); |
| 163 |
if (match) { |
| 164 |
return match[1]; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
return null; |
| 169 |
} |
| 170 |
|
| 171 |
open() { |
| 172 |
|
| 173 |
|
| 174 |
this.modal.style.display = 'flex'; |
| 175 |
document.body.style.overflow = 'hidden'; |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
close() { |
| 180 |
this.modal.style.display = 'none'; |
| 181 |
document.body.style.overflow = ''; |
| 182 |
|
| 183 |
// Stop video playback by clearing iframe src |
| 184 |
this.iframe.src = ''; |
| 185 |
this.currentVideo = null; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
// Create global instance |
| 190 |
window.YatraVideoPlayer = new YatraVideoPlayer(); |
| 191 |
|
| 192 |
// Log successful initialization |
| 193 |
|
| 194 |
|
| 195 |
})(); |
| 196 |
|