forked from mathewthe2/Game2Text
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ankisettings.js
98 lines (94 loc) · 4.07 KB
/
ankisettings.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// const deckSelector = document.getElementById('deck_selector');
const deckSelector = document.getElementById('decks');
// const cardModelSelector = document.getElementById('card_model_selector');
const cardModelSelector = document.getElementById('card_models');
async function initDecks() {
const deckNames = await eel.invoke_anki('deckNames')();
if (typeof deckNames === 'string') {
// Failed to connect to Anki
return []
}
if (deckNames.length > 0) {
deckNames.forEach(deckName => {
const deckOption = document.createElement("option");
deckOption.value = deckName;
deckSelector.append(deckOption);
});
}
return deckNames;
}
async function initCardModels() {
const cardModelNames = await eel.invoke_anki('modelNames')();
if (typeof cardModelNames === 'string') {
// Failed to connect to Anki
return []
}
if (cardModelNames.length > 0) {
cardModelNames.forEach(cardModelName => {
const cardModelOption = document.createElement("option");
cardModelOption.value = cardModelName;
cardModelSelector.append(cardModelOption);
});
}
return cardModelNames;
}
async function getFieldNamesForModel(modelName) {
return await eel.invoke_anki('modelFieldNames', {modelName: modelName})()
}
function updateFieldValuesTable(fieldValues) {
const fieldValuesTable = document.getElementById('field_values_table');
if (Object.keys(fieldValues).length) {
const fieldValuesTableBody = fieldValuesTable.getElementsByTagName('tbody')[0];
const tupleTemplate = document.getElementById('field_values_tuple_template');
fieldValuesTableBody.innerHTML = '';
fieldValuesTableBody.append(tupleTemplate); // keeping the clone
fieldValues.forEach(fieldValue=>{
const tupleClone = tupleTemplate.cloneNode(true);
tupleClone.id = 'field_tuple' + fieldValue.replace(' ', '_');
const tupleLabel = tupleClone.getElementsByClassName('tuple_label')[0];
tupleLabel.innerHTML = fieldValue;
const fieldValueSelect = tupleClone.getElementsByClassName('field_value_select')[0];
fieldValueSelect.innerHTML = `<option></option>
<option>Selected Text</option>
<option>Reading</option>
<option>Glossary</option>
<option>Sentence</option>
<option>Screenshot</option>
<option>Audio</option>
<option>Word Audio</option>`;
tupleClone.hidden = false;
fieldValuesTableBody.append(tupleClone);
})
fieldValuesTable.hidden = false;
} else {
fieldValuesTable.hidden = true;
}
}
function applyFieldAndValuesToTable(fieldValueMap) {
if(fieldValueMap) {
const fieldValuesTable = document.getElementById('field_values_table');
const fieldValuesTableBody = fieldValuesTable.getElementsByTagName('tbody')[0];
const tupleTemplate = document.getElementById('field_values_tuple_template');
fieldValuesTableBody.innerHTML = '';
fieldValuesTableBody.append(tupleTemplate); // keeping the clone
for (const field in fieldValueMap) {
const tupleClone = tupleTemplate.cloneNode(true);
tupleClone.id = 'field_tuple' + field.replace(' ', '_');
const tupleLabel = tupleClone.getElementsByClassName('tuple_label')[0];
tupleLabel.innerHTML = field;
const fieldValueSelect = tupleClone.getElementsByClassName('field_value_select')[0];
fieldValueSelect.innerHTML = `<option></option>
<option>Selected Text</option>
<option>Reading</option>
<option>Glossary</option>
<option>Sentence</option>
<option>Screenshot</option>
<option>Audio</option>
<option>Word Audio</option>`;
fieldValueSelect.value = fieldValueMap[field];
tupleClone.hidden = false;
fieldValuesTableBody.append(tupleClone);
}
fieldValuesTable.hidden = false;
}
}