admin.jsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import './public-path';
  2. import React from 'react';
  3. import { createRoot } from 'react-dom/client';
  4. import Rails 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. Rails.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. Rails.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. Rails.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. Rails.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. Rails.delegate(document, '.media-spoiler-show-button', 'click', () => {
  81. [].forEach.call(document.querySelectorAll('button.media-spoiler'), (element) => {
  82. element.click();
  83. });
  84. });
  85. Rails.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. Rails.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. Rails.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. Rails.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_require_invite_text'), (input) => {
  121. input.disabled = !enabled;
  122. if (enabled) {
  123. let element = input;
  124. do {
  125. element.classList.remove('disabled');
  126. element = element.parentElement;
  127. } while (element && !element.classList.contains('fields-group'));
  128. } else {
  129. let element = input;
  130. do {
  131. element.classList.add('disabled');
  132. element = element.parentElement;
  133. } while (element && !element.classList.contains('fields-group'));
  134. }
  135. });
  136. };
  137. const convertUTCDateTimeToLocal = (value) => {
  138. const date = new Date(value + 'Z');
  139. const twoChars = (x) => (x.toString().padStart(2, '0'));
  140. return `${date.getFullYear()}-${twoChars(date.getMonth()+1)}-${twoChars(date.getDate())}T${twoChars(date.getHours())}:${twoChars(date.getMinutes())}`;
  141. };
  142. const convertLocalDatetimeToUTC = (value) => {
  143. const re = /^([0-9]{4,})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2})/;
  144. const match = re.exec(value);
  145. const date = new Date(match[1], match[2] - 1, match[3], match[4], match[5]);
  146. const fullISO8601 = date.toISOString();
  147. return fullISO8601.slice(0, fullISO8601.indexOf('T') + 6);
  148. };
  149. Rails.delegate(document, '#form_admin_settings_registrations_mode', 'change', ({ target }) => onChangeRegistrationMode(target));
  150. ready(() => {
  151. const domainBlockSeverityInput = document.getElementById('domain_block_severity');
  152. if (domainBlockSeverityInput) onDomainBlockSeverityChange(domainBlockSeverityInput);
  153. const enableBootstrapTimelineAccounts = document.getElementById('form_admin_settings_enable_bootstrap_timeline_accounts');
  154. if (enableBootstrapTimelineAccounts) onEnableBootstrapTimelineAccountsChange(enableBootstrapTimelineAccounts);
  155. const registrationMode = document.getElementById('form_admin_settings_registrations_mode');
  156. if (registrationMode) onChangeRegistrationMode(registrationMode);
  157. const checkAllElement = document.querySelector('#batch_checkbox_all');
  158. if (checkAllElement) {
  159. checkAllElement.checked = [].every.call(document.querySelectorAll(batchCheckboxClassName), (content) => content.checked);
  160. checkAllElement.indeterminate = !checkAllElement.checked && [].some.call(document.querySelectorAll(batchCheckboxClassName), (content) => content.checked);
  161. }
  162. document.querySelector('a#add-instance-button')?.addEventListener('click', (e) => {
  163. const domain = document.querySelector('input[type="text"]#by_domain')?.value;
  164. if (domain) {
  165. const url = new URL(event.target.href);
  166. url.searchParams.set('_domain', domain);
  167. e.target.href = url;
  168. }
  169. });
  170. [].forEach.call(document.querySelectorAll('input[type="datetime-local"]'), element => {
  171. if (element.value) {
  172. element.value = convertUTCDateTimeToLocal(element.value);
  173. }
  174. if (element.placeholder) {
  175. element.placeholder = convertUTCDateTimeToLocal(element.placeholder);
  176. }
  177. });
  178. Rails.delegate(document, 'form', 'submit', ({ target }) => {
  179. [].forEach.call(target.querySelectorAll('input[type="datetime-local"]'), element => {
  180. if (element.value && element.validity.valid) {
  181. element.value = convertLocalDatetimeToUTC(element.value);
  182. }
  183. });
  184. });
  185. const announcementStartsAt = document.querySelector('input[type="datetime-local"]#announcement_starts_at');
  186. if (announcementStartsAt) {
  187. setAnnouncementEndsAttributes(announcementStartsAt);
  188. }
  189. [].forEach.call(document.querySelectorAll('[data-admin-component]'), element => {
  190. const componentName = element.getAttribute('data-admin-component');
  191. const componentProps = JSON.parse(element.getAttribute('data-props'));
  192. import('../mastodon/containers/admin_component').then(({ default: AdminComponent }) => {
  193. return import('../mastodon/components/admin/' + componentName).then(({ default: Component }) => {
  194. const root = createRoot(element);
  195. root.render (
  196. <AdminComponent>
  197. <Component {...componentProps} />
  198. </AdminComponent>,
  199. );
  200. });
  201. }).catch(error => {
  202. console.error(error);
  203. });
  204. });
  205. });