admin.jsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import './public-path';
  2. import React from 'react';
  3. import { createRoot } from 'react-dom/client';
  4. import { delegate } from '@rails/ujs';
  5. import ready from '../mastodon/ready';
  6. const setAnnouncementEndsAttributes = (target) => {
  7. const valid = target?.value && target?.validity?.valid;
  8. const element = document.querySelector('input[type="datetime-local"]#announcement_ends_at');
  9. if (valid) {
  10. element.classList.remove('optional');
  11. element.required = true;
  12. element.min = target.value;
  13. } else {
  14. element.classList.add('optional');
  15. element.removeAttribute('required');
  16. element.removeAttribute('min');
  17. }
  18. };
  19. delegate(document, 'input[type="datetime-local"]#announcement_starts_at', 'change', ({ target }) => {
  20. setAnnouncementEndsAttributes(target);
  21. });
  22. const batchCheckboxClassName = '.batch-checkbox input[type="checkbox"]';
  23. const showSelectAll = () => {
  24. const selectAllMatchingElement = document.querySelector('.batch-table__select-all');
  25. selectAllMatchingElement.classList.add('active');
  26. };
  27. const hideSelectAll = () => {
  28. const selectAllMatchingElement = document.querySelector('.batch-table__select-all');
  29. const hiddenField = document.querySelector('#select_all_matching');
  30. const selectedMsg = document.querySelector('.batch-table__select-all .selected');
  31. const notSelectedMsg = document.querySelector('.batch-table__select-all .not-selected');
  32. selectAllMatchingElement.classList.remove('active');
  33. selectedMsg.classList.remove('active');
  34. notSelectedMsg.classList.add('active');
  35. hiddenField.value = '0';
  36. };
  37. delegate(document, '#batch_checkbox_all', 'change', ({ target }) => {
  38. const selectAllMatchingElement = document.querySelector('.batch-table__select-all');
  39. [].forEach.call(document.querySelectorAll(batchCheckboxClassName), (content) => {
  40. content.checked = target.checked;
  41. });
  42. if (selectAllMatchingElement) {
  43. if (target.checked) {
  44. showSelectAll();
  45. } else {
  46. hideSelectAll();
  47. }
  48. }
  49. });
  50. delegate(document, '.batch-table__select-all button', 'click', () => {
  51. const hiddenField = document.querySelector('#select_all_matching');
  52. const active = hiddenField.value === '1';
  53. const selectedMsg = document.querySelector('.batch-table__select-all .selected');
  54. const notSelectedMsg = document.querySelector('.batch-table__select-all .not-selected');
  55. if (active) {
  56. hiddenField.value = '0';
  57. selectedMsg.classList.remove('active');
  58. notSelectedMsg.classList.add('active');
  59. } else {
  60. hiddenField.value = '1';
  61. notSelectedMsg.classList.remove('active');
  62. selectedMsg.classList.add('active');
  63. }
  64. });
  65. delegate(document, batchCheckboxClassName, 'change', () => {
  66. const checkAllElement = document.querySelector('#batch_checkbox_all');
  67. const selectAllMatchingElement = document.querySelector('.batch-table__select-all');
  68. if (checkAllElement) {
  69. checkAllElement.checked = [].every.call(document.querySelectorAll(batchCheckboxClassName), (content) => content.checked);
  70. checkAllElement.indeterminate = !checkAllElement.checked && [].some.call(document.querySelectorAll(batchCheckboxClassName), (content) => content.checked);
  71. if (selectAllMatchingElement) {
  72. if (checkAllElement.checked) {
  73. showSelectAll();
  74. } else {
  75. hideSelectAll();
  76. }
  77. }
  78. }
  79. });
  80. delegate(document, '.media-spoiler-show-button', 'click', () => {
  81. [].forEach.call(document.querySelectorAll('button.media-spoiler'), (element) => {
  82. element.click();
  83. });
  84. });
  85. delegate(document, '.media-spoiler-hide-button', 'click', () => {
  86. [].forEach.call(document.querySelectorAll('.spoiler-button.spoiler-button--visible button'), (element) => {
  87. element.click();
  88. });
  89. });
  90. delegate(document, '.filter-subset--with-select select', 'change', ({ target }) => {
  91. target.form.submit();
  92. });
  93. const onDomainBlockSeverityChange = (target) => {
  94. const rejectMediaDiv = document.querySelector('.input.with_label.domain_block_reject_media');
  95. const rejectReportsDiv = document.querySelector('.input.with_label.domain_block_reject_reports');
  96. if (rejectMediaDiv) {
  97. rejectMediaDiv.style.display = (target.value === 'suspend') ? 'none' : 'block';
  98. }
  99. if (rejectReportsDiv) {
  100. rejectReportsDiv.style.display = (target.value === 'suspend') ? 'none' : 'block';
  101. }
  102. };
  103. delegate(document, '#domain_block_severity', 'change', ({ target }) => onDomainBlockSeverityChange(target));
  104. const onEnableBootstrapTimelineAccountsChange = (target) => {
  105. const bootstrapTimelineAccountsField = document.querySelector('#form_admin_settings_bootstrap_timeline_accounts');
  106. if (bootstrapTimelineAccountsField) {
  107. bootstrapTimelineAccountsField.disabled = !target.checked;
  108. if (target.checked) {
  109. bootstrapTimelineAccountsField.parentElement.classList.remove('disabled');
  110. bootstrapTimelineAccountsField.parentElement.parentElement.classList.remove('disabled');
  111. } else {
  112. bootstrapTimelineAccountsField.parentElement.classList.add('disabled');
  113. bootstrapTimelineAccountsField.parentElement.parentElement.classList.add('disabled');
  114. }
  115. }
  116. };
  117. delegate(document, '#form_admin_settings_enable_bootstrap_timeline_accounts', 'change', ({ target }) => onEnableBootstrapTimelineAccountsChange(target));
  118. const onChangeRegistrationMode = (target) => {
  119. const enabled = target.value === 'approved';
  120. [].forEach.call(document.querySelectorAll('.form_admin_settings_registrations_mode .warning-hint'), (warning_hint) => {
  121. warning_hint.style.display = target.value === 'open' ? 'inline' : 'none';
  122. });
  123. [].forEach.call(document.querySelectorAll('#form_admin_settings_require_invite_text'), (input) => {
  124. input.disabled = !enabled;
  125. if (enabled) {
  126. let element = input;
  127. do {
  128. element.classList.remove('disabled');
  129. element = element.parentElement;
  130. } while (element && !element.classList.contains('fields-group'));
  131. } else {
  132. let element = input;
  133. do {
  134. element.classList.add('disabled');
  135. element = element.parentElement;
  136. } while (element && !element.classList.contains('fields-group'));
  137. }
  138. });
  139. };
  140. const convertUTCDateTimeToLocal = (value) => {
  141. const date = new Date(value + 'Z');
  142. const twoChars = (x) => (x.toString().padStart(2, '0'));
  143. return `${date.getFullYear()}-${twoChars(date.getMonth()+1)}-${twoChars(date.getDate())}T${twoChars(date.getHours())}:${twoChars(date.getMinutes())}`;
  144. };
  145. const convertLocalDatetimeToUTC = (value) => {
  146. const re = /^([0-9]{4,})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2})/;
  147. const match = re.exec(value);
  148. const date = new Date(match[1], match[2] - 1, match[3], match[4], match[5]);
  149. const fullISO8601 = date.toISOString();
  150. return fullISO8601.slice(0, fullISO8601.indexOf('T') + 6);
  151. };
  152. delegate(document, '#form_admin_settings_registrations_mode', 'change', ({ target }) => onChangeRegistrationMode(target));
  153. ready(() => {
  154. const domainBlockSeverityInput = document.getElementById('domain_block_severity');
  155. if (domainBlockSeverityInput) onDomainBlockSeverityChange(domainBlockSeverityInput);
  156. const enableBootstrapTimelineAccounts = document.getElementById('form_admin_settings_enable_bootstrap_timeline_accounts');
  157. if (enableBootstrapTimelineAccounts) onEnableBootstrapTimelineAccountsChange(enableBootstrapTimelineAccounts);
  158. const registrationMode = document.getElementById('form_admin_settings_registrations_mode');
  159. if (registrationMode) onChangeRegistrationMode(registrationMode);
  160. const checkAllElement = document.querySelector('#batch_checkbox_all');
  161. if (checkAllElement) {
  162. checkAllElement.checked = [].every.call(document.querySelectorAll(batchCheckboxClassName), (content) => content.checked);
  163. checkAllElement.indeterminate = !checkAllElement.checked && [].some.call(document.querySelectorAll(batchCheckboxClassName), (content) => content.checked);
  164. }
  165. document.querySelector('a#add-instance-button')?.addEventListener('click', (e) => {
  166. const domain = document.querySelector('input[type="text"]#by_domain')?.value;
  167. if (domain) {
  168. const url = new URL(event.target.href);
  169. url.searchParams.set('_domain', domain);
  170. e.target.href = url;
  171. }
  172. });
  173. [].forEach.call(document.querySelectorAll('input[type="datetime-local"]'), element => {
  174. if (element.value) {
  175. element.value = convertUTCDateTimeToLocal(element.value);
  176. }
  177. if (element.placeholder) {
  178. element.placeholder = convertUTCDateTimeToLocal(element.placeholder);
  179. }
  180. });
  181. delegate(document, 'form', 'submit', ({ target }) => {
  182. [].forEach.call(target.querySelectorAll('input[type="datetime-local"]'), element => {
  183. if (element.value && element.validity.valid) {
  184. element.value = convertLocalDatetimeToUTC(element.value);
  185. }
  186. });
  187. });
  188. const announcementStartsAt = document.querySelector('input[type="datetime-local"]#announcement_starts_at');
  189. if (announcementStartsAt) {
  190. setAnnouncementEndsAttributes(announcementStartsAt);
  191. }
  192. [].forEach.call(document.querySelectorAll('[data-admin-component]'), element => {
  193. const componentName = element.getAttribute('data-admin-component');
  194. const componentProps = JSON.parse(element.getAttribute('data-props'));
  195. import('../mastodon/containers/admin_component').then(({ default: AdminComponent }) => {
  196. return import('../mastodon/components/admin/' + componentName).then(({ default: Component }) => {
  197. const root = createRoot(element);
  198. root.render (
  199. <AdminComponent>
  200. <Component {...componentProps} />
  201. </AdminComponent>,
  202. );
  203. });
  204. }).catch(error => {
  205. console.error(error);
  206. });
  207. });
  208. });