Commit dac2c080 authored by Irina's avatar Irina

v2.4

* Refactoring according to Uncle Bob's clean code
* Now space is actually on 10th character
parent ccfe95d2
eclipse.preferences.version=1
encoding//js/GenerateString.js=UTF-8
......@@ -8,8 +8,11 @@ Tartu, Estonia -->
<head>
<title>String Generator</title>
<link rel = 'stylesheet' type = 'text/css' href = 'style.css' />
<script src = 'js/generateString.js' type = 'text/javascript' charset="utf-8"></script>
<script src = 'js/calculateLength.js' type = 'text/javascript' charset="utf-8"></script>
<script src = 'js/GenerateString.js' type = 'text/javascript' charset="utf-8"></script>
<script src = 'js/CalculateLength.js' type = 'text/javascript' charset="utf-8"></script>
<script src = 'js/DisplayResult.js' type = 'text/javascript' charset="utf-8"></script>
<script src = 'js/KeyListener.js' type = 'text/javascript' charset="utf-8"></script>
<script src = 'js/Variables.js' type = 'text/javascript' charset="utf-8"></script>
</head>
<body>
......
function calculateLength() {
setVariablesForCalculateLength();
var calculatedLength = string.length;
returnCalculatedLength(calculatedLength);
}
\ No newline at end of file
// Handler
if (window.addEventListener) {
window.addEventListener('load', listenInputLength, false);
window.addEventListener('load', listenRadioButtons, false);
window.addEventListener('load', listenSpaceCheckbox, false);
window.addEventListener('load', listenUmlautCheckbox, false);
} else if (window.attachEvent) {
window.attachEvent('onload', listenInputLength);
window.attachEvent('onload', listenRadioButtons);
window.attachEvent('onload', listenSpaceCheckbox);
window.attachEvent('onload', listenUmlautCheckbox);
} else {
document.addEventListener('load', listenInputLength, false);
document.addEventListener('load', listenRadioButtons, false);
document.addEventListener('load', listenSpaceCheckbox, false);
document.addEventListener('load', listenUmlautCheckbox, false);
function returnError(errorText) {
setErrorText(errorText);
colorBorderToRed();
selectInputLength();
throw '';
}
function listenInputLength() {
var inputLength = document.getElementById('inputLength');
if (inputLength.addEventListener) {
inputLength.addEventListener('keydown', inputLengthListener, false);
} else if (inputLength.attachEvent) {
inputLength.attachEvent('keydown', inputLengthListener);
}
function clearError() {
var clearError = document.getElementById('error');
clearError.innerHTML = '<br /><br />';
clearBorderStyle();
}
function listenRadioButtons() {
var inputTypeNumber = document.getElementById('inputTypeNumber');
var inputTypeText = document.getElementById('inputTypeText');
if (inputTypeNumber.addEventListener) {
inputTypeNumber.addEventListener('change', setFocusAfterRadioButton, false);
inputTypeText.addEventListener('change', setFocusAfterRadioButton, false);
} else if (inputTypeNumber.attachEvent) {
inputTypeNumber.attachEvent('change', setFocusAfterRadioButton);
}
function setErrorText(errorText) {
var divError = document.getElementById('error');
divError.innerHTML = errorText + '<br /><br />';
}
function listenSpaceCheckbox() {
var inputSpace = document.getElementById('inputSpace');
if (inputSpace.addEventListener) {
inputSpace.addEventListener('change', setFocusAfterSpaceCheckbox, false);
} else if (inputSpace.attachEvent) {
inputSpace.attachEvent('change', setFocusAfterSpaceCheckbox);
}
function colorBorderToRed() {
var inputLength = document.getElementById('inputLength');
inputLength.style.borderColor = '#FF0000';
}
function listenUmlautCheckbox() {
var inputUmlaut = document.getElementById('inputUmlaut');
if (inputUmlaut.addEventListener) {
inputUmlaut.addEventListener('change', setFocusAfterUmlautCheckbox, false);
} else if (inputUmlaut.attachEvent) {
inputUmlaut.attachEvent('change', setFocusAfterUmlautCheckbox);
}
function clearBorderStyle() {
var inputLength = document.getElementById('inputLength');
inputLength.style.border = '';
}
function returnGeneratedString(generatedString) {
var textarea = document.getElementById('generatedString');
textarea.value = textarea.value + generatedString;
selectGeneratedString();
}
function returnCalculatedLength(calculatedLength) {
var divCalculatedLength = document.getElementById('calculatedLength');
divCalculatedLength.innerHTML = 'Length: ' + calculatedLength + '<br />';
}
function setFocusOnInputLength() {
document.getElementById('inputLength').focus();
}
function setFocusOnInputString() {
var inputString = document.getElementById('inputString');
inputString.focus();
}
function setFocusAfterRadioButton(event) {
var radioButton = event.target;
if (radioButton.checked) {
if (document.getElementById('inputTypeNumber').checked) {
if (document.getElementById('inputTypeNumber').checked)
disableUmlautCheckbox();
} else {
else
enableUmlautCheckbox();
}
setFocusOnInputLength();
selectInputLength();
......@@ -73,6 +68,7 @@ function setFocusAfterRadioButton(event) {
function setFocusAfterSpaceCheckbox(event) {
var spaceCheckbox = event.target;
if (spaceCheckbox.checked) {
setFocusOnInputLength();
selectInputLength();
......@@ -84,6 +80,7 @@ function setFocusAfterSpaceCheckbox(event) {
function setFocusAfterUmlautCheckbox(event) {
var umlautCheckbox = event.target;
if (umlautCheckbox.checked) {
setFocusOnInputLength();
selectInputLength();
......@@ -91,30 +88,15 @@ function setFocusAfterUmlautCheckbox(event) {
setFocusOnInputLength();
selectInputLength();
}
}
}
function inputLengthListener(e) {
if (e.keyCode == 13) { // 13 = Enter
generateString();
} else if (e.keyCode == 39) { // 39 = right arrow = text
checkTextRadiobutton();
selectInputLength();
} else if (e.keyCode == 37) { // 37 = left arrow = number
checkNumberRadiobutton();
selectInputLength();
} else if (e.keyCode == 38) { // 38 = up arrow = space
if (document.getElementById('inputSpace').checked == true) {
uncheckSpaceCheckbox();
} else {
checkSpaceCheckbox();
}
} else if (e.keyCode == 40) { // 40 = down arrow = umlaut
if (document.getElementById('inputUmlaut').checked && document.getElementById('inputTypeText').checked) {
uncheckUmlautCheckbox();
} else if (document.getElementById('inputTypeText').checked) {
checkUmlautCheckbox();
}
}
function selectInputLength() {
document.getElementById('inputLength').select();
}
function clearInputLength() {
var inputLength = document.getElementById('inputLength');
inputLength.value = '';
}
function checkTextRadiobutton() {
......@@ -151,162 +133,8 @@ function uncheckUmlautCheckbox() {
umlautCheckbox.checked = false;
}
// Constant variables
var type;
var length;
var string;
var space;
var umlaut;
function setVariablesForStringGenerator() {
// get type
var inputType;
if (document.getElementById('inputTypeNumber').checked) {
inputType = 'number';
} else if (document.getElementById('inputTypeText').checked) {
inputType = 'text';
}
// set type
if (inputType == 'number' || inputType == 'text') {
type = inputType;
} else {
returnError('Inserted type is incorrect');
}
// get length
var inputLength = document.getElementById('inputLength').value;
// set length
if (inputLength.indexOf('.') == -1 && inputLength.indexOf(',') == -1 && inputLength > 0) {
length = inputLength;
} else {
returnError('Inserted number is incorrect');
}
// get Space
if (document.getElementById('inputSpace').checked) {
space = 'true';
} else {
space = 'false';
}
//get Umlaut
if (document.getElementById('inputUmlaut').checked) {
umlaut = 'true';
} else {
umlaut = 'false';
}
}
function generateString() {
clearGeneratedString();
clearError();
setVariablesForStringGenerator();
// Generate number string
if (type == 'number') {
if (length > 2) {
string = '1';
for (var i = 1; i < length-1; i++) {
if (i % 10 == 0) {
if (space == 'true') {
string += ' ';
} else {
string += '0';
}
} else {
string += '0';
}
}
string += '9';
} else if (length == 2) {
string = '19';
} else {
string = '1';
}
// Generate text string
} else if (type == 'text') {
if (umlaut == 'false') {
if (length > 2) {
string = 'A';
for (var i = 1; i < length-1; i++) {
if (i % 10 == 0) {
if (space == 'true') {
string += ' ';
} else {
string += 'x';
}
} else {
string += 'x';
}
}
string += 'Z';
} else if (length == 2) {
string = 'AZ';
} else {
string = 'A';
}
} else if (umlaut == 'true') {
if (length == 1) {
string = 'Ä';
} else if (length == 2) {
string = 'ÄÖ';
} else if (length == 3) {
string = 'ÄöÜ';
} else if (length == 4) {
string = 'ÄöüÕ';
} else if (length == 5) {
string = 'ÄöüõZ';
} else if (length > 5) {
string = 'Äöüõ';
for (var i = 1; i < length-4; i++) {
if (i % 10 == 0) {
if (space == 'true') {
string += ' ';
} else {
string += 'x';
}
} else {
string += 'x';
}
}
string += 'Z';
}
} else {
clearGeneratedString();
returnError('Can not generate the string');
}
}
returnGeneratedString(string);
}
function returnGeneratedString(generatedString) {
var textarea = document.getElementById('generatedString');
textarea.value = textarea.value + generatedString;
selectGeneratedString();
}
function setFocusOnInputLength() {
document.getElementById('inputLength').focus();
}
function selectGeneratedString() {
document.getElementById('generatedString').select();
}
function selectInputLength() {
document.getElementById('inputLength').select();
function enableUmlautCheckbox() {
document.getElementById('inputUmlaut').disabled = false;
}
function disableUmlautCheckbox() {
......@@ -314,34 +142,11 @@ function disableUmlautCheckbox() {
document.getElementById('inputUmlaut').checked = false;
}
function enableUmlautCheckbox() {
document.getElementById('inputUmlaut').disabled = false;
}
function returnError(errorText) {
var divError = document.getElementById('error');
divError.innerHTML = errorText + '<br /><br />';
var inputLength = document.getElementById('inputLength');
inputLength.style.borderColor = '#FF0000';
selectInputLength();
throw '';
function selectGeneratedString() {
document.getElementById('generatedString').select();
}
function clearGeneratedString() {
var clearTextarea = document.getElementById('generatedString');
clearTextarea.value = '';
}
function clearError() {
var clearError = document.getElementById('error');
clearError.innerHTML = '<br /><br />';
var inputLength = document.getElementById('inputLength');
inputLength.style.border = '';
}
function clearInputLength() {
var inputLength = document.getElementById('inputLength');
inputLength.value = '';
}
\ No newline at end of file
function generateString() {
clearGeneratedString();
clearError();
setVariablesForStringGeneration();
if (type == 'number')
generateNumberString();
else if (type == 'text')
generateTextString();
returnGeneratedString(generatedString);
}
function generateNumberString() {
if (length > 2) {
generatedString = '1';
generateMiddleContent(false, '0');
generatedString += '9';
} else if (length == 2)
generatedString = '19';
else
generatedString = '1';
}
function generateTextString() {
if (umlaut)
generateTextStringWithUmplauts();
else
generateTextStringWithoutUmlauts();
}
function generateTextStringWithUmplauts() {
if (length == 1)
generatedString = 'Ä';
else if (length == 2)
generatedString = 'Äö';
else if (length == 3)
generatedString = 'Äöü';
else if (length == 4)
generatedString = 'Äöüõ';
else if (length == 5)
generatedString = 'ÄöüõZ';
else if (length > 5) {
generatedString = 'Äöüõ';
generateMiddleContent(true, 'x');
generatedString += 'Z';
}
}
function generateTextStringWithoutUmlauts() {
if (length > 2) {
generatedString = 'A';
generateMiddleContent(false, 'x');
generatedString += 'Z';
} else if (length == 2)
generatedString = 'AZ';
else
generatedString = 'A';
}
function generateMiddleContent(isUmlaut, middleCharacter) {
var i;
if (isUmlaut)
i = 5;
else
i = 2;
for (i; i < length; i++) {
if (i % 10 == 0) {
if (space)
generatedString += ' ';
else
generatedString += middleCharacter;
} else
generatedString += middleCharacter;
}
}
\ No newline at end of file
if (window.addEventListener)
window.addEventListener('load', listenKeys, false);
else if (window.attachEvent)
window.attachEvent('onload', listenKeys);
else
document.addEventListener('load', listenKeys, false);
function listenKeys(){
listenInputLength(document.getElementById('inputLength'));
listenRadioButtons(document.getElementById('inputTypeNumber'), document.getElementById('inputTypeText'));
listenSpaceCheckbox(document.getElementById('inputSpace'));
listenUmlautCheckbox(document.getElementById('inputUmlaut'));
listenInputString(document.getElementById('inputString'));
}
function listenInputLength(inputLength) {
if (inputLength.addEventListener)
inputLength.addEventListener('keydown', inputLengthListener, false);
else if (inputLength.attachEvent)
inputLength.attachEvent('keydown', inputLengthListener);
}
function listenRadioButtons(inputTypeNumber, inputTypeText) {
if (inputTypeNumber.addEventListener) {
inputTypeNumber.addEventListener('change', setFocusAfterRadioButton, false);
inputTypeText.addEventListener('change', setFocusAfterRadioButton, false);
} else if (inputTypeNumber.attachEvent)
inputTypeNumber.attachEvent('change', setFocusAfterRadioButton);
}
function listenSpaceCheckbox(inputSpace) {
if (inputSpace.addEventListener)
inputSpace.addEventListener('change', setFocusAfterSpaceCheckbox, false);
else if (inputSpace.attachEvent)
inputSpace.attachEvent('change', setFocusAfterSpaceCheckbox);
}
function listenUmlautCheckbox(inputUmlaut) {
if (inputUmlaut.addEventListener)
inputUmlaut.addEventListener('change', setFocusAfterUmlautCheckbox, false);
else if (inputUmlaut.attachEvent)
inputUmlaut.attachEvent('change', setFocusAfterUmlautCheckbox);
}
function listenInputString(inputString) {
if(inputString.addEventListener)
inputString.addEventListener('keydown', calculateLengthListener, false);
else if(inputString.attachEvent)
inputString.attachEvent('keydown', calculateLengthListener);
}
var enter = 13;
var rightArrow = 39;
var leftArrow = 37;
var upArrow = 38;
var downArrow = 40;
function inputLengthListener(e) {
if (e.keyCode == enter)
generateString();
else if (e.keyCode == rightArrow) {
checkTextRadiobutton();
selectInputLength();
} else if (e.keyCode == leftArrow) {
checkNumberRadiobutton();
selectInputLength();
} else if (e.keyCode == upArrow) {
if (document.getElementById('inputSpace').checked)
uncheckSpaceCheckbox();
else
checkSpaceCheckbox();
} else if (e.keyCode == downArrow) {
if (document.getElementById('inputUmlaut').checked && document.getElementById('inputTypeText').checked)
uncheckUmlautCheckbox();
else if (document.getElementById('inputTypeText').checked)
checkUmlautCheckbox();
}
}
function calculateLengthListener(e) {
if (e.keyCode == enter)
calculateLength();
}
\ No newline at end of file
var length;
var type;
var space;
var umlaut;
var generatedString;
function setVariablesForStringGeneration() {
var inputType = getInputType();
setType(inputType);
var inputLength = document.getElementById('inputLength').value;
setLength(inputLength);
setSpace();
setUmlaut();
}
function setLength(inputLength) {
if (inputLength.indexOf('.') == -1 && inputLength.indexOf(',') == -1 && inputLength > 0)
length = inputLength;
else
returnError('Inserted number is incorrect');
}
function getInputType() {
if (document.getElementById('inputTypeNumber').checked)
return 'number';
else if (document.getElementById('inputTypeText').checked)
return 'text';
}
function setType(inputType) {
if (inputType == 'number' || inputType == 'text')
type = inputType;
else
returnError('Inserted type is incorrect');
}
function setSpace() {
if (document.getElementById('inputSpace').checked)
space = true;
else
space = false;
}
function setUmlaut() {
if (document.getElementById('inputUmlaut').checked)
umlaut = true;
else
umlaut = false;
}
var string;
function setVariablesForCalculateLength() {
string = document.getElementById('inputString').value;
}
\ No newline at end of file
function listenForInputString() {
var inputString = document.getElementById('inputString');
if(inputString.addEventListener){
inputString.addEventListener('keydown', calculateLength, false);
} else if(inputString.attachEvent){
inputString.attachEvent('keydown', calculateLength);
}
}
// Handler
if(window.addEventListener){
window.addEventListener('load', listenForInputString, false);
} else if(window.attachEvent){
window.attachEvent('onload', listenForInputString);
} else{
document.addEventListener('load', listenForInputString, false);
}
// Constant variables
var inputString;
function setVariablesForCalculateLength() {
inputString = document.getElementById('inputString').value;
}
function calculateLength(e) {
if (e.keyCode == 13) {
setVariablesForCalculateLength();
var calculatedLength = inputString.length;
returnCalculatedLength(calculatedLength);
}
}
function returnCalculatedLength(calculatedLength) {
var divCalculatedLength = document.getElementById('calculatedLength');
divCalculatedLength.innerHTML = 'Length: ' + calculatedLength + '<br />';
}
function setFocusOnInputString() {
var inputString = document.getElementById('inputString');
inputString.focus();
}
\ No newline at end of file
{
"name": "String Generator",
"version": "2.3",
"version": "2.4",
"manifest_version": 2,
"description": "Extension generates the string of specified length or calculates the length of specified string.",
"homepage_url": "https://chrome.google.com/webstore/detail/string-generator/mklinoolelpkognneckhecljdfhehfbc",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment