45 lines
1.5 KiB
JavaScript
45 lines
1.5 KiB
JavaScript
/**
|
|
* VideoJS Player initialization
|
|
* This script initializes VideoJS for all video elements, handling both
|
|
* videos with data-setup attribute and regular videos.
|
|
*/
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
// First, initialize all videos with the data-setup attribute
|
|
// VideoJS will automatically process these
|
|
|
|
// Then find any remaining videos (without video-js class) that need to be initialized
|
|
const regularVideos = document.querySelectorAll('video:not(.video-js)');
|
|
|
|
if (regularVideos.length) {
|
|
regularVideos.forEach(function(video) {
|
|
// Ensure the video has an ID
|
|
const id = video.id || 'video-' + Math.floor(Math.random() * 1000000);
|
|
video.id = id;
|
|
|
|
// Add VideoJS class
|
|
video.classList.add('video-js');
|
|
video.classList.add('vjs-big-play-centered');
|
|
|
|
// Initialize with default options
|
|
videojs(id, {
|
|
controls: true,
|
|
autoplay: false,
|
|
preload: 'auto'
|
|
});
|
|
});
|
|
}
|
|
|
|
// Additional check to ensure all video.video-js elements are initialized
|
|
// This helps with videos that have the class but weren't auto-initialized
|
|
const videojsElements = document.querySelectorAll('video.video-js');
|
|
videojsElements.forEach(function(videoElement) {
|
|
const id = videoElement.id;
|
|
if (id) {
|
|
// Check if this element already has a VideoJS instance
|
|
if (!videojs.getPlayers()[id]) {
|
|
// Initialize VideoJS for this element
|
|
videojs(id);
|
|
}
|
|
}
|
|
});
|
|
});
|