0
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2025-02-11 19:14:46 -05:00
forgejo/web_src/js/features/user-settings.js

51 lines
2 KiB
JavaScript
Raw Normal View History

import {hideElem, showElem} from '../utils/dom.js';
2023-09-26 00:56:20 -05:00
function onPronounsDropdownUpdate() {
const pronounsCustom = document.getElementById('label-pronouns-custom');
const pronounsCustomInput = pronounsCustom.querySelector('input');
const pronounsDropdown = document.getElementById('pronouns-dropdown');
const pronounsInput = pronounsDropdown.querySelector('input');
2024-02-26 13:43:04 -06:00
// must be kept in sync with `routers/web/user/setting/profile.go`
2023-09-26 00:56:20 -05:00
const isCustom = !(
pronounsInput.value === '' ||
2023-09-26 00:56:20 -05:00
pronounsInput.value === 'he/him' ||
pronounsInput.value === 'she/her' ||
pronounsInput.value === 'they/them' ||
2023-09-26 12:54:18 -05:00
pronounsInput.value === 'it/its' ||
2024-02-24 12:24:12 -06:00
pronounsInput.value === 'any pronouns'
2023-09-26 00:56:20 -05:00
);
if (isCustom) {
if (pronounsInput.value === '!') {
pronounsCustomInput.value = '';
} else {
pronounsCustomInput.value = pronounsInput.value;
}
showElem(pronounsCustom);
2023-09-26 00:56:20 -05:00
} else {
hideElem(pronounsCustom);
2023-09-26 00:56:20 -05:00
}
}
function onPronounsCustomUpdate() {
const pronounsCustomInput = document.querySelector('#label-pronouns-custom input');
2023-09-26 00:56:20 -05:00
const pronounsInput = document.querySelector('#pronouns-dropdown input');
pronounsInput.value = pronounsCustomInput.value;
2023-09-26 00:56:20 -05:00
}
export function initUserSettings() {
if (!document.querySelectorAll('.user.settings.profile').length) return;
const pronounsDropdown = document.getElementById('label-pronouns');
const pronounsCustomInput = document.querySelector('#label-pronouns-custom input');
2023-09-26 00:56:20 -05:00
const pronounsInput = pronounsDropdown.querySelector('input');
2024-02-26 13:43:04 -06:00
// If JS is disabled, the page will show the custom input, as the dropdown requires JS to work.
// JS progressively enhances the input by adding a dropdown, but it works regardless.
pronounsCustomInput.removeAttribute('name');
pronounsInput.setAttribute('name', 'pronouns');
showElem(pronounsDropdown);
2024-02-26 13:43:04 -06:00
2023-09-26 00:56:20 -05:00
onPronounsDropdownUpdate();
pronounsInput.addEventListener('change', onPronounsDropdownUpdate);
pronounsCustomInput.addEventListener('input', onPronounsCustomUpdate);
}