From 0db031817e23d1bb5fd96589312b4a68fad1c486 Mon Sep 17 00:00:00 2001 From: KindaCrayCray Date: Sat, 5 Aug 2023 15:21:06 -0400 Subject: [PATCH 1/4] Add "switch" postfix to setting switch ids Previously the id for the `marseycursor` was the same as that of the cursor cat, meaning that the settings page would not have the marsey visible. --- files/templates/settings/personal.html | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/files/templates/settings/personal.html b/files/templates/settings/personal.html index f67e60c26..14d9ec7e4 100644 --- a/files/templates/settings/personal.html +++ b/files/templates/settings/personal.html @@ -249,9 +249,11 @@ {{common.toggle_section('Private Mode', 'privateswitch', 'private', v.is_private, 'This will hide your profile page from others.', false)}} {{common.toggle_section('Lifetime donated visiblity', 'lifetimedonated_visible', 'lifetimedonated_visible', v.lifetimedonated_visible, 'Make the "lifetime donated" field in your profile visible to everyone instead of just you.', false)}} - {{common.toggle_section('Cursor Marsey', 'cursormarsey', 'cursormarsey', v.cursormarsey, 'Have a cute marsey accompany you during your journey on the site.', false)}} + {{common.toggle_section('Cursor Marsey', 'cursormarseyswitch', 'cursormarsey', v.cursormarsey, 'Have a cute + marsey accompany you during your journey on the site.', false)}} {{common.toggle_section('Spider', 'spiderswitch', 'spider', v.spider, 'Have a spider friend accompany you during your journey on the site.', v.spider > 1)}} - {{common.toggle_section('Marsify', 'marsify', 'marsify', v.marsify, 'Automatically insert relevant marseys into your future comments.', v.marsify > 1)}} + {{common.toggle_section('Marsify', 'marsifyswitch', 'marsify', v.marsify, 'Automatically insert relevant + marseys into your future comments.', v.marsify > 1)}} -- 2.34.1 From 0fcef3f556662d07dfda2e020b6e8f6571451900 Mon Sep 17 00:00:00 2001 From: KindaCrayCray Date: Sun, 6 Aug 2023 11:07:17 -0400 Subject: [PATCH 2/4] Create new handler for settings toggles The previous handler was shared with several semi-related components and was just a mess. The new one is simplified and will reset itself on error. --- files/assets/js/bottom.js | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/files/assets/js/bottom.js b/files/assets/js/bottom.js index 21869aa07..78e42c9db 100644 --- a/files/assets/js/bottom.js +++ b/files/assets/js/bottom.js @@ -66,12 +66,37 @@ for (const element of undisable_element) { const setting_switchs = document.getElementsByClassName('setting_switch'); for (const element of setting_switchs) { + console.log(element); if (element.dataset.nonce != nonce) { console.log("Nonce check failed!") continue } - element.addEventListener('change', () => { - postToastSwitch(element,`/settings/personal?${element.name}=${element.checked}`); + element.addEventListener('change', async (event) => { + element.disabled = true; + element.classList.add("disabled"); + const form = new FormData(); + form.append("formkey", formkey()); + const res = await fetch(`/settings/personal?${element.name}=${element.checked}`, { + method: "POST", + headers: { + xhr: "xhr", + }, + body: form, + }).catch(() => ({ok: false})); + let message; + if (res.ok) { + ({message} = await res.json()); + } else { + // toggle the input back if the request doesn't go through + element.checked = !element.checked; + } + let oldToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-' + (res.ok ? 'error': 'success'))); // intentionally reversed here: this is the old toast + oldToast.hide(); + showToast(res.ok, message); + element.disabled = false; + element.classList.remove("disabled"); + + // postToastSwitch(element,`/settings/personal?${element.name}=${element.checked}`); }); } -- 2.34.1 From 0f2a1a94592d0fdcac51735199c4db785f0e5689 Mon Sep 17 00:00:00 2001 From: KindaCrayCray Date: Sun, 6 Aug 2023 15:39:51 -0400 Subject: [PATCH 3/4] Create new function for handling setting switches The new function properly undos failed updates to setting toggles and also shows and hides the permanent option with the slur/profanity filters appropriately. --- files/assets/js/bottom.js | 67 +++++++++++++++----------- files/assets/js/settings_profile.js | 11 ----- files/templates/settings/personal.html | 27 ++++++----- 3 files changed, 53 insertions(+), 52 deletions(-) diff --git a/files/assets/js/bottom.js b/files/assets/js/bottom.js index 78e42c9db..985f3d619 100644 --- a/files/assets/js/bottom.js +++ b/files/assets/js/bottom.js @@ -64,40 +64,51 @@ for (const element of undisable_element) { }); } +async function handleSettingSwitch(event) { + let input = event.currentTarget; + input.disabled = true; + input.classList.add("disabled"); + const form = new FormData(); + form.append("formkey", formkey()); + const res = await fetch( + `/settings/personal?${input.name}=${input.checked}`, + { + method: "POST", + headers: { + xhr: "xhr", + }, + body: form, + }, + ).catch(() => ({ ok: false })); + let message; + if (res.ok) { + ({message} = await res.json()); + // the slur and profanity replacers have special make-permanent switches + if (["slurreplacerswitch", "profanityreplacerswitch"].includes(input.id)) { + document.getElementById( + `${input.id.replace("switch", "")}-perma-link` + ).hidden = !input.checked; + } + } else { + // toggle the input back if the request doesn't go through + input.checked = !input.checked; + } + let oldToast = bootstrap.Toast.getOrCreateInstance( + document.getElementById('toast-post-' + (res.ok ? 'error': 'success')) + ); // intentionally reversed here: this is the old toast + oldToast.hide(); + showToast(res.ok, message); + input.disabled = false; + input.classList.remove("disabled"); +} + const setting_switchs = document.getElementsByClassName('setting_switch'); for (const element of setting_switchs) { - console.log(element); if (element.dataset.nonce != nonce) { console.log("Nonce check failed!") continue } - element.addEventListener('change', async (event) => { - element.disabled = true; - element.classList.add("disabled"); - const form = new FormData(); - form.append("formkey", formkey()); - const res = await fetch(`/settings/personal?${element.name}=${element.checked}`, { - method: "POST", - headers: { - xhr: "xhr", - }, - body: form, - }).catch(() => ({ok: false})); - let message; - if (res.ok) { - ({message} = await res.json()); - } else { - // toggle the input back if the request doesn't go through - element.checked = !element.checked; - } - let oldToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-' + (res.ok ? 'error': 'success'))); // intentionally reversed here: this is the old toast - oldToast.hide(); - showToast(res.ok, message); - element.disabled = false; - element.classList.remove("disabled"); - - // postToastSwitch(element,`/settings/personal?${element.name}=${element.checked}`); - }); + element.addEventListener('change', handleSettingSwitch); } const setting_selects = document.getElementsByClassName('setting_select'); diff --git a/files/assets/js/settings_profile.js b/files/assets/js/settings_profile.js index afa34a052..90b6295f7 100644 --- a/files/assets/js/settings_profile.js +++ b/files/assets/js/settings_profile.js @@ -98,14 +98,3 @@ function updatebgselection(){ } } updatebgselection(); - -const sr_toggle = document.getElementById("slurreplacer"); -const sr_link = document.getElementById('slurreplacer-perma-link'); -const pr_toggle = document.getElementById("profanityreplacer"); -const pr_link = document.getElementById('profanityreplacer-perma-link'); -sr_toggle.addEventListener('change', function () { - sr_link.hidden = !sr_toggle.checked; -}); -pr_toggle.addEventListener('change', function () { - pr_link.hidden = !pr_toggle.checked; -}); diff --git a/files/templates/settings/personal.html b/files/templates/settings/personal.html index 14d9ec7e4..fc69ad992 100644 --- a/files/templates/settings/personal.html +++ b/files/templates/settings/personal.html @@ -247,7 +247,7 @@ {{common.text_area_section('profile-signature', '/settings/personal', 'sig', 'Signature', v.sig, 'Limit of 200 characters', 'Enter a signature...', true, false, 200)}} {{common.toggle_section('Private Mode', 'privateswitch', 'private', v.is_private, 'This will hide your profile page from others.', false)}} - {{common.toggle_section('Lifetime donated visiblity', 'lifetimedonated_visible', 'lifetimedonated_visible', v.lifetimedonated_visible, 'Make the "lifetime donated" field in your profile visible to everyone instead of just you.', false)}} + {{common.toggle_section('Lifetime donated visiblity', 'lifetimedonated_visibleswitch', 'lifetimedonated_visible', v.lifetimedonated_visible, 'Make the "lifetime donated" field in your profile visible to everyone instead of just you.', false)}} {{common.toggle_section('Cursor Marsey', 'cursormarseyswitch', 'cursormarsey', v.cursormarsey, 'Have a cute marsey accompany you during your journey on the site.', false)}} @@ -270,22 +270,27 @@
Filters
{% set ns = namespace(slurtext='Enable if you would like to automatically replace slurs.', profanitytext='Enable if you would like to automatically replace profanities.') %} - {% if FEATURES['USERS_PERMANENT_WORD_FILTERS'] and v.slurreplacer %} - {% if v.slurreplacer == 1 %} - {% set ns.slurtext = 'Enable if you would like to automatically replace slurs. Make filter permanent for a badge!' %} + {% if FEATURES['USERS_PERMANENT_WORD_FILTERS'] %} + {% if v.slurreplacer == 0 %} + {% set ns.slurtext = 'Enable if you would like to automatically replace slurs. ' %} + {% elif v.slurreplacer == 1 %} + {% set ns.slurtext = 'Enable if you would like to automatically replace slurs. Make filter permanent for a badge!' %} {% else %} {% set ns.slurtext = "You've enabled the slur replacer permanently! ✊🏿" %} {% endif %} {% endif %} - {% if FEATURES['USERS_PERMANENT_WORD_FILTERS'] and v.profanityreplacer %} - {% if v.profanityreplacer == 1 %} - {% set ns.profanitytext = 'Enable if you would like to automatically replace profanities. Make filter permanent for a badge!' %} + {% if FEATURES['USERS_PERMANENT_WORD_FILTERS'] %} + {% if v.profanityreplacer == 0 %} + {% set ns.profanitytext = 'Enable if you would like to automatically replace profanities. ' %} + {% elif v.profanityreplacer == 1 %} + {% set ns.profanitytext = 'Enable if you would like to automatically replace profanities. Make filter permanent for a badge!' %} {% else %} {% set ns.profanitytext = "You've enabled the profanity replacer permanently! 😇" %} {% endif %} {% endif %} - {{common.toggle_section("Slur Replacer", "slurreplacer", 'slurreplacer', v.slurreplacer, ns.slurtext, FEATURES['USERS_PERMANENT_WORD_FILTERS'] and v.slurreplacer > 1)}} - {{common.toggle_section("Profanity Replacer", "profanityreplacer", 'profanityreplacer', v.profanityreplacer, ns.profanitytext, FEATURES['USERS_PERMANENT_WORD_FILTERS'] and v.profanityreplacer > 1)}} + {{common.toggle_section("Slur Replacer", "slurreplacerswitch", 'slurreplacer', v.slurreplacer, ns.slurtext, FEATURES['USERS_PERMANENT_WORD_FILTERS'] and v.slurreplacer > 1)}} + {{common.toggle_section("Profanity Replacer", "profanityreplacerswitch", 'profanityreplacer', v + .profanityreplacer, ns.profanitytext, FEATURES['USERS_PERMANENT_WORD_FILTERS'] and v.profanityreplacer > 1)}}
@@ -354,10 +359,6 @@ {%- endif %} {% endmacro %} -{% if v.slurreplacer == 1 -%} {{permanent_filter_modal('slurreplacer', '/settings/personal', 'slurreplacer', 'Slur Replacer', 'Social Justice Berserker')}} -{%- endif %} -{% if v.profanityreplacer == 1 -%} {{permanent_filter_modal('profanityreplacer', '/settings/personal', 'profanityreplacer', 'Profanity Replacer', 'Soapy-Mouthed Angel')}} -{%- endif %} {% endblock %} -- 2.34.1 From a1874efd3ee3b576d9055130692605d833738141 Mon Sep 17 00:00:00 2001 From: KindaCrayCray Date: Sun, 6 Aug 2023 15:45:17 -0400 Subject: [PATCH 4/4] Reformat code --- files/templates/settings/personal.html | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/files/templates/settings/personal.html b/files/templates/settings/personal.html index fc69ad992..8f35fa154 100644 --- a/files/templates/settings/personal.html +++ b/files/templates/settings/personal.html @@ -278,8 +278,7 @@ {% else %} {% set ns.slurtext = "You've enabled the slur replacer permanently! ✊🏿" %} {% endif %} - {% endif %} - {% if FEATURES['USERS_PERMANENT_WORD_FILTERS'] %} + {% if v.profanityreplacer == 0 %} {% set ns.profanitytext = 'Enable if you would like to automatically replace profanities. ' %} {% elif v.profanityreplacer == 1 %} @@ -289,8 +288,7 @@ {% endif %} {% endif %} {{common.toggle_section("Slur Replacer", "slurreplacerswitch", 'slurreplacer', v.slurreplacer, ns.slurtext, FEATURES['USERS_PERMANENT_WORD_FILTERS'] and v.slurreplacer > 1)}} - {{common.toggle_section("Profanity Replacer", "profanityreplacerswitch", 'profanityreplacer', v - .profanityreplacer, ns.profanitytext, FEATURES['USERS_PERMANENT_WORD_FILTERS'] and v.profanityreplacer > 1)}} + {{common.toggle_section("Profanity Replacer", "profanityreplacerswitch", 'profanityreplacer', v .profanityreplacer, ns.profanitytext, FEATURES['USERS_PERMANENT_WORD_FILTERS'] and v.profanityreplacer > 1)}}
-- 2.34.1