index: release v1.18 with only altgr index linked
[sheet.git] / latinsample.js
1 function appendsample() {
2         var rows = document.getElementsByClassName('glyphs')[0].rows;
3         for (var row of rows) {
4                 // append sample column
5                 var samplecol = row.getElementsByClassName('sample');
6                 if (!this.value.length) {
7                         row.removeChild(samplecol[0]);
8                         continue;
9                 }
10                 if (samplecol.length) {
11                         samplecol = samplecol[0];
12                 }
13                 else {
14                         samplecol = row.appendChild(document.createElement('TD'));
15                         samplecol.className = 'sample';
16                 }
17
18                 // prepare alphabet lookup table
19                 cols = [ row.cells[0] ];
20                 for (var col = 1; col <= samplecol.cellIndex; col++) {
21                         var next = cols[col - 1].nextSibling;
22                         if (next == samplecol) break;
23                         cols[col] = next;
24                         for (var span = 1; span < cols[col].colSpan; span++) {
25                                 var same = cols[col];
26                                 cols[++col] = same;
27                         }
28                 }
29                 for (var col = 0; col < cols.length; col++) {
30                         cols[col] = cols[col].innerHTML.trimRight();
31                 }
32
33                 // copy letters into sample
34                 var output = '';
35                 var input = this.value.toUpperCase();
36                 for (var i = 0; i < input.length; i++) {
37                         var col = input.charCodeAt(i) - 64;
38                         if (col < 1) col = 27; // space
39                         else if (cols[28] && i && col == input.charCodeAt(i - 1) - 64) {
40                                 col = 28; // repetition char
41                         }
42                         if (row.id == 'suetterlin' && col == 19) {
43                                 var final = input.length == i + 1 || input[i + 1] == ' ';
44                                 output += cols[col].split('&nbsp;')[final ? 1 : 0];
45                         }
46                         else if (col < cols.length) {
47                                 var final = cols[col] || ' ';
48                                 if (!/^<svg/.test(cols[col])) final = `<span>${final}</span>`;
49                                 output += final;
50                         }
51                         else {
52                                 output += '<b> </b>';
53                         }
54                         if (row.parentNode.tagName == 'THEAD') {
55                                 output += col < 26 ? ' ' : 'ยท '; // number separator
56                         }
57                 }
58                 if (cols[29] && !cols[28]) {
59                         // circumfix sign if no repetition
60                         output = cols[29] + output + (cols[30] || cols[29]);
61                 }
62                 samplecol.innerHTML = output;
63         }
64 };
65
66 function getrequest(name) {
67         // find GET variable in page request
68         var match = new RegExp('[?&]'+name+'=([^&]*)');
69         var param = match.exec(window.location.search);
70         return param ? decodeURIComponent(param[1]) : '';
71 }
72
73 function prependinput(target) {
74         var form = document.createElement('FORM');
75         form.id = 'search';
76         form.className = 'aside';
77         form.onsubmit = function () { return false };
78
79         var input = document.createElement('INPUT');
80         input.oninput = appendsample;
81         input.placeholder = 'Sample';
82         input.type = 'search';
83         input.name = 'q';
84         if (input.value = getrequest('q')) {
85                 input.oninput();
86         }
87
88         form.appendChild(input);
89         target.parentNode.insertBefore(form, target);
90 }
91