-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractive.js
147 lines (129 loc) · 4.97 KB
/
interactive.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { Decoder } from "./decoder.js";
// Create a decoder object.
const decoder = new Decoder("");
// Assign event handlers to events.
const keyTextInput = document.getElementById("key");
const clearButton = document.getElementById("clear-state");
keyTextInput.style.backgroundColor = "yellow";
keyTextInput.addEventListener("keyup", validateKeyTextInput);
const encodeTextBox = document.getElementById("encode");
encodeTextBox.addEventListener("keyup", encodeText);
encodeTextBox.addEventListener("keyup", colorEncodedTextBox);
const decodeTextBox = document.getElementById("decode");
decodeTextBox.addEventListener("keyup", decodeText);
decodeTextBox.addEventListener("keyup", colorDecodedTextBox);
// TODO #2: Add event handlers for cypher key, encode, and decode text boxes.
// Every time the user types in a character, the cypher key and the
// encoded/decoded text boxes should have their state saved.
keyTextInput.addEventListener("keyup", () => saveState());
encodeTextBox.addEventListener("keyup", () => saveState());
decodeTextBox.addEventListener("keyup", () => saveState());
// TODO #2: Add an event handler for the clear button.
// When the user clicks the clear button, the state should be cleared.
clearButton.addEventListener("click", () => {
clearState();
});
// TODO #2: Call restoreState() to restore the state of the text boxes.
// This will restore the state of the app when loaded/reloaded.
restoreState();
function saveState() {
// TODO #2: Save the state of the cypher key, encode, and decode text boxes.
// Use the localStorage object to store the state. Make sure you use
// JSON.stringify() to convert the state to a string.
localStorage.setItem("key", keyTextInput.value);
localStorage.setItem("encode", encodeTextBox.value);
localStorage.setItem("decode", decodeTextBox.value);
localStorage.setItem("encoded", document.getElementById("encoded").value);
localStorage.setItem("decoded", document.getElementById("decoded").value);
localStorage.setItem(
"encodedColor",
document.getElementById("encoded").style.backgroundColor
);
localStorage.setItem(
"decodedColor",
document.getElementById("decoded").style.backgroundColor
);
localStorage.setItem(
"keyColor",
document.getElementById("key").backgroundColor
);
}
function restoreState() {
// TODO #2: Restore the state of cypher key, encode, and decode text boxes.
// Use the localStorage object to restore the state. Make sure you use the
// JSON.parse() method to convert the state string to an object.
encodeTextBox.value = localStorage.getItem("encode");
decodeTextBox.value = localStorage.getItem("decode");
keyTextInput.value = localStorage.getItem("key");
keyTextInput.backgroundColor = localStorage.getItem("keyColor");
document.getElementById("encoded").value = localStorage.getItem("encoded");
document.getElementById("decoded").value = localStorage.getItem("decoded");
document.getElementById("encoded").style.backgroundColor =
localStorage.getItem("encodedColor");
document.getElementById("decoded").style.backgroundColor =
localStorage.getItem("decodedColor");
}
function clearState() {
// TODO #2: Clear the state of the cypher key, encode, and decode text boxes.
// Use the localStorage object to clear the state. You can use the
// remoteItem() method to clear the state.
localStorage.clear();
}
/**
* Validate the cipher key text input.
*/
function validateKeyTextInput() {
if (
keyTextInput.value !== keyTextInput.value.toLowerCase() ||
keyTextInput.value.length !== 26
) {
keyTextInput.style.backgroundColor = "yellow";
} else {
keyTextInput.style.backgroundColor = "white";
decoder.cipher = keyTextInput.value;
encodeText();
decodeText();
}
}
/**
* Color the encoded text box.
*/
function colorEncodedTextBox() {
const encodedTextBox = document.getElementById("encoded");
if (encodedTextBox.value.length === 0) {
encodedTextBox.style.color = "black";
encodedTextBox.style.backgroundColor = "white";
} else {
encodedTextBox.style.color = "white";
encodedTextBox.style.backgroundColor = "red";
}
}
/**
* Encode the text in the encode text box.
*/
function encodeText() {
const encodeTextBox = document.getElementById("encode");
const encodedTextBox = document.getElementById("encoded");
encodedTextBox.value = decoder.encode(encodeTextBox.value);
}
/**
* Color the decoded text box.
*/
function colorDecodedTextBox() {
const decodedTextBox = document.getElementById("decoded");
if (decodedTextBox.value.length === 0) {
decodedTextBox.style.color = "black";
decodedTextBox.style.backgroundColor = "white";
} else {
decodedTextBox.style.color = "white";
decodedTextBox.style.backgroundColor = "green";
}
}
/**
* Decode the text in the decode text box.
*/
function decodeText() {
const decodeTextBox = document.getElementById("decode");
const decodedTextBox = document.getElementById("decoded");
decodedTextBox.value = decoder.decode(decodeTextBox.value);
}