2019-05-09 14:24:47 -06:00
|
|
|
import { SETTINGS_KEY, DEFAULT_SETTINGS } from "../constants";
|
2019-05-23 18:09:01 -06:00
|
|
|
import { cloneObject, removeTags } from "../helpers";
|
2019-05-09 14:24:47 -06:00
|
|
|
import { usePhondueDigraphs } from "./KeyboardFire/phondue/ipaField";
|
|
|
|
import { renderWords } from "./render";
|
2019-05-23 18:09:01 -06:00
|
|
|
import { addMessage, hasToken } from "./utilities";
|
2019-05-10 13:18:38 -06:00
|
|
|
import { enableHotKeys, disableHotKeys } from "./hotkeys";
|
2019-05-09 14:24:47 -06:00
|
|
|
|
|
|
|
export function loadSettings() {
|
|
|
|
const storedSettings = window.localStorage.getItem(SETTINGS_KEY);
|
|
|
|
window.settings = storedSettings ? JSON.parse(storedSettings) : cloneObject(DEFAULT_SETTINGS);
|
2019-06-05 17:28:49 -06:00
|
|
|
toggleIPAPronunciationFields(false);
|
2019-05-09 14:24:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
export function saveSettings() {
|
|
|
|
window.localStorage.setItem(SETTINGS_KEY, JSON.stringify(window.settings));
|
2019-05-09 14:34:32 -06:00
|
|
|
addMessage('Settings Saved!');
|
2019-05-09 14:24:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
export function openSettingsModal() {
|
2019-05-31 13:11:01 -06:00
|
|
|
const { useIPAPronunciationField, useHotkeys, defaultTheme } = window.settings;
|
2019-05-09 14:24:47 -06:00
|
|
|
|
|
|
|
document.getElementById('settingsUseIPA').checked = useIPAPronunciationField;
|
2019-05-10 13:18:38 -06:00
|
|
|
document.getElementById('settingsUseHotkeys').checked = useHotkeys;
|
2019-05-31 13:11:01 -06:00
|
|
|
document.getElementById('settingsDefaultTheme').value = defaultTheme;
|
2019-05-09 14:24:47 -06:00
|
|
|
|
|
|
|
document.getElementById('settingsModal').style.display = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
export function saveSettingsModal() {
|
|
|
|
window.settings.useIPAPronunciationField = document.getElementById('settingsUseIPA').checked;
|
2019-05-10 13:18:38 -06:00
|
|
|
window.settings.useHotkeys = document.getElementById('settingsUseHotkeys').checked;
|
2019-05-31 13:11:01 -06:00
|
|
|
window.settings.defaultTheme = document.getElementById('settingsDefaultTheme').value;
|
2019-05-09 14:24:47 -06:00
|
|
|
|
2019-05-23 18:09:01 -06:00
|
|
|
if (hasToken()) {
|
|
|
|
import('./account/index.js').then(account => {
|
|
|
|
const emailField = document.getElementById('accountSettingsEmail');
|
|
|
|
let email = removeTags(emailField.value).trim();
|
|
|
|
const publicName = document.getElementById('accountSettingsPublicName');
|
|
|
|
if (!/.+@.+\..+/.test(email)) {
|
|
|
|
email = window.account.email;
|
|
|
|
emailField.value = email;
|
|
|
|
}
|
|
|
|
window.account.email = email;
|
|
|
|
window.account.publicName = removeTags(publicName.value).trim();
|
|
|
|
window.account.allowEmails = document.getElementById('accountSettingsAllowEmails').checked;
|
|
|
|
|
2019-05-23 18:25:52 -06:00
|
|
|
const newPassword = document.getElementById('accountSettingsNewPassword').value;
|
|
|
|
|
|
|
|
account.editAccount(Object.assign({ newPassword }, window.account));
|
2019-05-23 18:09:01 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-09 14:24:47 -06:00
|
|
|
saveSettings();
|
2019-05-10 13:18:38 -06:00
|
|
|
toggleHotkeysEnabled();
|
2019-05-09 14:24:47 -06:00
|
|
|
toggleIPAPronunciationFields();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function saveAndCloseSettingsModal() {
|
|
|
|
saveSettingsModal();
|
|
|
|
document.getElementById('settingsModal').style.display = 'none';
|
|
|
|
}
|
|
|
|
|
2019-05-10 13:18:38 -06:00
|
|
|
export function toggleHotkeysEnabled() {
|
|
|
|
disableHotKeys();
|
|
|
|
if (window.settings.useHotkeys) {
|
|
|
|
enableHotKeys();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 17:28:49 -06:00
|
|
|
export function toggleIPAPronunciationFields(render = true) {
|
2019-05-09 14:24:47 -06:00
|
|
|
const ipaButtons = document.querySelectorAll('.ipa-table-button, .ipa-field-help-button'),
|
|
|
|
ipaFields = document.querySelectorAll('.ipa-field');
|
|
|
|
if (!window.settings.useIPAPronunciationField) {
|
|
|
|
Array.from(ipaButtons).forEach(button => {
|
|
|
|
button.style.display = 'none';
|
|
|
|
});
|
|
|
|
Array.from(ipaFields).forEach(field => {
|
|
|
|
field.removeEventListener('keypress', usePhondueDigraphs);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Array.from(ipaButtons).forEach(button => {
|
|
|
|
button.style.display = '';
|
|
|
|
});
|
|
|
|
Array.from(ipaFields).forEach(field => {
|
|
|
|
field.addEventListener('keypress', usePhondueDigraphs);
|
|
|
|
});
|
|
|
|
}
|
2019-06-05 17:28:49 -06:00
|
|
|
if (render) {
|
|
|
|
renderWords();
|
|
|
|
}
|
2019-05-09 14:24:47 -06:00
|
|
|
}
|