mirror of
https://git.j0bot.ch/admin_j0bot/ploxion-graphe.git
synced 2026-11-29 06:32:07 +00:00
217 lines
18 KiB
PHP
217 lines
18 KiB
PHP
{{-- PL0XI0N graphe — l'éditeur de workflow par NŒUDS (à la n8n). Primitive
|
|
atomique du catalogue de RS-7 : « Boîtes, ports, fils, exécution de flux.
|
|
Node editor générique (automation, dataflow). » On pose des nœuds (triggers
|
|
→ actions), on les relie par des fils, et on EXÉCUTE : la donnée coule de
|
|
nœud en nœud. Branché sur le xerboxion (ouvrir un ploxion, émettre un tsoin,
|
|
fetch…). Sauvegarde localStorage. 100% client-side, droit au silence §4.4.
|
|
window.jOSApps['graphe']. mount(c, ctx) -> { unmount }. --}}
|
|
<style>
|
|
.gr { display:flex; flex-direction:column; height:100%; background:#0b0c12; color:#e8e9f2; font-family:ui-sans-serif,system-ui,sans-serif; font-size:.82rem; }
|
|
.gr__bar { flex:0 0 auto; display:flex; align-items:center; gap:6px; flex-wrap:wrap; padding:7px 10px; border-bottom:1px solid rgba(150,140,255,.18); }
|
|
.gr__t { font-weight:700; color:rgba(190,180,255,.95); margin-right:4px; }
|
|
.gr__add { cursor:pointer; font-size:.7rem; padding:3px 9px; border-radius:7px; border:1px solid rgba(150,140,255,.3); background:rgba(40,36,70,.5); color:#dcd8ff; }
|
|
.gr__add:hover { background:rgba(70,60,140,.5); }
|
|
.gr__run { cursor:pointer; font-size:.74rem; padding:4px 13px; border-radius:7px; border:1px solid rgba(120,230,160,.55); background:rgba(40,120,80,.35); color:#bfffd6; font-weight:700; }
|
|
.gr__run:hover { background:rgba(50,150,100,.5); }
|
|
.gr__sp { flex:1; }
|
|
.gr__b2 { cursor:pointer; font-size:.68rem; padding:3px 9px; border-radius:7px; border:1px solid rgba(150,150,180,.3); background:none; color:rgba(210,210,230,.8); }
|
|
.gr__stage { flex:1; min-height:0; position:relative; overflow:auto; background:
|
|
radial-gradient(circle, rgba(150,140,255,.09) 1px, transparent 1.3px) 0 0/22px 22px, #07080c; }
|
|
.gr__cv { position:relative; width:2200px; height:1500px; }
|
|
.gr__wires { position:absolute; inset:0; width:100%; height:100%; pointer-events:none; overflow:visible; }
|
|
.gr__n { position:absolute; width:158px; background:rgba(22,22,34,.97); border:1px solid rgba(150,140,255,.4);
|
|
border-radius:10px; box-shadow:0 8px 24px rgba(0,0,0,.5); z-index:2; }
|
|
.gr__n.sel { border-color:rgba(180,170,255,.9); box-shadow:0 8px 30px rgba(120,100,255,.3); }
|
|
.gr__nh { display:flex; align-items:center; gap:5px; padding:5px 8px; cursor:grab; border-bottom:1px solid rgba(150,140,255,.18);
|
|
font-size:.72rem; font-weight:700; border-radius:10px 10px 0 0; background:rgba(60,52,110,.5); }
|
|
.gr__nh:active { cursor:grabbing; }
|
|
.gr__nx { margin-left:auto; cursor:pointer; color:rgba(255,150,150,.7); font-size:.8rem; }
|
|
.gr__nb { padding:7px 8px; display:flex; flex-direction:column; gap:5px; }
|
|
.gr__nb input, .gr__nb textarea, .gr__nb select { width:100%; box-sizing:border-box; background:rgba(0,0,0,.35); border:1px solid rgba(150,140,255,.25);
|
|
border-radius:6px; color:#eef; padding:4px 7px; font:0.72rem ui-sans-serif,system-ui; outline:none; }
|
|
.gr__nb textarea { resize:vertical; min-height:34px; }
|
|
.gr__nbtn { cursor:pointer; background:rgba(90,70,200,.35); border:1px solid rgba(150,140,255,.5); color:#fff; border-radius:6px; padding:4px; font-size:.72rem; }
|
|
.gr__out { font-family:ui-monospace,monospace; font-size:.62rem; color:rgba(160,230,180,.85); word-break:break-word; max-height:64px; overflow:auto; background:rgba(0,0,0,.3); border-radius:5px; padding:3px 5px; }
|
|
.gr__out:empty { display:none; }
|
|
.gr__port { position:absolute; width:13px; height:13px; border-radius:50%; background:#15151f; border:2px solid rgba(150,140,255,.8); top:50%; transform:translateY(-50%); cursor:crosshair; z-index:3; }
|
|
.gr__port.in { left:-8px; } .gr__port.out { right:-8px; border-color:rgba(120,230,160,.85); }
|
|
.gr__port:hover { background:rgba(150,140,255,.5); }
|
|
.gr__hint { position:absolute; bottom:8px; left:10px; font-size:.62rem; color:rgba(190,190,220,.4); pointer-events:none; z-index:5; }
|
|
@media (max-width:768px) {
|
|
.gr .gr__bar { flex-wrap:nowrap !important; overflow-x:auto; overflow-y:hidden; -webkit-overflow-scrolling:touch; scrollbar-width:none; padding:5px 10px; }
|
|
.gr .gr__bar::-webkit-scrollbar { display:none; }
|
|
.gr .gr__bar > * { flex:0 0 auto; white-space:nowrap; }
|
|
.gr .gr__add, .gr .gr__run, .gr .gr__b2 { min-height:44px; }
|
|
.gr .gr__sp { flex:0 0 12px; }
|
|
}
|
|
</style>
|
|
<script>
|
|
(function () {
|
|
'use strict';
|
|
function esc(s) { return window.jOSb.esc(s); }
|
|
|
|
function describe() {
|
|
var n = 0; try { var d = JSON.parse(localStorage.getItem('jos:graphe') || 'null'); n = (d && d.nodes) ? d.nodes.length : 0; } catch (e) {}
|
|
return { v: 1, id: 'graphe', title: 'Graphe', icon: '🕸', items: [
|
|
{ p: 1, kind: 'stat', label: 'nœuds', value: n }
|
|
] };
|
|
}
|
|
function mount(container, ctx) {
|
|
var KEY = 'jos:graphe', nodes = [], conns = [], seq = 0, alive = true, timers = [];
|
|
// ---- définitions des nœuds : in/out + corps + exécution ----
|
|
// exec(node, input, emit) : emit(value) propage la valeur en aval.
|
|
var DEFS = {
|
|
bouton: { t: '▶ Bouton', cat: 'trigger', in: false, out: true,
|
|
body: function (n) { return '<input data-f="v" placeholder="valeur émise" value="' + esc(n.params.v || 'go') + '"><button class="gr__nbtn" data-fire>déclencher</button>'; },
|
|
exec: function () {} },
|
|
intervalle: { t: '⏱ Intervalle', cat: 'trigger', in: false, out: true,
|
|
body: function (n) { return '<input data-f="sec" type="number" placeholder="sec" value="' + esc(n.params.sec || 3) + '"><button class="gr__nbtn" data-toggle>start / stop</button>'; },
|
|
exec: function () {} },
|
|
texte: { t: '✎ Texte', cat: 'source', in: true, out: true,
|
|
body: function (n) { return '<textarea data-f="text" placeholder="texte…">' + esc(n.params.text || '') + '</textarea>'; },
|
|
exec: function (n, input, emit) { emit(n.params.text || input || ''); } },
|
|
log: { t: '🪵 Log', cat: 'sink', in: true, out: false,
|
|
body: function () { return '<div class="gr__out" data-out></div>'; },
|
|
exec: function (n, input, emit, el) { var o = el.querySelector('[data-out]'); if (o) o.textContent = (typeof input === 'string' ? input : JSON.stringify(input)); } },
|
|
ouvrir: { t: '🪟 Ouvrir ploxion', cat: 'action', in: true, out: false,
|
|
body: function (n) { return '<input data-f="id" placeholder="id ploxion (ou via l\'entrée)" value="' + esc(n.params.id || '') + '">'; },
|
|
exec: function (n, input) { var id = (n.params.id || input || '').toString().trim(); if (id && window.jOS && window.jOS.openPloxion) window.jOS.openPloxion(id); else if (id && window.jOSMobile) window.jOSMobile.open(id); } },
|
|
emettre: { t: '📡 Émettre (tsoin)', cat: 'action', in: true, out: true,
|
|
body: function (n) { return '<input data-f="kind" placeholder="kind" value="' + esc(n.params.kind || 'tsoin') + '"><div class="gr__out" data-out></div>'; },
|
|
exec: function (n, input, emit, el) {
|
|
var content = (typeof input === 'string' ? input : JSON.stringify(input));
|
|
var o = el.querySelector('[data-out]'); if (o) o.textContent = '→ ' + (n.params.kind || 'tsoin') + ' : ' + content;
|
|
var m = document.querySelector('meta[name="csrf-token"]'), csrf = m ? m.getAttribute('content') : '';
|
|
fetch('/chat', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': csrf, Accept: 'application/json' },
|
|
body: JSON.stringify({ kind: n.params.kind || 'tsoin', content: content, room: 'prive' }) }).catch(function () {});
|
|
emit(input);
|
|
} },
|
|
fetch: { t: '🌐 Fetch', cat: 'action', in: true, out: true,
|
|
body: function (n) { return '<input data-f="url" placeholder="URL (ou via l\'entrée)" value="' + esc(n.params.url || '') + '"><div class="gr__out" data-out></div>'; },
|
|
exec: function (n, input, emit, el) { var url = (n.params.url || input || '').toString().trim(); if (!url) return;
|
|
var o = el.querySelector('[data-out]'); if (o) o.textContent = '…';
|
|
fetch(url).then(function (r) { return r.text(); }).then(function (t) { if (!alive) return; t = t.slice(0, 2000); if (o) o.textContent = t.slice(0, 200); emit(t); }).catch(function (e) { if (o) o.textContent = 'erreur'; }); } },
|
|
delai: { t: '⏳ Délai', cat: 'transform', in: true, out: true,
|
|
body: function (n) { return '<input data-f="ms" type="number" placeholder="ms" value="' + esc(n.params.ms || 1000) + '">'; },
|
|
exec: function (n, input, emit) { var id = setTimeout(function () { if (alive) emit(input); }, +n.params.ms || 1000); timers.push(id); } }
|
|
};
|
|
var ORDER = ['bouton', 'intervalle', 'texte', 'delai', 'fetch', 'emettre', 'ouvrir', 'log'];
|
|
|
|
container.innerHTML =
|
|
'<div class="gr">'
|
|
+ ' <div class="gr__bar"><span class="gr__t">🕸 Graphe</span>'
|
|
+ ORDER.map(function (k) { return '<button class="gr__add" data-add="' + k + '">+ ' + esc(DEFS[k].t.replace(/^.\s/, '')) + '</button>'; }).join('')
|
|
+ ' <span class="gr__sp"></span>'
|
|
+ ' <button class="gr__b2" data-save>💾</button><button class="gr__b2" data-clear>vider</button>'
|
|
+ ' <button class="gr__run" data-run>▶ exécuter tout</button></div>'
|
|
+ ' <div class="gr__stage" data-stage><div class="gr__cv" data-cv>'
|
|
+ ' <svg class="gr__wires" data-wires></svg>'
|
|
+ ' <div class="gr__hint">glisse les en-têtes · relie un port ● (sortie) à un ○ (entrée) · ▶ déclenche un trigger</div>'
|
|
+ ' </div></div>'
|
|
+ '</div>';
|
|
var cv = container.querySelector('[data-cv]'), wires = container.querySelector('[data-wires]'), stage = container.querySelector('[data-stage]');
|
|
var nodeEls = {};
|
|
|
|
function byId(id) { for (var i = 0; i < nodes.length; i++) if (nodes[i].id === id) return nodes[i]; return null; }
|
|
function newId() { return 'n' + (++seq) + '-' + Date.now().toString(36); }
|
|
|
|
// ---- exécution : propage une valeur en aval ----
|
|
function emitFrom(nodeId, value) {
|
|
conns.filter(function (c) { return c.from === nodeId; }).forEach(function (c) { deliver(c.to, value); });
|
|
}
|
|
function deliver(nodeId, value) {
|
|
var n = byId(nodeId); if (!n) return; var def = DEFS[n.type], el = nodeEls[nodeId];
|
|
try { def.exec(n, value, function (out) { emitFrom(nodeId, out); }, el); } catch (e) {}
|
|
}
|
|
function runAll() { nodes.forEach(function (n) { if (DEFS[n.type].cat === 'trigger') fireTrigger(n); }); }
|
|
function fireTrigger(n) { emitFrom(n.id, n.params.v || ''); }
|
|
|
|
// ---- rendu d'un nœud ----
|
|
function renderNode(n) {
|
|
var def = DEFS[n.type], el = document.createElement('div');
|
|
el.className = 'gr__n'; el.style.left = n.x + 'px'; el.style.top = n.y + 'px';
|
|
el.innerHTML = '<div class="gr__nh" data-drag>' + esc(def.t) + '<span class="gr__nx" data-del>✕</span></div>'
|
|
+ '<div class="gr__nb">' + def.body(n) + '</div>'
|
|
+ (def.in ? '<div class="gr__port in" data-port="in"></div>' : '')
|
|
+ (def.out ? '<div class="gr__port out" data-port="out"></div>' : '');
|
|
cv.appendChild(el); nodeEls[n.id] = el;
|
|
// champs -> params
|
|
el.querySelectorAll('[data-f]').forEach(function (f) { f.addEventListener('input', function () { n.params[f.getAttribute('data-f')] = f.value; }); });
|
|
// trigger bouton / intervalle
|
|
var fire = el.querySelector('[data-fire]'); if (fire) fire.addEventListener('click', function () { fireTrigger(n); });
|
|
var tog = el.querySelector('[data-toggle]'); if (tog) tog.addEventListener('click', function () {
|
|
if (n._iv) { clearInterval(n._iv); n._iv = 0; tog.style.opacity = ''; }
|
|
else { tog.style.opacity = '.6'; var k = 0; n._iv = setInterval(function () { if (alive) emitFrom(n.id, ++k); }, Math.max(500, (+n.params.sec || 3) * 1000)); timers.push(n._iv); }
|
|
});
|
|
el.querySelector('[data-del]').addEventListener('click', function () { removeNode(n.id); });
|
|
el.addEventListener('pointerdown', function () { select(n.id); }, true);
|
|
wireDrag(n, el);
|
|
wirePorts(n, el);
|
|
}
|
|
function select(id) { Object.keys(nodeEls).forEach(function (k) { nodeEls[k].classList.toggle('sel', k === id); }); }
|
|
|
|
// ---- drag d'un nœud ----
|
|
function wireDrag(n, el) {
|
|
var h = el.querySelector('[data-drag]'), d = null;
|
|
h.addEventListener('pointerdown', function (e) { if (e.target.closest('[data-del]')) return; var r = stage.getBoundingClientRect();
|
|
d = { ox: e.clientX, oy: e.clientY, nx: n.x, ny: n.y }; h.setPointerCapture(e.pointerId); e.preventDefault(); });
|
|
h.addEventListener('pointermove', function (e) { if (!d) return; n.x = Math.max(0, d.nx + (e.clientX - d.ox)); n.y = Math.max(0, d.ny + (e.clientY - d.oy));
|
|
el.style.left = n.x + 'px'; el.style.top = n.y + 'px'; drawWires(); });
|
|
function end() { d = null; } h.addEventListener('pointerup', end); h.addEventListener('pointercancel', end);
|
|
}
|
|
|
|
// ---- création de fils (port out -> port in) ----
|
|
var linking = null;
|
|
function wirePorts(n, el) {
|
|
var out = el.querySelector('[data-port="out"]'), inp = el.querySelector('[data-port="in"]');
|
|
if (out) out.addEventListener('pointerdown', function (e) { e.stopPropagation(); e.preventDefault(); linking = { from: n.id }; });
|
|
if (inp) inp.addEventListener('pointerup', function (e) { e.stopPropagation(); if (linking && linking.from && linking.from !== n.id) {
|
|
if (!conns.some(function (c) { return c.from === linking.from && c.to === n.id; })) { conns.push({ from: linking.from, to: n.id }); drawWires(); } } linking = null; });
|
|
}
|
|
stage.addEventListener('pointerup', function () { linking = null; });
|
|
|
|
// ---- rendu des fils (SVG bezier) ----
|
|
function portPos(nodeId, which) {
|
|
var el = nodeEls[nodeId]; if (!el) return null; var r = el.getBoundingClientRect(), cr = cv.getBoundingClientRect();
|
|
var y = r.top - cr.top + r.height / 2; var x = which === 'out' ? (r.left - cr.left + r.width) : (r.left - cr.left);
|
|
return { x: x, y: y };
|
|
}
|
|
function drawWires() {
|
|
var s = '';
|
|
conns.forEach(function (c, i) {
|
|
var a = portPos(c.from, 'out'), b = portPos(c.to, 'in'); if (!a || !b) return;
|
|
var dx = Math.max(30, Math.abs(b.x - a.x) * 0.5);
|
|
s += '<path d="M' + a.x + ' ' + a.y + ' C ' + (a.x + dx) + ' ' + a.y + ' ' + (b.x - dx) + ' ' + b.y + ' ' + b.x + ' ' + b.y + '" fill="none" stroke="rgba(150,200,255,.6)" stroke-width="2"/>'
|
|
+ '<circle cx="' + ((a.x + b.x) / 2) + '" cy="' + ((a.y + b.y) / 2) + '" r="6" fill="rgba(30,30,46,.9)" stroke="rgba(255,150,150,.5)" data-c="' + i + '" style="cursor:pointer;pointer-events:auto"/>';
|
|
});
|
|
wires.innerHTML = s;
|
|
wires.querySelectorAll('[data-c]').forEach(function (el) { el.addEventListener('click', function () { conns.splice(+el.getAttribute('data-c'), 1); drawWires(); }); });
|
|
}
|
|
|
|
function addNode(type, x, y) { var n = { id: newId(), type: type, x: x != null ? x : (stage.scrollLeft + 40 + (nodes.length % 5) * 24), y: y != null ? y : (stage.scrollTop + 40 + (nodes.length % 5) * 20), params: {} }; nodes.push(n); renderNode(n); return n; }
|
|
function removeNode(id) { conns = conns.filter(function (c) { return c.from !== id && c.to !== id; }); var n = byId(id); if (n && n._iv) clearInterval(n._iv);
|
|
nodes = nodes.filter(function (x) { return x.id !== id; }); if (nodeEls[id]) nodeEls[id].remove(); delete nodeEls[id]; drawWires(); }
|
|
function clearAll() { nodes.slice().forEach(function (n) { removeNode(n.id); }); }
|
|
function save() { try { localStorage.setItem(KEY, JSON.stringify({ nodes: nodes.map(function (n) { return { id: n.id, type: n.type, x: n.x, y: n.y, params: n.params }; }), conns: conns })); } catch (e) {} }
|
|
function loadSaved() { try { var d = JSON.parse(localStorage.getItem(KEY) || 'null'); if (d && d.nodes) { d.nodes.forEach(function (n) { n.params = n.params || {}; nodes.push(n); renderNode(n); var m = n.id.match(/^n(\d+)/); if (m) seq = Math.max(seq, +m[1]); }); conns = d.conns || []; drawWires(); return true; } } catch (e) {} return false; }
|
|
|
|
container.querySelectorAll('[data-add]').forEach(function (b) { b.addEventListener('click', function () { addNode(b.getAttribute('data-add')); }); });
|
|
container.querySelector('[data-run]').addEventListener('click', runAll);
|
|
container.querySelector('[data-save]').addEventListener('click', function () { save(); var b = container.querySelector('[data-save]'); b.textContent = '✓'; setTimeout(function () { b.textContent = '💾'; }, 1000); });
|
|
container.querySelector('[data-clear]').addEventListener('click', function () { if (confirm('Vider le graphe ?')) { clearAll(); save(); } });
|
|
|
|
// graphe sauvegardé, ou un exemple de démarrage
|
|
if (!loadSaved()) {
|
|
var a = addNode('bouton', 60, 60); a.params.v = 'graphe';
|
|
var b = addNode('ouvrir', 320, 60); b.params.id = '';
|
|
conns.push({ from: a.id, to: b.id }); drawWires();
|
|
}
|
|
|
|
return { unmount: function () { alive = false; timers.forEach(function (t) { clearInterval(t); clearTimeout(t); }); nodes.forEach(function (n) { if (n._iv) clearInterval(n._iv); }); container.innerHTML = ''; } };
|
|
}
|
|
|
|
window.jOSApps = window.jOSApps || {};
|
|
window.jOSApps['graphe'] = { manifest: { id: 'graphe', name: 'Graphe', icon: '🕸' }, mount: mount, describe: describe };
|
|
})();
|
|
</script>
|