-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateFile.html
More file actions
512 lines (411 loc) · 17 KB
/
GenerateFile.html
File metadata and controls
512 lines (411 loc) · 17 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<html>
<head>
<meta charset = "UTF-8">
</head>
<body style = "margin: 0;">
<div id = "mainPart" style = "background-color: gray; position: relative; left: 0; top: 0; width: 700px; height: 100%; margin-left: auto; margin-right: auto;">
<div style = "position: absolute; left: 15px; top: 5px; color: white; font-size: 16px;">
Последно актуализиран на: 03/05/2021
</br>
Изтегляне на проекта:
<a href = "https://github.com/taulfsime/schoolBellClock" target = "_blank">link</a>
</br>
Докладване за проблем:
<a href = "https://github.com/taulfsime/schoolBellClock/issues" target = "_blank">link</a>
</div>
<div style = "position: absolute; left: 570px; top: 15px; display: flex; flex-direction: column;">
<label for = "loadFile" style = "user-select: none; border: 1px solid white; border-radius: 5px; padding: 5px 7px; text-align: center; background-color: #404040; color: white;" onmouseover = "this.style.backgroundColor = '#787878';" onmouseout = "this.style.backgroundColor = '#404040';">
Зареди файл
</label>
<input id = "loadFile" type="file" onchange="loadFromFile(this)" style = "display: none;"/>
<label for = "downloadFile" style = "user-select: none; border: 1px solid white; border-radius: 5px; padding: 5px 7px; text-align: center; background-color: #404040; color: white;" onmouseover = "this.style.backgroundColor = '#787878';" onmouseout = "this.style.backgroundColor = '#404040';">
Изтегли файл
</label>
<input id = "downloadFile" type = "button" value = "Изтегли файл" onclick = "downloadFile()" style = "display: none;">
</div>
<div style = "position: absolute; left: 140px; top: 100px">
<input id = "buttonPlan1" type = "button" value = "План #1" onclick = "setPlan(1)" style = "padding: 10px; float: left; width: 200px; color: white; font-weight: bold; border-color: white;">
<input id = "buttonPlan2" type = "button" value = "План #2" onclick = "setPlan(2)" style = "padding: 10px; float: left; width: 200px; color: white; font-weight: bold; border-color: white;">
</div>
<div style = "position: absolute; left: 200; top: 160px; color: white; user-select: none" title = "Минути преди началния час за първия звъцен">
Минути (?):
<input type = "number" id = "minsBeforeFirstRing" style = "width: 70px;" min = "0" max = "59" value = "3"/>
</div>
<div style = "position: absolute; left: 450px; top: 160px; color: white; user-select: none" title = "Минути преди началния час за първия звъцен">
<input type = "button" value = "Нулирай" onclick = "clearTables();" title = "Нулиране на двата плана"/>
</div>
<div style = "position: absolute; left: 660px; top: 175px; color: white; user-select: none" title = "Максимален брой часове: 9">(?)</div>
</div>
<div style = "position: absolute; left: 10; bottom: 20;">
Created by
<a href = "https://github.com/taulfsime">taulfsime</a>
</div>
</body>
<script>
window.onbeforeunload = (event) => false;
document.addEventListener("DOMContentLoaded", function(e) {
function genTable(tableId) {
var table = document.createElement("table");
table.id = tableId;
table.style = "position: absolute; left: 30px; top: 200px; width: 640px; color: white; border: 1px solid black;";
//header row
var row = table.insertRow(0);
var cell = row.insertCell(0);
cell.style = "border: 1px solid black; width: 20px; user-select: none; text-align: center;";
cell.innerHTML = "#";
cell = row.insertCell(1);
cell.style = "border: 1px solid black; user-select: none; width: 130px; text-align: center;";
cell.innerHTML = "НАЧАЛО";
cell = row.insertCell(2);
cell.style = "border: 1px solid black; user-select: none; width: 130px; text-align: center;";
cell.innerHTML = "КРАЙ";
cell = row.insertCell(3);
cell.style = "border: 1px solid black; user-select: none; width: 300px; text-align: center;";
cell.title = "Сложи мишката върху бутона на функцията за повече информация"
cell.innerHTML = "ФУНКЦИИ (?)";
//add button row
row = table.insertRow(1);
cell = row.insertCell(0);
cell.style = "margin: auto;";
cell.colSpan = 4;
var buttonAdd = document.createElement("input");
buttonAdd.type = "button";
buttonAdd.value = "Добави час";
buttonAdd.onclick = () => { addRow(tableId); };
buttonAdd.style = "width: 100%;";
cell.appendChild(buttonAdd);
document.getElementById("mainPart").appendChild(table);
}
genTable("hoursTablePlan1");
genTable("hoursTablePlan2");
setPlan(1);
});
function updateNumbers(tableId) {
var table = document.getElementById(tableId);
if(table.rows.legend <= 2) {
return;
}
for(var i = 1; i < table.rows.length - 1; i++) {
table.rows[i].cells[0].innerHTML = i;
}
}
function validateRows(tableId) {
var table = document.getElementById(tableId);
if(table.rows.length <= 2) {
return false;
}
var allValid = true;
for(var i = 1; i < table.rows.length - 1; i++) {
var timeStart = table.rows[i].cells[1].childNodes[1].value;
var timeEnd = table.rows[i].cells[2].childNodes[1].value;
var valid = true;
if(timeStart.length == 0 || timeEnd.length == 0) {
valid = false;
}
if(valid && compareTimes(timeStart, timeEnd) != 1) {
valid = false;
}
if(i > 1) {
timeEnd = table.rows[i - 1].cells[2].childNodes[1].value;
if(valid && compareTimes(timeEnd, timeStart) != 1) {
valid = false;
}
}
if(!valid) {
allValid = false;
}
table.rows[i].style.backgroundColor = ( valid ? "" : "red");
}
return allValid;
}
function addMinutes(timeString, mins) {
var hours = parseInt(timeString.split(":")[0]);
var minutes = parseInt(timeString.split(":")[1]);
minutes += mins;
if(minutes < 0) {
hours -= 1;
minutes += 60;
}
else if(minutes >= 60) {
hours += 1;
minutes -= 60;
}
if(hours < 0) {
hours += 23;
}
else if(hours >= 24) {
hours -= 23;
}
timeString = (hours < 10 ? "0" : "") + hours.toString() + ":" + (minutes < 10 ? "0" : "") + minutes.toString();
return timeString;
}
function compareTimes(time1String, time2String) {
var hours1 = parseInt(time1String.split(":")[0]);
var minutes1 = parseInt(time1String.split(":")[1]);
var hours2 = parseInt(time2String.split(":")[0]);
var minutes2 = parseInt(time2String.split(":")[1]);
if(hours1 == hours2) {
if(minutes1 == minutes2) {
return 0;
}
else {
return (minutes2 > minutes1 ? 1 : -1);
}
}
else{
return (hours2 > hours1 ? 1 : -1);
}
}
function getDiffMinutes(time1String, time2String) {
var hours1 = parseInt(time1String.split(":")[0]);
var minutes1 = parseInt(time1String.split(":")[1]);
var hours2 = parseInt(time2String.split(":")[0]);
var minutes2 = parseInt(time2String.split(":")[1]);
var diff = (hours2 - hours1) * 60 + (minutes2 - minutes1)
return (diff < 0 ? -1 : 1) * diff;
}
function addRow(tableId) {
var table = document.getElementById(tableId);
var rowCount = table.rows.length;
if(rowCount == 11) {
return;
}
validateRows(tableId);
var row = table.insertRow(rowCount - 1);
var cell = row.insertCell(0);
cell.innerHTML = rowCount - 1;
//Cell for setting start hour
cell = row.insertCell(1);
//setting start hour checkbox
var elementCheckHourStart = document.createElement("input");
elementCheckHourStart.type = "checkbox";
elementCheckHourStart.checked = true;
cell.appendChild(elementCheckHourStart);
//sestting start hour time
var elementHourStart = document.createElement("input");
elementHourStart.type = "time";
elementHourStart.onchange = (e) => { validateRows(tableId); }
if(table.rows.length > 3) {
elementHourStart.value = table.rows[rowCount - 2].cells[2].childNodes[1].value;
}
cell.appendChild(elementHourStart);
//Cell for setting end hour
cell = row.insertCell(2);
//setting end hour checkbox
var elementCheckHourEnd = document.createElement("input");
elementCheckHourEnd.type = "checkbox";
elementCheckHourEnd.checked = false;
cell.appendChild(elementCheckHourEnd);
//sestting end hour time
var elementHourEnd = document.createElement("input");
elementHourEnd.type = "time";
elementHourEnd.onchange = (e) => { validateRows(tableId); }
cell.appendChild(elementHourEnd);
elementCheckHourStart.onchange = (e) => elementCheckHourEnd.checked = !e.target.checked;
elementCheckHourEnd.onchange = (e) => elementCheckHourStart.checked = !e.target.checked;
cell = row.insertCell(3);
cell.style = "position: relative; display: flex; flex-direction: row; justify-content: space-around;";
var buttonTexts = [ "=", "-10", "-5", "+5", "+10", "X" ];
var buttonTitles = [ "Копира времето от избрания елемент към другия елемент", "Изважда 10 минути от избраното време", "Изважда 5 минути от избраното време", "Добавя 5 минути към избраното време", "Добавя 10 минути към избраното време", "Изтрива реда" ];
var buttonFuncs = [
function() {
if(elementCheckHourStart.checked) {
if(elementHourStart.value.length > 0) {
elementHourEnd.value = elementHourStart.value;
}
}
else {
if(elementHourEnd.value.length > 0) {
elementHourStart.value = elementHourEnd.value;
}
}
},
function() {
var timeString = (elementCheckHourStart.checked ? elementHourStart.value : elementHourEnd.value);
timeString = addMinutes(timeString, -10);
if(elementCheckHourStart.checked) {
elementHourStart.value = timeString;
}
else {
elementHourEnd.value = timeString;
}
validateRows(tableId);
},
function() {
var timeString = (elementCheckHourStart.checked ? elementHourStart.value : elementHourEnd.value);
timeString = addMinutes(timeString, -5);
if(elementCheckHourStart.checked) {
elementHourStart.value = timeString;
}
else {
elementHourEnd.value = timeString;
}
validateRows(tableId);
},
function() {
var timeString = (elementCheckHourStart.checked ? elementHourStart.value : elementHourEnd.value);
timeString = addMinutes(timeString, 5);
if(elementCheckHourStart.checked) {
elementHourStart.value = timeString;
}
else {
elementHourEnd.value = timeString;
}
validateRows(tableId);
},
function() {
var timeString = (elementCheckHourStart.checked ? elementHourStart.value : elementHourEnd.value);
timeString = addMinutes(timeString, 10);
if(elementCheckHourStart.checked) {
elementHourStart.value = timeString;
}
else {
elementHourEnd.value = timeString;
}
validateRows(tableId);
},
function() {
var n = parseInt(row.cells[0].innerHTML);
if(confirm("Час #" + n + " ще бъде изтрит?")) {
table.deleteRow(n);
updateNumbers(tableId);
if(table.rows.length < 11) {
table.rows[table.rows.length - 1].cells[0].childNodes[0].disabled = false;
}
}
validateRows(tableId);
}
];
for(var i = 0; i < buttonTexts.length; i++) {
var btn = document.createElement("input");
btn.type = "button";
btn.value = buttonTexts[i];
btn.style = "position: relative;";
btn.onclick = buttonFuncs[i];
btn.title = buttonTitles[i];
cell.appendChild(btn);
}
if(table.rows.length == 11) {
table.rows[10].cells[0].childNodes[0].disabled = true;
}
return row;
}
function loadFromFile(element) {
if(element.files.length == 0) {
return;
}
var fileReader = new FileReader();
fileReader.onload = (e) => {
var values = e.target.result.split(" ");
if(values.length <= 0) {
return;
}
var index = 0;
var tempHour = values[index++] + ":" + values[index++];
var minsBeforeFirstRing = parseInt(values[index++]);
document.getElementById("minsBeforeFirstRing").value = minsBeforeFirstRing;
for(index; index < 28; index += 3) {
var row = addRow("hoursTablePlan1");
tempHour = addMinutes(tempHour, minsBeforeFirstRing);
row.cells[1].childNodes[1].value = tempHour;
tempHour = addMinutes(tempHour, parseInt(values[index]));
row.cells[2].childNodes[1].value = tempHour;
tempHour = addMinutes(tempHour, parseInt(values[index + 1]));
}
tempHour = values[index - 2] + ":" + values[index - 1];
for(index += 1; index < 28 * 2; index += 3) {
var row = addRow("hoursTablePlan2");
tempHour = addMinutes(tempHour, minsBeforeFirstRing);
row.cells[1].childNodes[1].value = tempHour;
tempHour = addMinutes(tempHour, parseInt(values[index]));
row.cells[2].childNodes[1].value = tempHour;
tempHour = addMinutes(tempHour, parseInt(values[index + 1]));
}
}
fileReader.readAsText(element.files[0]);
}
function prepTable(tableId) {
var minsBeforeFirstRing = parseInt(document.getElementById("minsBeforeFirstRing").value);
var table = document.getElementById(tableId);
var tableValues = [];
if(table.rows.length <= 2) {
return;
}
var tempTime = table.rows[1].cells[2].childNodes[1].value;
tempTime = addMinutes(tempTime, -1 * minsBeforeFirstRing);
tableValues.push(parseInt(tempTime.split(":")[0]));
tableValues.push(parseInt(tempTime.split(":")[1]));
for(var i = 1; i < 10; i++) {
if(i < table.rows.length - 1) {
tableValues.push(minsBeforeFirstRing);
tableValues.push(getDiffMinutes(table.rows[i].cells[2].childNodes[1].value, table.rows[i].cells[1].childNodes[1].value));
if(i < table.rows.length - 2) {
var diff = getDiffMinutes(table.rows[i].cells[2].childNodes[1].value, table.rows[i + 1].cells[1].childNodes[1].value);
tableValues.push(diff - minsBeforeFirstRing);
}
}
else {
tableValues.push(0);
tableValues.push(0);
tableValues.push(0);
}
}
return tableValues;
}
function downloadFile() {
var text = "";
if(!validateRows("hoursTablePlan1") || !validateRows("hoursTablePlan2")) {
alert("Файлът не може да бъде генериран! Проверете таблиците!");
return;
}
var values = prepTable("hoursTablePlan1");
if(typeof(values) == 'undefined') {
return;
}
for(var i = 0; i < values.length; i++) {
text += values[i].toString();
text += " ";
}
values = prepTable("hoursTablePlan2");
if(typeof(values) == 'undefined') {
return;
}
for(var i = 0; i < values.length; i++) {
text += values[i].toString();
if(i < values.length - 1) {
text += " ";
}
}
var element = document.createElement("a");
element.setAttribute("href", "data:text/plain; charset = utf-8," + encodeURIComponent(text));
element.setAttribute("download", "plan.txt");
element.style.display = "none";
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function clearTables() {
var tableIds = ["hoursTablePlan1", "hoursTablePlan2"];
for(var i = 0; i < tableIds.length; i++) {
var table = document.getElementById(tableIds[i]);
for(var j = table.rows.length - 2; j > 0; j--) {
table.deleteRow(j);
}
}
}
function setPlan(plan) {
if(plan == 1) {
document.getElementById("hoursTablePlan1").style.display = "";
document.getElementById("hoursTablePlan2").style.display = "none";
document.getElementById("buttonPlan1").style.backgroundColor = "#404040"
document.getElementById("buttonPlan2").style.backgroundColor = "#787878";
}
else {
document.getElementById("hoursTablePlan1").style.display = "none";
document.getElementById("hoursTablePlan2").style.display = "";
document.getElementById("buttonPlan1").style.backgroundColor = "#787878";
document.getElementById("buttonPlan2").style.backgroundColor = "#404040";
}
}
</script>
</html>