-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathjquery.youtubevideogallery.js
283 lines (271 loc) · 12.8 KB
/
jquery.youtubevideogallery.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
/**
* youtubeVideoGallery plugin for jquery
* Simple video gallery for youtube, for more details see http://plugins.jquery.com/youtubevideogallery
*
* @author clanceyp
* @see http://plugins.jquery.com/youtubevideogallery/
* @version 1.3.1
*
*/
;(function($) {
"use strict";
$.fn.extend({
youtubeVideoGallery:function(options) {
var version = '1.3.1',
isLegacyIE = (/\bMSIE [4|5|6|7]/.test(navigator.userAgent)),
defaults = {
assetFolder : '',
fancybox : {
arrows : false,
closeBtn : true,
closeEffect : 'fade',
helpers : {
media : {},
buttons : {}
},
openEffect : 'fade'
},
forceLegacyIE:false,
iframeTemplate:'<iframe title="Youtube video player" id="youtube-videogallery-iframe" style="height:{options.innerHeight}px;width:{options.innerWidth}px;" frameborder="0" src="about:blank" />',
innerHeight:344,
innerWidth:425,
newWindow: '(opens in a new window)',
playButton: '[email protected]',
plugin:'self',
style:'',
title:'default',
thumbWidth:150,
videos:[],
urlImg : 'http://img.youtube.com/vi/$id/0.jpg',
urlEmbed : 'http://www.youtube.com/embed/$id',
urlLink : 'http://www.youtube.com/watch?v=$id'
};
this.test = {};
this.version = version;
function log(message){
if ('console' in window && window.console.log){
window.console.log(message);
}
}
function getVideoLinks($this){
var arr = [],
a = $this.find("a");
a.each(function(i, el){
arr.push({
id: getId( $(el).attr('href') ),
title:$(el).text()
});
});
return arr;
}
function getIdFromEntry(obj){
if (typeof obj === 'string'){
return getId(obj);
} else if (obj.id && typeof obj.id === 'string'){
return obj.id;
} else if (obj.link && obj.link.length ){
for ( var i = 0, l = obj.link.length; i < l ; i++){
if (obj.link[i].type === 'text/html'){
return getId(obj.link[i].href)
}
}
}
log('could not find ID from object, maybe an unsupported API?')
return '';
}
function getId(href){
var id = '';
if (!!href && href.indexOf('www.youtube.com/v/') > 0) {
id = getBefore(href.split('www.youtube.com/v/')[1], '?')
} else if (!!href && href.indexOf('/embed/') > 0){
id = getBefore(href.split('/embed/')[1], '?');
} else if (!!href && href.indexOf('?v=') > 0){// this could be version if API call!!
id = getBefore(href.split('?v=')[1],'&');
} else if (!!href && href.indexOf('video:') > 0){
id = getBefore(href.split('video:')[1], ':');
} else if (!!href){
id = href;
}
return id;
}
function getBefore(str, before){
if (!!str && str.indexOf(before)){
str = str.split(before)[0];
}
return str;
}
function getIframeTemplate(innerWidth, innerHeight){
var str = options.iframeTemplate;
return str.replace('{options.innerHeight}', innerHeight).replace('{options.innerWidth}', innerWidth);
}
function open(e){
var el = e.currentTarget;
e.preventDefault();
$('div.youtube-videogallery-bodycover').css( { 'height':$(document).height()} );
$('div.youtube-videogallery-display').css({
'marginLeft':-options.innerWidth/2,
'marginTop':-options.innerHeight/2
});
$('#youtube-videogallery-iframe').attr( 'src', options.urlEmbed.replace("$id", $(el).attr('data-youtube-id') ) );
$('body').addClass('youtube-videogallery-active');
}
function close(e){
$('#youtube-videogallery-iframe').attr( 'src', 'about:blank');
$('body').removeClass('youtube-videogallery-active');
}
function setButtonMargin(w, h, context){
if (w === 0 || h === 0){
$(context).find("img.youtube-videogallery-play").remove();
return;
}
$(context).find("img.youtube-videogallery-play").css({
'marginLeft':-w/2 +'px',
'marginTop':-h/2 +'px'
});
}
function getStyle(style){
if (!!style){
return "youtube-videogallery-" + style;
}
return '';
}
function getTitleStyle(title){
return title === 'full' ? 'youtube-videogallery-allowtitle' : '';
}
function getVideosFromFeed(data){
var videos = [],
items = (data && data.data && data.data.items) ? data.data.items
: (data && data.feed && data.feed.entry) ? data.feed.entry : [];
$( items ).each(function(i, item){
var id = getIdFromEntry(item);
videos.push({
id: getIdFromEntry(item),
title: item.title.$t || item.title
});
});
return videos;
}
/**
* this.test = object
*
* Test object to expose private methods to a test API.
* This allows us to test private methods, without
* exposing them (e.g. they can't be overwritten)
*
*/
this.test = {
getBefore:getBefore,
getId:getId,
getVideoLinks:getVideoLinks,
getIframeTemplate:getIframeTemplate,
getTitleStyle:getTitleStyle,
getStyle:getStyle,
getVideosFromFeed:getVideosFromFeed,
getIdFromEntry:getIdFromEntry
};
function load($this, options) {
var videos = ( options.videos.length ) ? options.videos : getVideoLinks($this),
html = '',
href, src, titleSpan, video,
playButtonSrc = (!!options.assetFolder) ? options.assetFolder +'/'+ options.playButton : options.playButton ,
img = document.createElement('img');
img.onload = function(){
setButtonMargin(this.width, this.height, $this);
};
img.onerror = function(){
setButtonMargin(0, 0, $this);
};
img.src = playButtonSrc+'?'+ +(new Date());
for (var i = 0, l = videos.length; i < l; i++){
video = videos[i];
if (!video.id){continue;}
href = options.urlLink.replace("$id", video.id);
src = options.urlImg.replace("$id", video.id);
titleSpan = (!!video.title && options.title !== 'none') ? '<span class="youtube-videogallery-title">'+ video.title +'</span>' : '';
html+= '<li class="youtube-videogallery-item"><a title="'+video.title+'" data-youtube-id="'+ video.id +'" href="'+ href +'" class="youtube-videogallery-link" style="width:'+options.thumbWidth+'px"><img class="youtube-videogallery-play" src="'+ playButtonSrc +'" title="play" /><img class="youtube-videogallery-img" src="'+ src +'" style="width:'+options.thumbWidth+'px" />'+ titleSpan +'</a></li>';
}
$this.empty()
.append(html)
.addClass('youtube-videogallery-container')
.addClass( getStyle( options.style ) )
.addClass( getTitleStyle( options.title ) );
if (options.supported && options.plugin === 'colorbox' && $.colorbox){
$this.find("a.youtube-videogallery-link").each(function(i, el){
$(el)
.attr('href', options.urlEmbed.replace("$id", $(el).attr('data-youtube-id') ) )
.attr('aria-controls','youtube-videogallery-iframe')
.colorbox({iframe:true, innerWidth:options.innerWidth, innerHeight:options.innerHeight});
});
} else if (options.supported && options.plugin === 'fancybox' && !!$().fancybox){
$this.find("a.youtube-videogallery-link").each(function(i, el){
$(el)
.attr('rel', 'media-gallery')
.fancybox(options.fancybox);
});
} else if (options.supported && options.plugin === 'self'){
if (!$('div.youtube-videogallery-bodycover').length){
$('body')
.append('<div class="youtube-videogallery-bodycover"/>')
.append('<div class="youtube-videogallery-display">'+ getIframeTemplate(options.innerWidth, options.innerHeight) +'</div>');
$('div.youtube-videogallery-bodycover').on('click',close);
$(document).on('keydown', function(e) {
if (e.which === 27) {
close();
}
});
}
$this.find("a.youtube-videogallery-link").on('click',open);
} else {
$this.find("a.youtube-videogallery-link")
.attr('target','_blank')
.append('<span class="youtube-videogallery-screen-reader-only">'+options.newWindow+'</span>')
.each(function(i, el){
$(el).attr('title', $(el).attr('title')+' '+ options.newWindow);
});
}
return $this;
}
// Set options
options = $.extend(defaults, options);
options.supported = (
/* don't show lightbox if: old untested jquery version, or the lightbox is bigger than the viewport */
!!$().on &&
( $(window).width() > options.innerWidth || $(window).height() > options.innerHeight)
);
options.plugin = (isLegacyIE && !options.forceLegacyIE) ? 'none' : options.plugin;// by default turn off plugins for IE<=7
// it's an API, load promise and return the original object for chaining
if (!!options.apiUrl){
var $this = this,
apiUrl = options.apiUrl,
jqxhr = $.ajax({
url:apiUrl,
dataType:"jsonp",
beforeSend:function(){
if (!options.apiFallbackUrl){
return;
}
$this.each(function(i, el){
$(el).empty()
.append('<li><a /></li>')
.find('a')
.attr('href', options.apiFallbackUrl)
.text(options.apiFallbackUrl);
});
}})
.done(function(data) {
options.videos = getVideosFromFeed(data);
$this.each(function(i, el){
load($(el), options);
});
})
.fail(function(){
log('Error getting youtube API requested by jQuery.youtubeVideoGallery: '+ apiUrl);
});
return $this;
}
return this.each(function(i, el){
load($(el), options);
});
}
});
})(window.jQuery);