fa6b0f1ca60af7e6cae33a5dce46eb2b0fb8df5a
[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 (samplecol.length) {
7                         samplecol = samplecol[0];
8                 }
9                 else {
10                         samplecol = row.appendChild(document.createElement('TD'));
11                         samplecol.className = 'sample';
12                 }
13
14                 // prepare alphabet lookup table
15                 cols = [ row.cells[0] ];
16                 for (var col = 1; col <= samplecol.cellIndex; col++) {
17                         var next = cols[col - 1].nextSibling;
18                         if (next == samplecol) break;
19                         cols[col] = next;
20                         for (var span = 1; span < cols[col].colSpan; span++) {
21                                 var same = cols[col];
22                                 cols[++col] = same;
23                         }
24                 }
25                 for (var col = 0; col < cols.length; col++) {
26                         cols[col] = cols[col].innerHTML.trimRight();
27                 }
28
29                 // copy letters into sample
30                 var output = '';
31                 var input = this.value.toUpperCase();
32                 for (var i = 0; i < input.length; i++) {
33                         var col = input.charCodeAt(i) - 64;
34                         if (col < 1) col = 27; // space
35                         else if (cols[28] && i && col == input.charCodeAt(i - 1) - 64) {
36                                 col = 28; // repetition char
37                         }
38
39                         if (col < cols.length) {
40                                 output += '<span>' + (cols[col] || ' ') + '</span>';
41                         }
42                         else {
43                                 output += ' ';
44                         }
45                 }
46                 if (cols[29] && !cols[28]) {
47                         // circumfix sign if no repetition
48                         output = cols[29] + output + cols[29];
49                 }
50                 samplecol.innerHTML = output;
51         }
52 };
53
54 function getrequest(name) {
55         // find GET variable in page request
56         var match = new RegExp('[?&]'+name+'=([^&]*)');
57         var param = match.exec(window.location.search);
58         return param ? decodeURIComponent(param[1]) : '';
59 }
60
61 function prependinput(target) {
62         var form = document.createElement('FORM');
63         form.id = 'search';
64         form.className = 'aside';
65         form.onsubmit = function () { return false };
66
67         var input = document.createElement('INPUT');
68         input.oninput = appendsample;
69         input.placeholder = 'Sample';
70         input.type = 'search';
71         input.name = 'q';
72         if (input.value = getrequest('q')) {
73                 input.oninput();
74         }
75
76         form.appendChild(input);
77         target.parentNode.insertBefore(form, target);
78 }
79