admin.js 9.2 KB

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