global_view.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 */
  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, 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, #mark-read-pagination, #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. init_load_more(panel);
  27. init_posts();
  28. document.getElementById('overlay').classList.add('visible');
  29. panel.classList.add('visible');
  30. // Force the initial scroll to the top.
  31. // Without it, if one scrolls down in a category (for instance)
  32. // and then open another one, we risk being at the same scroll position
  33. panel.scrollTop = 0;
  34. document.documentElement.scrollTop = 0;
  35. // We already have a click listener in main.js
  36. panel.addEventListener('click', function (ev) {
  37. const b = ev.target.closest('#nav_menu_read_all button, #bigMarkAsRead');
  38. if (b) {
  39. console.log(b.formAction);
  40. const req2 = new XMLHttpRequest();
  41. req2.open('POST', b.formAction, false);
  42. req2.setRequestHeader('Content-Type', 'application/json');
  43. req2.send(JSON.stringify({
  44. _csrf: context.csrf,
  45. }));
  46. if (req2.status == 200) {
  47. location.reload(false);
  48. return false;
  49. }
  50. }
  51. });
  52. panel_loading = false;
  53. };
  54. req.send();
  55. }
  56. function init_close_panel() {
  57. const panel = document.getElementById('panel');
  58. document.querySelector('#overlay .close').onclick = function (ev) {
  59. panel.innerHTML = '';
  60. panel.classList.remove('visible');
  61. document.getElementById('overlay').classList.remove('visible');
  62. return false;
  63. };
  64. document.addEventListener('keydown', ev => {
  65. const k = (ev.key.trim() || ev.code).toUpperCase();
  66. if (k === 'ESCAPE' || k === 'ESC') {
  67. document.querySelector('#overlay .close').click();
  68. }
  69. return false;
  70. });
  71. }
  72. function init_global_view() {
  73. // TODO: should be based on generic classes
  74. document.querySelectorAll('.box a').forEach(function (a) {
  75. a.onclick = function (ev) {
  76. load_panel(a.href);
  77. return false;
  78. };
  79. });
  80. document.querySelectorAll('.nav_menu #nav_menu_read_all, .nav_menu .toggle_aside').forEach(function (el) {
  81. el.remove();
  82. });
  83. const panel = document.getElementById('panel');
  84. init_stream(panel);
  85. }
  86. function init_all_global_view() {
  87. if (!window.context) {
  88. if (window.console) {
  89. console.log('FreshRSS Global view waiting for JS…');
  90. }
  91. window.setTimeout(init_all_global_view, 50); // Wait for all js to be loaded
  92. return;
  93. }
  94. init_global_view();
  95. init_close_panel();
  96. }
  97. if (document.readyState && document.readyState !== 'loading') {
  98. init_all_global_view();
  99. } else {
  100. document.addEventListener('DOMContentLoaded', function () {
  101. init_all_global_view();
  102. }, false);
  103. }
  104. // @license-end