Live-editing attributes via the API
// Wrapper function for updating attribute values through the API
function postLiveEdit (name, value) {
axios.post(attributes[name].apis.update.url, new URLSearchParams({value: value})).then(res => {
// Update the local value of the attribute in the dump
attributes[name].value = value;
/* Any additional processing can be done here, e.g. cascading changes to other parts of the sheet */
// Notify user; for iframe-based transclusions, send it to the parent window
let message = "<em>" + attributes[name].name.replace(/\[range:.*\]/g, "") + "</em> updated successfully";
(document.querySelector(".toast-container")) ? window.showToast(message) : window.parent.showToast(message);
}).catch(err => {
// Notify user; for iframe-based transclusions, send it to the parent window
let message = "Failed to update <em>" + attributes[name].name.replace(/\[range:.*\]/g, "") + "</em> (error code: " + err.status + ")";
(document.querySelector(".toast-container")) ? window.showToast(message) : window.parent.showToast(message);
});
}// Watch live attribute containers to update their related attribute on change
document.querySelectorAll('.daggerheart [data-stat]').forEach( (liveAtt) => {
let attributeName = liveAtt.dataset.stat;
// React to clicks
liveAtt.addEventListener("click", (event) => {
let numTicked;
/* A bunch of processing occurs here to determine the new value based on what exactly was clicked within the div */
// Update server-side attribute
postLiveEdit(attributeName, numTicked);
});
});Last updated
