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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
var current_id = '';
var ids = [];
var player;
var litColor = '#eee';
var dimmedColor = '#555';
var interval;
var timestamp = '0';
$(document).ready(function () {
$('body').click(function (e) {
if (e.target.id !== 'results') {
$('#results').slideUp(100);
}
});
var userElem = $('#user');
if (localStorage['user'] && localStorage['user'].length > 0)
userElem.val(localStorage['user']);
userElem.change(function () {
localStorage['user'] = userElem.val();
});
for (var i = 0; i < ids.length; i++) {
drawBar(ids[i]);
}
getUpdates();
});
function getUpdates() {
$.ajax({
url: '/poll/',
dataType: 'json',
data: {pid: pid, timestamp: timestamp},
success: handleUpdates,
error: function (_, textStatus) {
switch (textStatus) {
case 'abort':
return;
case 'error':
case 'parsererror':
setTimeout(getUpdates, 1000);
return;
default:
getUpdates();
}
}
});
}
function handleUpdates(data) {
var updates = [];
for (var i = 0; i < data.length; i++) {
timestamp = data[i].Timestamp;
var update = {
'action': data[i].Action,
'yid': data[i].Song.Yid,
'title': data[i].Song.Title,
'user': data[i].Song.User
}
switch (update.action) {
case 0: // see updates.go:addAction
drawAdd(update);
break;
case 1:
drawRemove(update.yid);
break;
case 2:
case 3:
drawMove(update.yid, update.action);
break;
}
}
getUpdates();
}
function drawBar(id) {
var canvas = document.getElementById('c_' + id);
if (!canvas) return;
canvas.height = canvas.height; //clear
var context = canvas.getContext("2d");
if (current_id == id && player) {
context.fillStyle = dimmedColor;
context.fillRect(0, 9, 700, 2);
var loaded_start = player.getVideoStartBytes() / player.getVideoBytesTotal() * 700;
var loaded_length = player.getVideoBytesLoaded() / player.getVideoBytesTotal() * 700;
context.fillStyle = litColor;
context.fillRect(loaded_start, 9, loaded_length, 2);
var current_time = player.getVideoStartBytes() + player.getCurrentTime();
var current_pos = current_time / player.getDuration() * 690 + 5;
context.beginPath();
context.arc(current_pos, 10, 5, 0, Math.PI * 2, false);
context.closePath();
context.fill();
} else {
context.fillStyle = dimmedColor;
context.beginPath();
context.arc(5, 10, 5, 0, Math.PI * 2, false);
context.closePath();
context.fill();
context.fillRect(0, 9, 700, 2);
}
}
// function name is defined by youtube player api
function onYouTubePlayerReady(playerId) {
player = document.getElementById('player');
player.addEventListener('onStateChange', 'onStateChange');
}
function onStateChange(state) {
switch (state) {
case 1: //playing
interval = setInterval(drawBar, 100, current_id);
break;
case 0: //ended
showPlay(current_id);
case -1: //unstarted
case 2: //paused
case 3: //buffering
case 5: //cued
clearInterval(interval);
}
}
function play(yid) {
if (current_id != yid) {
player.loadVideoById(yid, 0, 'hd720');
drawBar(current_id, dimmedColor);
current_id = yid;
}
if (player.getPlayerState() in {'-1':1, '0':1, '5':1}) { // unstarted, ended, cued
player.seekTo(0, true);
}
player.playVideo();
for (var i = 0; i < ids.length; i++) {
if (ids[i] == yid)
showPause(ids[i]);
else
showPlay(ids[i]);
}
}
function pause(yid) {
if (current_id != yid)
return
player.pauseVideo();
showPlay(yid);
}
function stop(yid) {
if (current_id != yid)
return
player.stopVideo();
showPlay(yid);
drawBar(yid);
}
function showPlay(yid) {
var play = document.getElementById('play_' + yid);
var pause = document.getElementById('pause_' + yid);
if (play)
play.style.display = 'inline';
if (pause)
pause.style.display = 'none';
}
function showPause(yid) {
var pause = document.getElementById('pause_' + yid);
var play = document.getElementById('play_' + yid);
if (pause)
pause.style.display = 'inline';
if (play)
play.style.display = 'none';
}
function search(query) {
params = {
q: query,
'max-results': 10,
v: 2,
strict: true,
alt: 'json',
fields: 'entry(title,media:group(yt:videoid))',
safeSearch: 'none',
key: 'AI39si7SaaRqtvAZTa8lePOq6XC5r7S5Xzp3AB6oYPfeCArPbA4dIq7tSVeuIDwAkcFFDeI3rzNmYxkyN_fg8X_7w800pIvVOA'
}
$.getJSON('https://gdata.youtube.com/feeds/api/videos', params,
function (data) {
$('#results ul li').remove();
var results = $('#results ul');
var entries = data.feed.entry;
for (var i = 0; i < entries.length; i++) {
var title = entries[i].title.$t;
var escapedTitle = title.replace(/'/g, "\\'").replace(/"/g, '\\"');
var id = entries[i].media$group.yt$videoid.$t;
var li = $(document.createElement('li'))
.attr('id', 'r_' + id)
.append($(document.createElement('a'))
.attr('href', "javascript:add('" + id + "', '" + escapedTitle + "')")
.append($(document.createElement('img'))
.attr('src', 'http://i.ytimg.com/vi/' + id + '/1.jpg')
.attr('alt', id))
.append(title));
results.append(li);
}
$('#results').slideDown(100);
}
);
return false;
}
function add(id, title) {
params = {
pid: pid,
yid: id,
title: title,
user: $('#user').val()
}
$.getJSON('/add/', params);
}
function drawAdd(s) {
$('article').append($(document.createElement('section'))
.addClass('song').attr('id', s.yid)
.append($(document.createElement('div'))
.addClass('thumb')
.append($(document.createElement('img'))
.attr('src', 'http://i.ytimg.com/vi/' + s.yid + '/1.jpg')
.attr('alt', s.yid)
)
)
.append($(document.createElement('div'))
.addClass('info')
.append($(document.createElement('a'))
.attr('href', 'http://www.youtube.com/watch?v=' + s.yid)
.append(s.title)
)
)
.append($(document.createElement('div'))
.addClass('user')
.append(s.user)
)
.append($(document.createElement('div'))
.addClass('remove')
.append($(document.createElement('a'))
.attr('href', 'javascript:remove("' + s.yid + '")')
.append($(document.createElement('img'))
.attr('src', '/static/remove.png')
.attr('alt', 'remove')
)
)
)
.append($(document.createElement('div'))
.addClass('reorder')
.append($(document.createElement('a'))
.attr('href', 'javascript:move("' + s.yid + '", 2)')
.append($(document.createElement('img'))
.attr('src', '/static/up-arrow.png')
.attr('alt', 'up')
)
)
.append($(document.createElement('a'))
.attr('href', 'javascript:move("' + s.yid + '", 3)')
.append($(document.createElement('img'))
.attr('src', '/static/down-arrow.png')
.attr('alt', 'down')
)
)
)
.append($(document.createElement('canvas'))
.attr('id', 'c_' + s.yid)
.attr('width', 700)
.attr('height', 20)
)
.append($(document.createElement('br')))
.append($(document.createElement('img'))
.attr('src', '/static/player_pause.png')
.attr('alt', 'play')
.attr('id', 'play_' + s.yid)
.click(function() {play(s.yid)})
)
.append($(document.createElement('img'))
.attr('src', '/static/player_play.png')
.attr('alt', 'pause')
.attr('id', 'pause_' + s.yid)
.click(function() {pause(s.yid)})
)
.append($(document.createElement('img'))
.attr('src', '/static/player_stop.png')
.attr('alt', 'stop')
.click(function() {stop(s.yid)})
)
.append($(document.createElement('br'))
.addClass('clear')
)
);
ids.push(s.yid);
drawBar(s.yid);
}
function remove(id) {
params = {
pid: pid,
yid: id,
}
$.getJSON('/remove/', params);
}
function drawRemove(id) {
var element = $('#' + id);
stop(id);
element.slideUp(100, function () {
element.remove();
});
}
function move(id, dir) {
params = {
pid: pid,
yid: id,
direction: dir // see updates.go:moveUpAction
}
$.getJSON('/move/', params);
}
function drawMove(id, dir) {
var element1 = document.getElementById(id);
if (dir == 2) { //up
var element2 = element1.previousElementSibling;
if (!element2) return;
element1 = $('#' + id);
element2 = $('#' + element2.id);
element1.fadeOut(200, function () {
element2.before(element1);
element1.fadeIn(200);
});
} else if (dir == 3) { //down
var element2 = element1.nextElementSibling;
if (!element2) return;
element1 = $('#' + id);
element2 = $('#' + element2.id);
element1.fadeOut(200, function () {
element2.after(element1);
element1.fadeIn(200);
});
}
}
|