global_view.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. /* jshint esversion:6, strict:global */
  5. var panel_loading = false;
  6. function load_panel(link) {
  7. if (panel_loading) {
  8. return;
  9. }
  10. panel_loading = true;
  11. const req = new XMLHttpRequest();
  12. req.open('GET', link, true);
  13. req.responseType = 'document';
  14. req.onload = function (e) {
  15. if (this.status != 200) {
  16. return;
  17. }
  18. const html = this.response,
  19. foreign = html.querySelectorAll('.nav_menu, #stream .day, #stream .flux, #stream .pagination, #stream.prompt'),
  20. panel = document.getElementById('panel');
  21. foreign.forEach(function (el) {
  22. panel.appendChild(document.adoptNode(el));
  23. });
  24. panel.querySelectorAll('.nav_menu > :not([id="nav_menu_read_all"])').forEach(function (el) {
  25. el.remove();
  26. });
  27. init_load_more(panel);
  28. init_posts();
  29. document.getElementById('overlay').classList.add('visible');
  30. panel.classList.add('visible');
  31. // force le démarrage du scroll en haut.
  32. // Sans ça, si l'on scroll en lisant une catégorie par exemple,
  33. // en en ouvrant une autre ensuite, on se retrouve au même point de scroll
  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');
  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. return false;
  64. };
  65. }
  66. function init_global_view() {
  67. // TODO: should be based on generic classes
  68. document.querySelectorAll('.box a').forEach(function (a) {
  69. a.onclick = function (ev) {
  70. load_panel(a.href);
  71. return false;
  72. };
  73. });
  74. document.querySelectorAll('.nav_menu #nav_menu_read_all, .nav_menu .toggle_aside').forEach(function (el) {
  75. el.remove();
  76. });
  77. const panel = document.getElementById('panel');
  78. init_stream(panel);
  79. }
  80. function init_all_global_view() {
  81. if (!window.context) {
  82. if (window.console) {
  83. console.log('FreshRSS Global view waiting for JS…');
  84. }
  85. window.setTimeout(init_all_global_view, 50); //Wait for all js to be loaded
  86. return;
  87. }
  88. init_global_view();
  89. init_close_panel();
  90. }
  91. if (document.readyState && document.readyState !== 'loading') {
  92. init_all_global_view();
  93. } else {
  94. document.addEventListener('DOMContentLoaded', function () {
  95. init_all_global_view();
  96. }, false);
  97. }
  98. // @license-end