script.js 4.8 KB

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