global_view.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. "use strict";
  2. /* globals context, init_load_more, init_posts, init_stream */
  3. /* jshint esversion:6, strict:global */
  4. var 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. foreign = html.querySelectorAll('.nav_menu, #stream .day, #stream .flux, #stream .pagination, #stream.prompt'),
  19. 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 le démarrage du scroll en haut.
  31. // Sans ça, si l'on scroll en lisant une catégorie par exemple,
  32. // en en ouvrant une autre ensuite, on se retrouve au même point de scroll
  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. }
  65. function init_global_view() {
  66. // TODO: should be based on generic classes
  67. document.querySelectorAll('.box a').forEach(function (a) {
  68. a.onclick = function (ev) {
  69. load_panel(a.href);
  70. return false;
  71. };
  72. });
  73. document.querySelectorAll('.nav_menu #nav_menu_read_all, .nav_menu .toggle_aside').forEach(function (el) {
  74. el.remove();
  75. });
  76. const panel = document.getElementById('panel');
  77. init_stream(panel);
  78. }
  79. function init_all_global_view() {
  80. if (!window.context) {
  81. if (window.console) {
  82. console.log('FreshRSS Global view waiting for JS…');
  83. }
  84. window.setTimeout(init_all_global_view, 50); //Wait for all js to be loaded
  85. return;
  86. }
  87. init_global_view();
  88. init_close_panel();
  89. }
  90. if (document.readyState && document.readyState !== 'loading') {
  91. init_all_global_view();
  92. } else {
  93. document.addEventListener('DOMContentLoaded', function () {
  94. init_all_global_view();
  95. }, false);
  96. }