forked from saadan/embed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.easyembed.js
130 lines (105 loc) · 4.25 KB
/
jquery.easyembed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
;
(function ($, window, document) {
$.fn.easyEmbed = function (options) {
var $that = this;
// detect if device requires user interaction for playback
var mobile = /iPad|iPhone|iPod/.test(navigator.userAgent);
// translate shorthand
var shorthand = $that.data('easy-embed').split(':');
var settings = $.extend({
// general settings
id: ($that.data('id') || shorthand[1]) || 'ScMzIvxBSi4',
provider: ($that.data('provider') || shorthand[0]) || 'youtube',
width: $that.data('width') || 16,
height: $that.data('height') || 9,
// youtube settings
controls: $that.data('controls') || false,
showinfo: $that.data('showinfo') || false,
// vimeo settings
color: $that.data('color') || '00adef',
title: $that.data('title') || false,
byline: $that.data('byline') || false,
portrait: $that.data('portrait') || false,
}, options);
var getThumbnail = function (callback) {
switch (settings.provider.toLowerCase()) {
case 'youtube':
var base = '//img.youtube.com/vi/' + settings.id + '/';
var sizes = ['maxresdefault', 'hqdefault'];
(function getImage() {
var url = base + sizes[0] + '.jpg';
$("<img/>").attr('src', url).on("load", (function () {
if (this.width != 120 && this.height != 90) {
callback(url);
} else {
sizes.shift();
getImage();
}
}))
})();
break;
case 'vimeo':
//$.get('//vimeo.com/api/v2/video/' + settings.id + '.json', function (data) {
// callback(data[0].thumbnail_large);
//})
$.get('https://vimeo.com/api/oembed.json?url=http://vimeo.com/' + settings.id , function (data) {
callback(data.thumbnail_url);
})
break;
}
}
var getSource = function () {
switch (settings.provider.toLowerCase()) {
case 'youtube':
return '//youtube.com/embed/' + settings.id + '?rel=0&autoplay=1'
+ '&controls=' + (settings.controls + 0)
+ '&showinfo=' + (settings.showinfo + 0);
break;
case 'vimeo':
return '//player.vimeo.com/video/' + settings.id + '?autoplay=1'
+ '&color=' + settings.color
+ '&title=' + (settings.title + 0)
+ '&byline=' + (settings.byline + 0)
+ '&portrait=' + (settings.controls + 0);
break;
}
}
var setThumbnail = function (src) {
$that.css('background', 'black url(' + src + ') 50% 50% / cover no-repeat');
};
var setSize = function () {
$that.css('height', $that.width() / settings.width * settings.height);
}
var setIframe = function () {
$that.html($('<iframe>')
.attr('src', getSource())
.attr('width', '100%')
.attr('height', '100%')
.attr('frameborder', 0)
.attr('allowfullscreen', 1));
$that.addClass("playing-video");
}
setSize();
$(window).resize(function () {
setSize();
})
if (!mobile) {
getThumbnail(function (url) {
setThumbnail(url);
})
$that.find('*').addBack().click(function () {
setIframe();
});
} else {
setIframe();
}
return this;
};
$(document).ready(function () {
if ($('[data-easy-embed]').length > 0) {
$('[data-easy-embed]').each(function () {
$(this).easyEmbed();
})
}
})
})(jQuery, window, document);