123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- import './public-path';
- import { createRoot } from 'react-dom/client';
- import Rails from '@rails/ujs';
- import ready from '../mastodon/ready';
- const setAnnouncementEndsAttributes = (target: HTMLInputElement) => {
- const valid = target.value && target.validity.valid;
- const element = document.querySelector<HTMLInputElement>(
- 'input[type="datetime-local"]#announcement_ends_at',
- );
- if (!element) return;
- if (valid) {
- element.classList.remove('optional');
- element.required = true;
- element.min = target.value;
- } else {
- element.classList.add('optional');
- element.removeAttribute('required');
- element.removeAttribute('min');
- }
- };
- Rails.delegate(
- document,
- 'input[type="datetime-local"]#announcement_starts_at',
- 'change',
- ({ target }) => {
- if (target instanceof HTMLInputElement)
- setAnnouncementEndsAttributes(target);
- },
- );
- const batchCheckboxClassName = '.batch-checkbox input[type="checkbox"]';
- const showSelectAll = () => {
- const selectAllMatchingElement = document.querySelector(
- '.batch-table__select-all',
- );
- selectAllMatchingElement?.classList.add('active');
- };
- const hideSelectAll = () => {
- const selectAllMatchingElement = document.querySelector(
- '.batch-table__select-all',
- );
- const hiddenField = document.querySelector<HTMLInputElement>(
- 'input#select_all_matching',
- );
- const selectedMsg = document.querySelector(
- '.batch-table__select-all .selected',
- );
- const notSelectedMsg = document.querySelector(
- '.batch-table__select-all .not-selected',
- );
- selectAllMatchingElement?.classList.remove('active');
- selectedMsg?.classList.remove('active');
- notSelectedMsg?.classList.add('active');
- if (hiddenField) hiddenField.value = '0';
- };
- Rails.delegate(document, '#batch_checkbox_all', 'change', ({ target }) => {
- if (!(target instanceof HTMLInputElement)) return;
- const selectAllMatchingElement = document.querySelector(
- '.batch-table__select-all',
- );
- document
- .querySelectorAll<HTMLInputElement>(batchCheckboxClassName)
- .forEach((content) => {
- content.checked = target.checked;
- });
- if (selectAllMatchingElement) {
- if (target.checked) {
- showSelectAll();
- } else {
- hideSelectAll();
- }
- }
- });
- Rails.delegate(document, '.batch-table__select-all button', 'click', () => {
- const hiddenField = document.querySelector<HTMLInputElement>(
- '#select_all_matching',
- );
- if (!hiddenField) return;
- const active = hiddenField.value === '1';
- const selectedMsg = document.querySelector(
- '.batch-table__select-all .selected',
- );
- const notSelectedMsg = document.querySelector(
- '.batch-table__select-all .not-selected',
- );
- if (!selectedMsg || !notSelectedMsg) return;
- if (active) {
- hiddenField.value = '0';
- selectedMsg.classList.remove('active');
- notSelectedMsg.classList.add('active');
- } else {
- hiddenField.value = '1';
- notSelectedMsg.classList.remove('active');
- selectedMsg.classList.add('active');
- }
- });
- Rails.delegate(document, batchCheckboxClassName, 'change', () => {
- const checkAllElement = document.querySelector<HTMLInputElement>(
- 'input#batch_checkbox_all',
- );
- const selectAllMatchingElement = document.querySelector(
- '.batch-table__select-all',
- );
- if (checkAllElement) {
- const allCheckboxes = Array.from(
- document.querySelectorAll<HTMLInputElement>(batchCheckboxClassName),
- );
- checkAllElement.checked = allCheckboxes.every((content) => content.checked);
- checkAllElement.indeterminate =
- !checkAllElement.checked &&
- allCheckboxes.some((content) => content.checked);
- if (selectAllMatchingElement) {
- if (checkAllElement.checked) {
- showSelectAll();
- } else {
- hideSelectAll();
- }
- }
- }
- });
- Rails.delegate(
- document,
- '.filter-subset--with-select select',
- 'change',
- ({ target }) => {
- if (target instanceof HTMLSelectElement) target.form?.submit();
- },
- );
- const onDomainBlockSeverityChange = (target: HTMLSelectElement) => {
- const rejectMediaDiv = document.querySelector(
- '.input.with_label.domain_block_reject_media',
- );
- const rejectReportsDiv = document.querySelector(
- '.input.with_label.domain_block_reject_reports',
- );
- if (rejectMediaDiv && rejectMediaDiv instanceof HTMLElement) {
- rejectMediaDiv.style.display =
- target.value === 'suspend' ? 'none' : 'block';
- }
- if (rejectReportsDiv && rejectReportsDiv instanceof HTMLElement) {
- rejectReportsDiv.style.display =
- target.value === 'suspend' ? 'none' : 'block';
- }
- };
- Rails.delegate(document, '#domain_block_severity', 'change', ({ target }) => {
- if (target instanceof HTMLSelectElement) onDomainBlockSeverityChange(target);
- });
- const onEnableBootstrapTimelineAccountsChange = (target: HTMLInputElement) => {
- const bootstrapTimelineAccountsField =
- document.querySelector<HTMLInputElement>(
- '#form_admin_settings_bootstrap_timeline_accounts',
- );
- if (bootstrapTimelineAccountsField) {
- bootstrapTimelineAccountsField.disabled = !target.checked;
- if (target.checked) {
- bootstrapTimelineAccountsField.parentElement?.classList.remove(
- 'disabled',
- );
- bootstrapTimelineAccountsField.parentElement?.parentElement?.classList.remove(
- 'disabled',
- );
- } else {
- bootstrapTimelineAccountsField.parentElement?.classList.add('disabled');
- bootstrapTimelineAccountsField.parentElement?.parentElement?.classList.add(
- 'disabled',
- );
- }
- }
- };
- Rails.delegate(
- document,
- '#form_admin_settings_enable_bootstrap_timeline_accounts',
- 'change',
- ({ target }) => {
- if (target instanceof HTMLInputElement)
- onEnableBootstrapTimelineAccountsChange(target);
- },
- );
- const onChangeRegistrationMode = (target: HTMLSelectElement) => {
- const enabled = target.value === 'approved';
- document
- .querySelectorAll<HTMLElement>(
- '.form_admin_settings_registrations_mode .warning-hint',
- )
- .forEach((warning_hint) => {
- warning_hint.style.display = target.value === 'open' ? 'inline' : 'none';
- });
- document
- .querySelectorAll<HTMLInputElement>(
- 'input#form_admin_settings_require_invite_text',
- )
- .forEach((input) => {
- input.disabled = !enabled;
- if (enabled) {
- let element: HTMLElement | null = input;
- do {
- element.classList.remove('disabled');
- element = element.parentElement;
- } while (element && !element.classList.contains('fields-group'));
- } else {
- let element: HTMLElement | null = input;
- do {
- element.classList.add('disabled');
- element = element.parentElement;
- } while (element && !element.classList.contains('fields-group'));
- }
- });
- };
- const convertUTCDateTimeToLocal = (value: string) => {
- const date = new Date(value + 'Z');
- const twoChars = (x: number) => x.toString().padStart(2, '0');
- return `${date.getFullYear()}-${twoChars(date.getMonth() + 1)}-${twoChars(date.getDate())}T${twoChars(date.getHours())}:${twoChars(date.getMinutes())}`;
- };
- function convertLocalDatetimeToUTC(value: string) {
- const date = new Date(value);
- const fullISO8601 = date.toISOString();
- return fullISO8601.slice(0, fullISO8601.indexOf('T') + 6);
- }
- Rails.delegate(
- document,
- '#form_admin_settings_registrations_mode',
- 'change',
- ({ target }) => {
- if (target instanceof HTMLSelectElement) onChangeRegistrationMode(target);
- },
- );
- async function mountReactComponent(element: Element) {
- const componentName = element.getAttribute('data-admin-component');
- const stringProps = element.getAttribute('data-props');
- if (!stringProps) return;
- const componentProps = JSON.parse(stringProps) as object;
- const { default: AdminComponent } = await import(
- '@/mastodon/containers/admin_component'
- );
- const { default: Component } = (await import(
- `@/mastodon/components/admin/${componentName}`
- )) as { default: React.ComponentType };
- const root = createRoot(element);
- root.render(
- <AdminComponent>
- <Component {...componentProps} />
- </AdminComponent>,
- );
- }
- ready(() => {
- const domainBlockSeveritySelect = document.querySelector<HTMLSelectElement>(
- 'select#domain_block_severity',
- );
- if (domainBlockSeveritySelect)
- onDomainBlockSeverityChange(domainBlockSeveritySelect);
- const enableBootstrapTimelineAccounts =
- document.querySelector<HTMLInputElement>(
- 'input#form_admin_settings_enable_bootstrap_timeline_accounts',
- );
- if (enableBootstrapTimelineAccounts)
- onEnableBootstrapTimelineAccountsChange(enableBootstrapTimelineAccounts);
- const registrationMode = document.querySelector<HTMLSelectElement>(
- 'select#form_admin_settings_registrations_mode',
- );
- if (registrationMode) onChangeRegistrationMode(registrationMode);
- const checkAllElement = document.querySelector<HTMLInputElement>(
- 'input#batch_checkbox_all',
- );
- if (checkAllElement) {
- const allCheckboxes = Array.from(
- document.querySelectorAll<HTMLInputElement>(batchCheckboxClassName),
- );
- checkAllElement.checked = allCheckboxes.every((content) => content.checked);
- checkAllElement.indeterminate =
- !checkAllElement.checked &&
- allCheckboxes.some((content) => content.checked);
- }
- document
- .querySelector('a#add-instance-button')
- ?.addEventListener('click', (e) => {
- const domain = document.querySelector<HTMLInputElement>(
- 'input[type="text"]#by_domain',
- )?.value;
- if (domain && e.target instanceof HTMLAnchorElement) {
- const url = new URL(e.target.href);
- url.searchParams.set('_domain', domain);
- e.target.href = url.toString();
- }
- });
- document
- .querySelectorAll<HTMLInputElement>('input[type="datetime-local"]')
- .forEach((element) => {
- if (element.value) {
- element.value = convertUTCDateTimeToLocal(element.value);
- }
- if (element.placeholder) {
- element.placeholder = convertUTCDateTimeToLocal(element.placeholder);
- }
- });
- Rails.delegate(document, 'form', 'submit', ({ target }) => {
- if (target instanceof HTMLFormElement)
- target
- .querySelectorAll<HTMLInputElement>('input[type="datetime-local"]')
- .forEach((element) => {
- if (element.value && element.validity.valid) {
- element.value = convertLocalDatetimeToUTC(element.value);
- }
- });
- });
- const announcementStartsAt = document.querySelector<HTMLInputElement>(
- 'input[type="datetime-local"]#announcement_starts_at',
- );
- if (announcementStartsAt) {
- setAnnouncementEndsAttributes(announcementStartsAt);
- }
- document.querySelectorAll('[data-admin-component]').forEach((element) => {
- void mountReactComponent(element);
- });
- }).catch((reason: unknown) => {
- throw reason;
- });
|