script.js 6.7 KB

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