script.js 5.6 KB

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