Lots of refactoring. More, smaller files.

This commit is contained in:
Johannes Randerath
2024-08-29 16:30:19 +02:00
parent 36e0ec3ad1
commit 9dafd7504b
15 changed files with 1052 additions and 760 deletions

View File

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

102
static/js/base.js Normal file
View File

@@ -0,0 +1,102 @@
/* This file is part of inventur.
* inventur is a simple web app using rocket to help maintain inventory data.
* Copyright (C) 2024 Johannes Randerath
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
let ncol = 1;
let todelete = "";
function create_table() {
let form = document.getElementById('form_create_table');
form.submit();
form.reset();
document.getElementById('dynamic_columns').innerHTML = "";
ncol = 1;
}
function set_modal_target(modal_target) {
document.getElementById('modal_caller').innerHTML = modal_target;
}
function start_ajax(path, listener) {
let req = new XMLHttpRequest();
req.open('GET', path, true);
req.send();
req.onReadyStateChange = function() {
if (req.readyState == XMLHttpRequest.DONE) {
callback();
}
};
};
function import_table() {
console.log("1");
Papa.parse(document.getElementById('import_file').files[0], {
complete: function process(result) {
console.log("2");
let csv = result.data;
let req = new XMLHttpRequest();
req.open("POST", "/table/import", true);
req.responseType = 'blob';
req.setRequestHeader('Content-Type', 'application/json');
let cols = csv[0];
let rows = csv.slice(1);
if (rows[rows.length - 1].length != cols.length) {
rows = rows.slice(0, rows.length-1);
}
req.send(JSON.stringify({
name: document.getElementById('import_name').value,
columns: cols,
rows: rows,
}));
req.onload = function(resp) {
if (this.readyState == 4) {
window.location.href = req.responseURL;
}
};
}
});
};
function add_column() {
ncol += 1;
document.getElementById('dynamic_columns').innerHTML += `
<div class='row mt-3' id='columns'>
<div class='col-auto align-bottom'>
<div class='form-text me-1' id='field_index'>
<strong>${ncol}</strong>
</div>
</div>
<div class='col-auto'>
<input type='text' class='form-control' name='field_names' id='field_name' aria-describedby='field_name_label'>
</div>
<div class='col-auto'>
<select class='form-select' aria-label='data type' name='field_types'>
<option value='0' selected>Text</option>
<option value='1'>Number</option>
</select>
</div>
</div>`;
};
function confirm_delete(form) {
document.getElementById(`form_edit_${form}`).reset();
$('#confirm_delete_modal').modal('show');
document.getElementById('confirm_delete_modal_entity').innerHTML = form;
document.getElementById('confirm_delete_modal_button').onclick = function () {
document.getElementById(`form_delete_${form}`).submit();
}
}
function submit_and_reset(form) {
let f = document.getElementById(form);
f.submit();
f.reset();
}

85
static/js/table.js Normal file
View File

@@ -0,0 +1,85 @@
/* This file is part of inventur.
* inventur is a simple web app using rocket to help maintain inventory data.
* Copyright (C) 2024 Johannes Randerath
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
let edit_mode = false;
document.addEventListener("DOMContentLoaded", (event) => {
let rows = document.getElementById("content_table").getElementsByTagName('tbody')[0].rows;
for (let i = 0; i < rows.length; i++)
{
let row = rows[i];
let createClickHandler = function(r)
{
return function()
{
$('#edit_entry_modal').modal('show');
document.getElementById('modal_caller').innerHTML = i+1;
document.getElementById('form_edit_entry_rowpos').value = i+1;
document.getElementById('form_delete_entry_rowpos').value = i+1;
let cells = row.cells;
for (let j = 1; j < cells.length; j++) {
document.getElementById(`form_edit_entry_${j}`).value = cells[j].innerHTML;
}
};
};
row.onclick = createClickHandler(row);
}
});
function toggle_edit_tname() {
let span = document.getElementById('tname');
if (!edit_mode) {
span.innerHTML = `
<div class='row'>
<div class='col-auto ml-1 p-2'>
<form id='form_edit_table' action='/table/name/edit' method='post'>
<input name='tblid' value='${tblid}' hidden />
<input type='text' class='form-control' id='tname_edit' name='new_name' value='${tblname}' />
</form>
</div>
<div class='col-auto mt-0 pt-0 pr-1'>
<div class="btn-group" role="group">
<button class="btn btn-secondary" onclick="toggle_edit_tname();" type="button"><i class="bi bi-x"></i></button>
<button class='btn btn-success' type='button' onclick='edit_tname()'>
<i class='bi bi-check'></i>
</button>
</div>
<button class='btn btn-danger' type="button" onclick='confirm_delete("table");'><i class="bi bi-trash3-fill"></i></button>
</div>
<form id="form_delete_table" action="/table/delete" method="post">
<input value="${tblid}" name="tblid" hidden />
</form>`;
edit_mode = true;
document.getElementById('pencil_button_edit_tname').hidden = true;
} else {
document.getElementById('pencil_button_edit_tname').hidden = false;
span.innerHTML = `${tblname}`;
edit_mode = false;
}
}
function edit_tname() {
document.getElementById('form_edit_table').submit();
toggle_edit_tname();
}
function edit_column(clmn_index) {
document.getElementById('form_edit_column_name').value = column_names[clmn_index];
document.getElementById('form_edit_column_type').value = column_types[clmn_index];
document.getElementById('modal_caller').innerHTML = column_names[clmn_index];
document.getElementById('form_delete_column_idintbl').value = clmn_index + 1;
document.getElementById('form_edit_column_idintbl').value = clmn_index + 1;
}