class StateFlowBuilder { constructor() { this.states = []; this.connections = []; this.stateIdCounter = 0; this.connectionIdCounter = 0; this.isDragging = false; this.connectingFrom = null; this.tempConnection = null; this.draggedElement = null; this.dragOffset = { x: 0, y: 0 }; this.isConnecting = false; this.stateTemplates = [ { text: 'Eingabemaske Drucker', icon: 'printer', color: 'blue' }, { text: 'Drucker überprüfen', icon: 'check-circle', color: 'green' }, { text: 'Dokument verarbeiten', icon: 'file-text', color: 'purple' }, { text: 'Warten auf Bestätigung', icon: 'clock', color: 'yellow' }, { text: 'Fehlerbehandlung', icon: 'alert-triangle', color: 'red' }, { text: 'Abschließen', icon: 'check-square', color: 'indigo' }, { text: 'Benachrichtigung senden', icon: 'mail', color: 'pink' }, { text: 'Daten speichern', icon: 'save', color: 'teal' }, ]; this.init(); } init() { this.setupCanvas(); this.renderTemplates(); this.setupEventListeners(); this.updateConnectionLines(); } setupCanvas() { const canvas = document.getElementById('canvas'); canvas.classList.add('canvas-grid'); } renderTemplates() { const container = document.getElementById('stateTemplates'); container.innerHTML = ''; this.stateTemplates.forEach((template, index) => { const templateEl = document.createElement('div'); templateEl.className = `state-template p-4 bg-white border-2 border-gray-200 rounded-lg hover:border-indigo-400`; templateEl.draggable = true; templateEl.dataset.templateIndex = index; templateEl.innerHTML = `
${template.text}
`; container.appendChild(templateEl); templateEl.addEventListener('dragstart', (e) => { e.dataTransfer.effectAllowed = 'copy'; e.dataTransfer.setData('templateIndex', index); }); }); feather.replace(); } setupEventListeners() { // Canvas drag and drop const canvas = document.getElementById('canvas'); const canvasContent = document.getElementById('canvasContent'); canvas.addEventListener('dragover', (e) => { e.preventDefault(); e.dataTransfer.dropEffect = 'copy'; }); canvas.addEventListener('drop', (e) => { e.preventDefault(); const templateIndex = parseInt(e.dataTransfer.getData('templateIndex')); const rect = canvasContent.getBoundingClientRect(); const x = e.clientX - rect.left; const y = e.clientY - rect.top; this.addState(templateIndex, x, y); }); // Clear canvas document.getElementById('clearCanvas').addEventListener('click', () => { if (confirm('Möchtest du wirklich alle Zustände und Verbindungen löschen?')) { this.clearCanvas(); } }); // Global mouse events document.addEventListener('mousemove', (e) => this.handleMouseMove(e)); document.addEventListener('mouseup', () => this.handleMouseUp()); } addState(templateIndex, x, y) { const template = this.stateTemplates[templateIndex]; const state = { id: this.stateIdCounter++, x: x - 100, y: y - 30, width: 200, height: 60, ...template }; this.states.push(state); this.renderState(state); this.showToast('Zustand hinzugefügt', 'success'); } renderState(state) { const canvasContent = document.getElementById('canvasContent'); const stateEl = document.createElement('div'); stateEl.id = `state-${state.id}`; stateEl.className = `state-block absolute bg-white border-2 border-gray-300 rounded-lg shadow-lg p-4 flex items-center gap-3`; stateEl.style.left = `${state.x}px`; stateEl.style.top = `${state.y}px`; stateEl.style.width = `${state.width}px`; stateEl.innerHTML = `
${state.text} `; canvasContent.appendChild(stateEl); // Setup drag functionality stateEl.addEventListener('mousedown', (e) => this.startDrag(e, state)); // Setup connection points const connectionPoints = stateEl.querySelectorAll('.connection-point'); connectionPoints.forEach(point => { // Use mousedown instead of click for drag-to-connect point.addEventListener('mousedown', (e) => { e.stopPropagation(); const handled = this.handleConnectionPoint(point); if (handled) { e.preventDefault(); } }); // Also handle mouseup for connection completion point.addEventListener('mouseup', (e) => { if (this.isConnecting) { e.stopPropagation(); this.handleConnectionPoint(point); } }); // Visual feedback on hover during connection point.addEventListener('mouseenter', (e) => { if (this.isConnecting && point.dataset.type === 'input' && parseInt(point.dataset.state) !== this.connectingFrom.stateId) { point.classList.add('hover-target'); } }); point.addEventListener('mouseleave', (e) => { point.classList.remove('hover-target'); }); }); feather.replace(); } startDrag(e, state) { if (this.isConnecting || e.target.classList.contains('connection-point')) return; this.isDragging = true; this.draggedElement = document.getElementById(`state-${state.id}`); this.draggedElement.classList.add('dragging'); const rect = this.draggedElement.getBoundingClientRect(); const canvasRect = document.getElementById('canvasContent').getBoundingClientRect(); this.dragOffset = { x: e.clientX - rect.left, y: e.clientY - rect.top }; } handleMouseMove(e) { if (this.isConnecting && this.connectingFrom) { this.updateTempConnection(e); return; } if (this.isDragging && this.draggedElement) { const canvasRect = document.getElementById('canvasContent').getBoundingClientRect(); const x = e.clientX - canvasRect.left - this.dragOffset.x; const y = e.clientY - canvasRect.top - this.dragOffset.y; this.draggedElement.style.left = `${Math.max(0, x)}px`; this.draggedElement.style.top = `${Math.max(0, y)}px`; // Update state position const stateId = parseInt(this.draggedElement.id.replace('state-', '')); const state = this.states.find(s => s.id === stateId); if (state) { state.x = Math.max(0, x); state.y = Math.max(0, y); } // Throttled connection updates for smoother dragging if (!this.updateTimeout) { this.updateTimeout = requestAnimationFrame(() => { this.updateConnectionLinesForState(stateId); this.updateTimeout = null; }); } } } handleMouseUp() { if (this.draggedElement) { this.draggedElement.classList.remove('dragging'); } // Cancel any pending animation frame if (this.updateTimeout) { cancelAnimationFrame(this.updateTimeout); this.updateTimeout = null; } this.isDragging = false; this.draggedElement = null; } handleConnectionPoint(point) { const type = point.dataset.type; const stateId = parseInt(point.dataset.state); if (!this.isConnecting) { // Start connection if (type === 'output') { this.isConnecting = true; this.connectingFrom = { stateId, type, element: point }; point.classList.add('valid-target'); // Create temp connection line const svg = document.getElementById('connectionsSvg'); this.tempConnection = document.createElementNS('http://www.w3.org/2000/svg', 'line'); this.tempConnection.classList.add('connection-temp'); svg.appendChild(this.tempConnection); return true; // Prevent default drag behavior } else { this.showToast('Verbindungen starten nur von Ausgängen', 'error'); return false; } } else { // Complete connection if (type === 'input' && stateId !== this.connectingFrom.stateId) { this.addConnection(this.connectingFrom.stateId, stateId); this.showToast('Verbindung hergestellt', 'success'); } else if (type === 'output') { this.showToast('Verbindungen enden an Eingängen', 'error'); } else { this.showToast('Keine Selbstverbindungen erlaubt', 'error'); } // Reset this.connectingFrom.element.classList.remove('valid-target'); if (this.tempConnection) { this.tempConnection.remove(); this.tempConnection = null; } this.connectingFrom = null; this.isConnecting = false; return true; } } updateTempConnection(e) { if (!this.tempConnection || !this.connectingFrom) return; const fromPoint = this.getConnectionPointPosition(this.connectingFrom.stateId, this.connectingFrom.type); const canvasRect = document.getElementById('canvasContent').getBoundingClientRect(); const toX = e.clientX - canvasRect.left; const toY = e.clientY - canvasRect.top; this.tempConnection.setAttribute('x1', fromPoint.x); this.tempConnection.setAttribute('y1', fromPoint.y); this.tempConnection.setAttribute('x2', toX); this.tempConnection.setAttribute('y2', toY); } addConnection(fromStateId, toStateId) { // Check if connection already exists const exists = this.connections.some(c => c.from === fromStateId && c.to === toStateId ); if (exists) { this.showToast('Diese Verbindung existiert bereits', 'warning'); return; } this.connections.push({ id: this.connectionIdCounter++, from: fromStateId, to: toStateId }); this.updateConnectionLines(); } updateConnectionLines() { const svg = document.getElementById('connectionsSvg'); const existingLines = svg.querySelectorAll('.connection-line'); existingLines.forEach(line => line.remove()); this.connections.forEach(connection => { this.createConnectionLine(connection); }); } updateConnectionLinesForState(stateId) { const svg = document.getElementById('connectionsSvg'); this.connections.forEach(connection => { if (connection.from === stateId || connection.to === stateId) { // Find existing line for this connection const existingLine = svg.querySelector(`[data-connection-id="${connection.id}"]`); if (existingLine) { // Update existing line position const fromPoint = this.getConnectionPointPosition(connection.from, 'output'); const toPoint = this.getConnectionPointPosition(connection.to, 'input'); existingLine.setAttribute('x1', fromPoint.x); existingLine.setAttribute('y1', fromPoint.y); existingLine.setAttribute('x2', toPoint.x); existingLine.setAttribute('y2', toPoint.y); } else { // Create new line if not found this.createConnectionLine(connection); } } }); } createConnectionLine(connection) { const svg = document.getElementById('connectionsSvg'); const fromPoint = this.getConnectionPointPosition(connection.from, 'output'); const toPoint = this.getConnectionPointPosition(connection.to, 'input'); const line = document.createElementNS('http://www.w3.org/2000/svg', 'line'); line.classList.add('connection-line'); line.setAttribute('data-connection-id', connection.id); line.setAttribute('x1', fromPoint.x); line.setAttribute('y1', fromPoint.y); line.setAttribute('x2', toPoint.x); line.setAttribute('y2', toPoint.y); line.setAttribute('marker-end', 'url(#arrowhead)'); line.style.pointerEvents = 'stroke'; // Add click handler to remove connection line.addEventListener('click', () => { if (confirm('Verbindung entfernen?')) { this.removeConnection(connection.id); } }); svg.appendChild(line); } getConnectionPointPosition(stateId, type) { const stateEl = document.getElementById(`state-${stateId}`); const rect = stateEl.getBoundingClientRect(); const canvasRect = document.getElementById('canvasContent').getBoundingClientRect(); const point = stateEl.querySelector(`.connection-point[data-type="${type}"]`); const pointRect = point.getBoundingClientRect(); return { x: pointRect.left + pointRect.width / 2 - canvasRect.left, y: pointRect.top + pointRect.height / 2 - canvasRect.top }; } removeState(stateId) { // Remove related connections this.connections = this.connections.filter(c => c.from !== stateId && c.to !== stateId); // Remove state this.states = this.states.filter(s => s.id !== stateId); // Remove element document.getElementById(`state-${stateId}`).remove(); this.updateConnectionLines(); this.showToast('Zustand entfernt', 'info'); } removeConnection(connectionId) { this.connections = this.connections.filter(c => c.id !== connectionId); // Remove only the specific connection line const svg = document.getElementById('connectionsSvg'); const lineToRemove = svg.querySelector(`[data-connection-id="${connectionId}"]`); if (lineToRemove) { lineToRemove.remove(); } this.showToast('Verbindung entfernt', 'info'); } clearCanvas() { this.states = []; this.connections = []; this.stateIdCounter = 0; this.connectionIdCounter = 0; document.getElementById('canvasContent').innerHTML = ''; document.getElementById('connectionsSvg').querySelectorAll('.connection-line').forEach(line => line.remove()); this.showToast('Canvas geleert', 'info'); } showToast(message, type = 'info') { const container = document.getElementById('toastContainer'); const toast = document.createElement('div'); const colors = { success: 'bg-green-500', error: 'bg-red-500', warning: 'bg-yellow-500', info: 'bg-blue-500' }; toast.className = `toast ${colors[type]} text-white px-4 py-3 rounded-lg shadow-lg flex items-center gap-2`; toast.innerHTML = ` ${message} `; container.appendChild(toast); feather.replace(); setTimeout(() => { toast.style.animation = 'slideIn 0.3s ease-out reverse'; setTimeout(() => toast.remove(), 300); }, 3000); } } // Initialize the application const stateBuilder = new StateFlowBuilder();