forked from JRbemt/Enigma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.js
306 lines (291 loc) · 19.3 KB
/
gui.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
* Object : Gui
* purpose : makes settings changeable in a non-programmatic way (graphical user interface), also it displays output and some information
*
* @param : controlsRef reference to (@see (object) Controls)
*/
function Gui(controlsRef) {
this.controlsRef = controlsRef;
if (Enigma.settings.gui.position !== "") this.drawPosition();
if (Enigma.settings.gui.startposition !== "") this.drawStart();
if (Enigma.settings.gui.rotorselector !== "") this.drawTypeSelectorsRotor();
if (Enigma.settings.gui.reflectorselector !== "") this.drawTypeSelectorReflector();
if (Enigma.settings.gui.plugboardtarget !== "") this.drawPlugboard();
this.drawCheckboxes();
this.printSettings();
}
/**
* a lot of functions doing one of these things
: creating and storing html elements according to (@see Enigma.settings)
: updating gui, displaying info for _SHOWN objects
: input ("TO"), meaning getting input for _USER objects which change the settings
: locking elements, meaning they will be disabled for input (settings can't changed be when encoding a message)
function() printSetting used for giving info about the selected rotor/ reflector type
instead of creating a reverse map as in (@see Morse)
a reverse lookup is done finding the details from a selected rotor which can be less efficient
this is done because rotor types are dynamic (will change) and the (@see (enum) Rotor.RotorTypes) doesn't contain that much types
which makes it much more efficient (printSettings is updated every first KeyPress (@see (object) Keyboard))
*/
Gui.prototype = {
drawPosition : function(){ /* create start-slots (input text) */
var pos = document.getElementById(Enigma.settings.gui.position);
var slots = [];
for (var i = 0; i < Enigma.settings.rotors.length; i++) {
var slot = document.createElement("INPUT");
slot.disabled = true;
slot.id = "slot"+i;
slot.type = "text";
slot.className = "rotor-slot";
slots[i] = slot;
pos.appendChild(slot);
}
this.positionSlots = slots;
this.updatePositionRotors(this.controlsRef.ToString());
},
drawStart : function(){ /* create start-slots (input text) */
var pos = document.getElementById(Enigma.settings.gui.startposition);
var slots = [];
for (var i = 0; i < Enigma.settings.rotors.length; i++) {
var slot = document.createElement("INPUT");
slot.id = "start"+i;
slot.type = "text";
slot.className = "rotor-start";
slots[i] = slot;
pos.appendChild(slot);
}
this.startingSlots = slots;
this.updateStartRotors();
},
drawTypeSelectorsRotor : function(){ /* create type-slots (select) */
var pos = document.getElementById(Enigma.settings.gui.rotorselector);
var rotorTypes ="";
for (var prop in Rotor.RotorTypes) {
if (Rotor.RotorTypes.hasOwnProperty(prop)){
if (prop !== "properties") {
rotorTypes += "<option value=\""+prop+"\">"+prop+"</option>";
}
}
}
var slots = [];
for (var i = 0; i < Enigma.settings.rotors.length; i++) {
var slot = document.createElement("SELECT");
slot.innerHTML = rotorTypes;
slot.id = "rotorselect"+i;
//slot.value = Enigma.settings.rotors[i];
slot.class = "rotor-typeselector";
slots[i] = slot;
pos.appendChild(slot);
}
this.rotorSelectors = slots;
this.updateTypeRotors();
},
drawTypeSelectorReflector : function(){
var pos = document.getElementById(Enigma.settings.gui.reflectorselector);
var reflectorTypes ="";
for (var prop in Reflector.ReflectorTypes) {
if (Reflector.ReflectorTypes.hasOwnProperty(prop)){
if (prop !== "properties") {
reflectorTypes += "<option value=\""+prop+"\">"+prop+"</option>";
}
}
}
var slot = document.createElement("SELECT");
slot.innerHTML = reflectorTypes;
slot.id = "reflectorselect";
//slot.value = Enigma.settings.rotors[i];
slot.class = "reflector-typeselector";
pos.appendChild(slot);
this.reflectorSelector = slot;
this.updateTypeReflector();
},
drawPlugboard : function(){
var target = document.getElementById(Enigma.settings.output.plugboardtarget);
var plugboard = document.createElement("INPUT");
plugboard.type = "text";
plugboard.className ="plugboard";
this.plugboard = plugboard;
target.appendChild(plugboard);
this.updatePlugboard();
},
drawCheckboxes : function(){
if (Enigma.settings.gui.displayAs !== "") {
var displayAs = document.createElement("INPUT");
displayAs.type = "checkbox";
displayAs.checked = !Enigma.settings.gui.slotToChar;
this.displayAs = displayAs;
document.getElementById(Enigma.settings.gui.displayAs).appendChild(displayAs);
}
if (Enigma.settings.output.checkplug !== "") {
var checkplugboard = document.createElement("INPUT");
checkplugboard.type = "checkbox";
checkplugboard.checked = Enigma.settings.output.plugboard;
this.checkplugboard = checkplugboard;
document.getElementById(Enigma.settings.output.checkplug).appendChild(checkplugboard);
}
if (Enigma.settings.output.checkmorse !== ""){
var morseplug = document.createElement("INPUT");
morseplug.type = "checkbox";
morseplug.checked = Enigma.settings.output.toMorse;
this.checkmorse = morseplug;
document.getElementById(Enigma.settings.output.checkmorse).appendChild(morseplug);
}
},
updateTypeRotors : function(){ /* get type of rotors and set them to the type-slot(s) */
for (var x = 0; x < Enigma.settings.rotors.length; x++) {
loop2 :
for (var prop in Rotor.RotorTypes) {
if (Rotor.RotorTypes.hasOwnProperty(prop)) {
if (Enigma.settings.rotors[x].of_type === Rotor.RotorTypes[prop]) {
this.rotorSelectors[x].value = prop;
break loop2;
}
}
}
}
},
updateTypeReflector : function(){ /* get type of reflector and set them to the type-slot */
loop:
for (var prop in Reflector.ReflectorTypes) {
if (Reflector.ReflectorTypes.hasOwnProperty(prop)) {
if (Enigma.settings.reflector.of_type === Reflector.ReflectorTypes[prop]) {
this.reflectorSelector.value = prop;
break loop;
}
}
}
},
updatePositionRotors : function(){
var json = JSON.parse(this.controlsRef.ToString());
var i = 0;
for (var prop in json){
if (json.hasOwnProperty(prop)) {
var val = parseInt(json[prop]);
if (Enigma.settings.gui.slotToChar) val = String.fromCharCode(65 + val);
this.positionSlots[i].value = val;
}
i++;
}
},
updateStartRotors : function(){ /* get startpositions and set them to the start-slots */
for (var x = 0; x < this.startingSlots.length; x++) {
var val = this.controlsRef.rotors[x].startposition; /* already type */
if (Enigma.settings.gui.slotToChar) val = String.fromCharCode(65 + val);
this.startingSlots[x].value = val;
}
},
updatePlugboard : function(){
this.plugboard.value = Enigma.settings.output.plugboardconfig;
},
plugboardToSetting : function(){
Enigma.settings.output.plugboardconfig = this.plugboard.value;
this.plugboard.disabled = false;
},
checkboxesToSettings : function(){
if (Enigma.settings.output.checkmorse !== ""){
Enigma.settings.output.outputToMorse = this.checkmorse.checked;
this.checkmorse.disabled = false;
}
if (Enigma.settings.output.checkplug !== "") {
Enigma.settings.output.plugboard = this.checkplugboard.checked;
this.checkplugboard.disabled = false;
}
if (Enigma.settings.gui.displayAs !== "") {
if (Enigma.settings.gui.slotToChar === this.displayAs.checked){
Enigma.settings.gui.slotToChar = !this.displayAs.checked;
this.updateStartRotors();
}
}
},
startSlotToRotor : function(){ /* get start-slot value and write them to rotor' startpositions */
for (var i = 0; i < this.startingSlots.length; i++) {
var val = this.startingSlots[i].value;
if (val.length === 1 && val.match(/[a-z]/i)) { /* val = char */
val = val.toUpperCase();
val = val.charCodeAt();
val = parseInt(val) - 65;
}
else if (val.length <= 2){
val = parseInt(val);
if (typeof val !== 'number'){ /* val = int */
this.updateStartRotors(); /* reset */
throw "not a valid input as startposition";
}
}
else {
this.updateStartRotors(); /* reset */
throw "not a valid input as startposition";
}
if (this.controlsRef.rotors[i].startposition !== val){
this.controlsRef.rotors[i].startposition = val;
}
this.startingSlots[i].disabled = false;
}
this.updateStartRotors(); /* e.g. |A|10|A| => |A|A|A| */
this.controlsRef.Reset();
},
typeSlotToRotor : function(){ /* get type-slot value and write them to rotor' type */
for (var i = 0; i < this.rotorSelectors.length; i++) {
if (this.controlsRef.rotors[i].rotor !== Rotor.RotorTypes[this.rotorSelectors[i].value]) {
this.controlsRef.rotors[i].rotor = Rotor.RotorTypes[this.rotorSelectors[i].value];
}
this.rotorSelectors[i].disabled = false;
}
},
typeSlotToReflector : function(){
this.controlsRef.reflector.reflector = Reflector.ReflectorTypes[this.reflectorSelector.value];
this.reflectorSelector.disabled = false;
},
lockStartSlots : function(){ /* disable start-slots */
for (var i = 0; i < this.startingSlots.length; i++) {
this.startingSlots[i].disabled = true;
}
},
lockTypeSlotsRotor : function(){ /* disable type-slots */
for (var i = 0; i < this.rotorSelectors.length; i++) {
this.rotorSelectors[i].disabled = true;
}
},
lockTypeSlotsReflector : function(){
this.reflectorSelector.disabled = true;
},
lockCheckboxes : function(){
if (Enigma.settings.output.checkmorse !== ""){
this.checkmorse.disabled = true;
}
if (Enigma.settings.output.checkplug !== "") {
this.checkplugboard.disabled = true;
}
},
lockPlugboard : function(){
this.plugboard.disabled = true;
},
printSettings : function(){
var tab = document.createElement("DIV");
tab.className = "info-container";
for (var i = 0; i < Enigma.settings.rotors.length; i++) {
var rotormainInfo = document.createElement("DIV");
rotormainInfo.class = "rotorinfo-settings";
var info = "rotor "+i + " : \"" + this.rotorSelectors[i].value + "\" which means the wiring is \""+this.controlsRef.rotors[i].rotor+"\"";
rotormainInfo.innerHTML = info;
rotormainInfo.className = "rotor-maininfo";
var rotorsideInfo = document.createElement("DIV");
sideinfo = "_______" + " the model name is \""+ Rotor.RotorTypes.properties[this.rotorSelectors[i].value].model+"\" which was created on/in \"" + Rotor.RotorTypes.properties[this.rotorSelectors[i].value].introduced + "\"";
rotorsideInfo.innerHTML = sideinfo;
rotorsideInfo.className = "rotor-sideinfo";
tab.appendChild(rotormainInfo);
tab.appendChild(rotorsideInfo);
}
// TODO: make reflector
var reflectormainInfo = document.createElement("DIV");
reflectormainInfo.innerHTML = "reflector : \""+ this.reflectorSelector.value + "\" which means the wiring is \"" + this.controlsRef.reflector.reflector+"\"";
reflectormainInfo.className = "reflector-maininfo";
var reflectorsideInfo = document.createElement("DIV");
reflectorsideInfo.innerHTML = "_________" + " the model name is \""+ Reflector.ReflectorTypes.properties[this.reflectorSelector.value].model +"\" which was created on/in \"" + Reflector.ReflectorTypes.properties[this.reflectorSelector.value].introduced + "\"";
reflectorsideInfo.className = "reflector-sideinfo";
tab.appendChild(reflectormainInfo);
tab.appendChild(reflectorsideInfo);
var target = document.getElementById(Enigma.settings.gui.info);
target.innerHTML = "";
target.appendChild(tab);
}
};