journal/editpage.html

248 lines
9.5 KiB
HTML
Raw Normal View History

2023-11-09 18:11:16 +00:00
<!--
Copyright (c) 2023 Nikita Osokin.
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials provided
with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<input type="date" id="date" />
<p><p/>
<table id="schedule" border=1>
<tbody>
<tr class="schedRow">
<td>
1.
</td>
<td>
<input type="checkbox" checked />
</td>
<td>
<input type="text" />
</td>
<td>
<input type="text" />
</td>
</tr>
<tr class="schedRow">
<td>
2.
</td>
<td>
<input type="checkbox" checked />
</td>
<td>
<input type="text" />
</td>
<td>
<input type="text" />
</td>
</tr>
<tr class="schedRow">
<td>
3.
</td>
<td>
<input type="checkbox" checked />
</td>
<td>
<input type="text" />
</td>
<td>
<input type="text" />
</td>
</tr>
<tr class="schedRow">
<td>
4.
</td>
<td>
<input type="checkbox" checked />
</td>
<td>
<input type="text" />
</td>
<td>
<input type="text" />
</td>
</tr>
<tr class="schedRow">
<td>
5.
</td>
<td>
<input type="checkbox" checked />
</td>
<td>
<input type="text" />
</td>
<td>
<input type="text" />
</td>
</tr>
<tr class="schedRow">
<td>
6.
</td>
<td>
<input type="checkbox" checked />
</td>
<td>
<input type="text" />
</td>
<td>
<input type="text" />
</td>
</tr>
<tr class="schedRow">
<td>
7.
</td>
<td>
<input type="checkbox" checked />
</td>
<td>
<input type="text" />
</td>
<td>
<input type="text" />
</td>
</tr>
</tbody>
</table>
<p></p>
<input id="password" type="password" placeholder="Пароль" />
<input id="send" type="button" onclick="sendEdits()" value="Отправить изменения" />
<script>
function numToUint8Array(num, arr, off) {
for(var i = 0; i < 8; i++) {
arr[off + 7 - i] = num % 0x100;
num >>= 8;
}
}
function displayStatus(response) {
let sendButton = document.getElementById("send");
let statusText;
if(response.status == 200) {
statusText = document.createTextNode(" Расписание успешно изменено.");
} else if(response.status == 400) {
statusText = document.createTextNode(" Ошибка при изменении расписания: неправильный формат данных.");
} else if(response.status == 401) {
statusText = document.createTextNode(" Неверный пароль.");
} else if(response.status == 500) {
statusText = document.createTextNode(" Ошибка при изменении расписания: внутренняя ошибка сервера. Попробуйте отправить изменения ещё раз прямо сейчас или чуть позже.");
}
sendButton.after(statusText);
}
function displayNetError(e) {
let sendButton = document.getElementById("send");
let statusText = document.createTextNode(" Сетевая ошибка при отправке изменений. Возможно, вам стоит проверить своё интернет-соединение или подождать, а затем отправить изменения снова.");
sendButton.after(statusText);
}
function sendEdits() {
const schedule = document.getElementById("schedule");
let lessonsCount = 0;
let indices = [];
let names = [];
let namesLens = [];
let descs = [];
let descsLens = [];
let index = 0;
let textEncoder = new TextEncoder();
let password = textEncoder.encode(document.getElementById("password").value);
let bufferSize = 24 + password.length;
for(const row of schedule.children[0].children) {
if(row.children[1].children[0].checked) {
lessonsCount++;
indices.push(index);
let name = textEncoder.encode(row.children[2].children[0].value);
names.push(name);
namesLens.push(name.length);
let desc = textEncoder.encode(row.children[3].children[0].value);
descs.push(desc);
descsLens.push(desc.length);
bufferSize += 24 + name.length + desc.length;
}
index++;
}
let data = new Uint8Array(new ArrayBuffer(bufferSize));
numToUint8Array(password.length, data, 0);
for(let i = 0; i < password.length; i++) {
data[8 + i] = password[i];
}
let currentOff = 16 + password.length;
numToUint8Array(lessonsCount, data, currentOff);
currentOff += 8;
for(let i = 0; i < lessonsCount; i++) {
numToUint8Array(indices[i], data, currentOff);
currentOff += 8;
numToUint8Array(namesLens[i], data, currentOff);
currentOff += 8;
for(let j = 0; j < namesLens[i]; j++) {
data[currentOff + j] = names[i][j];
}
currentOff += namesLens[i];
numToUint8Array(descsLens[i], data, currentOff);
currentOff += 8;
for(let j = 0; j < descsLens[i]; j++) {
data[currentOff + j] = descs[i][j];
}
currentOff += descsLens[i];
}
let params = "?c=" + (new URLSearchParams(document.location.search)).get("c")
let day = document.getElementById("date").valueAsNumber;
if(day != NaN) {
params += "&d=" + (day / 86400000).toString();
}
fetch(
params,
{
method: "POST",
body: data
}
).then(
displayStatus,
displayNetError
);
}
</script>
</body>
</html>