Skip to content

Commit 562a6f4

Browse files
author
Julian Xhokaxhiu
committed
New way to handle video URLs by preserving querystrings.
Basically here we just deserialize the QS to a plain Object, then we play with it changing values then we serialize it back again to place it inside the iframe.
1 parent 04d4fd3 commit 562a6f4

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

src/js/jquery.swipebox.js

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
afterOpen: null,
1919
afterClose: null,
2020
loopAtEnd: false,
21-
autoplayVideos: false
21+
autoplayVideos: false,
22+
queryStringData: {}
2223
},
2324

2425
plugin = this,
@@ -727,27 +728,63 @@
727728

728729
},
729730

731+
/**
732+
* Parse URI querystring and:
733+
* - overrides value provided via dictionary
734+
* - rebuild it again returning a string
735+
*/
736+
parseUri : function (uri, customData) {
737+
var a = document.createElement('a'),
738+
qs = {};
739+
740+
// Decode the URI
741+
a.href = decodeURIComponent( uri );
742+
743+
// QueryString to Object
744+
qs = JSON.parse( a.search.toLowerCase().replace('?','').replace(/&/g,'","').replace(/=/g,'":"') );
745+
746+
// Extend with custom data
747+
if ( $.isPlainObject( customData ) ) {
748+
qs = $.extend( qs, customData, plugin.settings.queryStringData ); // The dev has always the final word
749+
}
750+
751+
// Return querystring as a string
752+
return $
753+
.map( qs, function (val, key) {
754+
if ( val && val > '' ) {
755+
return encodeURIComponent( key ) + '=' + encodeURIComponent( val );
756+
}
757+
})
758+
.join('&');
759+
},
760+
730761
/**
731762
* Get video iframe code from URL
732763
*/
733764
getVideo : function( url ) {
734765
var iframe = '',
735766
youtubeUrl = url.match( /watch\?v=([a-zA-Z0-9\-_]+)/ ),
736767
youtubeShortUrl = url.match(/youtu\.be\/([a-zA-Z0-9\-_]+)/),
737-
vimeoUrl = url.match( /vimeo\.com\/([0-9]*)/ );
768+
vimeoUrl = url.match( /vimeo\.com\/([0-9]*)/ ),
769+
qs = '';
738770
if ( youtubeUrl || youtubeShortUrl) {
739771
if ( youtubeShortUrl ) {
740772
youtubeUrl = youtubeShortUrl;
741773
}
742-
iframe = '<iframe width="560" height="315" src="//www.youtube.com/embed/' + youtubeUrl[1] + '?autoplay='+ plugin.settings.autoplayVideos + '" frameborder="0" allowfullscreen></iframe>';
774+
qs = ui.parseUri( url, {
775+
'autoplay' : ( plugin.settings.autoplayVideos ? '1' : '0' ),
776+
'v' : ''
777+
});
778+
iframe = '<iframe width="560" height="315" src="//www.youtube.com/embed/' + youtubeUrl[1] + '?' + qs + '" frameborder="0" allowfullscreen></iframe>';
743779

744780
} else if ( vimeoUrl ) {
745-
746-
iframe = '<iframe width="560" height="315" src="//player.vimeo.com/video/' + vimeoUrl[1] + '?byline=0&amp;portrait=0&amp;color=' + plugin.settings.vimeoColor + '&autoplay=' + plugin.settings.autoplayVideos + '" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
747-
748-
}
749-
750-
if ( youtubeUrl || youtubeShortUrl || vimeoUrl ) {
781+
qs = ui.parseUri( url, {
782+
'autoplay' : ( plugin.settings.autoplayVideos ? '1' : '0' ),
783+
'byline' : '0',
784+
'portrait' : '0',
785+
'color': plugin.settings.vimeoColor
786+
});
787+
iframe = '<iframe width="560" height="315" src="//player.vimeo.com/video/' + vimeoUrl[1] + '?' + qs + '" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
751788

752789
} else {
753790
iframe = '<iframe width="560" height="315" src="' + url + '" frameborder="0" allowfullscreen></iframe>';

0 commit comments

Comments
 (0)