-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackTemplate.html
478 lines (428 loc) · 18.6 KB
/
backTemplate.html
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
471
472
473
474
475
476
477
478
<script>
////////////////////////////////////////////////////////
///////////////////// Variables ////////////////////////
////////////////////////////////////////////////////////
// here you can set different preferences for the presets
// the main color for the card in RGB-format; base for calculating all other colors.
// must be in the format [r, g, b] where r, g, b are numbers between 0 and 255.
var colorRGB = [205, 0, 205];
// set which fields are to be shown by default
// allowed values are true and false
var show_pitch_by_default = false;
var show_pitch_graph_by_default = false;
var show_example_sentence_by_default = false;
var show_sentence_furigana_by_default = false;
var expr_font_size = 30; // font size in px for the expression at the very top; default: 30
var content_font_size = 18; // font size in px for the example sentence, pitch information, notes and the definitions; default: 18
var glossary_font_size = 15; // font size in px for the explanations at the bottom; default: 15
var tag_popup_font_size = 10; // font size in px for the tag explanations when hovering over them; default: 10
// !!! If you don't know what you are doing, do not procceed beyond this point !!!
////////////////////////////////////////////////////////
////////////////// End of Variables ////////////////////
////////////////////////////////////////////////////////
</script>
<div class="main" lang="ja">
<div id="expr-field" class="expression-field content">
<span class="expression fill-flex">
<span id="expr">{{Furigana}}</span>
<span id="audio">{{Audio}}</span>
</span>
<aside class="tab corner-offset">
<div id="pitch-tab" class="title">Pitch</div>
<div id="ex-tab" class="title">Example</div>
<div id="notes-tab" class="title">Notes</div>
</aside>
</div>
<div class="content-container">
<span id="pitch-content" class="content hidden top-border">
<div class="fill-flex padded">
<div id="pitch">
{{Pitch}}
</div>
<div id="pitch-graph" class="hidden">
{{Pitch Graph}}
</div>
</div>
<aside class="tab">
<div id="graph-tab" class="title">Graph</div>
</aside>
</span>
<div id="ex-content" class="content hidden top-border">
<div class="fill-flex padded">
<div id="ex-plain">
{{Sentence}}
</div>
<div id="ex-furi" class="hidden">
{{Sentence with furigana}}
</div>
</div>
<aside class="tab">
<div id="ex-furi-tab" class="title">Furigana</div>
</aside>
</div>
<span id="notes-content" class="content hidden top-border">
<div id="notes-info" class="padded">
{{Notes}}
</div>
</span>
</div>
<div class="glossary top-border">
<div class="content">
<span id="meaning" class="fill-flex padded">{{Meaning}}</span>
<aside id=dictionary-tabs class="tab">
</aside>
</div>
<div class="jisho-link">
<a class="jisho-link" href="http://jisho.org/search/{{Expression}}">≪Jisho.org≫</a>
</div>
</div>
</div>
<script>
////////////////////////////////////////////////////////////////////////////
///////////////////formating the provided field contents////////////////////
// Expression on top
var expression = document.getElementById('expr');
formatRuby(expression);
// Pitch and Pitch graph
var pitch = document.getElementById('pitch');
var pitchGraph = document.getElementById('pitch-graph');
formatPitchContent(pitch, pitchGraph);
// Example Sentence with and without Furigana
var sentence = document.getElementById('ex-plain');
var sentenceFurigana = document.getElementById('ex-furi');
// Notes
var notes = document.getElementById('notes-info').textContent.trim();
// Meaning
var meaning = document.getElementById('meaning');
formatMeaning(meaning);
// clip rgb values to fit [0, 255] range
for(var i=0; i<colorRGB.length; i++)
colorRGB[i] = (colorRGB[i]>255) ? 255 : ((colorRGB[i]<0) ? 0 : colorRGB[i]);
// apply the set variables
var root = document.querySelector(':root');
// set the corresponding variables on the style sheet
var colorHSL = RGBtoHSL(colorRGB[0], colorRGB[1], colorRGB[2]);
root.style.setProperty('--h', colorHSL[0] + '');
root.style.setProperty('--s', colorHSL[1] + '%');
root.style.setProperty('--l', colorHSL[2] + '%');
root.style.setProperty('--l-copy', colorHSL[2] + '%');
root.style.setProperty('--expr-font-size', expr_font_size + 'px');
root.style.setProperty('--content-font-size', content_font_size + 'px');
root.style.setProperty('--gloss-font-size', glossary_font_size + 'px');
root.style.setProperty('--tag-popup-font-size', tag_popup_font_size + 'px');
/** Converts a given RGB color to HSL format, compatible with CSS' HSL colors
*
* @param {int} r - red color value, between 0 and 255
* @param {int} g - green color value, between 0 and 255
* @param {int} b - blue color value, between 0 and 255
* @returns {number[]} array in the form [h, s, l]
*/
function RGBtoHSL(r, g, b) {
r /= 255;
g /= 255;
b /= 255;
var cMin = Math.min(r, g, b);
var cMax = Math.max(r, g, b);
var h, s, l;
l = (cMin + cMax) / 2;
if (cMin == cMax)
h = s = 0;
else {
var delta = cMax - cMin;
s = delta/(1-Math.abs(2*l - 1));
switch(cMax) {
case r:
h = (g-b)/delta + (g<b ? 6: 0);
break;
case g:
h = (b-r)/delta + 2;
break;
case b:
h = (r-g)/delta + 4;
break;
}
h /= 6;
}
return [h*360, s*100, l*100];
}
////////////////////////// Arrange Pitch Accent Field ///////////////////////////////
/** Unwrap all 'ol' or 'ul' elements into a flat array
*
* @param {HTMLElement} elem - source that the array will be created from
* @returns {HTMLElement[]}
*/
function htmlToArray(elem) {
var returnList = [];
if(elem.tagName === 'OL' || elem.tagName === 'UL')
elem.childNodes.forEach( child => { returnList = returnList.concat(htmlToArray(child)) });
else
if(elem.nodeType !== Node.TEXT_NODE)
returnList.push(elem);
return returnList;
}
/** Format the pitch and the pitch graph.
* Set them up, so the pitch element displays a list with the different marked readings
* and the graph element shows the readings list with the corresponding graphs above
*
* @param {HTMLElement} pitchElem - readings with the pitch markings
* @param {HTMLElement} graphElem - graph visualisations
*/
function formatPitchContent(pitchElem, graphElem) {
if(!pitchElem.firstElementChild) return; // no formating possible if no pitch is given
var pitchArray = htmlToArray(pitchElem.firstElementChild);
var graphArray = htmlToArray(graphElem.firstElementChild);
var pitchOl = document.createElement('ol');
var graphOl = document.createElement('ol');
for(var i=0; i<pitchArray.length; i++) {
var pitchPlain = pitchArray[i];
var graph = document.createElement('span');
graph.classList.add('graph');
// if the word starts with sutegana move the graph over to fit the mora slightly better
if('ゃャゅュょョ'.includes(pitchPlain.innerText[1]))
graph.classList.add('sutegana');
if(pitchPlain.tagName !== 'LI') { // ensure consistent formating
var liPitchElem = document.createElement('li');
liPitchElem.appendChild(pitchPlain);
pitchPlain = liPitchElem;
}
if(graphArray[i].tagName !== 'LI')
graph.appendChild(graphArray[i]);
else
graphArray[i].childNodes.forEach( child => { graph.appendChild(child) });
pitchOl.appendChild(pitchPlain);
graphOl.appendChild(graph);
graphOl.appendChild(pitchPlain.cloneNode(true));
}
pitchElem.textContent = '';
graphElem.textContent = '';
pitchElem.appendChild(pitchOl);
graphElem.appendChild(graphOl);
}
/** Reformat the element by putting each kanji in it's own HTMLElement
* and adding the popup funcionality for the stroke order
*
* @param {HTMLElement} rubyElem - element containing the ruby tags to format
*/
function formatRuby(rubyElem) {
rubyElem.childNodes.forEach( child => {
if (child.tagName === 'RUBY') {
child.childNodes.forEach( c => {
if(c.nodeType === Node.TEXT_NODE) {
var text = c.textContent;
c.textContent = '';
for(var i=0; i<text.length; i++) {
var kanjiElem = document.createElement('span');
var kanjiPopup = document.createElement('span');
kanjiElem.classList.add('popup');
kanjiPopup.classList.add('popup-text', 'stroke-order');
kanjiElem.innerText = kanjiPopup.innerText = text[i];
kanjiElem.appendChild(kanjiPopup);
child.insertBefore(kanjiElem, c);
}
child.removeChild(c);
}
});
}
});
}
/////////////////////////////////////////////////////////////////////////////////////
////////////////////////// format glossary from meaning /////////////////////////////
/** @typedef {Object} content
* @property {HTMLElement[]} tags - the tags associated with the given glossary entries
* @property {HTMLElement[]} entries - array of related glossary entries
*/
/** Extract the tag and glossary information from a dictionary element
*
* @param {HTMLElement} dictElem - dictionary element
* @returns {content} array of glossary content objects in the dictionary
*/
function getDictionaryContent(dictElem) {
var content = [];
var tags = [];
var entries = [];
dictElem.childNodes.forEach( entry => {
switch(entry.dataset?.type) {
case 'tag':
if (!entry.dataset.tagNotes.match(/Sense #/g)) { // filter out JMDicts numbered entry tags
if (entry.dataset.tagNotes === "") // disable the popup feature, if the popup has no content, anyway
entry.classList.remove('popup');
tags.push(entry);
}
break;
case 'glossary-entry':
entries.push(entry);
break;
case 'glossary':
// if there are any existing entry lists, complete those first
if(entries.length > 0)
content.push({tags: tags, entries: entries});
entries = [];
// glossaries are set up to only ever contain 'glossary-entries'
entry.childNodes.forEach( child => { entries.push(child) });
content.push({tags: tags, entries: entries});
entries = [];
tags = [];
break;
}
});
if(entries.length > 0)
content.push({tags: tags, entries: entries});
return content;
}
/**Reformat the elements contained in a dictionary object to a HTMLElement
* @param {Object} dict - dictionary object
* @param {string} dict.dictionary - name of the dictionary
* @param {content[]} dict.content - array of content objects
* @returns {HTMLElement} new element containing the dictionary's information
*/
function formatDictionary(dict) {
var dictElem = document.createElement('div');
dictElem.id = dict.dictionary;
dictElem.classList.add('dictionary');
var tagNames = [];
var oldTagNames = [];
dict.content.forEach( content => {
var tagListElem = document.createElement('span');
tagListElem.classList.add('tag-list')
var entryElem = document.createElement('ul');
var difTag = false;
content.tags.forEach( tag => {
if(tag.dataset?.tagName) {
tagNames.push(tag.dataset.tagName);
if(!oldTagNames.includes(tag.dataset.tagName))
difTag = true;
}
});
if(difTag) {
content.tags.forEach( t=>{tagListElem.appendChild(t)});
entryElem.appendChild(tagListElem);
}
content.entries.forEach( entry => {entryElem.appendChild(entry)});
dictElem.appendChild(entryElem);
oldTagNames = [...tagNames];
tagNames = [];
});
return dictElem;
}
/** Create individual elements to the MeaningElem for the provided dictionaries and their entries
* and add the tabs to switch between them.
*
* @param {HTMLElement} meaningElem - element containing the contents of the cards 'Meaning' field
*/
function formatMeaning(meaningElem) {
var meaningArray = [];
meaningElem.childNodes.forEach( dict => {
if(dict.dataset?.dictionary) {
var content = getDictionaryContent(dict);
if(dict.dataset.dictionary === meaningArray[meaningArray.length-1]?.dictionary) {
var dictEntry = meaningArray[meaningArray.length-1];
dictEntry.content = dictEntry.content.concat(content);
}
else
meaningArray.push({dictionary: dict.dataset.dictionary, content: content});
}
});
meaningElem.textContent = '';
var tabsElem = document.getElementById('dictionary-tabs');
meaningArray.forEach( dict => {
var tab = document.createElement('div');
tab.id = dict.dictionary + '-tab';
tab.classList.add('title', 'unselected');
tab.textContent = dict.dictionary;
tabsElem.appendChild(tab);
meaningElem.appendChild(formatDictionary(dict));
});
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////// Side Tab Logic ////////////////////////////////////////////////////////////////////
// disable the Sentence Tab. if it doesn't differ from the expression or no sentence has been provided
if(sentence.textContent.trim() === '' || `{{Expression}}`.trim() === sentence.textContent.trim())
{
document.getElementById('ex-tab').classList.add('disabled');
}
// disable Pitch Tab, if yomitan/-chan could not provide any data on it anyway
if(pitch.textContent.includes('No pitch accent data') || pitch.textContent === '') {
document.getElementById('pitch-tab').classList.add('disabled');
}
// remove Notes Tab, if no notes have been provided
if(notes === '') {
document.getElementById('notes-tab').classList.add('disabled');
document.getElementById('notes-tab').style.display = 'none';
}
// disable the Furigana Tab for Sentences, that don't contain any
if(sentence.textContent.trim() === sentenceFurigana.textContent.trim() || sentenceFurigana.textContent.trim() === '') {
document.getElementById('ex-furi-tab').classList.add('disabled');
}
// trigger side tabs
/** Switch an Elements state between hidden and unhidden
*
* @param {string} id - id of the element
* @param {boolean} hidden - if the element is currently hidden from the user
* @returns {boolean} new hidden-status of the element
*/
function hideUnhideContent(id, hidden)
{
var content = document.getElementById(id);
if(hidden){
content.classList.remove('hidden');
content.classList.add('shown');
}
else {
content.classList.remove('shown');
content.classList.add('hidden');
}
return !hidden;
}
/** Switch an array of elements to hidden and a selected one to visible
* and mark the selected tab
*
* @param {string[]} ids - ids of all the related elements
* @param {int} selectedIdIndex - index of the ID in ids which is to be switched to visible
*/
function switchHiddenContent(ids, selectedIdIndex) {
for(var i=0; i<ids.length; i++) {
hideUnhideContent(ids[i], i===selectedIdIndex);
document.getElementById(ids[i] + '-tab').classList.replace(i===selectedIdIndex? 'unselected' :'selected', i===selectedIdIndex? 'selected' :'unselected');
}
}
/** Init a tab's functionality to influence the visiblity of one or two elements
*
* @param {string} idTab - ID of the tab
* @param {string} idContent - ID of the content that should be hidden or unhidden when the tab is clicked
* @param {string} idSecondContent - if provided, a click on tab switches between the elements associated with this ID and idContent
* @param {boolean} showByDefault - if the content should be visible initially; default false
*/
function setupHideFunctionality(idTab, idContent, idSecondContent=null, showByDefault=false) {
var button = document.getElementById(idTab);
var isHidden = true;
if(!button.classList.contains('disabled')) {
button.addEventListener('click', () => {
if(idSecondContent)
hideUnhideContent(idSecondContent, !isHidden);
isHidden = hideUnhideContent(idContent, isHidden);
});
if(showByDefault) { //call the functions once to trigger the variable setup
if(idSecondContent)
hideUnhideContent(idSecondContent, !isHidden);
isHidden = hideUnhideContent(idContent, isHidden);
}
}
}
/** Init function for the switching between the different dictionaries
*/
function setupSwitchDicts() {
var ids = [];
for(let k=0; k<meaning.childNodes.length; k++) {
let id = meaning.childNodes[k].id;
ids.push(id);
document.getElementById(id+'-tab').addEventListener('click', () => { switchHiddenContent(ids, k) });
}
switchHiddenContent(ids, 0);
}
setupHideFunctionality('pitch-tab', 'pitch-content', null, show_pitch_by_default);
setupHideFunctionality('ex-tab', 'ex-content', null, show_example_sentence_by_default);
setupHideFunctionality('notes-tab', 'notes-content');
setupHideFunctionality('graph-tab', 'pitch-graph', 'pitch', show_pitch_graph_by_default);
setupHideFunctionality('ex-furi-tab', 'ex-furi', 'ex-plain', show_sentence_furigana_by_default);
setupSwitchDicts();
</script>