latin: move sample javascript to separate include
authorMischa POSLAWSKY <perl@shiar.org>
Mon, 10 Apr 2017 15:04:20 +0000 (17:04 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Mon, 24 Apr 2017 23:51:41 +0000 (01:51 +0200)
latin.plp
latinsample.js [new file with mode: 0644]

index cab913e75bcaa6a853a788aa82ee32d2af74d415..ed6f65b6f1490dacf3fb51e8a6ce23ac0ec14708 100644 (file)
--- a/latin.plp
+++ b/latin.plp
@@ -98,7 +98,7 @@ Html({
 
 <h1>Latin alphabet</h1>
 
-<p>Variant encodings of the common ASCII (latin, roman,
+<p id=intro>Variant encodings of the common ASCII (latin, roman,
 or <span title="fuck yeah!">'mercan</span>) letters A–Z.
 Also see <a href="/writing">related alphabets</a>
 and <a href="/chars/abc">font comparison</a>.</p>
@@ -139,45 +139,6 @@ else {
 
 :></div>
 
-<script>
-var inputel = document.createElement('INPUT');
-inputel.oninput = function () {
-       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;
-                       }
-               }
-               var samplecol = row.getElementsByClassName('sample');
-               if (samplecol.length) {
-                       samplecol = samplecol[0];
-               }
-               else {
-                       samplecol = row.appendChild(document.createElement('TD'));
-                       samplecol.className = 'sample';
-               }
-
-               var output = '';
-               var input = inputel.value.toUpperCase();
-               for (var i = 0; i < input.length; i++) {
-                       var col = input.charCodeAt(i) - 64;
-                       if (col < 1 || col > 26) {
-                               output += ' &nbsp; ';
-                       }
-                       else {
-                               output += '<span>' + cols[col].innerHTML.trimRight() + '</span>';
-                       }
-               }
-               samplecol.innerHTML = output;
-       }
-};
-
-var container = document.createElement('P');
-container.appendChild(inputel);
-document.body.appendChild(container);
+<script type="text/javascript" src="/latinsample.js"></script>
+<script type="text/javascript"> prependinput(document.getElementById('intro')) </script>
 
-</script>
diff --git a/latinsample.js b/latinsample.js
new file mode 100644 (file)
index 0000000..f4a6a6d
--- /dev/null
@@ -0,0 +1,50 @@
+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;
+                       }
+               }
+               var samplecol = row.getElementsByClassName('sample');
+               if (samplecol.length) {
+                       samplecol = samplecol[0];
+               }
+               else {
+                       samplecol = row.appendChild(document.createElement('TD'));
+                       samplecol.className = '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; ';
+                       }
+                       else {
+                               output += '<span>' + cols[col].innerHTML.trimRight() + '</span>';
+                       }
+               }
+               samplecol.innerHTML = output;
+       }
+};
+
+function prependinput(target) {
+       var form = document.createElement('FORM');
+       form.id = 'search';
+       form.className = 'aside';
+
+       var input = document.createElement('INPUT');
+       input.oninput = appendsample;
+       input.placeholder = 'Sample';
+       input.type = 'search';
+       input.name = 'q';
+
+       form.appendChild(input);
+       target.parentNode.insertBefore(form, target);
+}
+