script.js 5.3 KB

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