This repository has been archived by the owner on Apr 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
291 lines (241 loc) · 8.59 KB
/
renderer.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
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// No Node.js APIs are available in this process because
// `nodeIntegration` is turned off. Use `preload.js` to
// selectively enable features needed in the rendering
// process.
const {ipcRenderer, remote} = require('electron');
let aTemp, bTemp, resultTemp;
$(document).ready(() => {
$('input[type="number"]').on('keyup', function(){
$(this).val($(this).val().replace (/\D/, ''));
});
})
function setMatrixA(){
if($('#aW').val() != $('#bH').val()){
remote.dialog.showMessageBoxSync(remote.getCurrentWindow(), {
title: 'Ошибка!',
type: 'error',
message: 'Такие матрицы нельзя перемножить, так как количество столбцов матрицы А не равно количеству строк матрицы В.',
});
return false;
}
$('#matrixSize').hide();
const w = $('#aW').val();
const h = $('#aH').val();
let h4 = document.createElement('label');
h4.classList.add('form-label');
h4.innerText = 'Значения матрицы А:';
let matrixContainer = document.createElement('div');
matrixContainer.classList.add('matrixContainer');
document.querySelector('#matrixA').appendChild(matrixContainer)
document.querySelector("#matrixA").insertBefore(h4, document.querySelector('#matrixA > .matrixContainer'))
for(let rowI = 0; rowI < h; rowI++){
var row = document.createElement('div');
row.classList.add('row')
for(let col = 0; col < w; col++){
let colEl = document.createElement('div');
colEl.classList.add('col', 'mb-1')
let inputEl = document.createElement('input');
inputEl.type = 'text';
inputEl.step = 1;
inputEl.classList.add('form-control');
colEl.appendChild(inputEl);
row.prepend(colEl)
}
document.querySelector("#matrixA > .matrixContainer").appendChild(row);
if(h-1 != rowI) document.querySelector("#matrixA > .matrixContainer").appendChild(document.createElement('hr'));
}
let btnGroup = document.createElement('div');
btnGroup.classList.add('btn-group')
let next = document.createElement('button');
next.classList.add('btn', 'btn-dark', 'mt-2');
next.innerText = 'Далее';
next.onclick = setMatrixB;
btnGroup.appendChild(next)
let back = document.createElement('button');
back.classList.add('btn', 'btn-primary', 'mt-2');
back.innerText = 'Назад';
back.onclick = function () {
$('#matrixA').hide();
$('#matrixA').html('');
$('#matrixSize').show();
};
btnGroup.appendChild(back)
document.querySelector("#matrixA").appendChild(btnGroup)
$('#matrixA').show();
}
function setMatrixB(){
$('#matrixA').hide();
const w = $('#bW').val();
const h = $('#bH').val();
let h4 = document.createElement('label');
h4.classList.add('form-label');
h4.innerText = 'Значения матрицы B:';
let matrixContainer = document.createElement('div');
matrixContainer.classList.add('matrixContainer');
document.querySelector('#matrixB').appendChild(matrixContainer)
document.querySelector("#matrixB").insertBefore(h4, document.querySelector('#matrixB > .matrixContainer'))
for(let rowI = 0; rowI < h; rowI++){
var row = document.createElement('div');
row.classList.add('row')
for(let col = 0; col < w; col++){
let colEl = document.createElement('div');
colEl.classList.add('col', 'mb-1')
let inputEl = document.createElement('input');
inputEl.type = 'text';
inputEl.step = 1;
inputEl.classList.add('form-control');
colEl.appendChild(inputEl);
row.prepend(colEl)
}
document.querySelector("#matrixB > .matrixContainer").appendChild(row)
if(h-1 != rowI) document.querySelector("#matrixB > .matrixContainer").appendChild(document.createElement('hr'));
}
let btnGroup = document.createElement('div');
btnGroup.classList.add('btn-group')
let next = document.createElement('button');
next.classList.add('btn', 'btn-dark', 'mt-2');
next.innerText = 'Далее';
next.onclick = result;
btnGroup.appendChild(next)
let back = document.createElement('button');
back.classList.add('btn', 'btn-primary', 'mt-2');
back.innerText = 'Назад';
back.onclick = function () {
$('#matrixB').hide();
$('#matrixB').html('');
$('#matrixA').show();
};
btnGroup.appendChild(back)
document.querySelector("#matrixB").appendChild(btnGroup)
$('#matrixB').show();
}
async function result(){
let a = [], b = [];
let matrixRowsA = document.querySelectorAll('#matrixA > .matrixContainer > .row');
for(let row of matrixRowsA){
let cols = [];
for(let col of row.querySelectorAll('input')){
let valueCol = col.value.replace(/\s+/igm, '').replace(',', '.');
if(!valueCol){
valueCol = 0;
} else if(col.value.indexOf(',') || col.value.indexOf('.')){
valueCol = parseFloat(col.value);
} else {
valueCol = parseInt(col.value);
}
cols.push(valueCol);
}
a.push(cols);
}
let matrixRowsB = document.querySelectorAll('#matrixB > .matrixContainer > .row');
for(let row of matrixRowsB){
let cols = [];
for(let col of row.querySelectorAll('input')){
let valueCol = col.value.replace(/\s+/igm, '').replace(',', '.');
if(!valueCol){
valueCol = 0;
} else if(col.value.indexOf(',') || col.value.indexOf('.')){
valueCol = parseFloat(col.value);
} else {
valueCol = parseInt(col.value);
}
cols.push(valueCol);
}
b.push(cols);
}
$('#matrixB').hide();
let containerA = document.createElement('div');
containerA.classList.add('matrixContainer');
let tableA = document.createElement('table');
tableA.border = 1;
tableA.width = '100%';
for(let row of a){
let tr = document.createElement('tr');
for(let col of row){
let td = document.createElement('td');
td.innerText = col;
tr.appendChild(td);
}
tableA.appendChild(tr);
}
let h6A = document.createElement('h6');
h6A.innerText = 'Значения A:';
h6A.classList.add('form-label');
document.querySelector("#result").appendChild(h6A)
containerA.appendChild(tableA)
document.querySelector("#result").appendChild(containerA)
let containerB = document.createElement('div');
containerB.classList.add('matrixContainer');
let tableB = document.createElement('table');
tableB.border = 1;
tableB.width = '100%';
for(let row of b){
let tr = document.createElement('tr');
for(let col of row){
let td = document.createElement('td');
td.innerText = col;
tr.appendChild(td);
}
tableB.appendChild(tr);
}
let h6B = document.createElement('h6');
h6B.innerText = 'Значения B:';
h6B.classList.add('form-label');
document.querySelector("#result").appendChild(h6B)
containerB.appendChild(tableB)
document.querySelector("#result").appendChild(containerB);
await ipcRenderer.invoke('calc', {a, b}).then((result) => {
let containerResultCalc = document.createElement('div');
containerResultCalc.classList.add('matrixContainer');
let tableResultCalc = document.createElement('table');
tableResultCalc.border = 1;
tableResultCalc.width = '100%';
for(let row of result){
let tr = document.createElement('tr');
for(let col of row){
let td = document.createElement('td');
td.innerText = Math.round((col + Number.EPSILON) * 1000) / 1000;
tr.appendChild(td);
}
tableResultCalc.appendChild(tr);
}
let h6ResultCalc = document.createElement('h6');
h6ResultCalc.innerText = 'Результат умножения:';
h6ResultCalc.classList.add('form-label');
document.querySelector("#result").appendChild(h6ResultCalc)
containerResultCalc.appendChild(tableResultCalc)
document.querySelector("#result").appendChild(containerResultCalc);
let btnGroup = document.createElement('div');
btnGroup.classList.add('btn-group')
let exportBtn = document.createElement('button');
exportBtn.classList.add('btn', 'btn-success', 'mt-2');
exportBtn.innerText = 'Выбрать папку для экспорта данных';
aTemp = a;
bTemp = b;
resultTemp = result;
exportBtn.onclick = async function () {
await ipcRenderer.invoke('export', {a: aTemp, b: bTemp, result: resultTemp}).then((result) => {
});
};
btnGroup.appendChild(exportBtn);
let goToStart = document.createElement('button');
goToStart.classList.add('btn', 'btn-primary', 'mt-2');
goToStart.innerText = 'Перейти в начало';
goToStart.onclick = function () {
$('#result').hide();
$('#matrixB').html('');
$('#matrixA').html('');
$('#result').html('');
aTemp = null;
bTemp = null;
resultTemp = null;
$("#matrixSize").show();
};
btnGroup.appendChild(goToStart)
document.querySelector("#result").appendChild(btnGroup)
});
$('#result').show();
}
$('#setMatrixSize').on('click', setMatrixA);