decision-branchinator / components /decision-node.js
HDHaro's picture
create a yes, no decision tree generator
1602335 verified
Raw History Blame Contribute Delete
3.77 kB
class CustomDecisionNode extends HTMLElement {
constructor() {
super();
this.answerOptions = ['Yes', 'No'];
}
connectedCallback() {
const nodeId = this.getAttribute('node-id') || `node-${Math.random().toString(36).substr(2, 9)}`;
this.setAttribute('node-id', nodeId);
const isRoot = this.hasAttribute('is-root');
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
<style>
.node-card {
transition: all 0.2s;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.node-card:hover {
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
.answer-btn {
transition: all 0.2s;
}
.answer-btn:hover {
transform: translateY(-2px);
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.tooltip {
visibility: hidden;
opacity: 0;
transition: opacity 0.2s;
}
.tooltip-btn:hover + .tooltip {
visibility: visible;
opacity: 1;
}
</style>
<div class="node-card bg-white rounded-lg border border-gray-200 overflow-hidden">
<div class="p-4">
<div class="flex justify-between items-start mb-3">
<h3 class="text-lg font-medium text-gray-800">${isRoot ? 'Root Question' : 'Decision Point'}</h3>
<div class="flex space-x-2 relative">
<button class="tooltip-btn text-gray-500 hover:text-gray-700 p-1 rounded-full">
<i data-feather="more-vertical" class="w-4 h-4"></i>
</button>
<div class="tooltip absolute right-0 mt-6 w-48 bg-white rounded-md shadow-lg z-10 py-1">
<button onclick="deleteNode('${nodeId}')" class="flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 w-full text-left">
<i data-feather="trash-2" class="w-4 h-4 mr-2"></i> Delete
</button>
</div>
</div>
</div>
<input type="text" placeholder="Enter your question..." class="w-full px-3 py-2 border border-gray-300 rounded-md mb-4 focus:outline-none focus:ring-2 focus:ring-indigo-500">
<div class="flex flex-wrap gap-2 mb-2">
${this.answerOptions.map(answer => `
<button onclick="addChildNode('${nodeId}', '${answer}')" class="answer-btn bg-indigo-100 text-indigo-700 px-3 py-1 rounded-full text-sm font-medium flex items-center">
<i data-feather="plus" class="w-3 h-3 mr-1"></i> ${answer}
</button>
`).join('')}
</div>
</div>
<div class="children-container bg-gray-50 p-4 border-t border-gray-200"></div>
</div>
`;
// Replace feather icons after rendering
setTimeout(() => {
if (this.shadowRoot.querySelector('[data-feather]')) {
feather.replace({ class: 'inline-block', width: 16, height: 16 });
}
}, 0);
}
}
customElements.define('custom-decision-node', CustomDecisionNode);