mirror of
http://10.0.2.1:3031/sauer/bfa-dryer-design.git
synced 2026-06-30 11:36:42 +10:00
- New shape types: label (text), arrow (flow), temp_probe, output_indicator - Right-click context menu: change shape, animation, color, link to HMI control - Toolbar: add rect/circle/label/arrow/temp probe/output, delete selected - IN/OUT now draggable label elements - Temp probes with pulse animation and color coding - Arrows with adjustable length and rotation - Custom colors on any element (fill + stroke) - All positions/sizes/rotations saved to layout JSON Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
967 lines
49 KiB
HTML
967 lines
49 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
<title>BFA Banana Dryer — HMI Design Tool</title>
|
|
<script src="static/sortable.min.js"></script>
|
|
<style>
|
|
*{margin:0;padding:0;box-sizing:border-box;user-select:none;-webkit-tap-highlight-color:transparent}
|
|
:root{--bg:#0a0e17;--card:#131a2b;--border:#1e2a45;--text:#e8ecf4;--text2:#7a8baa;--dim:#4a5670;--blue:#2d7ff9;--green:#00c853;--amber:#ffab00;--red:#ff1744;--thot:#ff4444;--twarm:#ff8844;--tcool:#44aaff;--track:#1a2240}
|
|
body{font-family:'Segoe UI',system-ui,sans-serif;background:var(--bg);color:var(--text);height:100vh;overflow:hidden;display:flex;flex-direction:column}
|
|
|
|
/* Top toolbar */
|
|
.toolbar{display:flex;align-items:center;justify-content:space-between;height:48px;padding:0 16px;background:#111827;border-bottom:1px solid var(--border);flex-shrink:0}
|
|
.toolbar .title{font-size:18px;font-weight:700}
|
|
.toolbar .sub{font-size:13px;color:var(--text2);margin-left:16px}
|
|
.toolbar-right{display:flex;align-items:center;gap:12px}
|
|
.toolbar select,.toolbar button,.toolbar input{padding:6px 12px;border:1px solid var(--border);border-radius:6px;background:var(--card);color:var(--text);font-size:13px;cursor:pointer}
|
|
.toolbar button:hover{background:var(--border)}
|
|
.toolbar .mode-btn{padding:6px 16px;font-weight:600}
|
|
.toolbar .mode-btn.active{background:var(--blue);border-color:var(--blue)}
|
|
.toolbar .save-btn{background:var(--green);border-color:var(--green);color:#000;font-weight:600}
|
|
.toolbar .help-btn{width:32px;height:32px;border-radius:50%;display:flex;align-items:center;justify-content:center;font-size:16px;font-weight:700}
|
|
|
|
/* Split screen */
|
|
.split{display:flex;flex:1;overflow:hidden}
|
|
.panel{flex:1;display:flex;flex-direction:column;overflow:hidden}
|
|
.panel-left{border-right:2px solid var(--border)}
|
|
.panel-header{height:36px;display:flex;align-items:center;padding:0 12px;background:#0d1220;border-bottom:1px solid var(--border);font-size:13px;font-weight:600;color:var(--text2);gap:8px;flex-shrink:0}
|
|
.panel-header .tag{padding:2px 8px;border-radius:4px;font-size:11px}
|
|
.panel-header .tag-edit{background:rgba(255,171,0,0.15);color:var(--amber)}
|
|
.panel-header .tag-preview{background:rgba(0,200,83,0.15);color:var(--green)}
|
|
|
|
/* HMI Tabs */
|
|
.hmi-tabs{display:flex;height:42px;background:#0d1220;border-bottom:1px solid var(--border);flex-shrink:0;overflow-x:auto}
|
|
.hmi-tab{padding:0 20px;font-size:15px;font-weight:600;color:var(--text2);border:none;background:none;cursor:pointer;position:relative;white-space:nowrap}
|
|
.hmi-tab.active{color:var(--blue)}
|
|
.hmi-tab.active::after{content:'';position:absolute;bottom:0;left:8px;right:8px;height:3px;background:var(--blue);border-radius:2px 2px 0 0}
|
|
.hmi-tab .comment-badge{position:absolute;top:4px;right:4px;width:16px;height:16px;border-radius:50%;background:var(--amber);color:#000;font-size:9px;display:flex;align-items:center;justify-content:center;font-weight:700}
|
|
.hmi-tab-add{color:var(--dim);font-size:20px;padding:0 12px;cursor:pointer;border:none;background:none}
|
|
|
|
/* Card grid */
|
|
.card-grid{flex:1;display:flex;flex-wrap:wrap;align-content:flex-start;padding:8px;gap:8px;overflow-y:auto}
|
|
|
|
/* Cards */
|
|
.hmi-card{border-radius:10px;padding:12px;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:4px;cursor:pointer;transition:all 0.15s;position:relative;border:1px solid var(--border);min-height:80px}
|
|
.hmi-card.half{width:calc(50% - 4px);height:calc(33.3% - 6px)}
|
|
.hmi-card.full{width:100%;height:calc(33.3% - 6px)}
|
|
|
|
/* Card types */
|
|
.hmi-card.type-temp{background:var(--card)}
|
|
.hmi-card.type-motor,.hmi-card.type-output,.hmi-card.type-burner{background:var(--red)}
|
|
.hmi-card.on.type-motor,.hmi-card.on.type-output{background:var(--green)}
|
|
.hmi-card.on.type-burner{background:var(--amber)}
|
|
|
|
.hmi-card .card-label{font-size:18px;color:var(--text2);text-align:center}
|
|
.hmi-card .card-value{font-size:28px;font-weight:700}
|
|
.hmi-card .card-sub{font-size:12px;color:var(--dim)}
|
|
.hmi-card .card-bar{width:80%;height:4px;background:var(--track);border-radius:2px;overflow:hidden}
|
|
.hmi-card .card-bar-fill{height:100%;border-radius:2px;transition:width 0.5s}
|
|
|
|
/* Edit mode overlays */
|
|
.edit-overlay{display:none}
|
|
.edit-mode .edit-overlay{display:flex}
|
|
.hmi-card .edit-overlay{position:absolute;top:4px;right:4px;gap:4px}
|
|
.hmi-card .edit-btn{width:24px;height:24px;border-radius:4px;border:1px solid var(--border);background:var(--bg);color:var(--text2);font-size:12px;cursor:pointer;display:flex;align-items:center;justify-content:center}
|
|
.hmi-card .edit-btn:hover{background:var(--red);color:var(--text)}
|
|
.hmi-card .comment-btn:hover{background:var(--amber);color:#000}
|
|
.hmi-card .drag-handle{position:absolute;top:4px;left:4px;cursor:grab;color:var(--dim);font-size:14px}
|
|
.edit-mode .hmi-card{border:2px dashed var(--dim)}
|
|
.edit-mode .hmi-card:hover{border-color:var(--blue)}
|
|
.sortable-ghost{opacity:0.4;border:2px dashed var(--blue) !important}
|
|
|
|
/* Add card palette */
|
|
.add-palette{display:none;padding:8px;gap:8px;border-top:1px solid var(--border);flex-shrink:0}
|
|
.edit-mode .add-palette{display:flex}
|
|
.add-palette button{flex:1;padding:10px;border:2px dashed var(--dim);border-radius:8px;background:none;color:var(--text2);font-size:13px;cursor:pointer}
|
|
.add-palette button:hover{border-color:var(--blue);color:var(--blue)}
|
|
|
|
/* Comment badge on cards */
|
|
.hmi-card .card-comment-badge{position:absolute;bottom:4px;right:4px;width:20px;height:20px;border-radius:50%;background:var(--amber);color:#000;font-size:10px;font-weight:700;display:none;align-items:center;justify-content:center}
|
|
.hmi-card .card-comment-badge.has-comments{display:flex}
|
|
|
|
/* Comment panel */
|
|
.comment-panel{position:fixed;right:0;top:48px;width:360px;height:calc(100vh - 48px);background:var(--card);border-left:2px solid var(--border);z-index:100;display:none;flex-direction:column}
|
|
.comment-panel.open{display:flex}
|
|
.comment-panel-header{padding:12px;border-bottom:1px solid var(--border);display:flex;justify-content:space-between;align-items:center}
|
|
.comment-panel-header h3{font-size:16px}
|
|
.comment-list{flex:1;overflow-y:auto;padding:12px;display:flex;flex-direction:column;gap:8px}
|
|
.comment-item{background:var(--bg);border-radius:8px;padding:10px}
|
|
.comment-item .comment-user{font-size:12px;font-weight:600;color:var(--blue)}
|
|
.comment-item .comment-time{font-size:10px;color:var(--dim);margin-left:8px}
|
|
.comment-item .comment-text{font-size:14px;margin-top:4px}
|
|
.comment-input{display:flex;gap:8px;padding:12px;border-top:1px solid var(--border)}
|
|
.comment-input input{flex:1;padding:8px;border:1px solid var(--border);border-radius:6px;background:var(--bg);color:var(--text);font-size:14px}
|
|
.comment-input button{padding:8px 16px;border:none;border-radius:6px;background:var(--blue);color:var(--text);font-weight:600;cursor:pointer}
|
|
|
|
/* Machine simulation panel */
|
|
.sim-container{flex:1;position:relative;display:flex;align-items:center;justify-content:center;overflow:hidden;background:#080c14}
|
|
|
|
/* SVG Schematic */
|
|
.schematic{width:90%;max-height:90%}
|
|
.schematic .equip{fill:var(--card);stroke:var(--border);stroke-width:2}
|
|
.schematic .equip-label{fill:var(--text2);font-size:12px;text-anchor:middle;font-family:'Segoe UI',sans-serif}
|
|
.schematic .flow-arrow{fill:none;stroke:var(--dim);stroke-width:2;marker-end:url(#arrowhead)}
|
|
.schematic .anim-overlay{opacity:0;transition:opacity 0.3s}
|
|
.schematic .anim-overlay.active{opacity:1}
|
|
|
|
/* Animations */
|
|
@keyframes spin{100%{transform-origin:center;transform:rotate(360deg)}}
|
|
@keyframes pulse-glow{0%,100%{opacity:0.3}50%{opacity:1}}
|
|
@keyframes scroll-belt{100%{stroke-dashoffset:-20}}
|
|
@keyframes fall-particles{0%{opacity:1;transform:translateY(0)}100%{opacity:0;transform:translateY(30px)}}
|
|
.fan-anim{animation:spin 1s linear infinite}
|
|
.burner-glow{animation:pulse-glow 1.5s ease-in-out infinite}
|
|
.conveyor-anim{animation:scroll-belt 0.5s linear infinite}
|
|
.discharge-anim{animation:fall-particles 1s ease-out infinite}
|
|
.agitator-anim{animation:spin 2s linear infinite}
|
|
.pulse-anim{animation:pulse-glow 1.5s ease-in-out infinite}
|
|
|
|
/* Help panel */
|
|
.help-panel{position:fixed;right:0;top:48px;width:400px;height:calc(100vh - 48px);background:var(--card);border-left:2px solid var(--border);z-index:150;display:none;flex-direction:column;overflow-y:auto;padding:24px}
|
|
.help-panel.open{display:flex}
|
|
.help-panel h2{font-size:20px;margin-bottom:16px;color:var(--text)}
|
|
.help-panel h3{font-size:16px;margin-top:16px;margin-bottom:8px;color:var(--blue)}
|
|
.help-panel p{font-size:14px;color:var(--text2);line-height:1.6;margin-bottom:8px}
|
|
.help-panel .help-close{position:absolute;top:12px;right:12px;width:32px;height:32px;border-radius:50%;border:1px solid var(--border);background:var(--bg);color:var(--text);font-size:16px;cursor:pointer;display:flex;align-items:center;justify-content:center}
|
|
|
|
/* Inline edit */
|
|
/* Sim toolbar + context menu */
|
|
.sim-toolbar button{padding:4px 10px;border:1px solid var(--border);border-radius:4px;background:var(--card);color:var(--text2);font-size:11px;cursor:pointer}
|
|
.sim-toolbar button:hover{background:var(--border);color:var(--text)}
|
|
.eq-ctx-item{padding:8px 16px;font-size:13px;cursor:pointer;color:var(--text)}
|
|
.eq-ctx-item:hover{background:var(--border)}
|
|
|
|
.inline-edit{background:transparent;border:none;border-bottom:2px solid var(--blue);color:var(--text);font-size:inherit;font-family:inherit;text-align:center;outline:none;width:100%}
|
|
|
|
/* Popup */
|
|
#popup-overlay{position:fixed;inset:0;background:rgba(0,0,0,0.5);display:none;align-items:center;justify-content:center;z-index:50}
|
|
#popup-overlay.active{display:flex}
|
|
.popup{background:var(--card);border:3px solid var(--blue);border-radius:16px;padding:24px;width:500px;display:flex;flex-direction:column;align-items:center;gap:16px}
|
|
.popup .title{font-size:24px;color:var(--text2)}
|
|
.popup .live{font-size:32px;font-weight:700}
|
|
.popup .sp-row{display:flex;align-items:center;gap:20px}
|
|
.popup .sp-btn{width:70px;height:70px;border:2px solid var(--border);border-radius:10px;background:var(--bg);color:var(--text);font-size:28px;cursor:pointer;display:flex;align-items:center;justify-content:center}
|
|
.popup .sp-btn:active{background:var(--border)}
|
|
.popup .sp-val{font-size:32px;min-width:80px;text-align:center}
|
|
.popup .close-btn{width:160px;height:44px;border:none;border-radius:8px;background:var(--blue);color:var(--text);font-size:16px;font-weight:600;cursor:pointer}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<!-- TOOLBAR -->
|
|
<div class="toolbar">
|
|
<div style="display:flex;align-items:center">
|
|
<span class="title">BFA Banana Dryer</span>
|
|
<span class="sub">HMI Design Tool</span>
|
|
</div>
|
|
<div class="toolbar-right">
|
|
<select id="user-select"></select>
|
|
<button onclick="addUser()">+ Add Person</button>
|
|
<span style="color:var(--dim)">|</span>
|
|
<button class="mode-btn active" id="btn-edit" onclick="setMode('edit')">EDIT</button>
|
|
<button class="mode-btn" id="btn-preview" onclick="setMode('preview')">PREVIEW</button>
|
|
<span style="color:var(--dim)">|</span>
|
|
<button class="save-btn" onclick="saveLayout()">Save</button>
|
|
<button onclick="exportLayout()">Export JSON</button>
|
|
<button class="help-btn" onclick="toggleHelp()">?</button>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- SPLIT SCREEN -->
|
|
<div class="split">
|
|
<!-- LEFT: HMI -->
|
|
<div class="panel panel-left" id="hmi-panel">
|
|
<div class="panel-header">
|
|
HMI LAYOUT
|
|
<span class="tag tag-edit edit-overlay">EDIT MODE</span>
|
|
<span class="tag tag-preview" id="preview-tag" style="display:none">PREVIEW MODE</span>
|
|
</div>
|
|
<div class="hmi-tabs" id="hmi-tabs"></div>
|
|
<div class="card-grid edit-mode" id="card-grid"></div>
|
|
<div class="add-palette" id="add-palette">
|
|
<button onclick="addCard('temp')">+ Temperature</button>
|
|
<button onclick="addCard('motor')">+ Motor</button>
|
|
<button onclick="addCard('output')">+ Output</button>
|
|
<button onclick="addCard('burner')">+ Burner</button>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- RIGHT: MACHINE SIM -->
|
|
<div class="panel">
|
|
<div class="panel-header">
|
|
MACHINE SIMULATION
|
|
<button id="btn-sim-edit" onclick="toggleSimEdit()" style="margin-left:auto;padding:4px 12px;border:1px solid var(--border);border-radius:4px;background:var(--card);color:var(--text2);font-size:11px;cursor:pointer">EDIT LAYOUT</button>
|
|
</div>
|
|
<div class="sim-toolbar" id="sim-toolbar" style="display:none;padding:6px 8px;gap:6px;background:#0d1220;border-bottom:1px solid var(--border);flex-wrap:wrap;flex-shrink:0">
|
|
<button onclick="addEquip('rect')">+ Rectangle</button>
|
|
<button onclick="addEquip('circle')">+ Circle</button>
|
|
<button onclick="addEquip('label')">+ Label</button>
|
|
<button onclick="addEquip('arrow')">+ Arrow</button>
|
|
<span style="color:var(--dim)">|</span>
|
|
<button onclick="addEquip('temp_probe')">+ Temp Probe</button>
|
|
<button onclick="addEquip('output_indicator')">+ Output</button>
|
|
<span style="color:var(--dim)">|</span>
|
|
<button onclick="deleteSelectedEquip()" style="color:var(--red)">Delete Selected</button>
|
|
</div>
|
|
<!-- Context menu for equipment properties -->
|
|
<div id="eq-context" style="display:none;position:fixed;z-index:300;background:var(--card);border:1px solid var(--border);border-radius:8px;padding:4px 0;min-width:180px;box-shadow:0 8px 24px rgba(0,0,0,0.5)">
|
|
<div class="eq-ctx-item" onclick="ctxAction('rename')">Rename</div>
|
|
<div class="eq-ctx-item" onclick="ctxAction('shape')">Change Shape</div>
|
|
<div class="eq-ctx-item" onclick="ctxAction('anim')">Change Animation</div>
|
|
<div class="eq-ctx-item" onclick="ctxAction('link')">Link to HMI Control</div>
|
|
<div class="eq-ctx-item" onclick="ctxAction('color')">Change Color</div>
|
|
<div class="eq-ctx-item" onclick="ctxAction('delete')" style="color:var(--red)">Delete</div>
|
|
</div>
|
|
<div class="sim-container" id="sim-container">
|
|
<!-- SVG schematic inserted by JS -->
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- COMMENT PANEL -->
|
|
<div class="comment-panel" id="comment-panel">
|
|
<div class="comment-panel-header">
|
|
<h3 id="comment-target-name">Comments</h3>
|
|
<button onclick="closeComments()" style="background:none;border:none;color:var(--text);font-size:18px;cursor:pointer">✕</button>
|
|
</div>
|
|
<div class="comment-list" id="comment-list"></div>
|
|
<div class="comment-input">
|
|
<input id="comment-text" placeholder="Add a comment..." onkeydown="if(event.key==='Enter')submitComment()">
|
|
<button onclick="submitComment()">Post</button>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- HELP PANEL -->
|
|
<div class="help-panel" id="help-panel">
|
|
<button class="help-close" onclick="toggleHelp()">✕</button>
|
|
<h2>How to Use This Tool</h2>
|
|
<h3>Switching Modes</h3>
|
|
<p>Use the <b>EDIT</b> and <b>PREVIEW</b> buttons in the toolbar. In Edit mode you can rearrange and modify controls. In Preview mode the HMI works like the real device.</p>
|
|
<h3>Rearranging Controls</h3>
|
|
<p>In Edit mode, <b>drag any card</b> by its handle (top-left corner) to move it within a page or between pages (drag to the tab name).</p>
|
|
<h3>Adding Controls</h3>
|
|
<p>In Edit mode, use the <b>+ Temperature</b>, <b>+ Motor</b>, <b>+ Output</b>, or <b>+ Burner</b> buttons at the bottom to add a new control to the current page.</p>
|
|
<h3>Renaming</h3>
|
|
<p>In Edit mode, <b>click on any card's name</b> to edit it. Press Enter to confirm.</p>
|
|
<h3>Deleting</h3>
|
|
<p>In Edit mode, click the <b>✕</b> button on the top-right of a card to remove it.</p>
|
|
<h3>Pages / Tabs</h3>
|
|
<p>Click the <b>+</b> button after the last tab to add a new page. Right-click a tab name to rename or delete it.</p>
|
|
<h3>Comments</h3>
|
|
<p>Click the <b>💬</b> button on any card to open the comment panel. Type your comment and click Post. Everyone can see all comments.</p>
|
|
<h3>Saving</h3>
|
|
<p>Click <b>Save</b> to save the current layout to the server. Click <b>Export JSON</b> to download the layout file.</p>
|
|
<h3>Machine Simulation</h3>
|
|
<p>The right panel shows a schematic of the dryer. When you toggle controls in Preview mode, the equipment animations respond (fan spins, conveyor moves, burner glows, etc.).</p>
|
|
<h3>Choosing Your Name</h3>
|
|
<p>Use the dropdown in the toolbar to select your name. Click <b>+ Add Person</b> if you're not in the list.</p>
|
|
</div>
|
|
|
|
<!-- POPUP -->
|
|
<div id="popup-overlay" onclick="if(event.target===this)closePopup()">
|
|
<div class="popup" id="popup"></div>
|
|
</div>
|
|
|
|
<script>
|
|
// ══════════════════════════════════════════════════════
|
|
// STATE
|
|
// ══════════════════════════════════════════════════════
|
|
let layout = null;
|
|
let currentPage = 0;
|
|
let mode = 'edit'; // 'edit' or 'preview'
|
|
let currentUser = localStorage.getItem('bfa-user') || '';
|
|
let simState = {}; // id -> {on:bool, value:number}
|
|
let commentTarget = null;
|
|
let sortableInstance = null;
|
|
|
|
// ══════════════════════════════════════════════════════
|
|
// INIT
|
|
// ══════════════════════════════════════════════════════
|
|
async function init() {
|
|
const resp = await fetch('api/layout');
|
|
layout = await resp.json();
|
|
if (!currentUser && layout.users.length > 0) currentUser = layout.users[0];
|
|
initUsers();
|
|
initSimState();
|
|
renderTabs();
|
|
renderCards();
|
|
renderSchematic();
|
|
}
|
|
|
|
function initUsers() {
|
|
const sel = document.getElementById('user-select');
|
|
sel.innerHTML = '';
|
|
(layout.users || []).forEach(u => {
|
|
const o = document.createElement('option');
|
|
o.value = u; o.textContent = u;
|
|
if (u === currentUser) o.selected = true;
|
|
sel.appendChild(o);
|
|
});
|
|
sel.onchange = () => { currentUser = sel.value; localStorage.setItem('bfa-user', currentUser); };
|
|
}
|
|
|
|
function initSimState() {
|
|
layout.pages.forEach(p => p.cards.forEach(c => {
|
|
if (c.type === 'temp') simState[c.id] = {on: true, value: c.sp_default || 70};
|
|
else if (c.type === 'motor') simState[c.id] = {on: false, value: c.sp_default || 50};
|
|
else if (c.type === 'burner') simState[c.id] = {on: false, value: 0};
|
|
else simState[c.id] = {on: false, value: 0};
|
|
}));
|
|
}
|
|
|
|
async function addUser() {
|
|
const name = prompt('Enter name:');
|
|
if (!name) return;
|
|
await fetch('api/users', {method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify({name})});
|
|
layout.users.push(name);
|
|
currentUser = name;
|
|
localStorage.setItem('bfa-user', name);
|
|
initUsers();
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════
|
|
// MODE
|
|
// ══════════════════════════════════════════════════════
|
|
function setMode(m) {
|
|
mode = m;
|
|
document.getElementById('btn-edit').classList.toggle('active', m === 'edit');
|
|
document.getElementById('btn-preview').classList.toggle('active', m === 'preview');
|
|
document.getElementById('card-grid').classList.toggle('edit-mode', m === 'edit');
|
|
document.getElementById('preview-tag').style.display = m === 'preview' ? 'inline' : 'none';
|
|
document.querySelectorAll('.edit-overlay').forEach(e => e.style.display = m === 'edit' ? 'flex' : 'none');
|
|
document.getElementById('add-palette').style.display = m === 'edit' ? 'flex' : 'none';
|
|
if (m === 'edit') initSortable();
|
|
else if (sortableInstance) { sortableInstance.destroy(); sortableInstance = null; }
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════
|
|
// TABS
|
|
// ══════════════════════════════════════════════════════
|
|
function renderTabs() {
|
|
const container = document.getElementById('hmi-tabs');
|
|
container.innerHTML = '';
|
|
layout.pages.forEach((p, i) => {
|
|
const btn = document.createElement('button');
|
|
btn.className = 'hmi-tab' + (i === currentPage ? ' active' : '');
|
|
btn.textContent = p.name;
|
|
btn.onclick = () => { currentPage = i; renderTabs(); renderCards(); };
|
|
btn.oncontextmenu = (e) => { e.preventDefault(); tabContextMenu(i); };
|
|
// Comment badge for page
|
|
const pageComments = (layout.comments || []).filter(c => c.target === 'page:' + p.id);
|
|
if (pageComments.length > 0) {
|
|
const badge = document.createElement('span');
|
|
badge.className = 'comment-badge';
|
|
badge.textContent = pageComments.length;
|
|
btn.appendChild(badge);
|
|
}
|
|
container.appendChild(btn);
|
|
});
|
|
const addBtn = document.createElement('button');
|
|
addBtn.className = 'hmi-tab-add';
|
|
addBtn.textContent = '+';
|
|
addBtn.onclick = addPage;
|
|
container.appendChild(addBtn);
|
|
}
|
|
|
|
function addPage() {
|
|
const name = prompt('New page name:', 'NEW PAGE');
|
|
if (!name) return;
|
|
layout.pages.push({id: 'p' + Date.now(), name: name.toUpperCase(), cards: []});
|
|
currentPage = layout.pages.length - 1;
|
|
renderTabs(); renderCards();
|
|
}
|
|
|
|
function tabContextMenu(i) {
|
|
const action = prompt(`Page "${layout.pages[i].name}"\n\nType "rename" to rename, "delete" to delete, or "comment" to add a comment:`);
|
|
if (!action) return;
|
|
if (action.toLowerCase() === 'rename') {
|
|
const name = prompt('New name:', layout.pages[i].name);
|
|
if (name) { layout.pages[i].name = name.toUpperCase(); renderTabs(); }
|
|
} else if (action.toLowerCase() === 'delete') {
|
|
if (layout.pages.length <= 1) { alert('Must keep at least one page.'); return; }
|
|
if (confirm(`Delete page "${layout.pages[i].name}"?`)) {
|
|
layout.pages.splice(i, 1);
|
|
if (currentPage >= layout.pages.length) currentPage = layout.pages.length - 1;
|
|
renderTabs(); renderCards();
|
|
}
|
|
} else if (action.toLowerCase() === 'comment') {
|
|
openComments('page:' + layout.pages[i].id, layout.pages[i].name);
|
|
}
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════
|
|
// CARDS
|
|
// ══════════════════════════════════════════════════════
|
|
function renderCards() {
|
|
const grid = document.getElementById('card-grid');
|
|
grid.innerHTML = '';
|
|
const page = layout.pages[currentPage];
|
|
if (!page) return;
|
|
page.cards.forEach(c => {
|
|
const el = createCardEl(c);
|
|
grid.appendChild(el);
|
|
});
|
|
if (mode === 'edit') initSortable();
|
|
}
|
|
|
|
function createCardEl(c) {
|
|
const el = document.createElement('div');
|
|
el.className = `hmi-card ${c.width || 'half'} type-${c.type}`;
|
|
el.dataset.id = c.id;
|
|
const s = simState[c.id] || {on: false, value: 0};
|
|
if (c.type !== 'temp' && s.on) el.classList.add('on');
|
|
|
|
// Drag handle (edit mode)
|
|
el.innerHTML = `<span class="drag-handle edit-overlay">☰</span>`;
|
|
|
|
// Edit buttons
|
|
el.innerHTML += `<div class="edit-overlay" style="position:absolute;top:4px;right:4px;gap:4px">
|
|
<button class="edit-btn comment-btn" onclick="event.stopPropagation();openComments('${c.id}','${c.label}')">💬</button>
|
|
<button class="edit-btn" onclick="event.stopPropagation();deleteCard('${c.id}')">✕</button>
|
|
</div>`;
|
|
|
|
// Comment badge
|
|
const commentCount = (layout.comments || []).filter(x => x.target === c.id).length;
|
|
el.innerHTML += `<div class="card-comment-badge ${commentCount > 0 ? 'has-comments' : ''}">${commentCount}</div>`;
|
|
|
|
if (c.type === 'temp') {
|
|
const color = c.color || '#ff8844';
|
|
el.innerHTML += `<div class="card-label" onclick="if(mode==='edit'){event.stopPropagation();renameCard('${c.id}')}">${c.label}</div>`;
|
|
el.innerHTML += `<div class="card-value" style="color:${color}">${Math.round(s.value)} °C</div>`;
|
|
el.innerHTML += `<div class="card-bar"><div class="card-bar-fill" style="width:${Math.min(100,s.value/2)}%;background:${color}"></div></div>`;
|
|
el.innerHTML += `<div class="card-sub">SP: ${c.sp_default || 70} °C</div>`;
|
|
el.onclick = () => { if (mode === 'preview') openTempPopup(c); };
|
|
} else if (c.type === 'motor') {
|
|
el.innerHTML += `<div style="display:flex;gap:10px;align-items:center">
|
|
<span class="card-label" onclick="if(mode==='edit'){event.stopPropagation();renameCard('${c.id}')}">${c.label}</span>
|
|
<span style="font-size:18px;color:var(--text2)">${c.sp_default || 50}%</span>
|
|
</div>`;
|
|
el.innerHTML += `<div class="card-value">${s.on ? 'ON' : 'OFF'}</div>`;
|
|
el.onclick = () => { if (mode === 'preview') toggleSim(c.id); };
|
|
} else if (c.type === 'burner') {
|
|
el.innerHTML += `<div class="card-label" onclick="if(mode==='edit'){event.stopPropagation();renameCard('${c.id}')}">${c.label}</div>`;
|
|
el.innerHTML += `<div class="card-value">${s.on ? 'ON' : 'OFF'}</div>`;
|
|
el.onclick = () => { if (mode === 'preview') toggleSim(c.id); };
|
|
} else { // output
|
|
el.innerHTML += `<div class="card-label" onclick="if(mode==='edit'){event.stopPropagation();renameCard('${c.id}')}" style="text-align:center">${c.label}</div>`;
|
|
el.innerHTML += `<div class="card-value">${s.on ? 'ON' : 'OFF'}</div>`;
|
|
el.onclick = () => { if (mode === 'preview') toggleSim(c.id); };
|
|
}
|
|
return el;
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════
|
|
// DRAG & DROP
|
|
// ══════════════════════════════════════════════════════
|
|
function initSortable() {
|
|
if (sortableInstance) sortableInstance.destroy();
|
|
const grid = document.getElementById('card-grid');
|
|
sortableInstance = new Sortable(grid, {
|
|
animation: 150,
|
|
handle: '.drag-handle',
|
|
ghostClass: 'sortable-ghost',
|
|
onEnd: (evt) => {
|
|
const page = layout.pages[currentPage];
|
|
const [moved] = page.cards.splice(evt.oldIndex, 1);
|
|
page.cards.splice(evt.newIndex, 0, moved);
|
|
}
|
|
});
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════
|
|
// CARD ACTIONS
|
|
// ══════════════════════════════════════════════════════
|
|
function addCard(type) {
|
|
const id = type + '_' + Date.now();
|
|
const defaults = {
|
|
temp: {label: 'New Temp', color: '#ff8844', sp_default: 70},
|
|
motor: {label: 'New Motor', sp_default: 50},
|
|
output: {label: 'New Output'},
|
|
burner: {label: 'Burner'}
|
|
};
|
|
const card = {id, type, width: 'half', ...defaults[type]};
|
|
layout.pages[currentPage].cards.push(card);
|
|
simState[id] = {on: false, value: card.sp_default || 0};
|
|
renderCards();
|
|
}
|
|
|
|
function deleteCard(id) {
|
|
if (!confirm('Delete this control?')) return;
|
|
const page = layout.pages[currentPage];
|
|
page.cards = page.cards.filter(c => c.id !== id);
|
|
layout.comments = (layout.comments || []).filter(c => c.target !== id);
|
|
renderCards();
|
|
}
|
|
|
|
function renameCard(id) {
|
|
const page = layout.pages[currentPage];
|
|
const card = page.cards.find(c => c.id === id);
|
|
if (!card) return;
|
|
const name = prompt('New name:', card.label);
|
|
if (name) { card.label = name; renderCards(); }
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════
|
|
// SIMULATION (PREVIEW MODE)
|
|
// ══════════════════════════════════════════════════════
|
|
function toggleSim(id) {
|
|
if (!simState[id]) simState[id] = {on: false, value: 0};
|
|
simState[id].on = !simState[id].on;
|
|
renderCards();
|
|
updateSchematicAnimations();
|
|
}
|
|
|
|
function openTempPopup(c) {
|
|
const p = document.getElementById('popup');
|
|
const s = simState[c.id] || {value: 70};
|
|
p.style.borderColor = c.color || 'var(--blue)';
|
|
p.innerHTML = `
|
|
<div class="title">${c.label}</div>
|
|
<div class="live" style="color:${c.color || 'var(--twarm)'}">${Math.round(s.value)} °C</div>
|
|
<div style="font-size:14px;color:var(--dim)">SETPOINT</div>
|
|
<div class="sp-row">
|
|
<button class="sp-btn" onclick="adjTempSP('${c.id}',-1)">-</button>
|
|
<div class="sp-val" id="sp-pv">${c.sp_default || 70}</div>
|
|
<button class="sp-btn" onclick="adjTempSP('${c.id}',1)">+</button>
|
|
</div>
|
|
<button class="close-btn" onclick="closePopup()">CLOSE</button>`;
|
|
document.getElementById('popup-overlay').classList.add('active');
|
|
}
|
|
|
|
function adjTempSP(id, d) {
|
|
const card = findCard(id);
|
|
if (!card) return;
|
|
card.sp_default = Math.max(0, Math.min(200, (card.sp_default || 70) + d));
|
|
document.getElementById('sp-pv').textContent = card.sp_default;
|
|
if (simState[id]) simState[id].value = card.sp_default;
|
|
renderCards();
|
|
}
|
|
|
|
function closePopup() { document.getElementById('popup-overlay').classList.remove('active'); }
|
|
|
|
function findCard(id) {
|
|
for (const p of layout.pages) { const c = p.cards.find(x => x.id === id); if (c) return c; }
|
|
return null;
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════
|
|
// COMMENTS
|
|
// ══════════════════════════════════════════════════════
|
|
function openComments(target, name) {
|
|
commentTarget = target;
|
|
document.getElementById('comment-target-name').textContent = 'Comments: ' + name;
|
|
const list = document.getElementById('comment-list');
|
|
const comments = (layout.comments || []).filter(c => c.target === target);
|
|
list.innerHTML = comments.map(c => `
|
|
<div class="comment-item">
|
|
<span class="comment-user">${c.user}</span>
|
|
<span class="comment-time">${c.time}</span>
|
|
<div class="comment-text">${c.text}</div>
|
|
</div>
|
|
`).join('') || '<p style="color:var(--dim);font-size:13px">No comments yet.</p>';
|
|
document.getElementById('comment-panel').classList.add('open');
|
|
document.getElementById('comment-text').focus();
|
|
}
|
|
|
|
function closeComments() { document.getElementById('comment-panel').classList.remove('open'); commentTarget = null; }
|
|
|
|
async function submitComment() {
|
|
const text = document.getElementById('comment-text').value.trim();
|
|
if (!text || !commentTarget) return;
|
|
const resp = await fetch('api/comment', {
|
|
method: 'POST', headers: {'Content-Type': 'application/json'},
|
|
body: JSON.stringify({target: commentTarget, user: currentUser, text})
|
|
});
|
|
const comment = await resp.json();
|
|
layout.comments.push(comment);
|
|
document.getElementById('comment-text').value = '';
|
|
openComments(commentTarget, document.getElementById('comment-target-name').textContent.replace('Comments: ', ''));
|
|
renderCards(); renderTabs();
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════
|
|
// SAVE / EXPORT
|
|
// ══════════════════════════════════════════════════════
|
|
async function saveLayout() {
|
|
await fetch('api/layout', {method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify(layout)});
|
|
alert('Layout saved!');
|
|
}
|
|
|
|
function exportLayout() {
|
|
const blob = new Blob([JSON.stringify(layout, null, 2)], {type: 'application/json'});
|
|
const a = document.createElement('a'); a.href = URL.createObjectURL(blob);
|
|
a.download = 'bfa-dryer-layout.json'; a.click();
|
|
}
|
|
|
|
function toggleHelp() { document.getElementById('help-panel').classList.toggle('open'); }
|
|
|
|
// ══════════════════════════════════════════════════════
|
|
// MACHINE SCHEMATIC — data-driven, editable
|
|
// ══════════════════════════════════════════════════════
|
|
const defaultEquipment = [
|
|
{id:'eq_loading', label:'Loading', shape:'rect', x:40, y:160, w:90, h:80, anim:null, link:null},
|
|
{id:'eq_drum', label:'Dryer Drum', shape:'rect', x:200, y:130, w:140, h:140, anim:null, link:null},
|
|
{id:'eq_burner', label:'Burner', shape:'circle', x:200, y:200, r:20, anim:'burner', link:'burner_0'},
|
|
{id:'eq_fan', label:'Fan', shape:'circle', x:340, y:150, r:22, anim:'fan', link:'motor_0'},
|
|
{id:'eq_conveyor', label:'Conveyor', shape:'rect', x:410, y:180, w:130, h:40, anim:'conveyor', link:'motor_1'},
|
|
{id:'eq_agit1', label:'A1', shape:'circle', x:440, y:170, r:12, anim:'agitator', link:'motor_3'},
|
|
{id:'eq_agit2', label:'A2', shape:'circle', x:510, y:170, r:12, anim:'agitator', link:'motor_4'},
|
|
{id:'eq_spinner', label:'Spin', shape:'circle', x:475, y:155, r:14, anim:'fan', link:'motor_2'},
|
|
{id:'eq_discharge',label:'Discharge', shape:'rect', x:580, y:160, w:80, h:60, anim:null, link:null},
|
|
{id:'eq_mill', label:'Mill', shape:'rect', x:580, y:250, w:70, h:50, anim:null, link:'output_4'},
|
|
{id:'eq_shaker', label:'Shaker Sep.',shape:'rect', x:670, y:250, w:90, h:50, anim:null, link:'output_5'},
|
|
{id:'eq_brush', label:'Brush', shape:'rect', x:670, y:160, w:60, h:40, anim:null, link:'output_1'},
|
|
{id:'eq_in', label:'IN \u25B6', shape:'label', x:60, y:148, anim:null, link:null, color:'#4a5670'},
|
|
{id:'eq_out', label:'\u25B6 OUT', shape:'label', x:740, y:320, anim:null, link:null, color:'#4a5670'},
|
|
{id:'eq_t_heat', label:'T1', shape:'circle', x:230, y:180, r:10, anim:'pulse', link:'temp_0', color:'#ff4444'},
|
|
{id:'eq_t_prod1', label:'T2', shape:'circle', x:310, y:200, r:10, anim:'pulse', link:'temp_1', color:'#ff8844'},
|
|
{id:'eq_t_prod2', label:'T3', shape:'circle', x:460, y:195, r:10, anim:'pulse', link:'temp_2', color:'#ff8844'},
|
|
{id:'eq_t_exh', label:'T4', shape:'circle', x:350, y:130, r:10, anim:'pulse', link:'temp_3', color:'#44aaff'},
|
|
{id:'eq_arrow1', label:'', shape:'arrow', x:130, y:200, w:60, anim:null, link:null},
|
|
{id:'eq_arrow2', label:'', shape:'arrow', x:345, y:200, w:55, anim:null, link:null},
|
|
{id:'eq_arrow3', label:'', shape:'arrow', x:545, y:190, w:30, anim:null, link:null},
|
|
];
|
|
|
|
let simEditMode = false;
|
|
let dragEquip = null, dragOffset = {x:0, y:0};
|
|
let selectedEquip = null;
|
|
let resizeHandle = null; // 'w','h','r','rot'
|
|
|
|
function getEquipment() {
|
|
return layout.equipment || JSON.parse(JSON.stringify(defaultEquipment));
|
|
}
|
|
|
|
function toggleSimEdit() {
|
|
simEditMode = !simEditMode;
|
|
selectedEquip = null;
|
|
const btn = document.getElementById('btn-sim-edit');
|
|
btn.textContent = simEditMode ? 'DONE EDITING' : 'EDIT LAYOUT';
|
|
btn.style.background = simEditMode ? 'var(--amber)' : 'var(--card)';
|
|
btn.style.color = simEditMode ? '#000' : 'var(--text2)';
|
|
document.getElementById('sim-toolbar').style.display = simEditMode ? 'flex' : 'none';
|
|
hideCtx();
|
|
renderSchematic();
|
|
}
|
|
|
|
// Context menu
|
|
let ctxEquip = null;
|
|
function showCtx(e, eq) {
|
|
e.preventDefault();
|
|
ctxEquip = eq;
|
|
const m = document.getElementById('eq-context');
|
|
m.style.display = 'block';
|
|
m.style.left = e.clientX + 'px';
|
|
m.style.top = e.clientY + 'px';
|
|
}
|
|
function hideCtx() {
|
|
document.getElementById('eq-context').style.display = 'none';
|
|
ctxEquip = null;
|
|
}
|
|
document.addEventListener('click', e => { if (!e.target.closest('#eq-context')) hideCtx(); });
|
|
|
|
function ctxAction(action) {
|
|
if (!ctxEquip) return;
|
|
const eq = ctxEquip;
|
|
hideCtx();
|
|
|
|
if (action === 'rename') {
|
|
renameEquip(eq);
|
|
} else if (action === 'shape') {
|
|
const shapes = ['rect', 'circle', 'label', 'arrow'];
|
|
const cur = shapes.indexOf(eq.shape);
|
|
const choice = prompt('Shape — type one of: rect, circle, label, arrow\nCurrent: ' + eq.shape, eq.shape);
|
|
if (choice && shapes.includes(choice)) {
|
|
eq.shape = choice;
|
|
if (choice === 'rect' && !eq.w) { eq.w = 80; eq.h = 60; }
|
|
if (choice === 'circle' && !eq.r) { eq.r = 20; }
|
|
saveEquip(eq); renderSchematic();
|
|
}
|
|
} else if (action === 'anim') {
|
|
const anims = ['none', 'fan', 'agitator', 'burner', 'conveyor', 'pulse'];
|
|
const choice = prompt('Animation — type one of: none, fan, agitator, burner, conveyor, pulse\nCurrent: ' + (eq.anim || 'none'), eq.anim || 'none');
|
|
if (choice && anims.includes(choice)) {
|
|
eq.anim = choice === 'none' ? null : choice;
|
|
saveEquip(eq); renderSchematic();
|
|
}
|
|
} else if (action === 'link') {
|
|
// Show all available HMI card IDs
|
|
const ids = [];
|
|
layout.pages.forEach(p => p.cards.forEach(c => ids.push(c.id + ' (' + c.label + ')')));
|
|
const choice = prompt('Link to HMI control ID:\n\nAvailable:\n' + ids.join('\n') + '\n\nCurrent: ' + (eq.link || 'none') + '\nType ID or "none":', eq.link || '');
|
|
if (choice !== null) {
|
|
eq.link = choice === 'none' || choice === '' ? null : choice.split(' ')[0];
|
|
saveEquip(eq); renderSchematic();
|
|
}
|
|
} else if (action === 'color') {
|
|
const choice = prompt('Color hex (e.g. #ff4444, #44aaff, #00c853):\nCurrent: ' + (eq.color || 'default'), eq.color || '#7a8baa');
|
|
if (choice) { eq.color = choice; saveEquip(eq); renderSchematic(); }
|
|
} else if (action === 'delete') {
|
|
deleteEquipById(eq.id);
|
|
}
|
|
}
|
|
|
|
function addEquip(type) {
|
|
if (!layout.equipment) layout.equipment = getEquipment();
|
|
const id = 'eq_' + Date.now();
|
|
const base = {id, label:'New', rot:0, anim:null, link:null, color:null};
|
|
|
|
if (type === 'rect') {
|
|
layout.equipment.push({...base, shape:'rect', x:350, y:150, w:80, h:60});
|
|
} else if (type === 'circle') {
|
|
layout.equipment.push({...base, shape:'circle', x:400, y:200, r:25});
|
|
} else if (type === 'label') {
|
|
layout.equipment.push({...base, shape:'label', label:'Label', x:400, y:100});
|
|
} else if (type === 'arrow') {
|
|
layout.equipment.push({...base, shape:'arrow', x:300, y:200, w:80, label:''});
|
|
} else if (type === 'temp_probe') {
|
|
layout.equipment.push({...base, shape:'circle', label:'T', r:14, x:380, y:180, color:'#ff4444', anim:'pulse', link:null});
|
|
} else if (type === 'output_indicator') {
|
|
layout.equipment.push({...base, shape:'rect', label:'Output', x:360, y:160, w:60, h:35, color:'#00c853', link:null});
|
|
}
|
|
renderSchematic();
|
|
}
|
|
|
|
function deleteSelectedEquip() {
|
|
if (!selectedEquip) { alert('Click an element first'); return; }
|
|
deleteEquipById(selectedEquip.id);
|
|
}
|
|
|
|
function deleteEquipById(id) {
|
|
if (!layout.equipment) layout.equipment = getEquipment();
|
|
layout.equipment = layout.equipment.filter(e => e.id !== id);
|
|
if (selectedEquip && selectedEquip.id === id) selectedEquip = null;
|
|
renderSchematic();
|
|
}
|
|
|
|
function svgPt(e) {
|
|
const svg = document.getElementById('sim-svg');
|
|
const pt = svg.createSVGPoint();
|
|
pt.x = e.clientX; pt.y = e.clientY;
|
|
return pt.matrixTransform(svg.getScreenCTM().inverse());
|
|
}
|
|
|
|
function saveEquip(eq) {
|
|
if (!layout.equipment) layout.equipment = getEquipment();
|
|
const e = layout.equipment.find(x => x.id === eq.id);
|
|
if (e) Object.assign(e, {x:eq.x, y:eq.y, w:eq.w, h:eq.h, r:eq.r, rot:eq.rot, label:eq.label});
|
|
}
|
|
|
|
function mkSvgEl(tag, attrs) {
|
|
const el = document.createElementNS('http://www.w3.org/2000/svg', tag);
|
|
for (const [k,v] of Object.entries(attrs)) el.setAttribute(k, v);
|
|
return el;
|
|
}
|
|
|
|
function handle(svg, cx, cy, type, eq, cursor) {
|
|
const h = mkSvgEl('circle', {cx, cy, r:'6', fill:'var(--blue)', stroke:'#fff', 'stroke-width':'1.5', cursor, 'data-handle':type});
|
|
h.addEventListener('mousedown', e => { e.stopPropagation(); resizeHandle = type; dragEquip = eq; dragOffset = svgPt(e); });
|
|
svg.appendChild(h);
|
|
}
|
|
|
|
function renderSchematic() {
|
|
const equip = getEquipment();
|
|
const container = document.getElementById('sim-container');
|
|
container.innerHTML = '';
|
|
|
|
const svg = mkSvgEl('svg', {'class':'schematic', viewBox:'0 0 800 400', id:'sim-svg'});
|
|
svg.innerHTML = `<defs><marker id="arrowhead" markerWidth="8" markerHeight="6" refX="8" refY="3" orient="auto"><polygon points="0 0, 8 3, 0 6" fill="var(--dim)"/></marker></defs>`;
|
|
container.appendChild(svg);
|
|
|
|
equip.forEach(eq => {
|
|
if (eq.rot === undefined) eq.rot = 0;
|
|
const g = mkSvgEl('g', {'data-eq-id': eq.id});
|
|
const isSel = selectedEquip && selectedEquip.id === eq.id;
|
|
const col = eq.color || null;
|
|
const editStroke = simEditMode ? {stroke: isSel?'var(--blue)':'var(--amber)', 'stroke-width': isSel?'3':'2', 'stroke-dasharray': isSel?'':'4 4'} : {};
|
|
|
|
// Rotation center
|
|
let cx, cy;
|
|
if (eq.shape === 'rect' || eq.shape === 'arrow') { cx = eq.x+(eq.w||80)/2; cy = eq.y+(eq.h||60)/2; }
|
|
else { cx = eq.x; cy = eq.y; }
|
|
if (eq.rot) g.setAttribute('transform', `rotate(${eq.rot} ${cx} ${cy})`);
|
|
g.style.cursor = simEditMode ? 'grab' : 'default';
|
|
|
|
// ── RECT ──
|
|
if (eq.shape === 'rect') {
|
|
const w=eq.w||80, h=eq.h||60;
|
|
const rectAttrs = {'class':'equip', x:eq.x, y:eq.y, width:w, height:h, rx:'6', ...editStroke};
|
|
if (col) { rectAttrs.fill = col; rectAttrs['fill-opacity'] = '0.2'; rectAttrs.stroke = col; rectAttrs['stroke-width'] = '2'; rectAttrs['stroke-dasharray'] = ''; }
|
|
g.appendChild(mkSvgEl('rect', rectAttrs));
|
|
const t = mkSvgEl('text', {'class':'equip-label', x:eq.x+w/2, y:eq.y+h/2+5, ...(col?{fill:col}:{})});
|
|
t.textContent = eq.label; g.appendChild(t);
|
|
|
|
if (eq.anim === 'conveyor') {
|
|
g.appendChild(mkSvgEl('line', {'class':'anim-overlay conveyor-anim', id:'sim-'+eq.id,
|
|
x1:eq.x+10, y1:eq.y+h-5, x2:eq.x+w-10, y2:eq.y+h-5,
|
|
stroke: col||'var(--green)', 'stroke-width':'3', 'stroke-dasharray':'10 10'}));
|
|
} else if (eq.anim === 'pulse') {
|
|
g.appendChild(mkSvgEl('rect', {'class':'anim-overlay pulse-anim', id:'sim-'+eq.id,
|
|
x:eq.x, y:eq.y, width:w, height:h, rx:'6',
|
|
fill:'none', stroke: col||'var(--green)', 'stroke-width':'3'}));
|
|
}
|
|
|
|
// ── CIRCLE ──
|
|
} else if (eq.shape === 'circle') {
|
|
const r = eq.r||20;
|
|
const circAttrs = {'class':'equip', cx:eq.x, cy:eq.y, r, ...editStroke};
|
|
if (col) { circAttrs.fill = col; circAttrs['fill-opacity'] = '0.25'; circAttrs.stroke = col; circAttrs['stroke-width'] = '2'; circAttrs['stroke-dasharray'] = ''; }
|
|
g.appendChild(mkSvgEl('circle', circAttrs));
|
|
const t = mkSvgEl('text', {'class':'equip-label', x:eq.x, y:eq.y+4, 'font-size':r<15?'8':'10', ...(col?{fill:col}:{})});
|
|
t.textContent = eq.label; g.appendChild(t);
|
|
|
|
if (eq.anim === 'burner') {
|
|
g.appendChild(mkSvgEl('circle', {'class':'anim-overlay burner-glow', id:'sim-'+eq.id,
|
|
cx:eq.x, cy:eq.y, r:r+4, fill:'none', stroke:col||'var(--amber)', 'stroke-width':'4'}));
|
|
} else if (eq.anim === 'fan' || eq.anim === 'agitator') {
|
|
const ag = mkSvgEl('g', {'class':`anim-overlay ${eq.anim==='fan'?'fan-anim':'agitator-anim'}`, id:'sim-'+eq.id, 'transform-origin':`${eq.x} ${eq.y}`});
|
|
ag.innerHTML = `<line x1="${eq.x}" y1="${eq.y-r}" x2="${eq.x}" y2="${eq.y+r}" stroke="${col||'var(--blue)'}" stroke-width="2"/><line x1="${eq.x-r}" y1="${eq.y}" x2="${eq.x+r}" y2="${eq.y}" stroke="${col||'var(--blue)'}" stroke-width="2"/>`;
|
|
g.appendChild(ag);
|
|
} else if (eq.anim === 'pulse') {
|
|
g.appendChild(mkSvgEl('circle', {'class':'anim-overlay pulse-anim', id:'sim-'+eq.id,
|
|
cx:eq.x, cy:eq.y, r:r+3, fill:'none', stroke:col||'var(--green)', 'stroke-width':'2'}));
|
|
}
|
|
|
|
// ── LABEL ──
|
|
} else if (eq.shape === 'label') {
|
|
const t = mkSvgEl('text', {x:eq.x, y:eq.y, fill:col||'var(--dim)', 'font-size':'12', 'text-anchor':'middle', 'font-family':'sans-serif', 'font-weight':'600'});
|
|
t.textContent = eq.label; g.appendChild(t);
|
|
if (simEditMode) {
|
|
// Invisible hit area for dragging
|
|
g.appendChild(mkSvgEl('rect', {x:eq.x-30, y:eq.y-12, width:60, height:18, fill:'transparent', ...editStroke}));
|
|
}
|
|
|
|
// ── ARROW ──
|
|
} else if (eq.shape === 'arrow') {
|
|
const w = eq.w || 60;
|
|
g.appendChild(mkSvgEl('line', {'class':'flow-arrow', x1:eq.x, y1:eq.y, x2:eq.x+w, y2:eq.y,
|
|
fill:'none', stroke:col||'var(--dim)', 'stroke-width':'2', 'marker-end':'url(#arrowhead)'}));
|
|
if (eq.label) {
|
|
const t = mkSvgEl('text', {x:eq.x+w/2, y:eq.y-6, fill:col||'var(--dim)', 'font-size':'9', 'text-anchor':'middle', 'font-family':'sans-serif'});
|
|
t.textContent = eq.label; g.appendChild(t);
|
|
}
|
|
if (simEditMode) {
|
|
g.appendChild(mkSvgEl('rect', {x:eq.x-2, y:eq.y-6, width:w+4, height:12, fill:'transparent', ...editStroke}));
|
|
}
|
|
}
|
|
|
|
svg.appendChild(g);
|
|
|
|
// Edit mode events
|
|
if (simEditMode) {
|
|
g.addEventListener('mousedown', e => {
|
|
if (resizeHandle) return;
|
|
hideCtx();
|
|
selectedEquip = eq;
|
|
dragEquip = eq;
|
|
const p = svgPt(e);
|
|
dragOffset = {x: p.x - eq.x, y: p.y - eq.y};
|
|
e.preventDefault();
|
|
renderSchematic();
|
|
});
|
|
g.addEventListener('dblclick', () => renameEquip(eq));
|
|
g.addEventListener('contextmenu', e => showCtx(e, eq));
|
|
}
|
|
});
|
|
|
|
// Draw selection handles for selected equipment
|
|
if (simEditMode && selectedEquip) {
|
|
const eq = selectedEquip;
|
|
const addRotHandle = (hx, hy, rcx, rcy) => {
|
|
svg.appendChild(mkSvgEl('line', {x1:hx, y1:hy+6, x2:hx, y2:hy-14, stroke:'var(--amber)', 'stroke-width':'1', 'stroke-dasharray':'3 2'}));
|
|
const rh = mkSvgEl('circle', {cx:hx, cy:hy-20, r:'6', fill:'var(--amber)', stroke:'#fff', 'stroke-width':'1.5', cursor:'crosshair'});
|
|
rh.addEventListener('mousedown', e => { e.stopPropagation(); resizeHandle='rot'; dragEquip=eq; dragOffset={cx:rcx,cy:rcy}; });
|
|
svg.appendChild(rh);
|
|
};
|
|
|
|
if (eq.shape === 'rect') {
|
|
const w=eq.w||80, h=eq.h||60;
|
|
handle(svg, eq.x+w, eq.y+h/2, 'w', eq, 'ew-resize');
|
|
handle(svg, eq.x+w/2, eq.y+h, 'h', eq, 'ns-resize');
|
|
handle(svg, eq.x+w, eq.y+h, 'wh', eq, 'nwse-resize');
|
|
addRotHandle(eq.x+w/2, eq.y, eq.x+w/2, eq.y+h/2);
|
|
} else if (eq.shape === 'circle') {
|
|
const r=eq.r||20;
|
|
handle(svg, eq.x+r, eq.y, 'r', eq, 'ew-resize');
|
|
addRotHandle(eq.x, eq.y-r, eq.x, eq.y);
|
|
} else if (eq.shape === 'arrow') {
|
|
// Length handle at end of arrow
|
|
handle(svg, eq.x+(eq.w||60), eq.y, 'w', eq, 'ew-resize');
|
|
addRotHandle(eq.x+(eq.w||60)/2, eq.y, eq.x+(eq.w||60)/2, eq.y);
|
|
}
|
|
// Labels: no resize handles, just move + rotate not needed
|
|
}
|
|
|
|
if (simEditMode) {
|
|
svg.addEventListener('mousemove', onSvgMouseMove);
|
|
svg.addEventListener('mouseup', onSvgMouseUp);
|
|
svg.addEventListener('mouseleave', onSvgMouseUp);
|
|
// Click on empty space deselects
|
|
svg.addEventListener('click', e => { if (e.target === svg) { selectedEquip = null; renderSchematic(); } });
|
|
}
|
|
updateSchematicAnimations();
|
|
}
|
|
|
|
function onSvgMouseMove(e) {
|
|
if (!dragEquip) return;
|
|
const p = svgPt(e);
|
|
|
|
if (resizeHandle === 'rot') {
|
|
// Rotation: angle from center to mouse
|
|
const cx = dragOffset.cx, cy = dragOffset.cy;
|
|
const angle = Math.round(Math.atan2(p.x - cx, -(p.y - cy)) * 180 / Math.PI);
|
|
dragEquip.rot = angle;
|
|
saveEquip(dragEquip);
|
|
renderSchematic();
|
|
} else if (resizeHandle === 'w') {
|
|
dragEquip.w = Math.max(30, Math.round(p.x - dragEquip.x));
|
|
saveEquip(dragEquip); renderSchematic();
|
|
} else if (resizeHandle === 'h') {
|
|
dragEquip.h = Math.max(20, Math.round(p.y - dragEquip.y));
|
|
saveEquip(dragEquip); renderSchematic();
|
|
} else if (resizeHandle === 'wh') {
|
|
dragEquip.w = Math.max(30, Math.round(p.x - dragEquip.x));
|
|
dragEquip.h = Math.max(20, Math.round(p.y - dragEquip.y));
|
|
saveEquip(dragEquip); renderSchematic();
|
|
} else if (resizeHandle === 'r') {
|
|
dragEquip.r = Math.max(8, Math.round(Math.sqrt((p.x-dragEquip.x)**2 + (p.y-dragEquip.y)**2)));
|
|
saveEquip(dragEquip); renderSchematic();
|
|
} else if (dragEquip && !resizeHandle) {
|
|
// Move
|
|
dragEquip.x = Math.round(p.x - dragOffset.x);
|
|
dragEquip.y = Math.round(p.y - dragOffset.y);
|
|
saveEquip(dragEquip);
|
|
renderSchematic();
|
|
}
|
|
}
|
|
|
|
function onSvgMouseUp() { dragEquip = null; resizeHandle = null; }
|
|
|
|
function renameEquip(eq) {
|
|
const name = prompt('Rename equipment:', eq.label);
|
|
if (!name) return;
|
|
eq.label = name;
|
|
saveEquip(eq);
|
|
renderSchematic();
|
|
}
|
|
|
|
function updateSchematicAnimations() {
|
|
const equip = getEquipment();
|
|
equip.forEach(eq => {
|
|
if (!eq.link || !eq.anim) return;
|
|
const el = document.getElementById('sim-' + eq.id);
|
|
if (!el) return;
|
|
const s = simState[eq.link];
|
|
el.classList.toggle('active', s && s.on);
|
|
});
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════
|
|
// BOOT
|
|
// ══════════════════════════════════════════════════════
|
|
init();
|
|
</script>
|
|
</body>
|
|
</html>
|