JSFiddle - React, Tailwind, and code Playground
by yufeiy
HTML
<h2>Configuration</h2>
<h3>Keyword</h3>
<input type="text" id="keyword" maxlength="8">
<button id="updateBtn" disabled>update</button>
<div id="tblKeyword"></div>
<h2>Encoding</h2>
<h3>Source Text</h3>
<table id="tblSrc" border="1"></table>
<input type="text" id="inputStr" readonly="readonly">
<button id="clearBtn">clear</button>
<h3>Cipher Text</h3>
<input type="text" id="outputStr" readonly="readonly">
CSS
h2 {
text-decoration: underline;
color: #808080
}
table {
border-collapse: collapse;
margin-top: 20px;
margin-bottom: 10px;
}
td {
width: 20px;
height: 20px;
}
.highlight {
background-color: #ADD8E6;
}
button {
background-color: green;
border: none;
color: white;
}
JavaScript
var index = 0;
var offsets = [];
var vocab = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z'
];
var charCodeOfA = 65;
var charCodeOfZ = 90;
var validInput = false;
var updateClicked = false;
$(document).ready(function() {
//delay for keyup funciton
var delay = (function() {
var timer = 0;
return function(callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
}
})();
//validate input
$('#keyword').keyup(function() {
delay(function() {
//remove keyword table
$('#tblKeyword').html("");
updateClicked = false;
//validate A: case B: length
var text = $('#keyword').val();
if (window.isUpperCase(text) == false) {
$('#updateBtn').prop('disabled', true);
validInput = false;
alert("Invalid input: Keyword must be a single word in uppercase!");
}
else if (text.length < 3 || text.length > 8) {
$('#updateBtn').prop('disabled', true);
validInput = false;
alert("Invalid input: keyword must be a single word with 3 to 8 characters!");
}
else {
validInput = true;
$('#updateBtn').prop('disabled', false);
}
}, 1000);
});
//buttons row in source text table
var srcHtml = "<tr>";
for (var i = 0; i < 26; i++) {
srcHtml += "<td><button onclick=\"window.addChar('" + vocab[i] + "'," + i + ")\">" + vocab[i] + "</button></td>";
}
srcHtml += "</tr>";
//default cipher row in source text table
var row2 = "<tr id='row2'>";
for (var j = 0; j < 26; j++) {
row2 += "<td></td>";
}
row2 += "</tr>";
srcHtml += row2;
$('#tblSrc').html(srcHtml);
//update button onclick event
$("#updateBtn").click(function() {
updateClicked = true;
//reset textboxes and index in keyword
$('#inputStr').prop('diabled', false);
offsets = [];
$('#inputStr').val("");
...