-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
140 lines (120 loc) · 5.33 KB
/
main.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
// Put the machine on the window scope as it's useful to access in the console.
var enigmaMachine;
$(function () {
var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
// Setup options based on the machine type.
function setup(type) {
var machineType = enigma.machine.types[type];
var reflectorElement = $('#reflector');
_.map(machineType.reflectorOptions, function (option) {
reflectorElement.append($("<option>").val(option).html(enigma.reflector.types[option].name));
});
// Thin Rotors.
_.map(_.range(1, machineType.thinRotorCount + 1), function (rotorIndex) {
var rotorElement = $('#thin-rotor-no-' + rotorIndex);
_.map(machineType.thinRotorOptions, function (option) {
rotorElement.append($("<option>").val(option).html(enigma.rotor.types[option].rotorNo));
});
var groundSettingElement = $('#ground-setting-thin-rotor-no-' + rotorIndex);
_.map(letters, function (option) {
groundSettingElement.append($("<option>").val(option).html(option));
});
var ringSettingElement = $('#ring-setting-thin-rotor-no-' + rotorIndex);
_.map(letters, function (option) {
ringSettingElement.append($("<option>").val(option).html(option));
});
});
// Rotors.
_.map(_.range(1, enigma.machine.types.M4.rotorCount + 1), function (rotorIndex) {
var rotorElement = $('#rotor-no-' + rotorIndex);
_.map(enigma.machine.types.M4.rotorOptions, function (option) {
rotorElement.append($("<option>").val(option).html(enigma.rotor.types[option].rotorNo));
});
var groundSettingElement = $('#ground-setting-rotor-no-' + rotorIndex);
_.map(letters, function (option) {
groundSettingElement.append($("<option>").val(option).html(option));
});
var ringSettingElement = $('#ring-setting-rotor-no-' + rotorIndex);
_.map(letters, function (option) {
ringSettingElement.append($("<option>").val(option).html(option));
});
});
}
// Set the options based on the state of the machine.
function update(state) {
var reflectorElement = $('#reflector');
reflectorElement.val(state.reflector.type);
_.map(state.thinRotors, function (rotor, i) {
var rotorIndex = i + 1;
var rotorElement = $('#thin-rotor-no-' + rotorIndex);
rotorElement.val(rotor.type);
var groundSettingElement = $('#ground-setting-thin-rotor-no-' + rotorIndex);
groundSettingElement.val(rotor.groundSetting);
var ringSettingElement = $('#ring-setting-thin-rotor-no-' + rotorIndex);
ringSettingElement.val(rotor.ringSetting);
});
_.map(state.rotors, function (rotor, i) {
var rotorIndex = i + 1;
var rotorElement = $('#rotor-no-' + rotorIndex);
rotorElement.val(rotor.type);
var groundSettingElement = $('#ground-setting-rotor-no-' + rotorIndex);
groundSettingElement.val(rotor.groundSetting);
var ringSettingElement = $('#ring-setting-rotor-no-' + rotorIndex);
ringSettingElement.val(rotor.ringSetting);
});
}
enigmaMachine = enigma.machine.create('M4');
setup(enigmaMachine.getType());
update(enigmaMachine.getState());
$('#reflector').change(function () {
enigmaMachine.setReflector($(this).val());
});
$('#plugboard-input').change(function () {
var input = $(this).val();
var validInput = enigma.util.validatePlugboardInput(input);
$('#plugboard-input-form-group').toggleClass('has-error', !validInput);
if (validInput) {
enigmaMachine.setPlugboard(input);
var connections = enigmaMachine.getState().plugboard.connections;
var html = _.map(connections, function (connection) {
return connection.slice(0, 1) + '-' + connection.slice(1, 2);
}).join(', ');
$('#plugboard').html(html);
}
});
function getSelector(prefix) {
return "*[id^='" + prefix + "']";
}
function getNumberFromId(element, prefix) {
return _.toNumber(element.attr('id').replace(prefix, ''));
}
$(getSelector('thin-rotor-no-')).change(function () {
enigmaMachine.setThinRotor(getNumberFromId($(this), 'thin-rotor-no-'), $(this).val());
});
$(getSelector('rotor-no-')).change(function () {
enigmaMachine.setRotor(getNumberFromId($(this), 'rotor-no-'), $(this).val());
});
$(getSelector('ground-setting-thin-rotor-no-')).change(function () {
enigmaMachine.setThinRotorGroundSetting(getNumberFromId($(this), 'ground-setting-thin-rotor-no-'), $(this).val());
});
$(getSelector('ground-setting-rotor-no-')).change(function () {
enigmaMachine.setRotorGroundSetting(getNumberFromId($(this), 'ground-setting-rotor-no-'), $(this).val());
});
$(getSelector('ring-setting-thin-rotor-no-')).change(function () {
enigmaMachine.setThinRotorRingSetting(getNumberFromId($(this), 'ring-setting-thin-rotor-no-'), $(this).val());
});
$(getSelector('ring-setting-rotor-no-')).change(function () {
enigmaMachine.setRotorRingSetting(getNumberFromId($(this), 'ring-setting-rotor-no-'), $(this).val());
});
$('.randomise').click(function () {
enigmaMachine.randomise();
update(enigmaMachine.getState());
});
$('.run').click(function () {
var message = enigma.util.tidyUpMessage($('.message').val());
var result = enigmaMachine.readString(message);
$('.ciphertext-input').text(message);
$('.ciphertext-output').text(result);
update(enigmaMachine.getState());
});
});