cff51e38b9e295869fca2bde87eb49b0037509df
[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                         if (col < cols.length) {
33                                 output += '<span>' + cols[col].innerHTML.trimRight() + '</span>';
34                         }
35                         else {
36                                 output += ' ';
37                         }
38                 }
39                 samplecol.innerHTML = output;
40         }
41 };
42
43 function getrequest(name) {
44         // find GET variable in page request
45         var match = new RegExp('[?&]'+name+'=([^&]*)');
46         var param = match.exec(window.location.search);
47         return param ? decodeURIComponent(param[1]) : '';
48 }
49
50 function prependinput(target) {
51         var form = document.createElement('FORM');
52         form.id = 'search';
53         form.className = 'aside';
54         form.onsubmit = function () { return false };
55
56         var input = document.createElement('INPUT');
57         input.oninput = appendsample;
58         input.placeholder = 'Sample';
59         input.type = 'search';
60         input.name = 'q';
61         if (input.value = getrequest('q')) {
62                 input.oninput();
63         }
64
65         form.appendChild(input);
66         target.parentNode.insertBefore(form, target);
67 }
68