feed.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0
  2. 'use strict';
  3. /* globals init_archiving, init_configuration_alert, init_password_observers, init_slider */
  4. // <popup>
  5. let popup = null;
  6. let popup_iframe_container = null;
  7. let popup_iframe = null;
  8. let popup_working = false;
  9. function openPopupWithSource(source) {
  10. if (popup_working === true) {
  11. return false;
  12. }
  13. popup_working = true;
  14. popup_iframe.src = source;
  15. popup_iframe_container.style.display = 'table-row';
  16. popup.style.display = 'block';
  17. }
  18. function closePopup() {
  19. popup.style.display = 'none';
  20. popup_iframe_container.style.display = 'none';
  21. popup_iframe.src = 'about:blank';
  22. popup_working = false;
  23. }
  24. function init_popup() {
  25. // Fetch elements.
  26. popup = document.getElementById('popup');
  27. if (popup) {
  28. popup_iframe_container = document.getElementById('popup-iframe-container');
  29. popup_iframe = document.getElementById('popup-iframe');
  30. // Configure close button.
  31. document.getElementById('popup-close').addEventListener('click', function (ev) {
  32. closePopup();
  33. });
  34. // Configure close-on-click.
  35. window.addEventListener('click', function (ev) {
  36. if (ev.target == popup) {
  37. closePopup();
  38. }
  39. });
  40. }
  41. }
  42. // </popup>
  43. function init_popup_preview_selector() {
  44. const link = document.getElementById('popup-preview-selector');
  45. if (!link) {
  46. return;
  47. }
  48. link.addEventListener('click', function (ev) {
  49. const selector_entries = document.getElementById('path_entries').value;
  50. const href = link.href.replace('selector-token', encodeURIComponent(selector_entries));
  51. openPopupWithSource(href);
  52. ev.preventDefault();
  53. });
  54. }
  55. /**
  56. * Allow a <select class="select-show"> to hide/show elements defined by <option data-show="elem-id"></option>
  57. */
  58. function init_select_show(parent) {
  59. const listener = (select) => {
  60. const options = select.querySelectorAll('option[data-show]');
  61. for (const option of options) {
  62. const elem = document.getElementById(option.dataset.show);
  63. if (elem) {
  64. elem.style.display = option.selected ? 'block' : 'none';
  65. }
  66. }
  67. };
  68. const selects = parent.querySelectorAll('select.select-show');
  69. for (const select of selects) {
  70. select.addEventListener('change', (e) => listener(e.target));
  71. listener(select);
  72. }
  73. }
  74. /** Automatically validate XPath textarea fields */
  75. function init_valid_xpath(parent) {
  76. const listener = (textarea) => {
  77. const evaluator = new XPathEvaluator();
  78. try {
  79. if (textarea.value === '' || evaluator.createExpression(textarea.value) != null) {
  80. textarea.setCustomValidity('');
  81. }
  82. } catch (ex) {
  83. textarea.setCustomValidity(ex);
  84. }
  85. };
  86. const textareas = parent.querySelectorAll('textarea.valid-xpath');
  87. for (const textarea of textareas) {
  88. textarea.addEventListener('change', (e) => listener(e.target));
  89. listener(textarea);
  90. }
  91. }
  92. function init_feed_afterDOM() {
  93. if (!window.init_slider) {
  94. if (window.console) {
  95. console.log('FreshRSS feed waiting for JS…');
  96. }
  97. setTimeout(init_feed_afterDOM, 50);
  98. return;
  99. }
  100. const slider = document.getElementById('slider');
  101. if (slider) {
  102. slider.addEventListener('freshrss:slider-load', function (e) {
  103. init_popup();
  104. init_popup_preview_selector();
  105. init_select_show(slider);
  106. init_password_observers(slider);
  107. init_valid_xpath(slider);
  108. });
  109. init_slider(slider);
  110. init_archiving(slider);
  111. } else {
  112. init_archiving(document.body);
  113. init_popup();
  114. init_popup_preview_selector();
  115. init_select_show(document.body);
  116. init_password_observers(document.body);
  117. init_valid_xpath(document.body);
  118. }
  119. init_configuration_alert();
  120. if (window.console) {
  121. console.log('FreshRSS feed init done.');
  122. }
  123. }
  124. if (document.readyState && document.readyState !== 'loading') {
  125. init_feed_afterDOM();
  126. } else {
  127. document.addEventListener('DOMContentLoaded', function () {
  128. if (window.console) {
  129. console.log('FreshRSS feed waiting for DOMContentLoaded…');
  130. }
  131. init_feed_afterDOM();
  132. }, false);
  133. }
  134. // @license-end