Created
October 15, 2012 19:01
-
-
Save bsturdivan/3894413 to your computer and use it in GitHub Desktop.
mediaResize - Uncommented
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(['jquery', 'pkg.utils', 'plugins/widget'], function($, utils) { | |
$.widget("cbsi.mediaResize", $.cbsi.widget, { | |
options: { | |
mediaObjects: null; | |
}, | |
//VARS | |
hasEvents: false, //Events have been added | |
selectors: [ | |
"iframe", | |
"object", | |
"embed" | |
], //Elements to resize | |
//DOM | |
$mediaObjects: null, //Object of found selectors | |
$window: $(window), | |
_create: function() { | |
var self = this; | |
this.mediaObjects = self.options.mediaObjects || this.element.find(this.selectors.join(',')); | |
this.resizeMedia(this.mediaObjects); | |
this._addEvents(); | |
}, | |
_addEvents: function() { | |
if(this.hasEvents) { | |
return; | |
} | |
this.hasEvents = true; | |
this.$window.on('orientationchange.mediaResize', function(e) { | |
self.resizeMedia(this.mediaObjects); | |
}); | |
}, | |
_removeEvents: function() { | |
this.hasEvents = false; | |
this.$window.off('.mediaResize'); | |
}, | |
resizeMedia: function(mediaObjects) { | |
var winWidth = $(window).width(), | |
numVideos = mediaObjects.length, | |
container, | |
aspectRatio, | |
originalVideoWidth, | |
originalVideoHeight, | |
bodyPos, | |
newEl; | |
if(numVideos < 1) { return false; } | |
for(;numVideos -- > 0;) { | |
container = $('<div/>').css({ | |
padding: 0, | |
position: 'relative', | |
width: '100%' | |
}); | |
originalVideoWidth = mediaObjects[numVideos].getAttribute('width'); | |
originalVideoHeight = mediaObjects[numVideos].getAttribute('height'); | |
aspectRatio = originalVideoHeight / originalVideoWidth; | |
bodyPos = mediaObjects[numVideos].parentNode; | |
newEl = $(mediaObjects[numVideos].cloneNode(false)); | |
newEl = $(newEl); | |
if(winWidth > originalVideoWidth) { | |
continue; | |
} | |
container.css({ paddingTop: (aspectRatio * 100)+"%" }); | |
newEl.css({ | |
height: '100%', | |
left: 0, | |
maxHeight: originalVideoHeight+'px', | |
maxWidth: originalVideoWidth+'px', | |
position: 'absolute', | |
top: 0, | |
width: '100%' | |
}).removeAttr('height').removeAttr('width'); | |
container.html(newEl); | |
bodyPos.replaceChild(container.get(0), mediaObjects[numVideos]); | |
container = null; | |
} | |
}, | |
destroy: function() { | |
this._removeEvents(); | |
$.Widget.prototype.destroy.call(this); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment