1
0

user.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. function render_videos(results) {
  2. if (results.length < 6)
  3. $(window).unbind('scroll', load_more);
  4. if (results.length == 0)
  5. return;
  6. $('#no_videos').hide();
  7. for (var i = 0; i < results.length; i++) {
  8. var result = results[i];
  9. var html = '';
  10. var tags = '';
  11. for (var j = 0; j < result.tags_list.length; j++) {
  12. tags += '<a class="tag is-light" href="/search?q=' + result.tags_list[j] + '">';
  13. tags += result.tags_list[j];
  14. tags += '</a>'
  15. }
  16. html += '<div class="column is-half">';
  17. html += '<div class="card">';
  18. html += '<div class="card-image">';
  19. html += '<figure class="image is-16by9">';
  20. html += '<a href="/watch/' + String(result.id) + '">';
  21. html += '<img src="' + result.thumbnail + '">';
  22. html += '</a>';
  23. html += '</figure>';
  24. html += '</div>';
  25. html += '<div class="card-content">';
  26. html += '<div class="content">';
  27. html += '<div class="tags are-normal">' + tags + '</div>';
  28. html += '</div>';
  29. html += '<div class="media">';
  30. html += '<div class="media-left">';
  31. html += '<figure class="image is-48x48">';
  32. html += '<a href="/user/' + result.uploader_id + '"><img id="avatar_' + String(result.id) + '" src="/static/avatars/default/default.128.png"></a>';
  33. html += '</figure>';
  34. html += '</div>';
  35. html += '<div class="media-content">';
  36. html += '<p class="title is-5"><a id="username_' + String(result.id) + '" href="/user/' + result.uploader_id + '">the void</a></p>';
  37. html += '<p class="subtitle is-6"><i class="fas fa-heart"></i> ' + String(result.likes_count) + '</p>';
  38. html += '</div>';
  39. html += '</div>';
  40. html += '</div>';
  41. html += '</div>';
  42. html += '</div>';
  43. $('#user_videos').append(html);
  44. api('user', {user_id: result.uploader_id}, function (data, additional_data) {
  45. var video_id = additional_data[0];
  46. $('#avatar_' + String(video_id)).attr('src', data.avatar128);
  47. $('#username_' + String(video_id)).text(data.username);
  48. }, result.id);
  49. }
  50. if (results.length >= 6) {
  51. window.offset += results.length;
  52. $(window).bind('scroll', load_more);
  53. }
  54. }
  55. function get_user_videos() {
  56. api('videos', {user_id: window.user_id, offset: window.offset}, function (data) {
  57. $('#videos_count').text(data.videos_count);
  58. $('#videos_count_suffix').text(data.videos_count != 1? 's': '');
  59. render_videos(data.videos);
  60. });
  61. }
  62. function load_more() {
  63. if (!scrolled_down(100))
  64. return;
  65. get_user_videos();
  66. $(window).unbind('scroll', load_more);
  67. }
  68. $(function () {
  69. window.offset = 0;
  70. $('#change_avatar').hide();
  71. api_unchecked('user', {user_id: window.user_id}, function (data) {
  72. if (data.error !== undefined) {
  73. document.location.href = '/404';
  74. return;
  75. }
  76. $('#avatar').attr('src', data.avatar128);
  77. $('#username').text(data.username);
  78. $('#signup_date').text(moment.unix(data.signup_ts).format('L'));
  79. get_user_videos();
  80. });
  81. if (window.user_id == window.my_user_id) {
  82. $('#change_avatar').show();
  83. $('#upload_avatar_button').click(function () {
  84. $('#upload_avatar_button').addClass('is-loading');
  85. $('#upload_avatar_button').prop('disabled', true);
  86. var form = new FormData();
  87. form.append('avatar', $('#avatar_input').prop('files')[0]);
  88. api_unchecked('upload/avatar', form, function (data) {
  89. $('#upload_avatar_button').removeClass('is-loading');
  90. $('#upload_avatar_button').prop('disabled', false);
  91. if (data.error !== undefined) {
  92. popup('danger', 'Error: ' + data.error);
  93. return;
  94. }
  95. popup('primary', 'Successfully updated your avatar!');
  96. setTimeout(function () {
  97. window.location.reload();
  98. }, 1500);
  99. });
  100. });
  101. }
  102. });