Refactor video overlay implementation to improve control handling and visibility
This commit is contained in:
		@@ -86,7 +86,7 @@
 | 
			
		||||
			height: 100%;
 | 
			
		||||
			background-color: black;
 | 
			
		||||
			z-index: 10000; /* Above other page content */
 | 
			
		||||
			display: flex;
 | 
			
		||||
			display: none; /* Initially hidden, JS will change to flex */
 | 
			
		||||
			align-items: center;
 | 
			
		||||
			justify-content: center;
 | 
			
		||||
		}
 | 
			
		||||
@@ -174,13 +174,24 @@
 | 
			
		||||
			clearTimeout(inactivityTimer);
 | 
			
		||||
			if (overlayContainer) {
 | 
			
		||||
				overlayContainer.css('cursor', 'default');
 | 
			
		||||
                overlayContainer.off('mousemove.inactivityControls');
 | 
			
		||||
                // Detach document-level fullscreen listener specific to this playback
 | 
			
		||||
                $(document).off('fullscreenchange.videoPlayback webkitfullscreenchange.videoPlayback mozfullscreenchange.videoPlayback MSFullscreenChange.videoPlayback', handleFullscreenChange);
 | 
			
		||||
                overlayContainer.remove();
 | 
			
		||||
				overlayContainer.off('mousemove.inactivityControls'); // Remove session-specific listener
 | 
			
		||||
				overlayContainer.hide(); // Hide instead of remove
 | 
			
		||||
			}
 | 
			
		||||
            // Attempt to exit fullscreen if the document is still in fullscreen mode
 | 
			
		||||
            // and our container was likely the one in fullscreen.
 | 
			
		||||
 | 
			
		||||
			$(document).off('fullscreenchange.videoPlayback webkitfullscreenchange.videoPlayback mozfullscreenchange.videoPlayback MSFullscreenChange.videoPlayback', handleFullscreenChange);
 | 
			
		||||
 | 
			
		||||
			if (videoElement) {
 | 
			
		||||
				$(videoElement).off('.currentVideo'); // Remove all namespaced video events
 | 
			
		||||
				videoElement.src = ""; // Clear the source
 | 
			
		||||
				videoElement.load(); // Important to ensure the video unloads
 | 
			
		||||
			}
 | 
			
		||||
			if (seekBar) {
 | 
			
		||||
				seekBar.val(0);
 | 
			
		||||
				seekBar.css('background', ''); // Reset seek bar style
 | 
			
		||||
			}
 | 
			
		||||
			updatePlayPauseButtonState(); // Update button to show play icon usually
 | 
			
		||||
 | 
			
		||||
			// Attempt to exit fullscreen
 | 
			
		||||
			if (document.fullscreenElement || document.webkitIsFullScreen || document.mozFullScreen || document.msFullscreenElement) {
 | 
			
		||||
				if (document.exitFullscreen) {
 | 
			
		||||
					document.exitFullscreen().catch(() => {});
 | 
			
		||||
@@ -195,139 +206,133 @@
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		function showControlsAndResetTimer() {
 | 
			
		||||
            backButton.removeClass('button-hidden');
 | 
			
		||||
            customControls.removeClass('controls-hidden');
 | 
			
		||||
            overlayContainer.css('cursor', 'default'); // Show cursor
 | 
			
		||||
			if (backButton) backButton.removeClass('button-hidden');
 | 
			
		||||
			if (customControls) customControls.removeClass('controls-hidden');
 | 
			
		||||
			if (overlayContainer) overlayContainer.css('cursor', 'default');
 | 
			
		||||
 | 
			
		||||
			clearTimeout(inactivityTimer);
 | 
			
		||||
			inactivityTimer = setTimeout(function() {
 | 
			
		||||
                backButton.addClass('button-hidden');
 | 
			
		||||
                customControls.addClass('controls-hidden');
 | 
			
		||||
                overlayContainer.css('cursor', 'none'); // Hide cursor
 | 
			
		||||
            }, 3000); // 3 seconds
 | 
			
		||||
				if (backButton) backButton.addClass('button-hidden');
 | 
			
		||||
				if (customControls) customControls.addClass('controls-hidden');
 | 
			
		||||
				if (overlayContainer) overlayContainer.css('cursor', 'none');
 | 
			
		||||
			}, 3000);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
        // --- Moved Helper Function Definitions ---
 | 
			
		||||
		function handlePlayPauseClick() {
 | 
			
		||||
			if (videoElement) {
 | 
			
		||||
				if (videoElement.paused) {
 | 
			
		||||
					videoElement.play();
 | 
			
		||||
				} else {
 | 
			
		||||
					videoElement.pause();
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		function updatePlayPauseButtonState() {
 | 
			
		||||
            if (!playPauseButton) return; // Ensure button exists
 | 
			
		||||
			if (!playPauseButton || !videoElement) return;
 | 
			
		||||
			if (videoElement.paused) {
 | 
			
		||||
                playPauseButton.text('►'); // Play icon
 | 
			
		||||
				playPauseButton.text('►');
 | 
			
		||||
			} else {
 | 
			
		||||
                playPauseButton.text('❚❚'); // Pause icon
 | 
			
		||||
				playPauseButton.text('❚❚');
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		function handleVideoLoadedMetadata() {
 | 
			
		||||
            if (!seekBar) return; // Ensure seekbar exists
 | 
			
		||||
			if (seekBar && videoElement) {
 | 
			
		||||
				seekBar.attr('max', videoElement.duration);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		function handleVideoTimeUpdate() {
 | 
			
		||||
            if (!seekBar) return; // Ensure seekbar exists
 | 
			
		||||
			if (seekBar && videoElement && videoElement.duration) { // Check videoElement.duration to prevent NaN
 | 
			
		||||
				seekBar.val(videoElement.currentTime);
 | 
			
		||||
				const percentage = (videoElement.currentTime / videoElement.duration) * 100;
 | 
			
		||||
				seekBar.css('background', `linear-gradient(to right, #3498db ${percentage}%, #555 ${percentage}%)`);
 | 
			
		||||
			} else if (seekBar) {
 | 
			
		||||
				seekBar.val(0); // Reset if duration is not available
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		function handleSeekBarInput() {
 | 
			
		||||
            if (!seekBar) return; // Ensure seekbar exists
 | 
			
		||||
			if (videoElement && seekBar) {
 | 
			
		||||
				videoElement.currentTime = seekBar.val();
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		function handleFullscreenChange() {
 | 
			
		||||
			const isActuallyFullscreen = document.fullscreenElement || document.webkitIsFullScreen || document.mozFullScreen || document.msFullscreenElement;
 | 
			
		||||
            if (!isActuallyFullscreen && overlayContainer && overlayContainer.length && !overlayContainer.is(document.fullscreenElement)) {
 | 
			
		||||
                if (overlayContainer.is(':visible')) {
 | 
			
		||||
			if (!isActuallyFullscreen && overlayContainer && overlayContainer.is(':visible')) {
 | 
			
		||||
				// If fullscreen exited and overlay was visible, treat as exiting video playback
 | 
			
		||||
				// This helps if user presses Esc.
 | 
			
		||||
				exitVideoPlayback();
 | 
			
		||||
			}
 | 
			
		||||
            }
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		$(document).ready(function() {
 | 
			
		||||
			// Initialize global selectors for static elements
 | 
			
		||||
			overlayContainer = $("#video-overlay-container");
 | 
			
		||||
			videoElement = overlayContainer.find(".video-player")[0];
 | 
			
		||||
			backButton = overlayContainer.find("#back-button");
 | 
			
		||||
			customControls = overlayContainer.find("#custom-video-controls");
 | 
			
		||||
			playPauseButton = customControls.find("#custom-play-pause-button");
 | 
			
		||||
			seekBar = customControls.find("#custom-seek-bar");
 | 
			
		||||
 | 
			
		||||
			// Pre-set all thumbnails to a placeholder state
 | 
			
		||||
			$(".video-thumbnail").attr("src", "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100%25' height='100%25' viewBox='0 0 16 9'%3E%3C/svg%3E");
 | 
			
		||||
 | 
			
		||||
			$.getJSON("/api/homepage", function(data) {
 | 
			
		||||
				currentVideo = data.currentVideo;
 | 
			
		||||
                // previousVideo = data.previousVideo;
 | 
			
		||||
                // nextVideo = data.nextVideo;
 | 
			
		||||
				//
 | 
			
		||||
                // // Set thumbnails
 | 
			
		||||
				$("#current-thumb").attr("src", currentVideo.thumbnail);
 | 
			
		||||
                // $("#prev-thumb").attr("src", previousVideo.thumbnail);
 | 
			
		||||
                // $("#next-thumb").attr("src", nextVideo.thumbnail);
 | 
			
		||||
 | 
			
		||||
                // Hide loader and show videos
 | 
			
		||||
				hideLoader();
 | 
			
		||||
			})
 | 
			
		||||
 | 
			
		||||
			// Attach event listeners that can be set up once for static controls
 | 
			
		||||
			if (playPauseButton) playPauseButton.click(handlePlayPauseClick);
 | 
			
		||||
			if (seekBar) seekBar.on('input', handleSeekBarInput);
 | 
			
		||||
			if (backButton) backButton.click(exitVideoPlayback);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
			$("#play-button").click(function() {
 | 
			
		||||
                if (currentVideo && currentVideo.url) {
 | 
			
		||||
                    // Create the overlay container
 | 
			
		||||
                    overlayContainer = $("<div id='video-overlay-container'></div>").appendTo("body");
 | 
			
		||||
				if (currentVideo && currentVideo.url && videoElement && overlayContainer) {
 | 
			
		||||
					// Set video source and load
 | 
			
		||||
					videoElement.src = currentVideo.url;
 | 
			
		||||
					videoElement.load();
 | 
			
		||||
 | 
			
		||||
                    // Create video element and append to container
 | 
			
		||||
                    videoElement = $("<video autoplay class='video-player'></video>") // Removed 'controls' attribute
 | 
			
		||||
                        .attr("src", currentVideo.url)
 | 
			
		||||
                        .appendTo(overlayContainer)[0];
 | 
			
		||||
 | 
			
		||||
                    // Create back button and append to container
 | 
			
		||||
                    backButton = $("<button id='back-button'>Back</button>")
 | 
			
		||||
                        .appendTo(overlayContainer);
 | 
			
		||||
 | 
			
		||||
                    // Create custom video controls container
 | 
			
		||||
                    customControls = $("<div id='custom-video-controls'></div>").appendTo(overlayContainer);
 | 
			
		||||
                    playPauseButton = $("<button id='custom-play-pause-button'>❚❚</button>").appendTo(customControls); // Assign to global var
 | 
			
		||||
                    seekBar = $("<input type='range' id='custom-seek-bar' value='0' />").appendTo(customControls); // Assign to global var
 | 
			
		||||
 | 
			
		||||
                    // Remove local inactivityTimer declaration, it's global now
 | 
			
		||||
                    // let inactivityTimer;
 | 
			
		||||
 | 
			
		||||
                    // --- Setup Inactivity Controls ---
 | 
			
		||||
                    overlayContainer.on('mousemove.inactivityControls', showControlsAndResetTimer);
 | 
			
		||||
					// --- Setup Inactivity Controls (session-specific) ---
 | 
			
		||||
					showControlsAndResetTimer(); // Initial call
 | 
			
		||||
 | 
			
		||||
                    // --- Setup Custom Video Controls ---
 | 
			
		||||
                    playPauseButton.click(handlePlayPauseClick);
 | 
			
		||||
                    $(videoElement).on('play', updatePlayPauseButtonState);
 | 
			
		||||
                    $(videoElement).on('pause', updatePlayPauseButtonState);
 | 
			
		||||
                    $(videoElement).on('loadedmetadata', handleVideoLoadedMetadata);
 | 
			
		||||
                    $(videoElement).on('timeupdate', handleVideoTimeUpdate);
 | 
			
		||||
                    seekBar.on('input', handleSeekBarInput);
 | 
			
		||||
                    // Initial call to set play/pause button state, especially if autoplay is effective
 | 
			
		||||
                    updatePlayPauseButtonState();
 | 
			
		||||
					updatePlayPauseButtonState(); // Initial state for the button
 | 
			
		||||
 | 
			
		||||
                    // --- Setup Video End and Back Button ---
 | 
			
		||||
                    $(videoElement).on('ended', exitVideoPlayback);
 | 
			
		||||
                    backButton.click(exitVideoPlayback);
 | 
			
		||||
 | 
			
		||||
                    // --- Setup Fullscreen Change Listener ---
 | 
			
		||||
                    // Detach any previous listeners first (using the specific named handler for accuracy if needed, but general works too)
 | 
			
		||||
                    $(document).off('fullscreenchange.videoPlayback webkitfullscreenchange.videoPlayback mozfullscreenchange.videoPlayback MSFullscreenChange.videoPlayback');
 | 
			
		||||
                    $(document).on('fullscreenchange.videoPlayback webkitfullscreenchange.videoPlayback mozfullscreenchange.videoPlayback MSFullscreenChange.videoPlayback', handleFullscreenChange);
 | 
			
		||||
 | 
			
		||||
                    // --- Request Fullscreen ---
 | 
			
		||||
					// Show overlay and request fullscreen
 | 
			
		||||
					overlayContainer.css('display', 'flex');
 | 
			
		||||
					const containerEl = overlayContainer[0];
 | 
			
		||||
					if (containerEl.requestFullscreen) {
 | 
			
		||||
                        containerEl.requestFullscreen();
 | 
			
		||||
                    } else if (containerEl.mozRequestFullScreen) { /* Firefox */
 | 
			
		||||
						containerEl.requestFullscreen().catch(err => console.error("Error attempting to enable full-screen mode:", err));
 | 
			
		||||
					} else if (containerEl.mozRequestFullScreen) {
 | 
			
		||||
						containerEl.mozRequestFullScreen();
 | 
			
		||||
                    } else if (containerEl.webkitRequestFullscreen) { /* Chrome, Safari & Opera */
 | 
			
		||||
					} else if (containerEl.webkitRequestFullscreen) {
 | 
			
		||||
						containerEl.webkitRequestFullscreen();
 | 
			
		||||
                    } else if (containerEl.msRequestFullscreen) { /* IE/Edge */
 | 
			
		||||
					} else if (containerEl.msRequestFullscreen) {
 | 
			
		||||
						containerEl.msRequestFullscreen();
 | 
			
		||||
					}
 | 
			
		||||
                    // NOTE: exitVideoPlayback was defined above.
 | 
			
		||||
                    // The original snippet showed its definition here, it's now grouped with other helpers.
 | 
			
		||||
 | 
			
		||||
					videoElement.play().catch(err => console.error("Error attempting to play video:", err));
 | 
			
		||||
				}
 | 
			
		||||
			});
 | 
			
		||||
 | 
			
		||||
            // --- Setup Custom Video Controls Events (session-specific for video element) ---
 | 
			
		||||
            $(videoElement).on('play.currentVideo', updatePlayPauseButtonState);
 | 
			
		||||
            $(videoElement).on('pause.currentVideo', updatePlayPauseButtonState);
 | 
			
		||||
            $(videoElement).on('loadedmetadata.currentVideo', handleVideoLoadedMetadata);
 | 
			
		||||
            $(videoElement).on('timeupdate.currentVideo', handleVideoTimeUpdate);
 | 
			
		||||
            $(videoElement).on('ended.currentVideo', exitVideoPlayback);
 | 
			
		||||
 | 
			
		||||
            // --- Setup Fullscreen Change Listener (session-specific) ---
 | 
			
		||||
            $(document).off('fullscreenchange.videoPlayback webkitfullscreenchange.videoPlayback mozfullscreenchange.videoPlayback MSFullscreenChange.videoPlayback', handleFullscreenChange); // Ensure clean state
 | 
			
		||||
            $(document).on('fullscreenchange.videoPlayback webkitfullscreenchange.videoPlayback mozfullscreenchange.videoPlayback MSFullscreenChange.videoPlayback', handleFullscreenChange);
 | 
			
		||||
 | 
			
		||||
            overlayContainer.on('mousemove.inactivityControls', showControlsAndResetTimer);
 | 
			
		||||
		})
 | 
			
		||||
	</script>
 | 
			
		||||
</head>
 | 
			
		||||
@@ -339,5 +344,15 @@
 | 
			
		||||
		<img id="next-thumb" class="video-thumbnail side-video" alt="Next Video">
 | 
			
		||||
		<div class="play-button" id="play-button"></div>
 | 
			
		||||
	</div>
 | 
			
		||||
 | 
			
		||||
	<!-- STATIC VIDEO OVERLAY STRUCTURE -->
 | 
			
		||||
	<div id="video-overlay-container">
 | 
			
		||||
		<video class="video-player"></video> <!-- no src, no autoplay, no controls initially -->
 | 
			
		||||
		<button id="back-button">Back</button>
 | 
			
		||||
		<div id="custom-video-controls">
 | 
			
		||||
			<button id="custom-play-pause-button">►</button> <!-- Default to Play icon -->
 | 
			
		||||
			<input type="range" id="custom-seek-bar" value="0" />
 | 
			
		||||
		</div>
 | 
			
		||||
	</div>
 | 
			
		||||
</body>
 | 
			
		||||
</html>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user