201 lines
5.5 KiB
JavaScript
Raw Normal View History

2024-01-24 01:18:10 +01:00
// fixed height
const height = 350;
2024-01-23 00:35:07 +01:00
// set the margins of the graph
const margin = {
top: 10,
2024-01-24 01:18:10 +01:00
right: height,
2024-02-12 22:25:14 +01:00
bottom: 20,
left: 50
2024-01-23 00:35:07 +01:00
};
2024-01-24 01:18:10 +01:00
// get window width
2024-01-23 00:35:07 +01:00
const width = window.innerWidth - margin.right - margin.left -10;
2024-01-24 01:18:10 +01:00
2024-01-29 23:35:20 +01:00
// url
const currentUrl = window.location.href;
2024-02-05 09:30:29 +01:00
// by default, do not show hidden items
var show_hidden = false;
document.getElementById("global_toggleshow").addEventListener("click", function() {
show_hidden = !show_hidden;
if (document.getElementById("global_toggleshow").innerHTML == "show hidden") {
document.getElementById("global_toggleshow").innerHTML = "hide";
} else {
document.getElementById("global_toggleshow").innerHTML = "show hidden";
}
2024-02-05 09:30:29 +01:00
refresh_graphs();
});
2024-01-23 00:35:07 +01:00
// reload page when window is resized
// (resizes every elements)
window.onresize = function(){ location.reload(); }
function render_graphs_wrapper() {
get_data().then(function(data){
2024-02-05 09:30:29 +01:00
render_graphs(data[0], data[1], show_hidden);
})
}
// refresh render
function refresh_graphs() {
const node = document.getElementById("graphs");
while (node.firstChild) {
node.removeChild(node.lastChild);
}
render_graphs_wrapper();
}
2024-01-31 01:06:26 +01:00
// render graphs
render_graphs_wrapper();
2024-01-31 01:06:26 +01:00
// add item
document.getElementById("addbutton").addEventListener("click", addItem);
async function addItem() {
const apiUrl = `${currentUrl}app/add`;
const postData = {
itemid: document.getElementById("additemid").value,
attributes: document.getElementById("addattributes").value
};
// clear input fields
document.getElementById("additemid").value='';
document.getElementById("addattributes").value='';
2024-01-31 01:06:26 +01:00
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(postData)
};
const response = await fetch(apiUrl, requestOptions)
.catch(error => {
console.error('Error during POST request:', error);
});
if (response.ok) {
console.log(response);
// refresh graphs to display new item
refresh_graphs();
2024-01-31 01:06:26 +01:00
} else {
throw new Error('Error in server response');
}
2024-02-01 23:27:39 +01:00
}
2024-02-05 09:30:29 +01:00
async function toggle_hide(uuid) {
const apiUrl = `${currentUrl}app/show`;
const postData = {
uuid: uuid
};
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(postData)
};
const response = await fetch(apiUrl, requestOptions)
.catch(error => {
console.error('Error during POST request:', error);
});
if (response.ok) {
console.log(response);
// refresh graphs to stop displaying hidden items
// or display shown items
2024-02-05 09:30:29 +01:00
refresh_graphs();
} else {
throw new Error('Error in server response');
}
}
2024-02-01 23:27:39 +01:00
async function delItem(uuid) {
const apiUrl = `${currentUrl}app/delete`;
const postData = {
uuid: uuid
};
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(postData)
};
const response = await fetch(apiUrl, requestOptions)
.catch(error => {
console.error('Error during POST request:', error);
});
if (response.ok) {
console.log(response);
// refresh graphs to stop displaying deleted item
refresh_graphs();
} else {
throw new Error('Error in server response');
}
2024-01-31 01:06:26 +01:00
}
2024-02-13 22:53:03 +01:00
// function to create a list
document.getElementById("create_list_add").addEventListener("click", addList);
async function addList() {
const checked = getCheckedCheckboxIds('.checkbox-itemlist');
const items_uuid = checked.map(str => str.substring(9)).join('#');
const listname = document.getElementById("listName").value;
if (listname.length !== 0) {
const apiUrl = `${currentUrl}app/create-list`;
const postData = {
name: listname,
description: document.getElementById("listDescription").value,
items: items_uuid
};
// clear input fields
document.getElementById("listName").value='';
document.getElementById("listDescription").value='';
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(postData)
};
const response = await fetch(apiUrl, requestOptions)
.catch(error => {
console.error('Error during POST request:', error);
});
if (response.ok) {
createListPopup.style.display = 'none';
console.log(response);
} else {
throw new Error('Error in server response');
}
} else {
console.log("cannot create list without a name")
}
}
// get an array of id of checked checkboxes of a specific class name
function getCheckedCheckboxIds(classname) {
const checkboxes = d3.selectAll(classname);
const checkedIds = checkboxes.filter(function() {
return this.checked;
}).nodes().map(function(checkbox) {
return checkbox.id;
});
return checkedIds;
}