feed.js 4.5 KB

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