feed.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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, init_url_observers */
  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 selector_entries_filter = document.getElementById('path_entries_filter').value;
  51. const href = link.href.replace('selector-token', encodeURIComponent(selector_entries))
  52. .replace('selector-filter-token', encodeURIComponent(selector_entries_filter));
  53. openPopupWithSource(href);
  54. ev.preventDefault();
  55. });
  56. }
  57. /**
  58. * Allow a <select class="select-show"> to hide/show elements defined by <option data-show="elem-id"></option>
  59. */
  60. function init_disable_elements_on_update(parent) {
  61. const inputs = parent.querySelectorAll('input[data-disable-update]');
  62. for (const input of inputs) {
  63. input.addEventListener('input', (e) => {
  64. const elem = document.getElementById(e.target.dataset.disableUpdate);
  65. if (elem) {
  66. elem.disabled = true;
  67. elem.remove();
  68. }
  69. });
  70. }
  71. }
  72. /**
  73. * Allow a <select class="select-show"> to hide/show elements defined by <option data-show="elem-id"></option>
  74. */
  75. function init_select_show(parent) {
  76. const listener = (select) => {
  77. const options = select.querySelectorAll('option[data-show]');
  78. for (const option of options) {
  79. const elem = document.getElementById(option.dataset.show);
  80. if (elem) {
  81. elem.style.display = option.selected ? 'block' : 'none';
  82. }
  83. }
  84. };
  85. const selects = parent.querySelectorAll('select.select-show');
  86. for (const select of selects) {
  87. select.addEventListener('change', (e) => listener(e.target));
  88. listener(select);
  89. }
  90. }
  91. /** Automatically validate XPath textarea fields */
  92. function init_valid_xpath(parent) {
  93. const listener = (textarea) => {
  94. const evaluator = new XPathEvaluator();
  95. try {
  96. if (textarea.value === '' || evaluator.createExpression(textarea.value) != null) {
  97. textarea.setCustomValidity('');
  98. }
  99. } catch (ex) {
  100. textarea.setCustomValidity(ex);
  101. }
  102. };
  103. const textareas = parent.querySelectorAll('textarea.valid-xpath');
  104. for (const textarea of textareas) {
  105. textarea.addEventListener('change', (e) => listener(e.target));
  106. listener(textarea);
  107. }
  108. }
  109. function init_feed_afterDOM() {
  110. if (!window.init_slider) {
  111. if (window.console) {
  112. console.log('FreshRSS feed waiting for JS…');
  113. }
  114. setTimeout(init_feed_afterDOM, 50);
  115. return;
  116. }
  117. const slider = document.getElementById('slider');
  118. if (slider) {
  119. slider.addEventListener('freshrss:slider-load', function (e) {
  120. init_popup();
  121. init_popup_preview_selector();
  122. init_select_show(slider);
  123. init_disable_elements_on_update(slider);
  124. init_password_observers(slider);
  125. init_url_observers(slider);
  126. init_valid_xpath(slider);
  127. });
  128. init_slider(slider);
  129. init_archiving(slider);
  130. } else {
  131. init_archiving(document.body);
  132. init_popup();
  133. init_popup_preview_selector();
  134. init_select_show(document.body);
  135. init_disable_elements_on_update(document.body);
  136. init_password_observers(document.body);
  137. init_valid_xpath(document.body);
  138. }
  139. init_configuration_alert();
  140. if (window.console) {
  141. console.log('FreshRSS feed init done.');
  142. }
  143. }
  144. if (document.readyState && document.readyState !== 'loading') {
  145. init_feed_afterDOM();
  146. } else {
  147. document.addEventListener('DOMContentLoaded', function () {
  148. if (window.console) {
  149. console.log('FreshRSS feed waiting for DOMContentLoaded…');
  150. }
  151. init_feed_afterDOM();
  152. }, false);
  153. }
  154. // @license-end