HDHaro's picture
create a yes, no decision tree generator
1602335 verified
Raw History Blame Contribute Delete
1.75 kB
document.addEventListener('DOMContentLoaded', () => {
// Global state for our decision tree
const state = {
nodes: [],
currentNodeId: 0
};
// Function to generate unique IDs for nodes
const generateId = () => {
return `node-${state.currentNodeId++}`;
};
// Function to handle adding child nodes
window.addChildNode = (parentId, answerText) => {
const parentNode = document.querySelector(`custom-decision-node[node-id="${parentId}"]`);
if (!parentNode) return;
const newNode = document.createElement('custom-decision-node');
newNode.setAttribute('node-id', generateId());
newNode.classList.add('new-node');
const pathContainer = document.createElement('div');
pathContainer.className = 'decision-path mt-4 pl-6';
const answerLabel = document.createElement('div');
answerLabel.className = 'text-sm font-medium text-gray-600 mb-2 connector-line';
answerLabel.textContent = answerText;
pathContainer.appendChild(answerLabel);
pathContainer.appendChild(newNode);
parentNode.shadowRoot.querySelector('.children-container').appendChild(pathContainer);
// Scroll to the new node
setTimeout(() => {
newNode.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}, 100);
};
// Function to delete a node and its children
window.deleteNode = (nodeId) => {
const node = document.querySelector(`custom-decision-node[node-id="${nodeId}"]`);
if (!node) return;
if (confirm('Delete this node and all its children?')) {
node.parentNode.remove();
}
};
});