Skip to content

Commit

Permalink
Disable irrelevant fields unless cleanup is enabled (mastodon#26562)
Browse files Browse the repository at this point in the history
  • Loading branch information
c960657 committed Aug 4, 2024
1 parent 19f4aa1 commit 3d6e8d6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
36 changes: 36 additions & 0 deletions app/javascript/entrypoints/public.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,42 @@ Rails.delegate(document, 'img.custom-emoji', 'mouseout', ({ target }) => {
target.src = target.dataset.static;
});

const setInputDisabled = (
input: HTMLInputElement | HTMLSelectElement,
disabled: boolean,
) => {
input.disabled = disabled;

const wrapper = input.closest('.with_label');
if (wrapper) {
wrapper.classList.toggle('disabled', input.disabled);

const hidden =
input.type === 'checkbox' &&
wrapper.querySelector<HTMLInputElement>('input[type=hidden][value="0"]');
if (hidden) {
hidden.disabled = input.disabled;
}
}
};

Rails.delegate(
document,
'#account_statuses_cleanup_policy_enabled',
'change',
({ target }) => {
if (!(target instanceof HTMLInputElement) || !target.form) return;

target.form
.querySelectorAll<
HTMLInputElement | HTMLSelectElement
>('input:not([type=hidden], #account_statuses_cleanup_policy_enabled), select')
.forEach((input) => {
setInputDisabled(input, !target.checked);
});
},
);

// Empty the honeypot fields in JS in case something like an extension
// automatically filled them.
Rails.delegate(document, '#registration_new_user,#new_user', 'submit', () => {
Expand Down
9 changes: 9 additions & 0 deletions app/views/statuses_cleanup/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
wrapper: :with_label
.fields-row__column.fields-row__column-6.fields-group
= f.input :min_status_age,
disabled: !@policy.enabled?,
collection: AccountStatusesCleanupPolicy::ALLOWED_MIN_STATUS_AGE.map(&:to_i),
hint: false,
include_blank: false,
Expand All @@ -28,35 +29,41 @@
.fields-row
.fields-row__column.fields-row__column-6.fields-group
= f.input :keep_pinned,
disabled: !@policy.enabled?,
hint: t('statuses_cleanup.keep_pinned_hint'),
label: t('statuses_cleanup.keep_pinned'),
wrapper: :with_label
.fields-row__column.fields-row__column-6.fields-group
= f.input :keep_direct,
disabled: !@policy.enabled?,
hint: t('statuses_cleanup.keep_direct_hint'),
label: t('statuses_cleanup.keep_direct'),
wrapper: :with_label

.fields-row
.fields-row__column.fields-row__column-6.fields-group
= f.input :keep_self_fav,
disabled: !@policy.enabled?,
hint: t('statuses_cleanup.keep_self_fav_hint'),
label: t('statuses_cleanup.keep_self_fav'),
wrapper: :with_label
.fields-row__column.fields-row__column-6.fields-group
= f.input :keep_self_bookmark,
disabled: !@policy.enabled?,
hint: t('statuses_cleanup.keep_self_bookmark_hint'),
label: t('statuses_cleanup.keep_self_bookmark'),
wrapper: :with_label

.fields-row
.fields-row__column.fields-row__column-6.fields-group
= f.input :keep_polls,
disabled: !@policy.enabled?,
hint: t('statuses_cleanup.keep_polls_hint'),
label: t('statuses_cleanup.keep_polls'),
wrapper: :with_label
.fields-row__column.fields-row__column-6.fields-group
= f.input :keep_media,
disabled: !@policy.enabled?,
hint: t('statuses_cleanup.keep_media_hint'),
label: t('statuses_cleanup.keep_media'),
wrapper: :with_label
Expand All @@ -66,12 +73,14 @@
.fields-row
.fields-row__column.fields-row__column-6.fields-group
= f.input :min_favs,
disabled: !@policy.enabled?,
hint: t('statuses_cleanup.min_favs_hint'),
input_html: { min: 1, placeholder: t('statuses_cleanup.ignore_favs') },
label: t('statuses_cleanup.min_favs'),
wrapper: :with_label
.fields-row__column.fields-row__column-6.fields-group
= f.input :min_reblogs,
disabled: !@policy.enabled?,
hint: t('statuses_cleanup.min_reblogs_hint'),
input_html: { min: 1, placeholder: t('statuses_cleanup.ignore_reblogs') },
label: t('statuses_cleanup.min_reblogs'),
Expand Down

0 comments on commit 3d6e8d6

Please sign in to comment.