Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #1 from jseros/patch-1

Optimizations
  • Loading branch information...
commit 3b21bc0cfcd344a235d6c67503829a014134505c 2 parents b368d2b + 66b764e
Juan Pablo Buritica authored
Showing with 19 additions and 36 deletions.
  1. +19 −36 js/async-media-load.js
View
55 js/async-media-load.js
@@ -5,36 +5,20 @@
/*var defaults = {src: 'youtube'};
var opts = $.extend(defaults, options);*/
- var youtube_regex = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/;
- var vimeo_regex = /^.*vimeo\.com\/(\d+).*/;
+ var regex = {
+ youtube: /^.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/,
+ vimeo: /^.*vimeo\.com\/(\d+).*/
+ };
var getProvider = function(href)
{
- var vimeo = href.indexOf('vimeo.com');
- if (vimeo >= 0){
- return 'vimeo'
- }else{
- return 'youtube';
- }
+ return href.indexOf('vimeo.com') >= 0 ? 'vimeo' : 'youtube';
}
var getVideoId = function(href, provider)
{
- if (provider == 'vimeo'){
- var match = href.match(vimeo_regex);
- if (match){
- return match[1];
- }else{
- //error
- }
- }else{
- var match = href.match(youtube_regex);
- if (match && match[2].length==11){
- return match[2];
- }else{
- //error
- }
- }
+ var match = href.match( regex[provider] );
+ return (match && match[1]) || null;
}
var insertImg = function($a, img_src, width, height)
@@ -45,8 +29,7 @@
var img_top = (height - img_height)/2;
var $img = $(this);
$img.width(width).height(img_height).css({position: 'absolute', top: img_top+'px', left: '0'});
- $a.css({position: 'relative'});
- $a.parent().css({overflow: 'hidden'});
+ $a.css({position: 'relative'}).parent().css({overflow: 'hidden'});
$a.append($img);
}
image.src = img_src;
@@ -75,30 +58,30 @@
{
e.preventDefault();
+ var embed_url = '';
+
if (e.data.provider == 'vimeo'){
- var embed_url = 'http://player.vimeo.com/video/'+e.data.id+'?autoplay=1';
+ embed_url = 'http://player.vimeo.com/video/'+e.data.id+'?autoplay=1';
}else{
- var embed_url = 'http://www.youtube.com/embed/'+e.data.id+'?rel=0&autoplay=1';
+ embed_url = 'http://www.youtube.com/embed/'+e.data.id+'?rel=0&autoplay=1';
}
var $embed = $('<iframe width="'+e.data.width+'" height="'+e.data.height+'" src="'+embed_url+'" frameborder="0" allowfullscreen></iframe>');
$(this).replaceWith($embed);
}
- return this.each(function()
- {
- var href = $(this).attr('href');
+ return this.each(function(){
+ var $this = $(this);
+ var href = $this.attr('href');
var provider = getProvider(href);
-
var video_id = getVideoId(href, provider);
-
- var width = $(this).parent().width();
- var height = $(this).parent().height();
+ var width = $this.parent().width();
+ var height = $this.parent().height();
if (provider == 'vimeo'){
- getVideoDetails($(this), video_id, width, height);
+ getVideoDetails($this, video_id, width, height);
}else{
var img_src = 'http://img.youtube.com/vi/'+video_id+'/hqdefault.jpg';
- insertImg($(this), img_src, width, height);
+ insertImg($this, img_src, width, height);
}
$(this).on('click', {width: width, height: height, id: video_id, provider: provider}, playVid);
Please sign in to comment.
Something went wrong with that request. Please try again.