latin: half deck row with playing cards glyphs
[sheet.git] / latinsample.js
1 function appendsample() {
2         var rows = document.getElementsByClassName('glyphs')[0].rows;
3         for (var row of rows) {
4                 cols = [ row.cells[0] ];
5                 for (var col = 1; col <= 26; col++) {
6                         cols[col] = cols[ col - 1 ].nextSibling;
7                         for (var span = 1; span < cols[col].colSpan; span++) {
8                                 var same = cols[col];
9                                 cols[++col] = same;
10                         }
11                 }
12                 var samplecol = row.getElementsByClassName('sample');
13                 if (samplecol.length) {
14                         samplecol = samplecol[0];
15                 }
16                 else {
17                         samplecol = row.appendChild(document.createElement('TD'));
18                         samplecol.className = 'sample';
19                 }
20
21                 var output = '';
22                 var input = this.value.toUpperCase();
23                 for (var i = 0; i < input.length; i++) {
24                         var col = input.charCodeAt(i) - 64;
25                         if (col < 1 || col > 26) {
26                                 output += ' &nbsp; ';
27                         }
28                         else {
29                                 output += '<span>' + cols[col].innerHTML.trimRight() + '</span>';
30                         }
31                 }
32                 samplecol.innerHTML = output;
33         }
34 };
35
36 function getrequest(name) {
37         // find GET variable in page request
38         var match = new RegExp('[?&]'+name+'=([^&]*)');
39         var param = match.exec(window.location.search);
40         return param ? decodeURIComponent(param[1]) : '';
41 }
42
43 function prependinput(target) {
44         var form = document.createElement('FORM');
45         form.id = 'search';
46         form.className = 'aside';
47         form.onsubmit = function () { return false };
48
49         var input = document.createElement('INPUT');
50         input.oninput = appendsample;
51         input.placeholder = 'Sample';
52         input.type = 'search';
53         input.name = 'q';
54         if (input.value = getrequest('q')) {
55                 input.oninput();
56         }
57
58         form.appendChild(input);
59         target.parentNode.insertBefore(form, target);
60 }
61