community.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. 'use strict';
  2. var community_data = JSON.parse(document.getElementById('community_data').textContent);
  3. function hide_youtube_replies(event) {
  4. var target = event.target;
  5. var sub_text = target.getAttribute('data-inner-text');
  6. var inner_text = target.getAttribute('data-sub-text');
  7. var body = target.parentNode.parentNode.children[1];
  8. body.style.display = 'none';
  9. target.innerHTML = sub_text;
  10. target.onclick = show_youtube_replies;
  11. target.setAttribute('data-inner-text', inner_text);
  12. target.setAttribute('data-sub-text', sub_text);
  13. }
  14. function show_youtube_replies(event) {
  15. var target = event.target;
  16. var sub_text = target.getAttribute('data-inner-text');
  17. var inner_text = target.getAttribute('data-sub-text');
  18. var body = target.parentNode.parentNode.children[1];
  19. body.style.display = '';
  20. target.innerHTML = sub_text;
  21. target.onclick = hide_youtube_replies;
  22. target.setAttribute('data-inner-text', inner_text);
  23. target.setAttribute('data-sub-text', sub_text);
  24. }
  25. function get_youtube_replies(target, load_more) {
  26. var continuation = target.getAttribute('data-continuation');
  27. var body = target.parentNode.parentNode;
  28. var fallback = body.innerHTML;
  29. body.innerHTML =
  30. '<h3 style="text-align:center"><div class="loading"><i class="icon ion-ios-refresh"></i></div></h3>';
  31. var url = '/api/v1/channels/comments/' + community_data.ucid +
  32. '?format=html' +
  33. '&hl=' + community_data.preferences.locale +
  34. '&thin_mode=' + community_data.preferences.thin_mode +
  35. '&continuation=' + continuation;
  36. helpers.xhr('GET', url, {}, {
  37. on200: function (response) {
  38. if (load_more) {
  39. body = body.parentNode.parentNode;
  40. body.removeChild(body.lastElementChild);
  41. body.innerHTML += response.contentHtml;
  42. } else {
  43. body.removeChild(body.lastElementChild);
  44. var p = document.createElement('p');
  45. var a = document.createElement('a');
  46. p.appendChild(a);
  47. a.href = 'javascript:void(0)';
  48. a.onclick = hide_youtube_replies;
  49. a.setAttribute('data-sub-text', community_data.hide_replies_text);
  50. a.setAttribute('data-inner-text', community_data.show_replies_text);
  51. a.textContent = community_data.hide_replies_text;
  52. var div = document.createElement('div');
  53. div.innerHTML = response.contentHtml;
  54. body.appendChild(p);
  55. body.appendChild(div);
  56. }
  57. },
  58. onNon200: function (xhr) {
  59. body.innerHTML = fallback;
  60. },
  61. onTimeout: function (xhr) {
  62. console.warn('Pulling comments failed');
  63. body.innerHTML = fallback;
  64. }
  65. });
  66. }