-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.ytChanPlayer.js
133 lines (132 loc) · 3.91 KB
/
jquery.ytChanPlayer.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
/* jQuery Youtube Channel Player 0.1.3
* Author: Marc Loehe (boundaryfunctions)
* Based on jQuery.youtubeChannel by Miguel Guerreiro (dharyk)
* Licensed under the MIT license
*/
(function ($) {
"use strict";
$.fn.ytChanPlayer = function (settings) {
var $ytEl = $(this),
$ytPlayer,
$ytList = $('<ul/>', {'class': 'yt-channel-list'}),
options = $.extend({}, {
username: '',
query: '',
startIndex: 1,
maxResults: 10,
orderBy: 'published',
playerOpts: {
autohide: 1,
autoplay: 0,
egm: 1,
fs: 1,
showinfo: 0,
wmode: 'opaque'
}
}, settings),
videos = [],
// accessory functions
buildUrl = function () {
var base = 'https://gdata.youtube.com/feeds/api/videos',
params = [
'alt=json',
'orderby=' + options.orderBy,
'start-index=' + options.startIndex,
'max-results=' + options.maxResults,
'callback=?'
];
if (options.username !== '') {
params.push('author=' + options.username);
} else if (options.query !== '') {
params.push('q=' + encodeURIComponent(options.query));
}
return base + '?' + params.join('&');
},
buildPlayer = function (id) {
if (id.length > 0) {
if (!$ytPlayer) {
$ytPlayer = $('<iframe/>', {'class': 'yt-player'});
}
var src = 'http://www.youtube-nocookie.com/embed/' + id,
opt;
if (options.playerOpts) {
src += '?';
for (opt in options.playerOpts) {
if (options.playerOpts.hasOwnProperty(opt)) {
src += opt + '=' + options.playerOpts[opt] + '&';
}
}
src += '_a=b';
}
$ytPlayer.attr('src', src).prependTo($ytEl);
}
},
zeroFill = function (number, width) {
width -= number.toString().length;
if (width > 0) {
return [width + (/\./.test(number) ? 2 : 1) ].join('0') + number;
}
return (number).toString();
},
parseTime = function (secs) {
var m, s = parseInt(secs, 10);
m = Math.floor(s / 60);
s -= (m * 60);
return m + ':' + zeroFill(s, 2);
};
// setup the html
$ytEl.addClass('yt-channel-holder');
$ytList.appendTo($ytEl);
// parse the feed
$.getJSON(buildUrl(), function (data) {
var i, html, vid, e;
// add the header
if (data.feed.entry) {
if (options.sticky) {
buildPlayer(options.sticky);
} else {
buildPlayer(data.feed.entry[0].id.$t.match('[^/]*$'));
}
// add the items
for (i = 0; i < data.feed.entry.length; i++) {
e = data.feed.entry[i];
vid = {
link: (e ? e.media$group.media$player[0].url : ''),
title: (e ? e.media$group.media$title.$t : ''),
thumb: (e ? e.media$group.media$thumbnail[1].url : ''),
duration: (e ? e.media$group.yt$duration.seconds : 0),
views: (e && e.yt$statistics ? e.yt$statistics.viewCount : 0),
id: (e ? e.id.$t.match('[^/]*$') : '')
};
html = $('<li/>', {'class': 'yt-channel-video'})
.html([
'<a href="', vid.link, '" title="', vid.title, ' (', parseTime(vid.duration), ')" target="_blank">',
'<img class="vid-thumb" alt="', vid.title, '" src="', vid.thumb, '"/>',
'</a>'
].join(''))
.data('id', vid.id).click(function (e) {
e.preventDefault();
options.playerOpts = $.extend(options.playerOpts, {autoplay: 1});
buildPlayer($(this).data('id'));
})
.css('opacity', '.7')
.hover(function () {
$(this).stop().animate({
opacity: '1'
}, 400);
}, function () {
$(this).stop().animate({
opacity: '.7'
}, 200);
});
videos.push(vid);
html.appendTo($ytList);
}
} else {
$('<li/>', {'class': 'yt-channel-video'})
.html('<a>NO RESULTS</a>').appendTo($ytList);
}
});
return this;
};
}(jQuery));