4
0

feed.js 4.8 KB

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