script.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. var current_id = '';
  2. var player;
  3. var litColor = '#eee';
  4. var dimmedColor = '#555';
  5. var interval;
  6. var timestamp = '-1';
  7. $(document).ready(function () {
  8. $('body').click(function (e) {
  9. if (e.target.id !== 'results') {
  10. $('#results').slideUp(100);
  11. }
  12. });
  13. var userElem = $('#user');
  14. if (localStorage['user'] && localStorage['user'].length > 0)
  15. userElem.val(localStorage['user']);
  16. userElem.change(function () {
  17. localStorage['user'] = userElem.val();
  18. });
  19. getUpdates();
  20. });
  21. function getUpdates() {
  22. $.ajax({
  23. url: '/poll/',
  24. dataType: 'json',
  25. data: {pid: pid, timestamp: timestamp},
  26. success: handleUpdates,
  27. error: function (_, textStatus) {
  28. switch (textStatus) {
  29. case 'abort':
  30. return;
  31. case 'error':
  32. case 'parsererror':
  33. setTimeout(getUpdates, 1000);
  34. return;
  35. default:
  36. getUpdates();
  37. }
  38. }
  39. });
  40. }
  41. function handleUpdates(data) {
  42. var updates = [];
  43. for (var i = 0; i < data.length; i++) {
  44. timestamp = data[i].Timestamp;
  45. var update = {
  46. 'action': data[i].Action,
  47. 'yid': data[i].Song.Yid,
  48. 'title': data[i].Song.Title,
  49. 'user': data[i].Song.User
  50. }
  51. switch (update.action) {
  52. case 0: // see updates.go:addAction
  53. drawAdd(update);
  54. break;
  55. case 1:
  56. drawRemove(update.yid);
  57. break;
  58. case 2:
  59. case 3:
  60. drawMove(update.yid, update.action);
  61. break;
  62. }
  63. }
  64. if (timestamp == '-1')
  65. timestamp = '0';
  66. getUpdates();
  67. }
  68. function drawBar(id) {
  69. var canvas = document.getElementById('c_' + id);
  70. if (!canvas) return;
  71. canvas.height = canvas.height; //clear
  72. if (current_id == id) {
  73. var context = canvas.getContext("2d");
  74. context.fillStyle = dimmedColor;
  75. context.fillRect(0, 9, 700, 2);
  76. if (player) {
  77. var loaded_start = player.getVideoStartBytes() / player.getVideoBytesTotal() * 700,
  78. loaded_length = player.getVideoBytesLoaded() / player.getVideoBytesTotal() * 700;
  79. context.fillStyle = litColor;
  80. context.fillRect(loaded_start, 9, loaded_length, 2);
  81. var current_time = player.getVideoStartBytes() + player.getCurrentTime(),
  82. current_pos = current_time / player.getDuration() * 690 + 5;
  83. context.beginPath();
  84. context.arc(current_pos, 10, 5, 0, Math.PI * 2, false);
  85. context.closePath();
  86. context.fill();
  87. }
  88. }
  89. }
  90. // function name is defined by youtube player api
  91. function onYouTubePlayerReady(playerId) {
  92. player = document.getElementById('player');
  93. player.addEventListener('onStateChange', 'onStateChange');
  94. }
  95. function onStateChange(state) {
  96. switch (state) {
  97. case 1: //playing
  98. interval = setInterval(drawBar, 100, current_id);
  99. break;
  100. case 0: //ended
  101. clearInterval(interval);
  102. showPlay(current_id);
  103. var songs = $('article section.song');
  104. for (var i = 0; i < songs.length - 1; i++) {
  105. if (songs[i].id == current_id) {
  106. play(songs[i+1].id);
  107. break;
  108. }
  109. }
  110. case -1: //unstarted
  111. case 2: //paused
  112. case 3: //buffering
  113. case 5: //cued
  114. clearInterval(interval);
  115. }
  116. }
  117. function play(yid) {
  118. if (current_id != yid) {
  119. var previous_id = current_id;
  120. current_id = yid;
  121. drawBar(previous_id);
  122. player.loadVideoById(yid, 0, 'hd720');
  123. }
  124. if (player.getPlayerState() in {'-1':1, '0':1, '5':1}) { // unstarted, ended, cued
  125. player.seekTo(0, true);
  126. }
  127. player.playVideo();
  128. var songs = $('article section.song');
  129. for (var i = 0; i < songs.length; i++) {
  130. if (songs[i].id == yid)
  131. showPause(songs[i].id);
  132. else
  133. showPlay(songs[i].id);
  134. }
  135. drawBar(current_id);
  136. }
  137. function pause(yid) {
  138. if (current_id != yid)
  139. return
  140. player.pauseVideo();
  141. showPlay(yid);
  142. }
  143. function stop(yid) {
  144. if (current_id != yid)
  145. return
  146. player.stopVideo();
  147. showPlay(yid);
  148. drawBar(yid);
  149. }
  150. function showPlay(yid) {
  151. var play = document.getElementById('play_' + yid);
  152. var pause = document.getElementById('pause_' + yid);
  153. if (play)
  154. play.style.display = 'inline';
  155. if (pause)
  156. pause.style.display = 'none';
  157. }
  158. function showPause(yid) {
  159. var pause = document.getElementById('pause_' + yid);
  160. var play = document.getElementById('play_' + yid);
  161. if (pause)
  162. pause.style.display = 'inline';
  163. if (play)
  164. play.style.display = 'none';
  165. }
  166. function search(query) {
  167. params = {
  168. q: query,
  169. 'max-results': 10,
  170. v: 2,
  171. strict: true,
  172. alt: 'json',
  173. fields: 'entry(title,media:group(yt:videoid))',
  174. safeSearch: 'none',
  175. key: 'AI39si7SaaRqtvAZTa8lePOq6XC5r7S5Xzp3AB6oYPfeCArPbA4dIq7tSVeuIDwAkcFFDeI3rzNmYxkyN_fg8X_7w800pIvVOA'
  176. }
  177. $.getJSON('https://gdata.youtube.com/feeds/api/videos', params,
  178. function (data) {
  179. $('#results ul li').remove();
  180. var results = $('#results ul');
  181. var entries = data.feed.entry;
  182. for (var i = 0; i < entries.length; i++) {
  183. var title = entries[i].title.$t;
  184. var escapedTitle = title.replace(/'/g, "\\'").replace(/"/g, '\\"');
  185. var id = entries[i].media$group.yt$videoid.$t;
  186. var li = $(document.createElement('li'))
  187. .attr('id', 'r_' + id)
  188. .append($(document.createElement('a'))
  189. .attr('href', "javascript:add('" + id + "', '" + escapedTitle + "')")
  190. .append($(document.createElement('img'))
  191. .attr('src', 'http://i.ytimg.com/vi/' + id + '/1.jpg')
  192. .attr('alt', id))
  193. .append(title));
  194. results.append(li);
  195. }
  196. $('#results').slideDown(100);
  197. }
  198. );
  199. return false;
  200. }
  201. function userKeyPress(e) {
  202. if (e.keyCode == 13) {// enter
  203. $('#song').focus();
  204. return false;
  205. }
  206. }
  207. function add(id, title) {
  208. params = {
  209. pid: pid,
  210. yid: id,
  211. title: title,
  212. user: $('#user').val()
  213. }
  214. $.getJSON('/add/', params);
  215. }
  216. function drawAdd(s) {
  217. $('article').append($(document.createElement('section'))
  218. .addClass('song').attr('id', s.yid)
  219. .append($(document.createElement('div'))
  220. .addClass('thumb')
  221. .append($(document.createElement('img'))
  222. .attr('src', 'http://i.ytimg.com/vi/' + s.yid + '/1.jpg')
  223. .attr('alt', s.yid)
  224. )
  225. )
  226. .append($(document.createElement('div'))
  227. .addClass('info')
  228. .append($(document.createElement('a'))
  229. .attr('href', 'http://www.youtube.com/watch?v=' + s.yid)
  230. .append(s.title)
  231. )
  232. )
  233. .append($(document.createElement('div'))
  234. .addClass('user')
  235. .append(s.user)
  236. )
  237. .append($(document.createElement('div'))
  238. .addClass('remove')
  239. .append($(document.createElement('a'))
  240. .attr('href', 'javascript:remove("' + s.yid + '")')
  241. .append($(document.createElement('img'))
  242. .attr('src', '/static/remove.png')
  243. .attr('alt', 'remove')
  244. )
  245. )
  246. )
  247. .append($(document.createElement('div'))
  248. .addClass('reorder')
  249. .append($(document.createElement('a'))
  250. .attr('href', 'javascript:move("' + s.yid + '", 2)')
  251. .append($(document.createElement('img'))
  252. .attr('src', '/static/up-arrow.png')
  253. .attr('alt', 'up')
  254. )
  255. )
  256. .append($(document.createElement('a'))
  257. .attr('href', 'javascript:move("' + s.yid + '", 3)')
  258. .append($(document.createElement('img'))
  259. .attr('src', '/static/down-arrow.png')
  260. .attr('alt', 'down')
  261. )
  262. )
  263. )
  264. .append($(document.createElement('br')))
  265. .append($(document.createElement('img'))
  266. .attr('src', '/static/player_play.png')
  267. .attr('alt', 'play')
  268. .attr('id', 'play_' + s.yid)
  269. .click(function() {play(s.yid)})
  270. )
  271. .append($(document.createElement('img'))
  272. .attr('src', '/static/player_pause.png')
  273. .attr('alt', 'pause')
  274. .attr('id', 'pause_' + s.yid)
  275. .click(function() {pause(s.yid)})
  276. )
  277. .append($(document.createElement('img'))
  278. .attr('src', '/static/player_stop.png')
  279. .attr('alt', 'stop')
  280. .click(function() {stop(s.yid)})
  281. )
  282. .append($(document.createElement('canvas'))
  283. .attr('id', 'c_' + s.yid)
  284. .attr('width', 700)
  285. .attr('height', 20)
  286. )
  287. .append($(document.createElement('br'))
  288. .addClass('clear')
  289. )
  290. );
  291. }
  292. function remove(id) {
  293. params = {
  294. pid: pid,
  295. yid: id,
  296. }
  297. $.getJSON('/remove/', params);
  298. }
  299. function drawRemove(id) {
  300. var element = $('#' + id);
  301. stop(id);
  302. element.slideUp(100, function () {
  303. element.remove();
  304. });
  305. }
  306. function move(id, dir) {
  307. params = {
  308. pid: pid,
  309. yid: id,
  310. direction: dir // see updates.go:moveUpAction
  311. }
  312. $.getJSON('/move/', params);
  313. }
  314. function drawMove(id, dir) {
  315. var element1 = document.getElementById(id);
  316. if (dir == 2) { //up
  317. var element2 = element1.previousElementSibling;
  318. if (!element2) return;
  319. element1 = $('#' + id);
  320. element2 = $('#' + element2.id);
  321. element1.fadeOut(200, function () {
  322. element2.before(element1);
  323. element1.fadeIn(200);
  324. });
  325. } else if (dir == 3) { //down
  326. var element2 = element1.nextElementSibling;
  327. if (!element2) return;
  328. element1 = $('#' + id);
  329. element2 = $('#' + element2.id);
  330. element1.fadeOut(200, function () {
  331. element2.after(element1);
  332. element1.fadeIn(200);
  333. });
  334. }
  335. }