summaryrefslogtreecommitdiffstats
path: root/web_src/js/features/user-settings.js
blob: 717ef945e113084d5beb4c5368f674d936661238 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import {hideElem, showElem} from '../utils/dom.js';

function onPronounsDropdownUpdate() {
  const pronounsCustom = document.getElementById('pronouns-custom');
  const pronounsDropdown = document.getElementById('pronouns-dropdown');
  const pronounsInput = pronounsDropdown.querySelector('input');
  // must be kept in sync with `routers/web/user/setting/profile.go`
  const isCustom = !(
    pronounsInput.value === '' ||
    pronounsInput.value === 'he/him' ||
    pronounsInput.value === 'she/her' ||
    pronounsInput.value === 'they/them' ||
    pronounsInput.value === 'it/its' ||
    pronounsInput.value === 'any pronouns'
  );
  if (isCustom) {
    if (pronounsInput.value === '!') {
      pronounsCustom.value = '';
    } else {
      pronounsCustom.value = pronounsInput.value;
    }
    pronounsCustom.style.display = '';
  } else {
    pronounsCustom.style.display = 'none';
  }
}
function onPronounsCustomUpdate() {
  const pronounsCustom = document.getElementById('pronouns-custom');
  const pronounsInput = document.querySelector('#pronouns-dropdown input');
  pronounsInput.value = pronounsCustom.value;
}

export function initUserSettings() {
  if (!document.querySelectorAll('.user.settings.profile').length) return;

  const usernameInput = document.getElementById('username');
  if (!usernameInput) return;
  usernameInput.addEventListener('input', function () {
    const prompt = document.getElementById('name-change-prompt');
    const promptRedirect = document.getElementById('name-change-redirect-prompt');
    if (this.value.toLowerCase() !== this.getAttribute('data-name').toLowerCase()) {
      showElem(prompt);
      showElem(promptRedirect);
    } else {
      hideElem(prompt);
      hideElem(promptRedirect);
    }
  });

  const pronounsDropdown = document.getElementById('pronouns-dropdown');
  const pronounsCustom = document.getElementById('pronouns-custom');
  const pronounsInput = pronounsDropdown.querySelector('input');

  // 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.
  pronounsCustom.removeAttribute('name');
  pronounsInput.setAttribute('name', 'pronouns');
  pronounsDropdown.style.display = '';

  onPronounsDropdownUpdate();
  pronounsInput.addEventListener('change', onPronounsDropdownUpdate);
  pronounsCustom.addEventListener('input', onPronounsCustomUpdate);
}