feed.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. function init_json_feed_detection(parent) {
  98. const url = parent.querySelector('#url_rss');
  99. const kind = parent.querySelector('#feed_kind');
  100. if (!url || !kind) {
  101. return;
  102. }
  103. const jsonKind = kind.querySelector('option[data-json-feed="1"]');
  104. const details = kind.closest('details');
  105. const detect = () => {
  106. if (kind.value === kind.options[0]?.value && jsonKind && /(?:\b|_)json(?:\b|_)/i.test(url.value)) {
  107. kind.value = jsonKind.value;
  108. kind.dispatchEvent(new Event('change', { bubbles: true }));
  109. if (details) {
  110. details.open = true;
  111. }
  112. }
  113. };
  114. url.addEventListener('input', detect);
  115. detect();
  116. }
  117. /** Automatically validate XPath textarea fields */
  118. function init_valid_xpath(parent) {
  119. const listener = (textarea) => {
  120. const evaluator = new XPathEvaluator();
  121. try {
  122. if (textarea.value === '' || evaluator.createExpression(textarea.value) != null) {
  123. textarea.setCustomValidity('');
  124. }
  125. } catch (ex) {
  126. textarea.setCustomValidity(ex);
  127. }
  128. };
  129. const textareas = parent.querySelectorAll('textarea.valid-xpath');
  130. for (const textarea of textareas) {
  131. textarea.addEventListener('change', (e) => listener(e.target));
  132. listener(textarea);
  133. }
  134. }
  135. function init_feed_afterDOM() {
  136. if (!window.init_slider) {
  137. if (window.console) {
  138. console.log('FreshRSS feed waiting for JS…');
  139. }
  140. setTimeout(init_feed_afterDOM, 50);
  141. return;
  142. }
  143. const slider = document.getElementById('slider');
  144. if (slider) {
  145. slider.addEventListener('freshrss:slider-load', function (e) {
  146. init_popup();
  147. init_popup_preview_selector();
  148. init_select_show(slider);
  149. init_disable_elements_on_update(slider);
  150. init_password_observers(slider);
  151. init_url_observers(slider);
  152. init_valid_xpath(slider);
  153. });
  154. init_slider(slider);
  155. init_archiving(slider);
  156. } else {
  157. init_archiving(document.body);
  158. init_popup();
  159. init_popup_preview_selector();
  160. init_select_show(document.body);
  161. init_json_feed_detection(document.body);
  162. init_disable_elements_on_update(document.body);
  163. init_password_observers(document.body);
  164. init_valid_xpath(document.body);
  165. }
  166. init_configuration_alert();
  167. if (window.console) {
  168. console.log('FreshRSS feed init done.');
  169. }
  170. }
  171. if (document.readyState && document.readyState !== 'loading') {
  172. init_feed_afterDOM();
  173. } else {
  174. document.addEventListener('DOMContentLoaded', function () {
  175. if (window.console) {
  176. console.log('FreshRSS feed waiting for DOMContentLoaded…');
  177. }
  178. init_feed_afterDOM();
  179. }, false);
  180. }
  181. // @license-end