// Themify Lightbox and Fullscreen /////////////////////////
var ThemifyGallery = {};
(function($){
'use strict';
ThemifyGallery = {
origHash:'',
config: {
fullscreen: themifyScript.lightbox.fullscreenSelector,
lightbox: themifyScript.lightbox.lightboxSelector,
lightboxGallery: themifyScript.lightbox.gallerySelector,
lightboxContentImages: themifyScript.lightbox.lightboxContentImagesSelector,
is_touch:$('body').hasClass('touch'),
context: document
},
init: function(config){
ThemifyGallery.origHash = window.location.hash;
if (config && typeof config == 'object') {
$.extend(ThemifyGallery.config, config);
}
if (config.extraLightboxArgs && typeof config == 'object') {
for (var attrname in config.extraLightboxArgs) {
themifyScript.lightbox[attrname] = config.extraLightboxArgs[attrname];
}
}
this.parseArgs();
this.doLightbox();
this.openAnchor();
},
parseArgs: function(){
$.each(themifyScript.lightbox, function(index, value){
if( 'false' == value || 'true' == value ){
themifyScript.lightbox[index] = 'false'!=value;
} else if( parseInt(value) ){
themifyScript.lightbox[index] = parseInt(value);
} else if( parseFloat(value) ){
themifyScript.lightbox[index] = parseFloat(value);
}
});
},
doLightbox: function(){
var context = this.config.context,
patterns = {},
socialSharing = '
';
if(typeof $.fn.magnificPopup !== 'undefined' && typeof themifyScript.lightbox.lightboxOn !== 'undefined') {
// Lightbox Link
$(context).on('click', ThemifyGallery.config.lightbox, function(event){
event.preventDefault();
if ( $('.mfp-wrap.mfp-gallery').length ) return;
var $self = $(this),
targetItems,
$link = ( $self.find( '> a' ).length > 0 ) ? $self.find( '> a' ).attr( 'href' ) : $self.attr('href'),
$type = ThemifyGallery.getFileType($link),
$is_video = ThemifyGallery.isVideo($link),
$groupItems = $type === 'inline' || $type === 'iframe' ? [] : ($self.data('rel')?$('a[data-rel="'+$self.data('rel')+'"]'):$self.closest( '.themify_builder_row, .loops-wrapper' ).find( '.themify_lightbox > img' ).parent()),
index = $groupItems.length > 1 ? $groupItems.index( this ) : 0,
$title = (typeof $(this).children('img').prop('alt') !== 'undefined') ? $(this).children('img').prop('alt') : $(this).prop('title'),
$iframe_width = $is_video ? '100%' : (ThemifyGallery.getParam('width', $link)) ? ThemifyGallery.getParam('width', $link) : '94%',
$iframe_height = $is_video ? '100%' : (ThemifyGallery.getParam('height', $link)) ? ThemifyGallery.getParam('height', $link) : '100%';
if($iframe_width.indexOf("%") === -1) $iframe_width += 'px';
if($iframe_height.indexOf("%") === -1) $iframe_height += 'px';
if($is_video){
if( ThemifyGallery.isYoutube( $link ) ) {
var params = ThemifyGallery.getCustomParams( $link );
// YouTube URL pattern
if( params ) {
patterns.youtube = {
id: 'v=',
index: 'youtube.com/',
src: '//www.youtube.com/embed/%id%' + params
};
}
// YouTube sanitize the URL properly
$link = ThemifyGallery.getYoutubePath( $link );
} else if( ThemifyGallery.isVimeo( $link ) ) {
var params = ThemifyGallery.getCustomParams( $link );
// Vimeo URL pattern
if( params ) {
patterns.vimeo = {
id: '/',
index: 'vimeo.com/',
src: '//player.vimeo.com/video/%id%' + params
};
}
$link = $link.split('?')[0];
}
}
if( $groupItems.length > 1 && index !== -1 ) {
targetItems = [];
$groupItems.each( function( i, el ) {
targetItems.push( {
src: ThemifyGallery.getiFrameLink( $(el).prop( 'href' ) ),
title: (typeof $(el).find('img').prop('alt') !== 'undefined') ? $(el).find('img').prop('alt') : '',
type: ThemifyGallery.getFileType( $(el).prop( 'href' ) )
} );
} );
// remove duplicate items (same "src" attr) from the lightbox group
var targetItems = targetItems.reduce( function( memo, e1 ) {
var matches = memo.filter( function( e2 ) {
return e1.src == e2.src;
} );
if ( matches.length == 0 )
memo.push( e1 );
return memo;
}, [] );
} else {
index = 0; // ensure index is set to 0 so the proper popup shows
targetItems = {
src: ThemifyGallery.getiFrameLink( $link ),
title: $title,
};
}
var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream,
iOSScrolling = iOS ? 'scrolling="no" ' : '';
var titleWrap = 'no' !== this.dataset.title ? '':'';
var $args = {
items: targetItems,
type: $type,
image: {
markup: '',
},
iframe: {
markup: ''+
'
'+
'
'+
''+
'
'+
socialSharing +
'
',
patterns: patterns
},
callbacks: {
open: function() {
ThemifyGallery.updateHash('open',this);
ThemifyGallery.openSharing(this);
var zoomConfig = $self.data( 'zoom-config' ),
cssRules = {};
if( !zoomConfig ) { return; }
zoomConfig = zoomConfig.split( '|' );
if( typeof zoomConfig[0] !== 'undefined' ) {
cssRules.width = zoomConfig[0];
}
if( typeof zoomConfig[1] !== 'undefined' ) {
cssRules.height = zoomConfig[1];
}
$(this.content).parent().css( cssRules );
},
change:function () {
ThemifyGallery.updateHash('open',this);
},
close:function () {
ThemifyGallery.updateHash('close');
}
}
};
if( $groupItems.length > 1 ) {
$.extend( $args, {
gallery: {
enabled: true
}
} );
}
if($self.find('img').length > 0) {
$.extend( $args, {
mainClass: 'mfp-with-zoom',
zoom: {
enabled: !ThemifyGallery.config.is_touch,
duration: 300,
easing: 'ease-in-out',
opener: function() {
return $self.find('img');
}
}
});
}
if($is_video){
$args['mainClass'] += ' video-frame';
} else {
$args['mainClass'] += ' standard-frame';
}
if(ThemifyGallery.isInIframe()) {
window.parent.jQuery.magnificPopup.open($args);
} else {
$.magnificPopup.open($args, index);
}
});
// Images in post content
$(themifyScript.lightbox.contentImagesAreas, context).each(function() {
var images = [],
links = [];
if(themifyScript.lightbox.lightboxContentImages && themifyScript.lightbox.lightboxGalleryOn){
$(ThemifyGallery.config.lightboxContentImages, $(this)).filter( function(){
if(!$(this).parent().hasClass('gallery-icon') && !$(this).hasClass('themify_lightbox')){
links.push($(this));
var description = $(this).prop('title');
if($(this).next('.wp-caption-text').length > 0){
// If there's a caption set for the image, use it
description = $(this).next('.wp-caption-text').html();
} else {
// Otherwise, see if there's an alt attribute set
description = $(this).children('img').prop('alt');
}
images.push({ src: $(this).prop('href'), title: description, type: 'image' });
return $(this);
}
}).each(function(index) {
if (links.length > 0) {
$(this).on('click', function(event){
event.preventDefault();
var $self = $(this),
titleWrap = 'no' !== this.dataset.title ? '':'';
$args = {
items: {
src: images[index].src,
title: images[index].title
},
image: {
markup: '',
},
type: 'image',
callbacks: {
open: function() {
ThemifyGallery.updateHash('open',this);
ThemifyGallery.openSharing(this);
},
change:function () {
ThemifyGallery.updateHash('open',this);
},
close:function () {
ThemifyGallery.updateHash('close');
}
}
};
if($self.find('img').length > 0) {
$.extend( $args, {
mainClass: 'mfp-with-zoom',
zoom: {
enabled: !ThemifyGallery.config.is_touch,
duration: 300,
easing: 'ease-in-out',
opener: function() {
return $self.find('img');
}
}
});
}
if(ThemifyGallery.isInIframe()) {
window.parent.jQuery.magnificPopup.open($args);
} else {
$.magnificPopup.open($args);
}
});
}
});
}
});
// Images in WP Gallery
if(themifyScript.lightbox.lightboxGalleryOn){
$('body').on('click', ThemifyGallery.config.lightboxGallery, function(event){
if( 'image' !== ThemifyGallery.getFileType( $(this).prop( 'href' ) ) ) {
return;
}
event.preventDefault();
var $gallery = $( ThemifyGallery.config.lightboxGallery, $( this ).closest( '.module, .gallery' ) ),
images = [];
$gallery.each(function() {
var description = $(this).prop('title');
if($(this).parent().next('.gallery-caption').length > 0){
// If there's a caption set for the image, use it
description = $(this).parent().next('.wp-caption-text').html();
} else if ( $(this).children('img').length > 0 ) {
// Otherwise, see if there's an alt attribute set
description = $(this).children('img').prop('alt');
} else if ( $(this).find('.gallery-caption').find('.entry-content').length > 0 ) {
description = $(this).find('.gallery-caption').find('.entry-content').text();
}
images.push({ src: $(this).prop('href'), title: description, type: 'image' });
});
var titleWrap = 'no' !== this.dataset.title ? '':'';
var $args = {
gallery: {
enabled: true
},
image: {
markup: '',
},
items: images,
mainClass: 'mfp-with-zoom',
zoom: {
enabled: !ThemifyGallery.config.is_touch,
duration: 300,
easing: 'ease-in-out',
opener: function(openerElement) {
var imageEl = $($gallery[openerElement.index]);
return imageEl.is('img') ? imageEl : imageEl.find('img');
}
},
callbacks: {
open: function() {
ThemifyGallery.updateHash('open',this);
ThemifyGallery.openSharing(this);
},
change:function () {
ThemifyGallery.updateHash('open',this);
},
close:function () {
ThemifyGallery.updateHash('close');
}
}
};
if(ThemifyGallery.isInIframe()){
window.parent.jQuery.magnificPopup.open($args, $gallery.index(this));
} else {
$.magnificPopup.open($args, $gallery.index(this));
}
});
}
}
},
countItems : function(type){
var context = this.config.context;
if('lightbox' === type) return $(this.config.lightbox, context).length + $(this.config.lightboxGallery, context).length + $(ThemifyGallery.config.lightboxContentImages, context).length;
else return $(this.config.fullscreen, context).length + $(ThemifyGallery.config.lightboxContentImages, context).length;
},
isInIframe: function(){
if( typeof ThemifyGallery.config.extraLightboxArgs !== 'undefined' ) {
return typeof ThemifyGallery.config.extraLightboxArgs.displayIframeContentsInParent !== 'undefined' && true == ThemifyGallery.config.extraLightboxArgs.displayIframeContentsInParent;
} else {
return false;
}
},
getFileType: function( itemSrc ) {
if ( itemSrc.match( /\.(gif|jpg|jpeg|tiff|png)(\?fit=\d+(,|%2C)\d+)?$/i ) ) { // ?fit is added by JetPack
return 'image';
} else if(itemSrc.match(/\bajax=true\b/i)) {
return 'ajax';
} else if(itemSrc.substr(0,1) === '#') {
return 'inline';
} else {
return 'iframe';
}
},
isVideo: function( itemSrc ) {
return ThemifyGallery.isYoutube( itemSrc )
|| ThemifyGallery.isVimeo(itemSrc) || itemSrc.match(/\b.mov\b/i)
|| itemSrc.match(/\b.swf\b/i);
},
isYoutube : function( itemSrc ) {
return itemSrc.match( /youtube\.com\/watch/i ) || itemSrc.match( /youtu\.be/i );
},
isVimeo : function( itemSrc ) {
return itemSrc.match(/vimeo\.com/i)
},
getYoutubePath : function( url ) {
if( url.match( /youtu\.be/i ) ) {
// convert youtu.be/ urls to youtube.com
return '//youtube.com/watch?v=' + url.match( /youtu\.be\/([^\?]*)/i )[1];
} else {
return '//youtube.com/watch?v=' + ThemifyGallery.getParam( 'v', url );
}
},
/**
* Add ?iframe=true to the URL if the lightbox is showing external page
* this enables us to detect the page is in an iframe in the server
*/
getiFrameLink : function( link ) {
if( ThemifyGallery.getFileType( link ) === 'iframe' && ThemifyGallery.isVideo( link ) === null ) {
link = Themify.UpdateQueryString( 'iframe', 'true', link )
}
return link;
},
getParam: function( name, url ) {
name = name.replace( /[\[]/, "\\\[" ).replace( /[\]]/, "\\\]" );
var regexS = "[\\?&]" + name + "=([^]*)",
regex = new RegExp( regexS ),
results = regex.exec( url );
return results == null ? "" : results[1];
},
getCustomParams: function( url ) {
var params = url.split( '?' )[1];
params = params ? '&' + params.replace( /[\\?&]?(v|autoplay)=[^]*/g, '' ).replace( /^&/g, '' ) : '';
return '?autoplay=1' + params;
},
openSharing: function(self){
// Load Themify Icons for social sharing icons
Themify.LoadCss(themify_vars.url + '/themify-icons/themify-icons.css', themify_vars.version);
self.content[0].getElementsByClassName('tb_social_sharing')[0].addEventListener('click',function(e){
var type = e.target.dataset['type'],
url = self.currItem.data.src,
title = self.currItem.data.title,
width = 550,
height = 300,
leftPosition,
topPosition;
url = url.replace('?iframe=true','');
leftPosition = (window.screen.width / 2) - ( ( width / 2 ) + 10 );
topPosition = (window.screen.height / 2) - (( height / 2 ) + 50);
var windowFeatures = 'status=no,height='+height+',width='+width+',resizable=yes,left='+leftPosition+',top='+topPosition+',screenX=#{left},screenY=#{top},toolbar=no,menubar=no,scrollbars=no,location=no,directories=no';
if('facebook' === type){
url = 'https://www.facebook.com/sharer.php?u='+url;
} else if ( 'twitter' === type) {
url = 'http://twitter.com/share?url='+url+'&text='+title;
} else if ( 'email' === type) {
url = 'mailto:?subject=Check%20this%20out!&body=' + title + ' - ' + url;
}
var win = window.open(url, 'sharer', windowFeatures);
win.moveTo(leftPosition, topPosition);
return false;
});
},
updateHash: function(action,instance){
var hash = 'open' === action ? instance.currItem.data.title : ThemifyGallery.origHash;
// Escape HTML
if('open' === action){
var div = document.createElement('div');
div.innerHTML=hash;
hash = div.textContent.trim();
div = null;
}
window.location.hash = hash;
},
openAnchor: function(){
if('' !== window.location.hash){
var hash = decodeURI(window.location.hash.substring(1)),
el = document.querySelector('[alt="'+hash+'"]');
el = null === el ? document.querySelector('[title="'+hash+'"]') : el;
if(null !== el){
el.click();
}
}
}
};
}(jQuery));