-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
129 lines (99 loc) · 3.24 KB
/
Copy pathscript.js
File metadata and controls
129 lines (99 loc) · 3.24 KB
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
class Algorithm {
setEncryptText(str) {
// this.encryptText - это то значение, которое я ввиду
// str - параметр нашей функции
this.encryptText = str;
}
setDecryptText(str) {
this.decryptText = str;
}
encrypt() {
return this.encryptText;
}
decrypt() {
return this.decryptText;
}
getResult(typeAction) {
if (typeAction === 'encrypt') {
return this.encrypt();
} else if (typeAction === 'decrypt') {
return this.decrypt();
}
}
}
// Algorithm #1
class Algorithm_0 extends Algorithm {
isEncrypted(str) {
return str.match( /a/g );
}
encrypt() {
return this.encryptText.replace(/a/g, '%1%');
}
decrypt() {
return this.decryptText.replace(/%1%/g, 'a');
}
}
// Algorithm #2
class Algorithm_1 extends Algorithm {
isEncrypted(str) {
return str.match( /b/g );
}
encrypt() {
return this.encryptText.replace(/b/g, '%1%');
}
decrypt() {
return this.decryptText.replace(/%1%/g, 'b');
}
}
// UI logic
$(function () {
const listAlg = [Algorithm_0, Algorithm_1];
let currentAlg = new listAlg[0]();
// смена алгоритвов
// по нажатию сменить
$('input[name=algorithm]').on('change', function () {
// listAlg - это либо 0 или 1 алгоритм
// this - это контекст input[name=algorithm]
currentAlg = new listAlg[$(this).val()]();
});
// очищаю textarea
$('input').click(function(){
$('textarea').val('');
});
// Зашифровать или разшифровать
$('button.btn-encrypt').click(function () {
let val = $('input[name=encrypt]').val();
if ( currentAlg.isEncrypted(val) ) {
currentAlg.setEncryptText(val);
$('textarea[name=result]').val(currentAlg.getResult('encrypt'));
} else {
currentAlg.setDecryptText(val);
$('textarea[name=result]').val(currentAlg.getResult('decrypt'));
}
$('input').val('');
});
// Зашифровать или разшифровать
$('button.btn-decrypt').click(function () {
let val = $('input[name=decrypt]').val();
if ( currentAlg.isEncrypted(val) ) {
currentAlg.setEncryptText(val);
$('textarea[name=result]').val(currentAlg.getResult('encrypt'));
} else {
currentAlg.setDecryptText(val);
$('textarea[name=result]').val(currentAlg.getResult('decrypt'));
}
$('input').val('');
});
// зашифровать
// $('button.btn-encrypt').click(function () {
// currentAlg.setEncryptText($('input[name=encrypt]').val());
// $('textarea[name=result]').val(currentAlg.getResult('encrypt'));
// $('input').val('');
// });
// разшифровать
// $('button.btn-decrypt').click(function () {
// currentAlg.setDecryptText($('input[name=decrypt]').val());
// $('textarea[name=result]').val(currentAlg.getResult('decrypt'));
// $('input').val('');
// });
});