script.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. var player;
  2. var litColor = '#eee';
  3. var dimmedColor = '#555';
  4. var interval;
  5. $(document).ready(function () {
  6. $('body').click(function (e) {
  7. if (e.target.id !== 'results') {
  8. $('#results').slideUp(100);
  9. }
  10. });
  11. for (var i = 0; i < ids.length; i++) {
  12. drawBar(ids[i]);
  13. }
  14. var poller = new Worker('/static/poller.js');
  15. poller.onmessage = function (e) {
  16. for (var i = 0; i < e.data.updates.length; i++) {
  17. update = e.data.updates[i];
  18. switch (update.action) {
  19. case 0: // see updates.go:addAction
  20. drawAdd(update);
  21. break;
  22. case 1:
  23. drawRemove(update.yid);
  24. break;
  25. }
  26. }
  27. }
  28. poller.postMessage({pid: pid});
  29. });
  30. function drawBar(id) {
  31. var canvas = document.getElementById('c_' + id);
  32. canvas.height = canvas.height; //clear
  33. var context = canvas.getContext("2d");
  34. if (current_id == id && player) {
  35. context.fillStyle = dimmedColor;
  36. context.fillRect(0, 9, 700, 2);
  37. var loaded_start = player.getVideoStartBytes() / player.getVideoBytesTotal() * 700;
  38. var loaded_length = player.getVideoBytesLoaded() / player.getVideoBytesTotal() * 700;
  39. context.fillStyle = litColor;
  40. context.fillRect(loaded_start, 9, loaded_length, 2);
  41. var current_time = player.getVideoStartBytes() + player.getCurrentTime();
  42. var current_pos = current_time / player.getDuration() * 690 + 5;
  43. context.beginPath();
  44. context.arc(current_pos, 10, 5, 0, Math.PI * 2, false);
  45. context.closePath();
  46. context.fill();
  47. } else {
  48. context.fillStyle = dimmedColor;
  49. context.beginPath();
  50. context.arc(5, 10, 5, 0, Math.PI * 2, false);
  51. context.closePath();
  52. context.fill();
  53. context.fillRect(0, 9, 700, 2);
  54. }
  55. }
  56. // function name is defined by youtube player api
  57. function onYouTubePlayerReady(playerId) {
  58. player = document.getElementById('player');
  59. player.addEventListener('onStateChange', 'onStateChange');
  60. }
  61. function onStateChange(state) {
  62. switch (state) {
  63. case 1: //playing
  64. interval = setInterval(drawBar, 100, current_id);
  65. break;
  66. case 0: //ended
  67. showPlay(current_id);
  68. case -1: //unstarted
  69. case 2: //paused
  70. case 3: //buffering
  71. case 5: //cued
  72. clearInterval(interval);
  73. }
  74. }
  75. function play(yid) {
  76. if (current_id != yid) {
  77. player.loadVideoById(yid, 0, 'hd720');
  78. drawBar(current_id, dimmedColor);
  79. current_id = yid;
  80. }
  81. if (player.getPlayerState() in {'-1':1, '0':1, '5':1}) { // unstarted, ended, cued
  82. player.seekTo(0, true);
  83. }
  84. player.playVideo();
  85. for (var i = 0; i < ids.length; i++) {
  86. if (ids[i] == yid)
  87. showPause(ids[i]);
  88. else
  89. showPlay(ids[i]);
  90. }
  91. }
  92. function pause(yid) {
  93. if (current_id != yid)
  94. return
  95. player.pauseVideo();
  96. showPlay(yid);
  97. }
  98. function stop(yid) {
  99. if (current_id != yid)
  100. return
  101. player.stopVideo();
  102. showPlay(yid);
  103. drawBar(yid);
  104. }
  105. function showPlay(yid) {
  106. var play = document.getElementById('play_' + yid);
  107. var pause = document.getElementById('pause_' + yid);
  108. play.style.display = 'inline';
  109. pause.style.display = 'none';
  110. }
  111. function showPause(yid) {
  112. var pause = document.getElementById('pause_' + yid);
  113. var play = document.getElementById('play_' + yid);
  114. pause.style.display = 'inline';
  115. play.style.display = 'none';
  116. }
  117. function search(query) {
  118. params = {
  119. q: query,
  120. 'max-results': 10,
  121. v: 2,
  122. strict: true,
  123. alt: 'json',
  124. fields: 'entry(title,media:group(yt:videoid))',
  125. safeSearch: 'none',
  126. key: 'AI39si7SaaRqtvAZTa8lePOq6XC5r7S5Xzp3AB6oYPfeCArPbA4dIq7tSVeuIDwAkcFFDeI3rzNmYxkyN_fg8X_7w800pIvVOA'
  127. }
  128. $.getJSON('https://gdata.youtube.com/feeds/api/videos', params,
  129. function (data) {
  130. var entries = data.feed.entry;
  131. var items = [];
  132. for (var i = 0; i < entries.length; i++) {
  133. var title = entries[i].title.$t;
  134. var escapedTitle = escape(title.replace("'", '\\\''));
  135. var id = entries[i].media$group.yt$videoid.$t;
  136. var html = '<li id="r_' + id + '">';
  137. html += '<a href="javascript:add(\'' + id + '\', \'' + escapedTitle + '\')">';
  138. html += '<img src="http://i.ytimg.com/vi/' + id + '/1.jpg" alt="' + id + '">';
  139. html += title;
  140. html += '</a></li>';
  141. items.push(html);
  142. }
  143. var results = $('#results ul').html(items.join('\n'));
  144. $('#results').slideDown(100);
  145. }
  146. );
  147. return false;
  148. }
  149. function add(id, title) {
  150. var user = $('#user').val();
  151. params = {
  152. pid: pid,
  153. yid: id,
  154. title: title,
  155. user: user
  156. }
  157. $.getJSON('/add/', params);
  158. }
  159. function drawAdd(s) {
  160. $('article').append('\
  161. <section class="song" id="' + s.yid + '">\
  162. <div class="thumb">\
  163. <img src="http://i.ytimg.com/vi/' + s.yid + '/1.jpg" alt="' + s.yid + '">\
  164. </div>\
  165. <div class="info">\
  166. <a href="http://www.youtube.com/watch?v=' + s.yid + '">' + s.title + '</a>\
  167. </div>\
  168. <div class="user">' + s.user + '</div>\
  169. <div class="remove" onclick="remove(\'' + s.yid + '\')">×</div>\
  170. <canvas id="c_' + s.yid + '" width="700" height="20"></canvas>\
  171. <br>\
  172. <img src="/static/player_play.png" alt="Play" onclick="play(\'' + s.yid + '\')" id="play_' + s.yid + '">\
  173. <img src="/static/player_pause.png" alt="Pause" onclick="pause(\'' + s.yid + '\')" id="pause_' + s.yid + '">\
  174. <img src="/static/player_stop.png" alt="Stop" onclick="stop(\'' + s.yid + '\')">\
  175. <br class="clear">\
  176. </section>\
  177. ');
  178. ids.push(s.yid);
  179. drawBar(s.yid);
  180. }
  181. function remove(id) {
  182. var user = $('#user').val();
  183. params = {
  184. pid: pid,
  185. yid: id,
  186. }
  187. $.getJSON('/remove/', params);
  188. }
  189. function drawRemove(id) {
  190. var element = $('#' + id);
  191. element.slideUp(100, function () {
  192. element.remove();
  193. });
  194. }