-
Notifications
You must be signed in to change notification settings - Fork 430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Video Support #137
base: dev
Are you sure you want to change the base?
Added Video Support #137
Changes from 23 commits
dc68d00
a84b2bb
a341970
6b5ec5f
e4b95f7
662130e
fd0bb45
ef8639d
1ef2c62
24935f8
7f5f62d
9827746
3828e6d
9a17e2b
8f558e4
4f05c46
8d6b47d
8f9faf1
0034778
c49d9f4
87ac750
39e4152
12b0eeb
af7da8b
250e5a3
acf93a1
f8d7c97
44e4819
ef25913
a4cb9b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
|
||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,8 +62,10 @@ | |
var touch = {}; | ||
// If set to true ignore touch events because animation was already fired | ||
var touchFlag = false; | ||
// Regex pattern to match image files | ||
var regex = /.+\.(gif|jpe?g|png|webp)/i; | ||
// Regex pattern to match image & video files | ||
var regex = /.+\.(gif|jpe?g|png|webp|mp4|webm)/i; | ||
// Pattern to match only videos | ||
var videoRegex = /.+\.(mp4|webm)/i; | ||
// Object of all used galleries | ||
var data = {}; | ||
// Array containing temporary images DOM elements | ||
|
@@ -467,12 +469,27 @@ | |
options.afterHide(); | ||
} | ||
}, 500); | ||
|
||
pauseAnyVideoPlaying(); | ||
|
||
documentLastFocus.focus(); | ||
} | ||
|
||
function pauseAnyVideoPlaying(){ | ||
[].forEach.call(imagesElements, function(imageElement) { | ||
if(imageElement.getElementsByTagName('video').length > 0){ | ||
imageElement.getElementsByTagName('video')[0].pause(); | ||
} | ||
}); | ||
} | ||
|
||
function loadImage(index, callback) { | ||
var imageContainer = imagesElements[index]; | ||
var galleryItem = currentGallery[index]; | ||
var isVideo = false; | ||
if(imageContainer !== undefined) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing space/ Also better use |
||
isVideo = videoRegex.test(galleryItem.imageElement.href); | ||
} | ||
|
||
// Return if the index exceeds prepared images in the overlay | ||
// or if the current gallery has been changed / closed | ||
|
@@ -481,7 +498,15 @@ | |
} | ||
|
||
// If image is already loaded run callback and return | ||
if (imageContainer.getElementsByTagName('img')[0]) { | ||
if (imageContainer.getElementsByTagName('img')[0] && !isVideo) { | ||
if (callback) { | ||
callback(); | ||
} | ||
return; | ||
} | ||
|
||
// If video is already loaded run callback and return | ||
if (imageContainer.getElementsByTagName('video')[0] && isVideo) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the same as above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you please explain what is problem here, i don't understand There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can merge this condition with the previous one instead of duplicating. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks merged |
||
if (callback) { | ||
callback(); | ||
} | ||
|
@@ -490,11 +515,23 @@ | |
|
||
// Get element reference, optional caption and source path | ||
var imageElement = galleryItem.imageElement; | ||
var thumbnailElement = imageElement.getElementsByTagName('img')[0]; | ||
var thumbnailElement = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could change this whole thing into a one liner var thumbnailElement = isvideo ? imageElement.getElementsByTagName('video')[0] : imageElement.getElementsByTagName('img')[0]; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you fixing |
||
if(isVideo) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing space afyer |
||
thumbnailElement = imageElement.getElementsByTagName('video')[0]; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
else { | ||
thumbnailElement = imageElement.getElementsByTagName('img')[0]; | ||
} | ||
var imageCaption = typeof options.captions === 'function' ? | ||
options.captions.call(currentGallery, imageElement) : | ||
imageElement.getAttribute('data-caption') || imageElement.title; | ||
var imageSrc = getImageSrc(imageElement); | ||
var imageSrc = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if(isVideo) { | ||
imageSrc = getVideoSrc(imageElement); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should use |
||
else { | ||
imageSrc = getImageSrc(imageElement); | ||
} | ||
|
||
// Prepare figure element | ||
var figure = create('figure'); | ||
|
@@ -512,29 +549,81 @@ | |
} | ||
imageContainer.appendChild(figure); | ||
|
||
// Prepare gallery img element | ||
var image = create('img'); | ||
image.onload = function() { | ||
// Remove loader element | ||
var spinner = document.querySelector('#baguette-img-' + index + ' .baguetteBox-spinner'); | ||
figure.removeChild(spinner); | ||
if (!options.async && callback) { | ||
callback(); | ||
if(isVideo){ | ||
// Prepare gallery video element | ||
var video = create('video'); | ||
//video.onload = function(){ | ||
video.addEventListener('loadeddata', function() { | ||
//Remove loader element | ||
var spinner = document.querySelector('#baguette-img-' + index + ' .baguetteBox-spinner'); | ||
figure.removeChild(spinner); | ||
if (!options.async && callback) { | ||
callback(); | ||
} | ||
}); | ||
var source = create('source'); | ||
source.setAttribute('src', imageSrc); | ||
video.appendChild(source); | ||
if (options.titleTag && imageCaption) { | ||
video.title = imageCaption; | ||
} | ||
}; | ||
image.setAttribute('src', imageSrc); | ||
image.alt = thumbnailElement ? thumbnailElement.alt || '' : ''; | ||
if (options.titleTag && imageCaption) { | ||
image.title = imageCaption; | ||
figure.appendChild(video); | ||
} | ||
else | ||
{ | ||
// Prepare gallery img element | ||
var image = create('img'); | ||
image.onload = function() { | ||
// Remove loader element | ||
var spinner = document.querySelector('#baguette-img-' + index + ' .baguetteBox-spinner'); | ||
figure.removeChild(spinner); | ||
if (!options.async && callback) { | ||
callback(); | ||
} | ||
}; | ||
image.setAttribute('src', imageSrc); | ||
image.alt = thumbnailElement ? thumbnailElement.alt || '' : ''; | ||
if (options.titleTag && imageCaption) { | ||
image.title = imageCaption; | ||
} | ||
figure.appendChild(image); | ||
} | ||
figure.appendChild(image); | ||
|
||
// Run callback | ||
if (options.async && callback) { | ||
callback(); | ||
} | ||
} | ||
|
||
// Get video source location, mostly used for responsive images | ||
function getVideoSrc(image) { | ||
// Set default image path from href | ||
var result = image.getElementsByTagName('video')[0].getElementsByTagName('source')[0].src; | ||
// If dataset is supported find the most suitable image | ||
if (image.dataset) { | ||
var srcs = []; | ||
// Get all possible image versions depending on the resolution | ||
for (var item in image.dataset) { | ||
if (item.substring(0, 3) === 'at-' && !isNaN(item.substring(3))) { | ||
srcs[item.replace('at-', '')] = image.dataset[item]; | ||
} | ||
} | ||
// Sort resolutions ascending | ||
var keys = Object.keys(srcs).sort(function(a, b) { | ||
return parseInt(a, 10) < parseInt(b, 10) ? -1 : 1; | ||
}); | ||
// Get real screen resolution | ||
var width = window.innerWidth * window.devicePixelRatio; | ||
// Find the first image bigger than or equal to the current width | ||
var i = 0; | ||
while (i < keys.length - 1 && keys[i] < width) { | ||
i++; | ||
} | ||
result = srcs[keys[i]] || result; | ||
} | ||
return result; | ||
} | ||
|
||
// Get image source location, mostly used for responsive images | ||
function getImageSrc(image) { | ||
// Set default image path from href | ||
|
@@ -609,6 +698,7 @@ | |
} | ||
|
||
function updateOffset() { | ||
pauseAnyVideoPlaying(); | ||
var offset = -currentIndex * 100 + '%'; | ||
if (options.animation === 'fadeIn') { | ||
slider.style.opacity = 0; | ||
|
@@ -625,6 +715,9 @@ | |
slider.style.transform = slider.style.webkitTransform = 'translate3d(' + offset + ',0,0)' | ||
: slider.style.left = offset; | ||
} | ||
if(imagesElements[currentIndex].getElementsByTagName('video').length > 0) { | ||
imagesElements[currentIndex].getElementsByTagName('video')[0].play(); | ||
} | ||
} | ||
|
||
// CSS 3D Transforms test | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be here.