video.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. function render_comments(comments) {
  2. for (var i = 0; i < comments.length; i++) {
  3. var comment = comments[i];
  4. var html = '';
  5. var ago = moment.unix(comment.publish_ts).calendar();
  6. html += '<div class="column is-full">';
  7. html += '<div class="card">';
  8. html += '<div class="card-content">';
  9. html += '<div class="media">';
  10. html += '<div class="media-left">';
  11. html += '<figure class="image is-64x64">';
  12. html += '<a href="/user/' + String(comment.author_id) + '"><img id="avatar_' + String(comment.id) + '" src="/static/avatars/default/default.128.png"></a>';
  13. html += '</figure>';
  14. html += '</div>';
  15. html += '<div class="media-content" style="overflow-x: hidden;">';
  16. html += '<div class="content">';
  17. html += '<p>';
  18. html += '<strong ><a id="username_' + String(comment.id) + '" href="/user/' + String(comment.author_id) + '">the void</a></strong> <small>' + ago + '</small>';
  19. html += '<br>';
  20. html += '<div class="content" style="word-break: break-all;">';
  21. html += comment.text;
  22. html += '</div>';
  23. html += '</p>';
  24. html += '</div>';
  25. html += '</div>';
  26. html += '</div>';
  27. html += '</div>';
  28. html += '</div>';
  29. html += '</div>';
  30. $('#comments').append(html);
  31. api('user', {user_id: comment.author_id}, function (data, additional_data) {
  32. var comment_id = additional_data[0];
  33. $('#avatar_' + String(comment_id)).attr('src', data.avatar128);
  34. $('#username_' + String(comment_id)).text(data.username);
  35. }, comment.id);
  36. }
  37. if (comments.length >= 5) {
  38. window.offset += comments.length;
  39. $(window).bind('scroll', load_more);
  40. }
  41. }
  42. function update_comments() {
  43. api('comments', {video_id: window.video_id, offset: window.offset}, function (data) {
  44. $('#comments_count').text(data.comments_count);
  45. $('#comments_count_suffix').text(data.comments_count != 1? 's': '');
  46. render_comments(data.comments);
  47. });
  48. }
  49. function load_more() {
  50. if (!scrolled_down(100))
  51. return;
  52. update_comments()
  53. $(window).unbind('scroll', load_more);
  54. }
  55. $(function () {
  56. window.offset = 0;
  57. window.player = new Plyr('#player');
  58. var last_video_id = window.video_id - 1;
  59. var next_video_id = window.video_id + 1;
  60. if (window.my_user_id < 0) {
  61. $('#comment_field').prop('disabled', true);
  62. $('#comment_field').attr('placeholder', 'You must log in to write comments.');
  63. $('#send_comment_field').hide();
  64. }
  65. $('#upload_date').text(moment.unix(window.video_upload_ts).calendar());
  66. $('#likes_icon').click(function () {
  67. if (window.my_user_id < 0) {
  68. popup('danger', 'You must be logged in to like videos.');
  69. return;
  70. }
  71. api('liked', {video_id: window.video_id}, function (data) {
  72. api(data.is_liked? 'unlike': 'like', {video_id: window.video_id}, function (data) {
  73. $('#likes_count').text(data.likes_count);
  74. });
  75. if (data.is_liked)
  76. $('#likes_icon').removeClass('liked');
  77. else
  78. $('#likes_icon').addClass('liked');
  79. });
  80. });
  81. api('likes', {video_id: window.video_id}, function (data) {
  82. $('#likes_count').text(data.likes_count);
  83. });
  84. if (window.my_user_id >= 0)
  85. api('liked', {video_id: window.video_id}, function (data) {
  86. if (data.is_liked)
  87. $('#likes_icon').addClass('liked');
  88. });
  89. api('exists', {video_id: last_video_id}, function (data) {
  90. if (data.is_exists) {
  91. $('#last_button').prop('disabled', false);
  92. $('#last_button').click(function () {
  93. document.location.href = '/watch/' + String(last_video_id);
  94. });
  95. }
  96. });
  97. api('exists', {video_id: next_video_id}, function (data) {
  98. if (data.is_exists) {
  99. $('#next_button').prop('disabled', false);
  100. $('#next_button').click(function () {
  101. document.location.href = '/watch/' + String(next_video_id);
  102. });
  103. }
  104. });
  105. $('#send_comment').click(function () {
  106. var text = $('#comment_field').val().trim();
  107. if (text.length < 2)
  108. return;
  109. $('#comment_field').val('');
  110. $('#send_comment').addClass('is-loading');
  111. api('comment', {video_id: window.video_id, text: text}, function () {
  112. $('#comments').html('');
  113. $('#characters_left').text('256');
  114. window.offset = 0;
  115. update_comments();
  116. $('#send_comment').removeClass('is-loading');
  117. });
  118. });
  119. api('user', {user_id: window.video_uploader_id}, function (data) {
  120. $('#uploader_avatar').attr('src', data.avatar128);
  121. $('#uploader_username').text(data.username);
  122. $('#uploader_username').attr('href', '/user/' + String(window.video_uploader_id));
  123. });
  124. api('tags', {video_id: window.video_id}, function (data) {
  125. for (var i = 0; i < data.tags_list.length; i++) {
  126. var html = '';
  127. html += '<a class="tag is-light" href="/search?q=' + data.tags_list[i] + '">';
  128. html += data.tags_list[i];
  129. html += '</a>'
  130. $('#tags').append(html);
  131. }
  132. });
  133. update_comments();
  134. $('textarea').each(function () {
  135. this.setAttribute('style', 'height:' + (this.scrollHeight) + 'px;overflow-y:hidden;resize:none;');
  136. }).on('input', function () {
  137. this.style.height = 'auto';
  138. this.style.height = (this.scrollHeight) + 'px';
  139. });
  140. $('#comment_field').keyup(function () {
  141. if ($('#comment_field').val().length > 256)
  142. $('#comment_field').val($('#comment_field').val().slice(0, 256));
  143. $('#characters_left').text(256 - $('#comment_field').val().length);
  144. });
  145. });