Repository navigation
Expand file tree
/
Copy pathoptions.js
More file actions
58 lines (54 loc) · 1.97 KB
/
Copy pathoptions.js
File metadata and controls
58 lines (54 loc) · 1.97 KB
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
import { fillLanguages, localize, send, t } from './ui.js';
localize();
const $ = id => document.getElementById(id);
const slots = [];
fillLanguages($('default-language'));
for (let index = 0; index < 6; index++) {
const label = document.createElement('label');
label.textContent = t('shortcutSlot', String(index + 1));
const select = document.createElement('select');
select.id = `shortcut-${index + 1}`;
fillLanguages(select, { empty: true });
select.addEventListener('change', updateSlots);
label.append(select);
slots.push(select);
$('shortcut-slots').append(label);
}
function updateSlots() {
const selected = new Set(slots.map(select => select.value).filter(Boolean));
for (const select of slots) {
for (const option of select.options) option.disabled = Boolean(option.value && option.value !== select.value && selected.has(option.value));
}
}
try {
const { settings } = await send({ type: 'getState' });
$('default-language').value = settings.defaultLanguage;
slots.forEach((select, index) => { select.value = settings.shortcuts[index] || ''; });
updateSlots();
$('settings-fields').disabled = false;
} catch (error) {
console.error(error);
$('status').textContent = t('loadError');
$('status').classList.add('error');
}
$('settings-form').addEventListener('input', () => { $('status').textContent = ''; });
$('settings-form').addEventListener('submit', async event => {
event.preventDefault();
const settings = {
defaultLanguage: $('default-language').value,
shortcuts: slots.map(select => select.value).filter(Boolean),
};
$('settings-fields').disabled = true;
$('status').textContent = '';
try {
await send({ type: 'saveSettings', settings });
$('status').textContent = t('settingsSaved');
$('status').classList.remove('error');
} catch (error) {
console.error(error);
$('status').textContent = t('saveError');
$('status').classList.add('error');
} finally {
$('settings-fields').disabled = false;
}
});