latin: substitution glyphs for cards and ics
[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
26                 // copy letters into sample
27                 var output = '';
28                 var input = this.value.toUpperCase();
29                 for (var i = 0; i < input.length; i++) {
30                         var col = input.charCodeAt(i) - 64;
31                         if (col < 1) col = 27; // space
32                         else if (cols[28] && i && col == input.charCodeAt(i - 1) - 64) {
33                                 col = 28; // repetition char
34                         }
35
36                         if (col < cols.length) {
37                                 output += '<span>' + (cols[col].innerHTML.trimRight() || ' ') + '</span>';
38                         }
39                         else {
40                                 output += ' ';
41                         }
42                 }
43                 samplecol.innerHTML = output;
44         }
45 };
46
47 function getrequest(name) {
48         // find GET variable in page request
49         var match = new RegExp('[?&]'+name+'=([^&]*)');
50         var param = match.exec(window.location.search);
51         return param ? decodeURIComponent(param[1]) : '';
52 }
53
54 function prependinput(target) {
55         var form = document.createElement('FORM');
56         form.id = 'search';
57         form.className = 'aside';
58         form.onsubmit = function () { return false };
59
60         var input = document.createElement('INPUT');
61         input.oninput = appendsample;
62         input.placeholder = 'Sample';
63         input.type = 'search';
64         input.name = 'q';
65         if (input.value = getrequest('q')) {
66                 input.oninput();
67         }
68
69         form.appendChild(input);
70         target.parentNode.insertBefore(form, target);
71 }
72