-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeck-new.js
470 lines (412 loc) · 15.9 KB
/
deck-new.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
'use strict';
// Global variables, why not
window.previewCards = [];
window.previewPhrase = null;
window.writer = null;
window.currentIME = "";
window.IMEIndex = 0;
/**
* Use the current IME(if any) to return a converted string
* @param { string } string - Input string
* @return { string } Output string
*/
function convertFromIME(string)
{
if (window.currentIME === "")
return string;
return soundTables[currentIME].convert(string, IMEIndex);
}
/**
* Constructs a preview card HTML element for a phrase or card
* @param { number } index - The index into "it"
* @param { Object? } it - The struct that whose data will be used to construct the preview card
* @param { HTMLElement } owner - Parent HTML element
* @returns { Object } - Reference to the current modified object. Could be "it", a card from local storage(if "it" is
* a phrase reference, or it's null) or a new card.
*/
function constructPreviewCardGeneric(index, it, owner)
{
let lit = it;
// Local it variable because phrases will require finding the card
if (it === null || it["phrase"])
{
if (it !== null)
{
for (let i in window.localStorageData.cards)
{
if (window.localStorageData.cards[i].character === it.phrase[index])
{
lit = window.localStorageData.cards[i];
break;
}
}
for (let i in window.previewCards)
if (window.previewCards[i].character === it.phrase[index])
return window.previewCards[i];
}
if (it === null || lit === it)
{
window.previewCards.push({
name: lc.unknown_character,
character: it === null ? window.CARD_DEFAULT_CHARACTER : it.phrase[index],
variant: "",
knowledge: 0,
definitions: []
});
lit = window.previewCards[window.previewCards.length - 1];
}
}
let root = addElement("div", "", `character-preview-${index}`, "card centered", "", owner);
addElement("h3", lit.name, `card-preview-name-${index}`, "", "", root);
let writerArea = addElement("div", "", `card-character-target-div-preview-${index}`, "", "", root);
addElement("p", `${lc.deck_definitions}`, "", "", "", root);
let list = addElement("ol", "", `card-preview-list-${index}`, "", "", root);
for (let definition in lit.definitions)
addElement("li", lit.definitions[definition], "", "", "", list);
writerArea.writer = HanziWriter.create(`card-character-target-div-preview-${index}`, lit.character + lit.variant,
{
width: window.CARD_WRITER_SIZE,
height: window.CARD_WRITER_SIZE,
padding: window.WRITER_PADDING,
showOutline: true,
strokeAnimationSpeed: window.CARD_WRITER_STROKE_ANIMATION_SPEED,
delayBetweenStrokes: window.CARD_WRITER_DELAY_BETWEEN_STROKES,
charDataLoader: charDataLoader,
})
writerArea.addEventListener("mouseover", function(){
this.writer.animateCharacter();
})
return lit;
}
/**
* Constructs an input element
* @param { HTMLElement } container - The parent HTML element to attach to
* @param { string } id - ID for the input element
* @param { string } classT - Class for the input element
* @param { string } type - Type attribute of the input element
* @param { string } ariaLabel - Aria label attribute of the input element
* @param { string } name - Name attribute of the input element
* @param { string } previewID - ID of the text field in the preview card that corresponds to the owning edit card
* @param { function } callback - Callback function when the data is changed
* @returns { HTMLElement } - The resulting input element
*/
function constructInputElement(container, id, classT, type, ariaLabel, name, previewID, callback)
{
let input = addElement("input", "", id, classT, "", container);
input.setAttribute("type", type);
input.setAttribute("aria-label", ariaLabel);
input.setAttribute("name", name);
input.previewID = previewID;
input.addEventListener("change", (event) =>
{
event.target.value = convertFromIME(event.target.value);
if (previewID !== "" && event.target.value !== "")
$(previewID).textContent = event.target.value;
});
if (callback !== null)
input.addEventListener("change", callback);
return input;
}
function constructPhraseEditCardPreview(it) {
let lit = it;
if (it === null)
{
window.previewPhrase = {
name: lc.unknown_phrase,
phrase: window.CARD_DEFAULT_CHARACTER,
knowledge: 0,
definitions: []
};
lit = window.previewPhrase;
}
let phrasePreviewRoot = addElement("div", "", "character-preview-phrase", "card centered", "", $("phrase-preview-section-container"));
addElement("h3", lit.name, "card-preview-name-phrase", "", "", phrasePreviewRoot);
addElement("h1", lit.phrase, "card-character-target-div-phrase", "phrase-card-header", "", phrasePreviewRoot);
addElement("p", `${lc.deck_definitions}`, "", "", "", phrasePreviewRoot);
let list = addElement("ol", "", "card-preview-list-phrase", "", "", phrasePreviewRoot)
for (let definition in lit.definitions)
addElement("li", lit.definitions[definition], "", "", "", list);
return lit;
}
/**
* Checks if a given character variant exists
* @param { string } character - The character in question
* @param { string } postfix - Variant postfix, like "-jp" or "-ko" for Japanese and Korean respectively
* @returns {Promise<undefined|Object>} - JSON data about the given character
*/
async function testVariantExists(character, postfix)
{
return await charDataLoader(character + postfix, null, null);
}
/**
* Constructs the character variant select box
* @param { HTMLElement } container - The parent HTML element
* @param { string } id - ID for the select box
* @param { string } classT - Class for the select box
* @param { string } ariaLabel - Aria label attribute for the select box
* @param { string } name - Name attribute for the select box
* @param { Object } it - Card object this corresponds to. Used to change the character's value in the change callback
* @returns {Promise<void>}
*/
async function constructCharacterVariantSelect(container, id, classT, ariaLabel, name, it)
{
let select = addElement("select", "", id, classT, "", container);
select.setAttribute("aria-label", ariaLabel);
select.setAttribute("name", name);
select.ownerReference = it;
addElement("option", lc.character_variant_default, "", "", "", select).setAttribute("value", "");
if (await testVariantExists(it.character, "-jp") !== undefined)
addElement("option", `🇯🇵 ${lc.character_variant_kanji}`, "", "", "", select).setAttribute("value", "-jp");
if (await testVariantExists(it.character, "-ko") !== undefined)
addElement("option", `🇰🇷 ${lc.character_variant_hanja}`, "", "", "", select).setAttribute("value", "-ko");
select.addEventListener("change", function() {
this.ownerReference.variant = this.value;
});
}
/**
* Reconstructs the definition list for a given card or phrase
* @param { HTMLElement } previewList - The HTML element that contains the definitions list inside its corresponding preview card
* @param { HTMLElement } editList - The HTML element that contains the definitions list inside its corresponding edit card
* @param { string[] } definitions - The list of definition strings
* @param { boolean } bReadOnly - Whether the definition list is read only, i.e. when editing a phrase and a character
* from it has a corresponding card that is already in the deck
*/
function reconstructDefinitionList(previewList, editList, definitions, bReadOnly)
{
if (previewList === null || editList === null)
return;
previewList.replaceChildren();
editList.replaceChildren();
for (let i in definitions)
{
addElement("li", definitions[i], "", "", "", previewList);
let li = addElement("li", definitions[i], "", "", "", editList);
if (bReadOnly)
return;
addTextNode(li, " ");
let button = addElement("button", "-", "", "card-button-edit small-button", "", li);
button.previewList = previewList;
button.editList = editList;
button.definitions = definitions;
button.defIndex = i;
runEventAfterAnimation(button, "click", (e) => {
e.target.definitions.splice(e.target.defIndex, 1);
reconstructDefinitionList(e.target.previewList, e.target.editList, e.target.definitions, false)
});
}
}
/**
* Constructs an edit card
* @param { string|number } index - Index into the array that holds "it"
* @param { Object } it - Object whose data will be used to construct an edit card
* @param { HTMLElement } root - The root element, to which the edit card should be attached to
* @param { boolean } bPhrase - Whether you're editing a phrase
*/
function constructEditCard(index, it, root, bPhrase)
{
let lit = it;
// If this is set to false, the data about a card can be edited. This will create a bunch of text boxes
// to be actually able to edit the card
let bReadOnly = true;
// If editing the character is allowed. When editing new cards as part of a phrase we set this to false
// as the character is determined by the
let bAllowChangingCharacter = true;
// Is a phrase object but not representing a phrase
if (it["phrase"] && !bPhrase)
{
for (let f in window.localStorageData.cards)
{
if (it.phrase[index] === window.localStorageData.cards[f].character)
{
lit = window.localStorageData.cards[f];
break;
}
}
for (let i in window.previewCards)
{
if (window.previewCards[i].character === it.phrase[index])
{
lit = window.previewCards[i];
bReadOnly = false;
for (let f = index - 1; f >= 0; --f)
if (it.phrase[f] === it.phrase[index])
return;
break;
}
}
}
else if (it["phrase"] && bPhrase)
bReadOnly = false;
else if (it["character"])
bReadOnly = false;
let container = addElement("div", "", `edit-phrase-${index}`, "card centered", "", root);
container.setAttribute("yy-readonly", bReadOnly.toString());
addElement("h3", `${lc.card_name}: ` + (bReadOnly
? lit.name
: ""),
"", "", "", container);
if (!bReadOnly)
{
constructInputElement(container, `name-text-field-${index}`, "", "text", lc.name_text_field_aria, lc.name_text_field_aria, `card-preview-name-${index}`, (event) => {
lit.name = event.target.value;
}).value = lit !== null ? lit.name : "";
}
addElement("h3",
bPhrase ? `${lc.card_phrase}: ` + (bReadOnly
? lit.phrase
: "")
: `${lc.card_character}: ` + (bReadOnly || !bAllowChangingCharacter
? lit.character
: ""),
"", "", "", container);
let characterInput = null;
if (!bReadOnly && bAllowChangingCharacter)
{
characterInput = constructInputElement(container, `character-text-field-${index}`, "", "text", lc.character_text_field_aria, lc.character_text_field_aria, "", null);
characterInput.value = lit !== null ? (lit["character"] ? lit.character : lit.phrase) : "";
}
if (lit !== null && characterInput !== null)
{
if (lit["phrase"])
{
characterInput.addEventListener("change", (event) => {
let phrasePreviewContainer = $("phrase-preview-section-container");
let cardEditSection = $("card-edit-section");
// Clear children and reconstruct previews
phrasePreviewContainer.replaceChildren();
cardEditSection.replaceChildren();
lit.phrase = event.target.value === "" ? window.CARD_DEFAULT_CHARACTER : event.target.value;
window.previewCards = [];
constructPhraseEditCardPreview(lit, phrasePreviewContainer);
constructEditCard("phrase", lit, cardEditSection, true);
for (let i in lit.phrase)
{
constructPreviewCardGeneric(i, lit, phrasePreviewContainer);
constructEditCard(i, lit, cardEditSection, false);
}
});
}
else if (lit["character"])
{
characterInput.addEventListener("change", (e) => {
$(`card-character-target-div-preview-${index}`).writer.setCharacter(e.target.value + lit.variant);
lit.character = e.target.value;
})
}
}
if (!bPhrase && !bReadOnly)
{
// A little padding
addTextNode(container, " ");
constructCharacterVariantSelect(container, `character-variant-box-${index}`, "centered", lc.character_variant_box_aria, lc.character_variant_box_aria, lit).then(_ => {});
}
addElement("h3", `${lc.deck_definitions}`, "", "", "", container);
if (!bReadOnly)
{
constructInputElement(container, `meaning-text-field-${index}`, "", "text", lc.meaning_text_field_aria, lc.meaning_text_field_aria, "", null);
// A little padding
addTextNode(container, " ");
let addButton = addElement("button", "+", `add-meaning-list-button-${index}`, "card-button-edit small-button", "", container);
runEventAfterAnimation(addButton, "click", (_) => {
let textField = $(`meaning-text-field-${index}`);
lit.definitions.push(textField.value);
textField.value = "";
reconstructDefinitionList($(`card-preview-list-${index}`), $(`definition-list-current-edit-${index}`), lit.definitions, false)
});
}
let ol = addElement("ol", "", `definition-list-current-edit-${index}`, "", "", container);
reconstructDefinitionList($(`card-preview-list-${index}`), ol, lit.definitions, bReadOnly)
}
function constructListElements()
{
const urlParams = new URLSearchParams(window.location.search);
let dataContainer = null;
let index = null;
let deleteButton = $("delete-edit-button");
if (urlParams.has("edit"))
{
dataContainer = window.localStorageData.cards;
index = urlParams.get("edit");
deleteButton.style.display = "inline-block";
deleteButton.cardIndex = index;
runEventAfterAnimation(deleteButton, "click", (e) => {
if (confirm(lc.deck_new_delete_card))
{
window.localStorageData.cards.splice(e.target.cardIndex, 1);
saveToLocalStorage(window.localStorageData);
location.href = "./deck.html";
}
});
}
else if (urlParams.has("phrase-edit"))
{
dataContainer = window.localStorageData.phrases;
index = urlParams.get("phrase-edit");
$("phrase-preview-section").style.display = "block";
deleteButton.style.display = "inline-block";
deleteButton.cardIndex = index;
runEventAfterAnimation(deleteButton, "click", (e) => {
if (confirm(lc.deck_new_delete_phrase))
{
window.localStorageData.phrases.splice(e.target.cardIndex, 1);
saveToLocalStorage(window.localStorageData);
location.href = "./deck.html";
}
});
}
let cardEditSection = $("card-edit-section");
if (dataContainer !== null && index <= dataContainer.length)
{
let it = dataContainer[index];
if (it["character"])
{
constructPreviewCardGeneric(0, it, cardEditSection);
constructEditCard(0, it, cardEditSection, true);
for (let i = 0; i < cardEditSection.childNodes.length; i++)
cardEditSection.insertBefore(cardEditSection.childNodes[i], cardEditSection.firstChild);
}
else
{
let phrasePreviewSectionContainer = $("phrase-preview-section-container");
constructPhraseEditCardPreview(it);
constructEditCard("phrase", it, cardEditSection, true);
for (let i in it["phrase"])
{
constructPreviewCardGeneric(i, it, phrasePreviewSectionContainer)
constructEditCard(i, it, cardEditSection, false);
}
}
}
else if (urlParams.has("phrase-new"))
{
$("phrase-preview-section").style.display = "block";
let lit = constructPhraseEditCardPreview(null);
constructEditCard("phrase", lit, cardEditSection, true);
}
else
{
constructPreviewCardGeneric(0, null, cardEditSection);
constructEditCard(0, window.previewCards[0], cardEditSection, false);
// Reverse children of $("card-edit-section") because we display the preview before the edit widget
for (let i = 0; i < cardEditSection.childNodes.length; i++)
cardEditSection.insertBefore(cardEditSection.childNodes[i], cardEditSection.firstChild);
}
}
function deckEditMain()
{
constructListElements();
runEventAfterAnimation($("finish-edit-button"), "click", function(_) {
if (window.previewPhrase !== null)
window.localStorageData.phrases.push(window.previewPhrase);
window.localStorageData.cards.push(...window.previewCards.filter((value, index) => {
const v = JSON.stringify(value);
return index === window.previewCards.findIndex(obj => {
return JSON.stringify(obj) === v;
});
}));
saveToLocalStorage(window.localStorageData);
location.href = "./deck.html";
});
runEventAfterAnimation($("cancel-edit-button"), "click", function() { location.href = './deck.html' })
}
deckEditMain();