/* 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 .
*/
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 += `
`;
};
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();
}