Skip to content

Commit

Permalink
Avoid multiple calls to this.responsive()
Browse files Browse the repository at this point in the history
Everyone has the same width and height...

Signed-off-by: Jessica González <suki@missallsunday.com>
  • Loading branch information
MissAllSunday committed May 29, 2016
1 parent 85252b4 commit f532e4b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
40 changes: 20 additions & 20 deletions Themes/default/scripts/ohyoutube.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,40 @@ var _oh = function(){

_oh.prototype.main = function(){

$this = this;
var $this = this;

$('.youtube').each(function(){

var videoID = this.id.replace('oh_',''),
imgsrc = $this.getImage(videoID),
imgHeight = $this.basedElement.height(),
imgWidth = $this.basedElement.width();
var _element = $(this);
_element.videoID = this.id.replace('oh_','');
_element.imgsrc = $this.getImage(_element.videoID);
_element.imgHeight = $this.basedElement.height();
_element.imgWidth = $this.basedElement.width();

if (typeof imgsrc !== 'undefined'){
$(this).css({'background-image': 'url('+ imgsrc +')', 'background-size': 'cover'});
if (typeof _element.imgsrc !== 'undefined'){
_element.css({'background-image': 'url('+ _element.imgsrc +')', 'background-size': 'cover'});
}

$(this).append($('<div/>', {'class': 'youtube_play'}));
_element.append($('<div/>', {'class': 'youtube_play'}));

$(this).one('click', function(){
var iframe_url = '//www.youtube.com/embed/' + videoID + '?autoplay=1&autohide=1';
_element.one('click', function(){
var iframe_url = '//www.youtube.com/embed/' + _element.videoID + '?autoplay=1&autohide=1';

if ($(this).data('params')){
iframe_url+='&'+$(this).data('params');
if (_element.data('params')){
iframe_url+='&'+ _element.data('params');
}

// The height and width of the iFrame should be the same as parent
var iframe = $('<iframe/>', {'frameborder': '0', 'src': iframe_url, 'width': imgWidth, 'height': imgHeight, 'allowfullscreen': ''});
var iframe = $('<iframe/>', {'frameborder': '0', 'src': iframe_url, 'width': _element.imgWidth, 'height': _element.imgHeight, 'allowfullscreen': 'allowfullscreen'});

// Append the YouTube HTML5 Player.
$(this).css({'background-image': 'none'}).append(iframe);
$(this).children('.youtube_play').css({'height': '0'});

// Gotta make sure the new iframe gets resized if needed.
$this.responsive();
_element.css({'background-image': 'none'}).append(iframe);
_element.children('.youtube_play').css({'height': '0'});
});
});

// Gotta make sure the new iframe gets resized if needed.
$this.responsive();
};

_oh.prototype.responsive = function()
Expand All @@ -58,9 +59,8 @@ _oh.prototype.responsive = function()

$(window).resize(function(){

// Get the new width and height.
var newWidth = $this.basedElement.width();
var newHeight = (newWidth * $this.aspectRatio) <= $this.defaultHeight ? (newWidth * $this.aspectRatio) : $this.defaultHeight;
newHeight = (newWidth * $this.aspectRatio) <= $this.defaultHeight ? (newWidth * $this.aspectRatio) : $this.defaultHeight;

// If the new width is lower than the "default width" then apply some resizing. No? then go back to our default sizes
var applyResize = (newWidth <= $this.defaultWidth),
Expand Down
5 changes: 3 additions & 2 deletions Themes/default/scripts/ohyoutube.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f532e4b

Please sign in to comment.