latin: substitution glyphs for cards and ics
[sheet.git] / latinsample.js
index f4a6a6de0af8bb11182c1d62383b6db62d449d32..b576f955514949eb24c59f3c4d93619824a6fe88 100644 (file)
@@ -1,14 +1,7 @@
 function appendsample() {
        var rows = document.getElementsByClassName('glyphs')[0].rows;
        for (var row of rows) {
-               cols = [ row.cells[0] ];
-               for (var col = 1; col <= 26; col++) {
-                       cols[col] = cols[ col - 1 ].nextSibling;
-                       for (var span = 1; span < cols[col].colSpan; span++) {
-                               var same = cols[col];
-                               cols[++col] = same;
-                       }
-               }
+               // append sample column
                var samplecol = row.getElementsByClassName('sample');
                if (samplecol.length) {
                        samplecol = samplecol[0];
@@ -18,31 +11,60 @@ function appendsample() {
                        samplecol.className = 'sample';
                }
 
+               // prepare alphabet lookup table
+               cols = [ row.cells[0] ];
+               for (var col = 1; col <= samplecol.cellIndex; col++) {
+                       var next = cols[col - 1].nextSibling;
+                       if (next == samplecol) break;
+                       cols[col] = next;
+                       for (var span = 1; span < cols[col].colSpan; span++) {
+                               var same = cols[col];
+                               cols[++col] = same;
+                       }
+               }
+
+               // copy letters into sample
                var output = '';
                var input = this.value.toUpperCase();
                for (var i = 0; i < input.length; i++) {
                        var col = input.charCodeAt(i) - 64;
-                       if (col < 1 || col > 26) {
-                               output += ' &nbsp; ';
+                       if (col < 1) col = 27; // space
+                       else if (cols[28] && i && col == input.charCodeAt(i - 1) - 64) {
+                               col = 28; // repetition char
+                       }
+
+                       if (col < cols.length) {
+                               output += '<span>' + (cols[col].innerHTML.trimRight() || ' ') + '</span>';
                        }
                        else {
-                               output += '<span>' + cols[col].innerHTML.trimRight() + '</span>';
+                               output += ' ';
                        }
                }
                samplecol.innerHTML = output;
        }
 };
 
+function getrequest(name) {
+       // find GET variable in page request
+       var match = new RegExp('[?&]'+name+'=([^&]*)');
+       var param = match.exec(window.location.search);
+       return param ? decodeURIComponent(param[1]) : '';
+}
+
 function prependinput(target) {
        var form = document.createElement('FORM');
        form.id = 'search';
        form.className = 'aside';
+       form.onsubmit = function () { return false };
 
        var input = document.createElement('INPUT');
        input.oninput = appendsample;
        input.placeholder = 'Sample';
        input.type = 'search';
        input.name = 'q';
+       if (input.value = getrequest('q')) {
+               input.oninput();
+       }
 
        form.appendChild(input);
        target.parentNode.insertBefore(form, target);