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 */
  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. init_load_more(panel);
  27. init_posts();
  28. document.getElementById('overlay').classList.add('visible');
  29. panel.classList.add('visible');
  30. document.documentElement.classList.add('slider-active');
  31. // Force the initial scroll to the top.
  32. // Without it, if one scrolls down in a category (for instance)
  33. // and then open another one, we risk being at the same scroll position
  34. panel.scrollTop = 0;
  35. document.documentElement.scrollTop = 0;
  36. // We already have a click listener in main.js
  37. panel.addEventListener('click', function (ev) {
  38. const b = ev.target.closest('#nav_menu_read_all button, #bigMarkAsRead');
  39. if (b) {
  40. console.log(b.formAction);
  41. const req2 = new XMLHttpRequest();
  42. req2.open('POST', b.formAction, false);
  43. req2.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
  44. req2.send(JSON.stringify({
  45. _csrf: context.csrf,
  46. }));
  47. if (req2.status == 200) {
  48. location.reload(false);
  49. return false;
  50. }
  51. }
  52. });
  53. panel_loading = false;
  54. };
  55. req.send();
  56. }
  57. function init_close_panel() {
  58. const panel = document.getElementById('panel');
  59. document.querySelector('#overlay .close').onclick = function (ev) {
  60. panel.innerHTML = '';
  61. panel.classList.remove('visible');
  62. document.getElementById('overlay').classList.remove('visible');
  63. document.documentElement.classList.remove('slider-active');
  64. return false;
  65. };
  66. document.addEventListener('keydown', ev => {
  67. const k = (ev.key.trim() || ev.code).toUpperCase();
  68. if (k === 'ESCAPE' || k === 'ESC') {
  69. document.querySelector('#overlay .close').click();
  70. }
  71. return false;
  72. });
  73. }
  74. function init_global_view() {
  75. // TODO: should be based on generic classes
  76. document.querySelectorAll('.box a').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