admin.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import './public-path';
  2. import { delegate } from '@rails/ujs';
  3. import ready from '../mastodon/ready';
  4. const batchCheckboxClassName = '.batch-checkbox input[type="checkbox"]';
  5. delegate(document, '#batch_checkbox_all', 'change', ({ target }) => {
  6. [].forEach.call(document.querySelectorAll(batchCheckboxClassName), (content) => {
  7. content.checked = target.checked;
  8. });
  9. });
  10. delegate(document, batchCheckboxClassName, 'change', () => {
  11. const checkAllElement = document.querySelector('#batch_checkbox_all');
  12. if (checkAllElement) {
  13. checkAllElement.checked = [].every.call(document.querySelectorAll(batchCheckboxClassName), (content) => content.checked);
  14. checkAllElement.indeterminate = !checkAllElement.checked && [].some.call(document.querySelectorAll(batchCheckboxClassName), (content) => content.checked);
  15. }
  16. });
  17. delegate(document, '.media-spoiler-show-button', 'click', () => {
  18. [].forEach.call(document.querySelectorAll('button.media-spoiler'), (element) => {
  19. element.click();
  20. });
  21. });
  22. delegate(document, '.media-spoiler-hide-button', 'click', () => {
  23. [].forEach.call(document.querySelectorAll('.spoiler-button.spoiler-button--visible button'), (element) => {
  24. element.click();
  25. });
  26. });
  27. delegate(document, '.filter-subset--with-select select', 'change', ({ target }) => {
  28. target.form.submit();
  29. });
  30. const onDomainBlockSeverityChange = (target) => {
  31. const rejectMediaDiv = document.querySelector('.input.with_label.domain_block_reject_media');
  32. const rejectReportsDiv = document.querySelector('.input.with_label.domain_block_reject_reports');
  33. if (rejectMediaDiv) {
  34. rejectMediaDiv.style.display = (target.value === 'suspend') ? 'none' : 'block';
  35. }
  36. if (rejectReportsDiv) {
  37. rejectReportsDiv.style.display = (target.value === 'suspend') ? 'none' : 'block';
  38. }
  39. };
  40. delegate(document, '#domain_block_severity', 'change', ({ target }) => onDomainBlockSeverityChange(target));
  41. const onEnableBootstrapTimelineAccountsChange = (target) => {
  42. const bootstrapTimelineAccountsField = document.querySelector('#form_admin_settings_bootstrap_timeline_accounts');
  43. if (bootstrapTimelineAccountsField) {
  44. bootstrapTimelineAccountsField.disabled = !target.checked;
  45. if (target.checked) {
  46. bootstrapTimelineAccountsField.parentElement.classList.remove('disabled');
  47. bootstrapTimelineAccountsField.parentElement.parentElement.classList.remove('disabled');
  48. } else {
  49. bootstrapTimelineAccountsField.parentElement.classList.add('disabled');
  50. bootstrapTimelineAccountsField.parentElement.parentElement.classList.add('disabled');
  51. }
  52. }
  53. };
  54. delegate(document, '#form_admin_settings_enable_bootstrap_timeline_accounts', 'change', ({ target }) => onEnableBootstrapTimelineAccountsChange(target));
  55. const onChangeRegistrationMode = (target) => {
  56. const enabled = target.value === 'approved';
  57. [].forEach.call(document.querySelectorAll('#form_admin_settings_require_invite_text'), (input) => {
  58. input.disabled = !enabled;
  59. if (enabled) {
  60. let element = input;
  61. do {
  62. element.classList.remove('disabled');
  63. element = element.parentElement;
  64. } while (element && !element.classList.contains('fields-group'));
  65. } else {
  66. let element = input;
  67. do {
  68. element.classList.add('disabled');
  69. element = element.parentElement;
  70. } while (element && !element.classList.contains('fields-group'));
  71. }
  72. });
  73. };
  74. delegate(document, '#form_admin_settings_registrations_mode', 'change', ({ target }) => onChangeRegistrationMode(target));
  75. ready(() => {
  76. const domainBlockSeverityInput = document.getElementById('domain_block_severity');
  77. if (domainBlockSeverityInput) onDomainBlockSeverityChange(domainBlockSeverityInput);
  78. const enableBootstrapTimelineAccounts = document.getElementById('form_admin_settings_enable_bootstrap_timeline_accounts');
  79. if (enableBootstrapTimelineAccounts) onEnableBootstrapTimelineAccountsChange(enableBootstrapTimelineAccounts);
  80. const registrationMode = document.getElementById('form_admin_settings_registrations_mode');
  81. if (registrationMode) onChangeRegistrationMode(registrationMode);
  82. document.querySelector('a#add-instance-button')?.addEventListener('click', (e) => {
  83. const domain = document.getElementById('by_domain')?.value;
  84. if (domain) {
  85. const url = new URL(event.target.href);
  86. url.searchParams.set('_domain', domain);
  87. e.target.href = url;
  88. }
  89. });
  90. const React = require('react');
  91. const ReactDOM = require('react-dom');
  92. [].forEach.call(document.querySelectorAll('[data-admin-component]'), element => {
  93. const componentName = element.getAttribute('data-admin-component');
  94. const { locale, ...componentProps } = JSON.parse(element.getAttribute('data-props'));
  95. import('../mastodon/containers/admin_component').then(({ default: AdminComponent }) => {
  96. return import('../mastodon/components/admin/' + componentName).then(({ default: Component }) => {
  97. ReactDOM.render((
  98. <AdminComponent locale={locale}>
  99. <Component {...componentProps} />
  100. </AdminComponent>
  101. ), element);
  102. });
  103. }).catch(error => {
  104. console.error(error);
  105. });
  106. });
  107. });