diff options
| author | historia <[not public]> | 2026-07-10 18:53:26 -0400 |
|---|---|---|
| committer | historia <[not public]> | 2026-07-10 18:53:26 -0400 |
| commit | b31958c6304a25279197c1a08b924b6b44798a99 (patch) | |
| tree | 35cea781069f4a5ccbd03f96cb8b0d35af056f90 /internal/admin/static/talktree.js | |
| parent | 141161781dd26bc0a84b0d50b291368b5bce0e54 (diff) | |
| download | thehouseoficarus-b31958c6304a25279197c1a08b924b6b44798a99.tar.gz | |
feat(admin): use intereditor.js for mob talk editor to further unify condition/action system
Diffstat (limited to 'internal/admin/static/talktree.js')
| -rw-r--r-- | internal/admin/static/talktree.js | 731 |
1 files changed, 205 insertions, 526 deletions
diff --git a/internal/admin/static/talktree.js b/internal/admin/static/talktree.js index 4758918..ef6efd0 100644 --- a/internal/admin/static/talktree.js +++ b/internal/admin/static/talktree.js @@ -1,6 +1,7 @@ -// talktree.js — Full-featured conversation tree editor. -// Inline editing for all TalkNode and TalkOption fields. -// No prompt() dialogs. Clause-based condition editor, action forms. +// talktree.js — Conversation tree editor with unified condition & action +// editors powered by intereditor.js. Tree navigation, messages, and options +// (text / goto) stay here; condition and action editing delegates to +// intereditor via talk-scoped contexts that target talk.nodes[key]... paths. (function() { 'use strict'; @@ -52,103 +53,143 @@ else { delete root[lk]; } } - // ---- Clause types ---- - - var CLAUSE_TYPES = [ - { key: 'flag', label: 'Flag', hasValue: true }, - { key: 'player_flag', label: 'Player Flag', hasValue: true }, - { key: 'has_item', label: 'Has Item', hasValue: false }, - { key: 'min_credits', label: 'Min Credits', hasValue: false } - ]; + // ---- Talk scopes for intereditor.js ---- + // Each scope targets a specific condition or action object inside the talk + // tree and handles re-render + talkSync on every mutation. + + // ---- Talk scopes for intereditor.js ---- + // Each scope targets a specific condition or action object inside the talk + // tree and handles re-render + talkSync on every mutation. + + var _talkScopeSeq = 0; + + function talkCondScope(nodeKey, optIdx) { + var token = 'tk' + (++_talkScopeSeq); + return ie_registerScope({ + getCond: function() { + var t = talkTarget(nodeKey, optIdx); + return t && t.condition || null; + }, + setCond: function(c) { + var t = talkTarget(nodeKey, optIdx); + if (!t) return; + if (c) t.condition = c; else delete t.condition; + }, + onChange: function() { + talkSync(); + renderTalkTree(window._talkData); + } + }, token); + } + + function talkActionScope(nodeKey, optIdx) { + var token = 'tk' + (++_talkScopeSeq); + return ie_registerScope({ + getStep: function() { + var t = talkTarget(nodeKey, optIdx); + return t && t.action || {}; + }, + setStep: function(s) { + var t = talkTarget(nodeKey, optIdx); + if (!t) return; + if (s && Object.keys(s).length > 0) t.action = s; else delete t.action; + }, + onChange: function() { + talkSync(); + renderTalkTree(window._talkData); + } + }, token); + } - function clauseTypeLabel(key) { - for (var i = 0; i < CLAUSE_TYPES.length; i++) { - if (CLAUSE_TYPES[i].key === key) return CLAUSE_TYPES[i].label; + function talkTarget(nodeKey, optIdx) { + var node = talkNodes() && talkNodes()[nodeKey]; + if (!node) return null; + if (optIdx !== null) { + if (!node.options || !node.options[optIdx]) return null; + return node.options[optIdx]; } - return key; + return node; } - function clauseTypeHasValue(key) { - for (var i = 0; i < CLAUSE_TYPES.length; i++) { - if (CLAUSE_TYPES[i].key === key) return CLAUSE_TYPES[i].hasValue; - } - return false; + // ---- Render a single action step for a talk node or option ---- + + function talkRenderActionStep(nodeKey, optIdx) { + var t = talkTarget(nodeKey, optIdx); + var step = (t && t.action) || {}; + var scopeToken = talkActionScope(nodeKey, optIdx); + var h = '<div class="ie-act-root" data-ie-scope="' + escAttr(scopeToken) + '">'; + h += ie_renderMessages(step, '', 0, 0, ''); + h += ie_renderAct(step, '', 0, 0, ''); + h += '<div class="ie-act-addbar">' + ie_renderStepAddbar(step, '', 0, 0, '') + '</div>'; + h += '</div>'; + return h; } - // Extract clauses from a condition object - function extractClauses(cond) { - if (!cond || typeof cond !== 'object' || Array.isArray(cond)) return { mode: 'none', clauses: [], groupKey: null }; - if (cond.all_of && Array.isArray(cond.all_of) && cond.all_of.length > 0) { - return { mode: 'multi', clauses: cond.all_of.slice(), groupKey: 'all_of' }; - } - if (cond.any_of && Array.isArray(cond.any_of) && cond.any_of.length > 0) { - return { mode: 'multi', clauses: cond.any_of.slice(), groupKey: 'any_of' }; - } - // Single clause from flat fields - for (var i = 0; i < CLAUSE_TYPES.length; i++) { - if (cond[CLAUSE_TYPES[i].key] !== undefined) { - var c = {}; - c[CLAUSE_TYPES[i].key] = cond[CLAUSE_TYPES[i].key]; - if (cond.not) c.not = cond.not; - if (cond.value !== undefined) c.value = cond.value; - if (cond.has_item && CLAUSE_TYPES[i].key !== 'has_item') c.has_item = cond.has_item; - if (cond.player_flag && CLAUSE_TYPES[i].key !== 'player_flag') c.player_flag = cond.player_flag; - if (cond.flag && CLAUSE_TYPES[i].key !== 'flag') c.flag = cond.flag; - return { mode: 'single', clauses: [c], groupKey: null }; - } - } - // Check for has_item or min_credits as the only key - for (var j = 0; j < CLAUSE_TYPES.length; j++) { - var ck = CLAUSE_TYPES[j].key; - if (cond[ck] !== undefined) { - var cc = {}; - cc[ck] = cond[ck]; - if (cond.not) cc.not = cond.not; - if (cond.value !== undefined) cc.value = cond.value; - return { mode: 'single', clauses: [cc], groupKey: null }; - } - } - return { mode: 'none', clauses: [], groupKey: null }; + // ---- talk cond/act add/remove helpers (called from onclick data-action) ---- + + function talkAddNodeCond(nodeKey) { + var node = talkNodes() && talkNodes()[nodeKey]; + if (!node || (node.condition && typeof node.condition === 'object')) return; + node.condition = {all_of: []}; + renderTalkTree(window._talkData); + talkSync(); } - // Determine the type key for a clause object - function clauseObjType(clause) { - for (var i = 0; i < CLAUSE_TYPES.length; i++) { - var k = CLAUSE_TYPES[i].key; - if (clause[k] !== undefined) return k; - } - return null; + function talkRemoveNodeCond(nodeKey) { + var node = talkNodes() && talkNodes()[nodeKey]; + if (!node) return; + delete node.condition; + renderTalkTree(window._talkData); + talkSync(); } - // Build a full condition object from clauses - function buildCondition(clauses, groupKey) { - if (!clauses || clauses.length === 0) return null; - if (clauses.length === 1) { - var flat = {}; - var c = clauses[0]; - for (var k in c) { if (c[k] !== undefined) flat[k] = c[k]; } - return flat; - } - var cond = {}; - cond[groupKey || 'all_of'] = clauses.map(function(c) { - var sub = {}; - for (var k in c) { if (c[k] !== undefined) sub[k] = c[k]; } - return sub; - }); - return cond; + function talkAddNodeAct(nodeKey) { + var node = talkNodes() && talkNodes()[nodeKey]; + if (!node || (node.action && typeof node.action === 'object')) return; + node.action = {}; + renderTalkTree(window._talkData); + talkSync(); } - // ---- Action field definitions ---- + function talkRemoveNodeAct(nodeKey) { + var node = talkNodes() && talkNodes()[nodeKey]; + if (!node) return; + delete node.action; + renderTalkTree(window._talkData); + talkSync(); + } + + function talkAddOptionCond(nodeKey, optIdx) { + var t = talkTarget(nodeKey, optIdx); + if (!t || (t.condition && typeof t.condition === 'object')) return; + t.condition = {all_of: []}; + renderTalkTree(window._talkData); + talkSync(); + } + + function talkRemoveOptionCond(nodeKey, optIdx) { + var t = talkTarget(nodeKey, optIdx); + if (!t) return; + delete t.condition; + renderTalkTree(window._talkData); + talkSync(); + } - var ACTION_FIELDS = [ - { key: 'give_item', label: 'Give Item', type: 'item_search' }, - { key: 'take_item', label: 'Take Item', type: 'item_search' }, - { key: 'credits', label: 'Credits (+/-)', type: 'number' }, - { key: 'teleport', label: 'TP Room', type: 'number' }, - { key: 'heal', label: 'Heal HP', type: 'number' }, - ]; + function talkAddOptionAct(nodeKey, optIdx) { + var t = talkTarget(nodeKey, optIdx); + if (!t || (t.action && typeof t.action === 'object')) return; + t.action = {}; + renderTalkTree(window._talkData); + talkSync(); + } - var ACTION_BOOLS = []; + function talkRemoveOptionAct(nodeKey, optIdx) { + var t = talkTarget(nodeKey, optIdx); + if (!t) return; + delete t.action; + renderTalkTree(window._talkData); + talkSync(); + } // ---- Event delegation ---- @@ -184,9 +225,6 @@ var nodeKey = el.getAttribute('data-nk'); var optIdx = el.getAttribute('data-oi'); - var subIdx = el.getAttribute('data-si'); - var subKey = el.getAttribute('data-sk'); - var condGroupKey = el.getAttribute('data-cgk'); switch (action) { case 'delete-node': @@ -207,32 +245,29 @@ case 'delete-message': deleteMessageLine(nodeKey, parseInt(optIdx, 10)); break; - case 'add-condition': - toggleCondition(nodeKey, optIdx !== null ? parseInt(optIdx, 10) : null); - break; - case 'remove-condition': - removeCondition(nodeKey, optIdx !== null ? parseInt(optIdx, 10) : null); + case 'add-node-cond': + talkAddNodeCond(nodeKey); break; - case 'add-action': - addAction(nodeKey, optIdx !== null ? parseInt(optIdx, 10) : null); + case 'remove-node-cond': + talkRemoveNodeCond(nodeKey); break; - case 'remove-action': - removeAction(nodeKey, optIdx !== null ? parseInt(optIdx, 10) : null); + case 'add-node-act': + talkAddNodeAct(nodeKey); break; - case 'add-clause': - addClause(nodeKey, optIdx !== null ? parseInt(optIdx, 10) : null, el.getAttribute('data-ct')); + case 'remove-node-act': + talkRemoveNodeAct(nodeKey); break; - case 'remove-clause': - removeClause(nodeKey, optIdx !== null ? parseInt(optIdx, 10) : null, parseInt(subIdx, 10)); + case 'add-opt-cond': + talkAddOptionCond(nodeKey, parseInt(optIdx, 10)); break; - case 'set-cond-group': - setCondGroup(nodeKey, optIdx !== null ? parseInt(optIdx, 10) : null, condGroupKey); + case 'remove-opt-cond': + talkRemoveOptionCond(nodeKey, parseInt(optIdx, 10)); break; - case 'add-kv': - addKVPair(nodeKey, optIdx !== null ? parseInt(optIdx, 10) : null, el.getAttribute('data-kvk')); + case 'add-opt-act': + talkAddOptionAct(nodeKey, parseInt(optIdx, 10)); break; - case 'remove-kv': - removeKVPair(nodeKey, optIdx !== null ? parseInt(optIdx, 10) : null, el.getAttribute('data-kvk'), subKey); + case 'remove-opt-act': + talkRemoveOptionAct(nodeKey, parseInt(optIdx, 10)); break; } } @@ -242,6 +277,13 @@ window.renderTalkTree = function(data) { var container = document.getElementById('talktree'); if (!container) return; + // Purge old talk scopes before re-rendering (new ones will be registered). + if (window._ieScopes) { + Object.keys(window._ieScopes).forEach(function(k) { + if (k.indexOf('tk') === 0) delete window._ieScopes[k]; + }); + } + _talkScopeSeq = 0; window._talkData = data; window._talkOpenNodes = window._talkOpenNodes || new Set(); window._talkOpenNodes.add('start'); @@ -351,7 +393,6 @@ var html = '<div class="talk-node' + (isOpen ? ' open' : '') + '" id="node-' + escAttr(rootKey) + '">'; - // Header — fix: check for data-action before toggling html += '<div class="talk-node-header" onclick="if(!event.target.closest(\'[data-action]\'))talkToggleNode(\'' + escAttr(rootKey) + '\',this.parentElement)">'; html += '<span class="talk-node-arrow"></span>'; html += '<span class="talk-node-key">' + esc(rootKey) + '</span>'; @@ -360,12 +401,10 @@ html += '<button class="talk-node-btn talk-node-del" data-action="delete-node" data-nk="' + escAttr(rootKey) + '">Delete</button>'; html += '</div>'; - // Body html += '<div class="talk-node-body">'; html += renderNodeBody(rootKey, node, nodes); - html += '</div>'; // .talk-node-body + html += '</div>'; - // Children var childKeys = []; if (node.goto && nodes[node.goto]) childKeys.push(node.goto); if (node.options) { @@ -379,7 +418,7 @@ html += '</div>'; } - html += '</div>'; // .talk-node + html += '</div>'; return html; } @@ -388,59 +427,60 @@ return ''; } - // ---- Node body: condition-gated content ---- + // ---- Node body: condition, messages, options, action via intereditor ---- - // Track open node state across re-renders window._talkOpenNodes = window._talkOpenNodes || new Set(); function renderNodeBody(nodeKey, node, nodes) { - var ec = extractClauses(node.condition); - var hasCond = ec.mode !== 'none' || (node.condition && typeof node.condition === 'object' && !Array.isArray(node.condition)); + var hasCond = !!(node.condition && typeof node.condition === 'object'); + var hasAct = !!(node.action && typeof node.action === 'object' && !Array.isArray(node.action)); var hasOpts = node.options && node.options.length > 0; var html = ''; + // Node condition (via intereditor) if (hasCond) { html += '<div class="talk-cond-card">'; html += '<div class="talk-cond-card-header">' + - 'Condition' + - '<button class="talk-section-remove" data-action="remove-condition" data-nk="' + escAttr(nodeKey) + '">Remove</button>' + + 'Condition (for this node)' + + '<button class="talk-section-remove" data-action="remove-node-cond" data-nk="' + escAttr(nodeKey) + '">Remove</button>' + '</div>'; html += '<div class="talk-cond-card-body">'; - html += renderConditionClauses(ec, nodeKey, null); - html += '<div class="talk-cond-card-sep"></div>'; - html += renderMessagesSection(nodeKey, node); - html += renderOptionsSection(nodeKey, node); - html += '<div class="talk-cond-card-sep"></div>'; + html += ie_renderCond(node.condition, '', 0, '', '', {scopeToken: talkCondScope(nodeKey, null), alwaysOuterAddbar: true}); + html += '</div>'; + html += '</div>'; + } else { + html += '<button class="talk-add-cond-btn" data-action="add-node-cond" data-nk="' + escAttr(nodeKey) + '">+ Add Condition</button>'; + } - var nodeAction = node.action; - var actionPrefix = 'nodes.' + escAttr(nodeKey) + '.action'; - if (nodeAction && typeof nodeAction === 'object' && !Array.isArray(nodeAction)) { - html += '<div class="talk-cond-card" style="margin-top:4px">'; - html += '<div class="talk-cond-card-header">' + - '<span class="talk-prop-label">Action</span>' + - '<button class="talk-section-remove" data-action="remove-action" data-nk="' + escAttr(nodeKey) + '">Remove</button></div>'; - html += '<div class="talk-cond-card-body">'; - html += renderActionForm(nodeAction, actionPrefix, nodeKey, null); - html += '</div>'; - html += '</div>'; - } else { - html += '<button class="talk-add-act-btn" style="margin-top:4px" data-action="add-action" data-nk="' + escAttr(nodeKey) + '">+ Add Action</button>'; - } + // Node messages (unchanged) + html += renderMessagesSection(nodeKey, node); - html += '<div class="talk-cond-card-sep"></div>'; - html += renderGotoFallback(nodeKey, node, 'If condition fails, go to'); - html += '</div>'; // .talk-cond-card-body - html += '</div>'; // .talk-cond-card + // Node options + html += renderOptionsSection(nodeKey, node); + + html += '<div class="talk-cond-card-sep"></div>'; + + // Node action (via intereditor) + if (hasAct) { + html += '<div class="talk-cond-card" style="margin-top:4px">'; + html += '<div class="talk-cond-card-header">' + + 'Action (at the start of this node)' + + '<button class="talk-section-remove" data-action="remove-node-act" data-nk="' + escAttr(nodeKey) + '">Remove</button></div>'; + html += '<div class="talk-cond-card-body">'; + html += talkRenderActionStep(nodeKey, null); + html += '</div>'; + html += '</div>'; } else { - html += '<button class="talk-add-cond-btn" data-action="add-condition" data-nk="' + escAttr(nodeKey) + '">+ Add Condition</button>'; - html += renderMessagesSection(nodeKey, node); - html += renderOptionsSection(nodeKey, node); + html += '<button class="talk-add-act-btn" style="margin-top:4px" data-action="add-node-act" data-nk="' + escAttr(nodeKey) + '">+ Add Action</button>'; + } + + // Goto fallback + if (hasCond) { html += '<div class="talk-cond-card-sep"></div>'; - html += renderActionSection(nodeKey, null, node.action); - if (!hasOpts) { - html += renderGotoFallback(nodeKey, node, 'Auto-advance to'); - } + html += renderGotoFallback(nodeKey, node, 'If condition fails, go to'); + } else if (!hasOpts) { + html += renderGotoFallback(nodeKey, node, 'Auto-advance to'); } return html; @@ -478,259 +518,6 @@ return html; } - // ---- Condition clauses ---- - - function renderConditionClauses(ec, nodeKey, optIdx) { - var clauses = ec.clauses; - var groupKey = ec.groupKey || 'all_of'; - var html = ''; - - // AND/OR mode selector (only when 2+ clauses) - if (clauses.length >= 2) { - html += '<div class="talk-cond-mode-row">' + - '<span class="talk-prop-label">Combine with</span>' + - '<select class="talk-select" data-action="set-cond-group" data-nk="' + escAttr(nodeKey) + '"' + (optIdx !== null ? ' data-oi="' + optIdx + '"' : '') + ' data-cgk="' + escAttr(groupKey) + '" onchange="event.stopPropagation(); talkCondGroupChange(this)">' + - '<option value="all_of"' + (groupKey === 'all_of' ? ' selected' : '') + '>All of (AND)</option>' + - '<option value="any_of"' + (groupKey === 'any_of' ? ' selected' : '') + '>Any of (OR)</option>' + - '</select>' + - '</div>'; - } - - // Clause rows - for (var i = 0; i < clauses.length; i++) { - var typeKey = clauseObjType(clauses[i]) || 'flag'; - var pathPrefix = clauses.length === 1 ? - 'nodes.' + escAttr(nodeKey) + (optIdx !== null ? '.options.' + optIdx : '') + '.condition' : - 'nodes.' + escAttr(nodeKey) + (optIdx !== null ? '.options.' + optIdx : '') + '.condition.' + groupKey + '.' + i; - html += renderClauseRow(clauses[i], pathPrefix, typeKey, nodeKey, optIdx, i); - } - - // Add clause buttons - html += '<div class="talk-clause-add-bar">'; - CLAUSE_TYPES.forEach(function(ct) { - html += '<button class="talk-clause-add-btn" data-action="add-clause" data-nk="' + escAttr(nodeKey) + '"' + - (optIdx !== null ? ' data-oi="' + optIdx + '"' : '') + ' data-ct="' + escAttr(ct.key) + '">+ ' + esc(ct.label) + '</button>'; - }); - html += '</div>'; - - return html; - } - - function renderClauseRow(clause, pathPrefix, typeKey, nodeKey, optIdx, clauseIdx) { - var hasValue = clauseTypeHasValue(typeKey); - var html = '<div class="talk-clause-row">'; - html += '<span class="talk-clause-type">' + esc(clauseTypeLabel(typeKey)) + '</span>'; - - if (typeKey === 'flag' || typeKey === 'player_flag') { - var notLabel = (clause.value !== undefined && clause.value !== '') ? 'Not' : 'Not Exists'; - html += '<span class="talk-clause-label">key:</span>'; - html += '<input class="talk-input talk-input-sm" data-tp="' + escAttr(pathPrefix + '.' + typeKey) + '" value="' + escAttr(clause[typeKey] || '') + '" placeholder="Flag name...">'; - html += '<label class="talk-check-label"><input type="checkbox" data-tp="' + escAttr(pathPrefix + '.not') + '"' + (clause.not ? ' checked' : '') + '> ' + notLabel + '</label>'; - html += '<span class="talk-clause-label">val:</span>'; - html += '<input class="talk-input talk-input-sm" data-tp="' + escAttr(pathPrefix + '.value') + '" value="' + escAttr(clause.value || '') + '" placeholder="Value match..." style="max-width:80px">'; - } else if (typeKey === 'has_item') { - html += '<span class="talk-clause-label">item:</span>'; - html += '<input class="talk-input talk-input-sm" data-tp="' + escAttr(pathPrefix + '.has_item') + '" value="' + escAttr(clause.has_item || '') + '" placeholder="Item ID...">'; - html += '<label class="talk-check-label"><input type="checkbox" data-tp="' + escAttr(pathPrefix + '.not') + '"' + (clause.not ? ' checked' : '') + '> Not Exists</label>'; - } else if (typeKey === 'min_credits') { - html += '<span class="talk-clause-label">cr:</span>'; - html += '<input class="talk-input-num" data-tp="' + escAttr(pathPrefix + '.min_credits') + '" type="number" value="' + (clause.min_credits || 0) + '" placeholder="0">'; - html += '<label class="talk-check-label"><input type="checkbox" data-tp="' + escAttr(pathPrefix + '.not') + '"' + (clause.not ? ' checked' : '') + '> Not</label>'; - } - - html += '<button class="talk-clause-del" data-action="remove-clause" data-nk="' + escAttr(nodeKey) + '"' + (optIdx !== null ? ' data-oi="' + optIdx + '"' : '') + ' data-si="' + clauseIdx + '" title="Remove clause">x</button>'; - html += '</div>'; - return html; - } - - // ---- Clause add/remove/migrate ---- - - function getConditionFor(nodeKey, optIdx) { - var node = talkNodes()[nodeKey]; - if (!node) return null; - if (optIdx !== null) { - if (!node.options || !node.options[optIdx]) return null; - return node.options[optIdx]; - } - return node; - } - - function condAddClause(target, typeKey, optIdx, nodeKey) { - var ec = extractClauses(target.condition); - var newClause = {}; - newClause[typeKey] = ''; - ec.clauses.push(newClause); - - if (ec.clauses.length === 1) { - // First clause: use flat fields - target.condition = buildCondition(ec.clauses, null); - } else { - ec.groupKey = ec.groupKey || 'all_of'; - target.condition = buildCondition(ec.clauses, ec.groupKey); - } - } - - function addClause(nodeKey, optIdx, typeKey) { - var target = getConditionFor(nodeKey, optIdx); - if (!target) return; - if (!target.condition || typeof target.condition !== 'object' || Array.isArray(target.condition)) { - target.condition = {}; - } - condAddClause(target, typeKey, optIdx, nodeKey); - renderTalkTree(window._talkData); - talkSync(); - } - - function removeClause(nodeKey, optIdx, clauseIdx) { - var target = getConditionFor(nodeKey, optIdx); - if (!target) return; - var ec = extractClauses(target.condition); - if (clauseIdx < 0 || clauseIdx >= ec.clauses.length) return; - - ec.clauses.splice(clauseIdx, 1); - - if (ec.clauses.length === 0) { - delete target.condition; - } else { - target.condition = buildCondition(ec.clauses, ec.groupKey); - } - renderTalkTree(window._talkData); - talkSync(); - } - - function setCondGroup(nodeKey, optIdx, newGroupKey) { - var target = getConditionFor(nodeKey, optIdx); - if (!target) return; - var ec = extractClauses(target.condition); - if (ec.clauses.length < 2) return; - ec.groupKey = newGroupKey; - target.condition = buildCondition(ec.clauses, ec.groupKey); - renderTalkTree(window._talkData); - talkSync(); - } - - // Called from onchange on the AND/OR select - window.talkCondGroupChange = function(select) { - var nodeKey = select.getAttribute('data-nk'); - var optIdx = select.getAttribute('data-oi'); - setCondGroup(nodeKey, optIdx !== null ? parseInt(optIdx, 10) : null, select.value); - }; - - // ---- Add/Remove whole Condition ---- - - function toggleCondition(nodeKey, optIdx) { - var target = getConditionFor(nodeKey, optIdx); - if (!target) return; - if (target.condition && typeof target.condition === 'object' && !Array.isArray(target.condition)) { - delete target.condition; - } else { - target.condition = {}; - if (optIdx !== null) { - window._talkAutoOpenDetails = new Set(); - window._talkAutoOpenDetails.add('cond_' + optIdx + '_' + escAttr(nodeKey)); - } - } - renderTalkTree(window._talkData); - talkSync(); - } - - function removeCondition(nodeKey, optIdx) { - var target = getConditionFor(nodeKey, optIdx); - if (!target) return; - delete target.condition; - renderTalkTree(window._talkData); - talkSync(); - } - - // ---- Action editor ---- - - function renderActionSection(nodeKey, optIdx, action) { - var prefix = optIdx !== null ? 'nodes.' + escAttr(nodeKey) + '.options.' + optIdx + '.action' : 'nodes.' + escAttr(nodeKey) + '.action'; - var hasAct = action && typeof action === 'object' && !Array.isArray(action); - - if (!hasAct) { - return '<div class="talk-section">' + - '<button class="talk-add-act-btn" data-action="add-action" data-nk="' + escAttr(nodeKey) + '"' + (optIdx !== null ? ' data-oi="' + optIdx + '"' : '') + '>+ Add Action</button>' + - '</div>'; - } - - return '<div class="talk-section">' + - '<div class="talk-cond-card-header">' + - '<span class="talk-prop-label">Action</span>' + - '<button class="talk-section-remove" data-action="remove-action" data-nk="' + escAttr(nodeKey) + '"' + (optIdx !== null ? ' data-oi="' + optIdx + '"' : '') + '>Remove</button>' + - '</div>' + - renderActionForm(action, prefix, nodeKey, optIdx) + - '</div>'; - } - - function renderActionForm(action, prefix, nodeKey, optIdx) { - var html = '<div class="talk-action-block">'; - - html += '<div class="talk-action-grid">'; - ACTION_FIELDS.forEach(function(f) { - var fpath = prefix + '.' + f.key; - var val = (action && action[f.key] !== undefined) ? action[f.key] : ''; - if (f.type === 'item_search') { - html += '<div class="talk-field-row">' + - '<span class="talk-prop-label">' + esc(f.label) + '</span>' + - '<div style="position:relative">' + - '<input class="talk-input talk-input-sm search-input" data-tp="' + escAttr(fpath) + '" data-search-type="items" value="' + escAttr(String(val)) + '" placeholder="Search items...">' + - '<div class="search-results" style="display:none"></div>' + - '</div>' + - '</div>'; - } else { - var narrowStyle = (f.key === 'teleport' || f.key === 'heal') ? ' style="max-width:70px"' : ''; - html += '<div class="talk-field-row">' + - '<span class="talk-prop-label">' + esc(f.label) + '</span>' + - '<input class="talk-input talk-input-sm"' + narrowStyle + ' data-tp="' + escAttr(fpath) + '" type="' + f.type + '" value="' + escAttr(String(val)) + '">' + - '</div>'; - } - }); - html += '</div>'; - - html += '<div style="margin-top:4px">'; - ACTION_BOOLS.forEach(function(b) { - html += '<label class="talk-check-label">' + - '<input type="checkbox" data-tp="' + escAttr(prefix + '.' + b.key) + '"' + (action && action[b.key] ? ' checked' : '') + '> ' + esc(b.label) + - '</label>'; - }); - html += '</div>'; - - html += renderKVEditor(action, prefix, 'set_flags', 'Set Flags', nodeKey, optIdx); - html += renderKVEditor(action, prefix, 'set_player_flags', 'Set Player Flags', nodeKey, optIdx); - - html += '</div>'; // .talk-action-block - return html; - } - - function renderKVEditor(action, prefix, key, label, nodeKey, optIdx) { - var kv = (action && action[key]) || {}; - var keys = Object.keys(kv); - var fcID = prefix.replace(/\./g, '_') + '_' + key; - var addKeyID = 'kvAddKey_' + fcID; - var nkAttr = escAttr(nodeKey); - var oiAttr = optIdx !== null ? ' data-oi="' + optIdx + '"' : ''; - var html = '<div class="talk-kv-section">'; - html += '<div class="talk-kv-header">'; - html += '<span class="talk-prop-label">' + esc(label) + '</span>'; - html += '</div>'; - keys.forEach(function(k) { - html += '<div class="talk-kv-row">' + - '<input class="talk-input" data-nk="' + nkAttr + '"' + oiAttr + ' data-kvk="' + escAttr(key) + '" data-oldkey="' + escAttr(k) + '" onchange="talkRenameKV(this)" value="' + escAttr(k) + '" placeholder="key" style="max-width:150px">' + - '<input class="talk-input" data-tp="' + escAttr(prefix + '.' + key + '.' + k) + '" value="' + escAttr(String(kv[k])) + '" placeholder="value">' + - '<button class="talk-kv-del" data-action="remove-kv" data-nk="' + nkAttr + '"' + oiAttr + ' data-kvk="' + escAttr(key) + '" data-sk="' + escAttr(k) + '">x</button>' + - '</div>'; - }); - html += '<div class="talk-kv-row">' + - '<input class="talk-input" id="' + escAttr(addKeyID) + '" placeholder="New key name" style="max-width:150px" data-nk="' + nkAttr + '"' + oiAttr + ' data-kvk="' + escAttr(key) + '">' + - '<input class="talk-input" id="' + escAttr(addKeyID) + '_val" placeholder="value" style="flex:1">' + - '<span style="font-size:9px;color:#888;white-space:nowrap;min-width:60px"></span>' + - '<button class="talk-kv-add" data-action="add-kv" data-nk="' + nkAttr + '"' + oiAttr + ' data-kvk="' + escAttr(key) + '">Add</button>' + - '</div>'; - html += '</div>'; - return html; - } - // ---- Options section ---- function renderOptionsSection(nodeKey, node) { @@ -747,43 +534,42 @@ html += '<div class="talk-opt-edit-actions">'; if (opt.condition && typeof opt.condition === 'object' && !Array.isArray(opt.condition)) { - html += '<span class="talk-opt-tag talk-opt-tag-cond" title="Has condition" onclick="toggleOptionDetail(event,\'cond\',' + idx + ',\'' + escAttr(nodeKey) + '\')">cond</span>'; + html += '<span class="talk-opt-tag talk-opt-tag-cond">cond</span>'; } else { - html += '<button class="talk-add-cond-btn" style="margin:0;margin-bottom:0" data-action="add-condition" data-nk="' + escAttr(nodeKey) + '" data-oi="' + idx + '" title="Add condition">cond</button>'; + html += '<button class="talk-add-cond-btn" style="margin:0;margin-bottom:0" data-action="add-opt-cond" data-nk="' + escAttr(nodeKey) + '" data-oi="' + idx + '" title="Add condition">cond</button>'; } if (opt.action) { - html += '<span class="talk-opt-tag talk-opt-tag-act" title="Has action" onclick="toggleOptionDetail(event,\'act\',' + idx + ',\'' + escAttr(nodeKey) + '\')">act</span>'; + html += '<span class="talk-opt-tag talk-opt-tag-act">act</span>'; } else { - html += '<button class="talk-add-act-btn" style="margin:0;margin-bottom:0" data-action="add-action" data-nk="' + escAttr(nodeKey) + '" data-oi="' + idx + '" title="Add action">act</button>'; + html += '<button class="talk-add-act-btn" style="margin:0;margin-bottom:0" data-action="add-opt-act" data-nk="' + escAttr(nodeKey) + '" data-oi="' + idx + '" title="Add action">act</button>'; } html += '</div>'; html += '<button class="talk-opt-del" data-action="delete-option" data-nk="' + escAttr(nodeKey) + '" data-oi="' + idx + '" title="Delete option">x</button>'; html += '</div>'; + // Option condition detail if (opt.condition && typeof opt.condition === 'object' && !Array.isArray(opt.condition)) { - var condDetailKey = 'cond_' + idx + '_' + escAttr(nodeKey); - var condOpen = window._talkAutoOpenDetails && window._talkAutoOpenDetails.has(condDetailKey) ? 'block' : 'none'; - html += '<div class="talk-opt-detail" id="optDetail_' + condDetailKey + '" style="display:' + condOpen + ';margin-left:18px">'; + html += '<div class="talk-opt-detail" style="display:block;margin-left:18px">'; html += '<div class="talk-cond-card" style="margin-top:2px">'; - html += '<div class="talk-cond-card-header">Condition' + - '<button class="talk-section-remove" data-action="remove-condition" data-nk="' + escAttr(nodeKey) + '" data-oi="' + idx + '">Remove</button></div>'; + html += '<div class="talk-cond-card-header">Condition (for this option to show up)' + + '<button class="talk-section-remove" data-action="remove-opt-cond" data-nk="' + escAttr(nodeKey) + '" data-oi="' + idx + '">Remove</button></div>'; html += '<div class="talk-cond-card-body">'; - html += renderConditionClauses(extractClauses(opt.condition), nodeKey, idx); + html += ie_renderCond(opt.condition, '', 0, '', '', {scopeToken: talkCondScope(nodeKey, idx), alwaysOuterAddbar: true}); html += '</div>'; html += '</div>'; html += '</div>'; } + + // Option action detail if (opt.action) { - var actDetailKey = 'act_' + idx + '_' + escAttr(nodeKey); - var actOpen = window._talkAutoOpenDetails && window._talkAutoOpenDetails.has(actDetailKey) ? 'block' : 'none'; - html += '<div class="talk-opt-detail" id="optDetail_' + actDetailKey + '" style="display:' + actOpen + ';margin-left:18px">'; + html += '<div class="talk-opt-detail" style="display:block;margin-left:18px">'; html += '<div class="talk-cond-card" style="margin-top:2px">'; html += '<div class="talk-cond-card-header">' + - '<span class="talk-prop-label">Action</span>' + - '<button class="talk-section-remove" data-action="remove-action" data-nk="' + escAttr(nodeKey) + '" data-oi="' + idx + '">Remove</button></div>'; + 'Action (when this option is chosen)' + + '<button class="talk-section-remove" data-action="remove-opt-act" data-nk="' + escAttr(nodeKey) + '" data-oi="' + idx + '">Remove</button></div>'; html += '<div class="talk-cond-card-body">'; - html += renderActionForm(opt.action, 'nodes.' + escAttr(nodeKey) + '.options.' + idx + '.action', nodeKey, idx); + html += talkRenderActionStep(nodeKey, idx); html += '</div>'; html += '</div>'; html += '</div>'; @@ -801,16 +587,10 @@ '<button class="talk-add-btn" data-action="add-option" data-nk="' + escAttr(nodeKey) + '">Add</button>' + '</div>'; - html += '</div>'; // .talk-options + html += '</div>'; return html; } - window.toggleOptionDetail = function(e, type, idx, nodeKey) { - e.stopPropagation(); - var el = document.getElementById('optDetail_' + type + '_' + idx + '_' + nodeKey); - if (el) el.style.display = el.style.display === 'none' ? 'block' : 'none'; - }; - window.talkToggleNode = function(nodeKey, nodeEl) { var open = nodeEl.classList.toggle('open'); if (open) { @@ -867,98 +647,6 @@ talkSync(); } - // ---- Add/Remove Action ---- - - function addAction(nodeKey, optIdx) { - var target = getConditionFor(nodeKey, optIdx); - if (!target) return; - target.action = {}; - if (optIdx !== null) { - window._talkAutoOpenDetails = new Set(); - window._talkAutoOpenDetails.add('act_' + optIdx + '_' + escAttr(nodeKey)); - } - renderTalkTree(window._talkData); - talkSync(); - } - - function removeAction(nodeKey, optIdx) { - var target = getConditionFor(nodeKey, optIdx); - if (!target) return; - delete target.action; - renderTalkTree(window._talkData); - talkSync(); - } - - // ---- Add/Remove KV Pairs (set_flags/set_player_flags) ---- - - function kvPath(nodeKey, optIdx, kvKey, flagKey) { - var base = optIdx !== null ? - 'nodes.' + nodeKey + '.options.' + optIdx + '.action.' + kvKey : - 'nodes.' + nodeKey + '.action.' + kvKey; - return flagKey ? base + '.' + flagKey : base; - } - - function addKVPair(nodeKey, optIdx, kvKey) { - var prefixBase = optIdx !== null ? - 'nodes.' + nodeKey + '.options.' + optIdx + '.action' : - 'nodes.' + nodeKey + '.action'; - var fcID = prefixBase.replace(/\./g, '_') + '_' + kvKey; - var addKeyID = 'kvAddKey_' + fcID; - var input = document.getElementById(addKeyID); - var key = input ? input.value.trim() : ''; - if (!key) return; - var valInput = document.getElementById(addKeyID + '_val'); - var rawVal = valInput ? valInput.value.trim() : 'true'; - var val = rawVal === 'true' ? true : rawVal === 'false' ? false : rawVal; - var numVal = parseFloat(rawVal); - if (!isNaN(numVal) && String(numVal) === rawVal) val = numVal; - var basePath = kvPath(nodeKey, optIdx, kvKey); - var obj = talkGet(basePath); - if (!obj || typeof obj !== 'object') { - talkSet(basePath, {}); - obj = talkGet(basePath); - } - if (obj[key] !== undefined) { notify('Flag "' + key + '" already exists.', 'error'); return; } - obj[key] = val; - renderTalkTree(window._talkData); - talkSync(); - } - - function removeKVPair(nodeKey, optIdx, kvKey, flagKey) { - var fullPath = kvPath(nodeKey, optIdx, kvKey, flagKey); - talkDel(fullPath); - var basePath = kvPath(nodeKey, optIdx, kvKey); - var obj = talkGet(basePath); - if (obj && typeof obj === 'object' && Object.keys(obj).length === 0) { - talkDel(basePath); - } - renderTalkTree(window._talkData); - talkSync(); - } - - window.talkRenameKV = function(el) { - var nodeKey = el.getAttribute('data-nk'); - var optIdxStr = el.getAttribute('data-oi'); - var optIdx = optIdxStr !== null ? parseInt(optIdxStr, 10) : null; - var kvKey = el.getAttribute('data-kvk'); - var oldKey = el.getAttribute('data-oldkey'); - var newKey = el.value.trim(); - if (!newKey || newKey === oldKey) return; - var basePath = optIdx !== null ? - 'nodes.' + nodeKey + '.options.' + optIdx + '.action.' + kvKey : - 'nodes.' + nodeKey + '.action.' + kvKey; - var obj = talkGet(basePath); - if (!obj || obj[newKey] !== undefined) { - el.value = oldKey; - if (obj && obj[newKey] !== undefined) notify('Key "' + newKey + '" already exists.', 'error'); - return; - } - obj[newKey] = obj[oldKey]; - delete obj[oldKey]; - renderTalkTree(window._talkData); - talkSync(); - }; - // ---- Global event listeners ---- document.addEventListener('input', function(e) { @@ -1004,15 +692,6 @@ addMessageLine(nk); return; } - - if (el.id && el.id.startsWith('kvAddKey_')) { - e.preventDefault(); - var nk = el.getAttribute('data-nk'); - var oi = el.getAttribute('data-oi'); - var kvk = el.getAttribute('data-kvk'); - addKVPair(nk, oi !== null ? parseInt(oi, 10) : null, kvk); - return; - } }); })(); |
