search.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. function render_results(results) {
  2. if (results.length < 6)
  3. $(window).unbind('scroll', load_more);
  4. if (results.length == 0) {
  5. if (window.offset == 0)
  6. $('#nothing_found').show();
  7. return;
  8. }
  9. for (var i = 0; i < results.length; i++) {
  10. var result = results[i];
  11. var html = '';
  12. var tags = '';
  13. for (var j = 0; j < result.tags_list.length; j++) {
  14. tags += '<a class="tag is-light" href="/search?q=' + result.tags_list[j] + '">';
  15. tags += result.tags_list[j];
  16. tags += '</a>'
  17. }
  18. html += '<div class="column is-half">';
  19. html += '<div class="card">';
  20. html += '<div class="card-image">';
  21. html += '<figure class="image is-16by9">';
  22. html += '<a href="/watch/' + String(result.id) + '">';
  23. html += '<img src="' + result.thumbnail + '">';
  24. html += '</a>';
  25. html += '</figure>';
  26. html += '</div>';
  27. html += '<div class="card-content">';
  28. html += '<div class="content">';
  29. html += '<div class="tags are-normal">' + tags + '</div>';
  30. html += '</div>';
  31. html += '<div class="media">';
  32. html += '<div class="media-left">';
  33. html += '<figure class="image is-48x48">';
  34. html += '<a href="/user/' + result.uploader_id + '"><img id="avatar_' + String(result.id) + '" src="/static/avatars/default/default.128.png"></a>';
  35. html += '</figure>';
  36. html += '</div>';
  37. html += '<div class="media-content">';
  38. html += '<p class="title is-5"><a id="username_' + String(result.id) + '" href="/user/' + result.uploader_id + '">the void</a></p>';
  39. html += '<p class="subtitle is-6"><i class="fas fa-heart"></i> ' + String(result.likes_count) + '</p>';
  40. html += '</div>';
  41. html += '</div>';
  42. html += '</div>';
  43. html += '</div>';
  44. html += '</div>';
  45. $('#search_results').append(html);
  46. api('user', {user_id: result.uploader_id}, function (data, additional_data) {
  47. var video_id = additional_data[0];
  48. $('#avatar_' + String(video_id)).attr('src', data.avatar128);
  49. $('#username_' + String(video_id)).text(data.username);
  50. }, result.id);
  51. }
  52. if (results.length >= 6) {
  53. window.offset += results.length;
  54. $(window).bind('scroll', load_more);
  55. }
  56. }
  57. function search(query) {
  58. $('#search_field').val(query);
  59. $('#suggestions').hide();
  60. $('#search_button').addClass('is-loading');
  61. api('search', {tags: query, offset: window.offset}, function (data) {
  62. render_results(data.videos);
  63. $('#search_button').removeClass('is-loading');
  64. });
  65. }
  66. function load_more() {
  67. if (!scrolled_down(100))
  68. return;
  69. var query = $('#search_field').val().trim();
  70. if (query.length == 0)
  71. return;
  72. search(query);
  73. $(window).unbind('scroll', load_more);
  74. }
  75. $(function() {
  76. window.offset = 0;
  77. $('#nothing_found').hide();
  78. // God Bless SO...
  79. // https://stackoverflow.com/a/901144
  80. var params = new Proxy(new URLSearchParams(window.location.search), {
  81. get: (searchParams, prop) => searchParams.get(prop),
  82. });
  83. // ^ taken from SO.
  84. if (params.q)
  85. search(params.q);
  86. });