File size: 1,754 Bytes
1602335
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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();
        }
    };
});