-
Notifications
You must be signed in to change notification settings - Fork 85
/
jquery.prettyembed.js
322 lines (263 loc) · 16.2 KB
/
jquery.prettyembed.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
**********************************************************
* prettyEmbed.js | https://github.com/mike-zarandona/prettyembed.js
*
* Version: v1.2.1
* Author: Mike Zarandona
* Release: July 31 2014
*
* Reqs: jQuery | http://jquery.com
* waitForImages | https://github.com/alexanderdickson/waitForImages
*
* Optional: FitVids.js | http://fitvidsjs.com
*
* Usage: $('#video-placeholder-element').prettyEmbed({
* videoID: 'aBcDeFg12345',
* previewSize: '',
* customPreviewImage: '',
*
* showInfo: true,
* showControls: true,
* loop: false,
* closedCaptions: false,
* colorScheme: 'dark',
* showRelated: false,
*
* useFitVids: true
* });
**********************************************************
*/
(function ($, undefined) {
$.fn.prettyEmbed = function (options) {
// Override defaults with specified options
options = $.extend({}, $.fn.prettyEmbed.options, options);
// Test for mobile devices
var mobile = (/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent))? true : false;
// Inject styles if not already present
if ( $('#pretty-embed-style').length === 0 ) {
var styles = $('<style />', {
id: 'pretty-embed-style',
html: '.pretty-embed{position:relative;cursor:pointer;display:block}.pretty-embed img{width:100%;height:auto}.pretty-embed iframe{border:0 solid transparent}.pretty-embed:after{display:block;content:"";position:absolute;top:50%;margin-top:-19px;left:50%;margin-left:-27px;width:54px;height:38px;background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGwAAABMCAYAAACIylL7AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAadEVYdFNvZnR3YXJlAFBhaW50Lk5FVCB2My41LjExR/NCNwAABmxJREFUeF7t3W9MVlUcB3BNs3Kt1h/XC9eL6kWCAg6E8h/YMLKFm4v+GNZCfYHQnDoNVzjLYbZG8SLCVqSrHGgWtnDWbK1CK/sHi5oF8n+9IMcqiIqA6PT9Xs6Nx+tPAkSec+9zfu7zgnvuOffhfr3Avc997pk0XjUrahZNgytgJsyCRFgC6XAfZEEObIZ8KIBCeB5eglehHN6Cd+Bd+AA+huPwBXwF30At/KDVQ9P/OAXu+t8Cx/gaOOYnUAXc1ntQCRWwH16Dl6EYnoWdsA22QC6shvthOdwGSRAN18OVcAlM1rtpYgsbZiCLYRO8CIeBO7AZfoG/4B9QloP7gvvkV2iBGuB/Qv4HyINUmK537/gVBo2BV6ATpBdmjd0fwJ8qt4Le42MsDHA17IW/QdqYNX54NB6CmXr3j67QMQHaQBrcunA6IFXHMLJChxT4TQ9gTbxeyNBxDF9Y8Wawv6vCj3+sLNCxyIUVLoLPdAcr/BrgUh3P2YVGnitJHa3w2aTjObPQMBl4Iil1ssLnR7hYxzRUWMirElIHK/xW6JiGCguL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Qw8CmgH3sgz9c6z5YxT7JzXyDD1bRgfniAnCEq3fCYuELzisirWSZ45COywnsIU+jZZ7HdFxOYDd4Gi3zLNJxOYERp2uSVrTCjxM/nPlkbSzwxVX7CFWuYxoqLIz3rGSZI13HNFRYSJytTupghU8rDJ5/eQsNd4esaJlhvY7n7EIjp/PgXJBSR2vicb7OoefVS4UV+Az7n3UHK3z+hEQdy/CFFTkfIychlQayLrweuEvHMbJCBz5a9ns9gDVx2mGxjmF0hY6XwZPAuT+kwa3xMwCcbfa6wb1/HoVBroL1cAz6QNqgNTa8isGZfOeC3uPjWBj0cuDk29nwDLwBnHybE5N1g51CeAj3Bd/H+gn4F9+nwBnYOTM8D4CFMPxfgReysHHOoTkdZsCNEAvzYSlw6nd3evt1sAG2wnZ4Ctwp7jkD+x54HfgsR36DnGr+CHDqeU47wiP9c/gS+Pm20GnuvVPau9z274B9qoEXCrgTOZ47rT0/a8D5lLltTmtfCi/Ac8DXydf7KHCHr4VMWAFpsAD4PfN75z7gvpiid8841KRJ/wIcsey9MCgPGwAAAABJRU5ErkJggg==);-webkit-background-size:cover;background-size:cover}.pretty-embed.play:after{display:none}.pretty-embed:hover:after{background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGwAAABMCAYAAACIylL7AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAadEVYdFNvZnR3YXJlAFBhaW50Lk5FVCB2My41LjExR/NCNwAABwhJREFUeF7t3GlsVFUYBuCDIi4xGreA1O6dmXbaYQ2oCAriGjUuRDSiETVRISGCu2BcAH8gagKKpnWJ5UcBFVQIGhMDFGwVGcqU0m2WtkPpIqNSbEHaQj/fM5yh9HLAAlP6TTlv8iSdc+899/a8PenMnxHRSp3LIQ2ASyAO0mEUjIe7YTJMhWnwAsyBebAQFkM2fAl58A18Dz/Az7ABNsFm2AIeKIZypRIC/8MLkfO3g5zDDXLOXyAf5L1+hNWwEpZBLuT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JRU5ErkJggg==)}'
}).appendTo('head');
}
// Error Checking
if (options.useFitVids) {
if (!$.isFunction($.fn.fitVids)) {
console.error('PrettyEmbed.js Error: options.useFitVids is set to true; FitVids not found!');
}
}
// Store $('this') in a variable to minimize DOM searches
var elem;
// if no selector, only fire on .pretty-embed
if ( $(this).length === 0 ) { elem = $('.pretty-embed'); }
else { elem = $(this); }
// Main setup loop
elem.each(function() {
// Tag the selected elements
$(this).addClass('pretty-embed');
var $newDOMElement,
thisVideoID = '',
thisPreviewSize = '',
thisCustomPreviewImage = '',
thisShowInfo,
thisShowControls,
thisLoop,
thisClosedCaptions,
thisLocalization,
thisColorScheme,
thisShowRelated,
thisAllowFullScreen,
fullScreenFlag;
// videoID
if ( $(this).attr('data-pe-videoid') !== undefined ) { thisVideoID = $(this).attr('data-pe-videoid'); }
else if ( $(this).attr('href') !== undefined ) { thisVideoID = youtube_parser( $(this).attr('href') ); }
else { thisVideoID = options.videoID; }
// previewsize
if ( $(this).attr('data-pe-preview-size') !== undefined ) { thisPreviewSize = $(this).attr('data-pe-preview-size'); }
else if ( options.previewSize !== undefined ) { thisPreviewSize = options.previewSize; }
else { thisPreviewSize = undefined; }
// custom placeholder
if ( $(this).attr('data-pe-custom-preview-image') !== undefined ) { thisCustomPreviewImage = $(this).attr('data-pe-custom-preview-image'); }
else if ( options.customPreviewImage !== undefined ) { thisCustomPreviewImage = options.customPreviewImage; }
else { thisCustomPreviewImage = undefined; }
// showinfo
if ( $(this).attr('data-pe-show-info') !== undefined ) { thisShowInfo = $(this).attr('data-pe-show-info'); }
else if ( options.showInfo !== undefined ) { thisShowInfo = options.showInfo; }
else { thisShowInfo = undefined; }
// show controls
if ( $(this).attr('data-pe-show-controls') !== undefined ) { thisShowControls = $(this).attr('data-pe-show-controls'); }
else if ( options.showControls !== undefined ) { thisShowControls = options.showControls; }
else { thisShowControls = undefined; }
// loop
if ( $(this).attr('data-pe-loop') !== undefined ) { thisLoop = $(this).attr('data-pe-loop'); }
else if ( options.loop !== undefined ) { thisLoop = options.loop; }
else { thisLoop = undefined; }
// closed captions
if ( $(this).attr('data-pe-closed-captions') !== undefined ) { thisClosedCaptions = $(this).attr('data-pe-closed-captions'); }
else if ( options.closedCaptions !== undefined ) { thisClosedCaptions = options.closedCaptions; }
else { thisClosedCaptions = undefined; }
// localization
if ( $(this).attr('data-pe-localization') !== undefined ) { thisLocalization = $(this).attr('data-pe-localization'); }
else if ( options.localization !== undefined ) { thisLocalization = options.localization; }
else { thisLocalization = undefined; }
// color scheme
if ( $(this).attr('data-pe-color-scheme') !== undefined ) { thisColorScheme = $(this).attr('data-pe-color-scheme'); }
else if ( options.colorScheme !== undefined ) { thisColorScheme = options.colorScheme; }
else { thisColorScheme = undefined; }
// show related
if ( $(this).attr('data-pe-show-related') !== undefined ) { thisShowRelated = $(this).attr('data-pe-show-related'); }
else if ( options.showRelated !== undefined ) { thisShowRelated = options.showRelated; }
else { thisShowRelated = undefined; }
// allow full screen
if ( $(this).attr('data-pe-allow-fullscreen') !== undefined ) { thisAllowFullScreen = $(this).attr('data-pe-allow-fullscreen'); }
else if ( options.allowFullScreen !== undefined ) { thisAllowFullScreen = options.allowFullScreen; }
else { thisAllowFullScreen = undefined; }
// If this element is an <a/>, create a placeholder replacement
if ( $(this).is('a') ) {
// build the DOM structure
$newDOMElement = $('<div />').addClass('pretty-embed');
// rebuild the data-pe- attributes
$newDOMElement.attr('data-pe-videoid', thisVideoID)
.attr('data-pe-preview-size', thisPreviewSize)
.attr('data-pe-custom-preview-image', thisCustomPreviewImage)
.attr('data-pe-show-info', thisShowInfo)
.attr('data-pe-show-controls', thisShowControls)
.attr('data-pe-loop', thisLoop)
.attr('data-pe-closed-captions', thisClosedCaptions)
.attr('data-pe-localization', thisLocalization)
.attr('data-pe-color-scheme', thisColorScheme)
.attr('data-pe-show-related', thisShowRelated)
.attr('data-pe-allow-fullscreen', thisAllowFullScreen);
// append the new element, and remove the <a/>
$(this).after($newDOMElement);
}
// Write the options.customPreviewImage OR choose a size
if ( thisCustomPreviewImage !== undefined && thisCustomPreviewImage !== '' ) {
$(this).html('<img src="' + thisCustomPreviewImage + '" width="100%" alt="YouTube Video Preview" />');
}
else {
var previewSizePrefix = '';
switch (thisPreviewSize) {
case 'thumb-default':
previewSizePrefix = 'default';
break;
case 'thumb-1':
previewSizePrefix = '1';
break;
case 'thumb-2':
previewSizePrefix = '2';
break;
case 'thumb-3':
previewSizePrefix = '3';
break;
case 'default':
previewSizePrefix = 'mqdefault';
break;
case 'medium':
previewSizePrefix = '0';
break;
case 'high':
previewSizePrefix = 'hqdefault';
break;
default: // 'hd' or max-resolution quality
previewSizePrefix = 'maxresdefault';
break;
}
// Write the <img/> element
$(this).html('<img src="//img.youtube.com/vi/' + thisVideoID + '/' + previewSizePrefix + '.jpg" width="100%" alt="YouTube Video Preview" />');
}
if ( $(this).is('a') ) {
$newDOMElement.html( $(this).html() );
$(this).remove();
}
// if mobile, go straight to iframe
if (mobile) {
$(window).on('load', function() {
$('.pretty-embed').trigger('click');
});
}
});
// Click handler: load the video
$('body').on('click', '.pretty-embed', function(e) {
e.preventDefault();
clickEventRunner( $(this) );
});
/**
* Function: clickEventRunner(obj)
* Contains the functionality for .on('click') events
* obj = the current object referenced passed from $(this)
*/
function clickEventRunner(obj) {
var wrapperWidth = obj.find('img').outerWidth(true),
wrapperHeight = obj.find('img').outerHeight(true),
playerOptions = '',
thisVideoID = '';
// videoid - set from data-pe- attribute or options
if ( obj.attr('data-pe-videoid') !== undefined ) { thisVideoID = obj.attr('data-pe-videoid'); }
else if ( options.videoID !== undefined ) { thisVideoID = options.videoID; }
else {
thisVideoID = undefined;
console.error('PrettyEmbed.js Error: Misformed or missing video ID.');
}
// Assemble the player options string
// showInfo
if ( (obj.attr('data-pe-show-info') === 'false') || (options.showInfo === false) ) { playerOptions += '&showinfo=0'; }
// showControls
if ( (obj.attr('data-pe-show-controls') === 'false') || (options.showControls === false) ) { playerOptions += '&controls=0'; }
// loop
if ( (obj.attr('data-pe-loop') === 'true') || (options.loop === true) ) { playerOptions += '&loop=1'; }
// closed captions
if ( (obj.attr('data-pe-closed-captions') === 'true') || (options.closedCaptions === true) ) { playerOptions += '&cc_load_policy=1'; }
// localization
if ( (obj.attr('data-pe-localization') !== undefined) || (options.localization !== undefined) ) {
if ( obj.attr('data-pe-localization') !== undefined ) {
playerOptions += '&hl=' + obj.attr('data-pe-localization');
}
else if ( options.localization !== undefined ) {
playerOptions += '&hl=' + options.localization;
}
}
// colorScheme
if ( (obj.attr('data-pe-color-scheme') == 'light') || (options.colorScheme == 'light') ) { playerOptions += '&theme=light'; }
// showRelated
if ( (obj.attr('data-pe-show-related') === 'false') || (options.showRelated === false) ) { playerOptions += '&rel=0'; } else { playerOptions += '&rel=1'; }
// allow full screen
if ( (obj.attr('data-pe-allow-fullscreen') === 'false') || (options.allowFullScreen === false) ) { fullScreenFlag = ''; } else { fullScreenFlag = 'allowfullscreen '; }
// Write the YouTube video iFrame into the element at the exact dimensions it is now
obj.html('<iframe width="' + wrapperWidth + '" height="' + wrapperHeight + '" ' + fullScreenFlag + 'style="border:none;" src="//www.youtube.com/embed/' + thisVideoID + '?autoplay=1' + playerOptions + '"></iframe>')
// Remove the YouTube 'play' button using a CSS class
.addClass('play');
// If we're using FitVids, call it now
if (options.useFitVids) {
if ( $.isFunction($.fn.fitVids) ) {
obj.parent().fitVids();
}
}
}
// Helper function: get video ID from youtube URLs
function youtube_parser(url) {
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
if (match && match[7].length == 11) {
return match[7];
} else {
console.error('PrettyEmbed.js Error: Bad URL.');
}
}
// Default the defaults
$.fn.prettyEmbed.options = {
videoID: '',
previewSize: '',
customPreviewImage: '',
// Embed controls
showInfo: true,
showControls: true,
loop: false,
closedCaptions: false,
colorScheme: 'dark',
showRelated: false,
// FitVids.js
useFitVids: false
};
};
})(jQuery);