global_view.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0
  2. 'use strict';
  3. /* globals context, init_load_more, init_posts, init_stream, enforce_referrer_allowlist */
  4. let panel_loading = false;
  5. function load_panel(link) {
  6. if (panel_loading) {
  7. return;
  8. }
  9. panel_loading = true;
  10. const req = new XMLHttpRequest();
  11. req.open('GET', link + '&ajax=1', true);
  12. req.responseType = 'document';
  13. req.onload = function (e) {
  14. if (this.status != 200) {
  15. return;
  16. }
  17. const html = this.response;
  18. const foreign = html.querySelectorAll('.nav_menu, #stream .day, #stream .flux, #stream-footer, #stream.prompt');
  19. const panel = document.getElementById('panel');
  20. foreign.forEach(function (el) {
  21. panel.appendChild(document.adoptNode(el));
  22. });
  23. panel.querySelectorAll('.nav_menu > :not([id="nav_menu_read_all"])').forEach(function (el) {
  24. el.remove();
  25. });
  26. enforce_referrer_allowlist(panel);
  27. init_load_more(panel);
  28. init_posts();
  29. document.getElementById('overlay').classList.add('visible');
  30. panel.classList.add('visible');
  31. document.documentElement.classList.add('slider-active');
  32. // Force the initial scroll to the top.
  33. // Without it, if one scrolls down in a category (for instance)
  34. // and then open another one, we risk being at the same scroll position
  35. panel.scrollTop = 0;
  36. document.documentElement.scrollTop = 0;
  37. // We already have a click listener in main.js
  38. panel.addEventListener('click', function (ev) {
  39. const b = ev.target.closest('#nav_menu_read_all button, #bigMarkAsRead');
  40. if (b) {
  41. console.log(b.formAction);
  42. const req2 = new XMLHttpRequest();
  43. req2.open('POST', b.formAction, false);
  44. req2.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
  45. req2.send(JSON.stringify({
  46. _csrf: context.csrf,
  47. }));
  48. if (req2.status == 200) {
  49. location.reload(false);
  50. return false;
  51. }
  52. }
  53. });
  54. panel_loading = false;
  55. };
  56. req.send();
  57. }
  58. function init_close_panel() {
  59. const panel = document.getElementById('panel');
  60. document.querySelector('#overlay .close').onclick = function (ev) {
  61. panel.innerHTML = '';
  62. panel.classList.remove('visible');
  63. document.getElementById('overlay').classList.remove('visible');
  64. document.documentElement.classList.remove('slider-active');
  65. return false;
  66. };
  67. document.addEventListener('keydown', ev => {
  68. const k = (ev.key.trim() || ev.code).toUpperCase();
  69. if (k === 'ESCAPE' || k === 'ESC') {
  70. document.querySelector('#overlay .close').click();
  71. }
  72. return false;
  73. });
  74. }
  75. function init_global_view() {
  76. document.querySelectorAll('.open-panel').forEach(function (a) {
  77. a.onclick = function (ev) {
  78. load_panel(a.href);
  79. return false;
  80. };
  81. });
  82. document.querySelectorAll('.nav_menu #nav_menu_read_all, .nav_menu .toggle_aside').forEach(function (el) {
  83. el.remove();
  84. });
  85. const panel = document.getElementById('panel');
  86. init_stream(panel);
  87. }
  88. function init_all_global_view() {
  89. if (!window.context) {
  90. if (window.console) {
  91. console.log('FreshRSS Global view waiting for JS…');
  92. }
  93. window.setTimeout(init_all_global_view, 50); // Wait for all js to be loaded
  94. return;
  95. }
  96. init_global_view();
  97. init_close_panel();
  98. }
  99. if (document.readyState && document.readyState !== 'loading') {
  100. init_all_global_view();
  101. } else {
  102. document.addEventListener('DOMContentLoaded', function () {
  103. init_all_global_view();
  104. }, false);
  105. }
  106. // @license-end