Initial commit: unified dashboard suite state with Khira and Image Manipulator 3-file setup
This commit is contained in:
@@ -0,0 +1,663 @@
|
||||
// ── Per-Field Toggle Bulk Edit Modal ──
|
||||
function showBulkEditFieldModal(path, colsStr) {
|
||||
const cols = colsStr.split(",");
|
||||
const modes = [
|
||||
{ v: "set", l: "Set Value" }, { v: "pattern", l: "Pattern {i}" },
|
||||
{ v: "sequence", l: "Sequence" }, { v: "find_replace", l: "Find & Replace" },
|
||||
{ v: "regex", l: "Regex" }, { v: "increment", l: "Increment" },
|
||||
{ v: "prefix_suffix", l: "Prefix/Suffix" }, { v: "conditional", l: "Conditional" }
|
||||
];
|
||||
const modeOpts = modes.map(m => `<option value="${m.v}">${m.l}</option>`).join("");
|
||||
const colOpts = cols.map(c => `<option value="${c}">${c}</option>`).join("");
|
||||
|
||||
let fieldsHtml = cols.map(c => `
|
||||
<div class="field-config" id="fc_${c}" data-field="${c}">
|
||||
<div class="field-config-header">
|
||||
<label class="toggle-switch">
|
||||
<input type="checkbox" onchange="toggleFieldConfig('${c}',this.checked)"/>
|
||||
<span class="toggle-slider"></span>
|
||||
</label>
|
||||
<span class="field-name">${c}</span>
|
||||
<span class="field-badge">unchanged</span>
|
||||
</div>
|
||||
<div class="field-config-body">
|
||||
<div style="display:grid;grid-template-columns:1fr 1fr;gap:8px">
|
||||
<div><label class="label-sm">Mode</label><select class="input input-sm" id="fc_mode_${c}" onchange="updateFieldModeUI('${c}')">${modeOpts}</select></div>
|
||||
<div><label class="label-sm">Type</label><select class="input input-sm" id="fc_type_${c}"><option value="string">String</option><option value="number">Number</option></select></div>
|
||||
</div>
|
||||
<div id="fc_opts_${c}" class="mt-sm">
|
||||
<div id="fc_set_${c}"><label class="label-sm mt-sm">Value</label><input class="input input-sm" id="fc_val_${c}" placeholder="Enter value..."></div>
|
||||
<div id="fc_pattern_${c}" style="display:none"><label class="label-sm mt-sm">Pattern</label><input class="input input-sm" id="fc_pat_${c}" placeholder="e.g. image_{i}.webp">
|
||||
<label class="label-sm mt-sm">Start {i} at</label><input class="input input-sm" id="fc_patstart_${c}" type="number" value="1"></div>
|
||||
<div id="fc_sequence_${c}" style="display:none"><label class="label-sm mt-sm">Start #</label><input class="input input-sm" id="fc_seqstart_${c}" type="number" value="1"></div>
|
||||
<div id="fc_find_replace_${c}" style="display:none"><label class="label-sm mt-sm">Find</label><input class="input input-sm" id="fc_find_${c}"><label class="label-sm mt-sm">Replace</label><input class="input input-sm" id="fc_repl_${c}"></div>
|
||||
<div id="fc_regex_${c}" style="display:none"><label class="label-sm mt-sm">Regex</label><input class="input input-sm font-mono" id="fc_rxfind_${c}"><label class="label-sm mt-sm">Replace</label><input class="input input-sm font-mono" id="fc_rxrepl_${c}"><label class="label-sm mt-sm">Start {i}</label><input class="input input-sm" id="fc_rxstart_${c}" type="number" value="1"></div>
|
||||
<div id="fc_increment_${c}" style="display:none"><label class="label-sm mt-sm">Increment By</label><input class="input input-sm" id="fc_inc_${c}" type="number" value="1"></div>
|
||||
<div id="fc_prefix_suffix_${c}" style="display:none"><label class="label-sm mt-sm">Prefix</label><input class="input input-sm" id="fc_pfx_${c}"><label class="label-sm mt-sm">Suffix</label><input class="input input-sm" id="fc_sfx_${c}"></div>
|
||||
<div id="fc_conditional_${c}" style="display:none">
|
||||
<div style="padding:8px;background:var(--bg-input);border-radius:6px;margin-top:6px">
|
||||
<label class="label-sm" style="color:var(--warning)">IF condition:</label>
|
||||
<div style="display:grid;grid-template-columns:1fr auto 1fr;gap:4px;margin-top:4px">
|
||||
<select class="input input-sm" id="fc_condfield_${c}">${colOpts}</select>
|
||||
<select class="input input-sm" id="fc_condop_${c}"><option value="equals">equals</option><option value="not_equals">≠</option><option value="contains">contains</option><option value="starts_with">starts</option><option value="ends_with">ends</option></select>
|
||||
<input class="input input-sm" id="fc_condval_${c}" placeholder="value">
|
||||
</div>
|
||||
</div>
|
||||
<label class="label-sm mt-sm">THEN set to</label><input class="input input-sm" id="fc_condnew_${c}" placeholder="e.g. premium_{i}">
|
||||
<label class="label-sm mt-sm">Start {i}</label><input class="input input-sm" id="fc_condstart_${c}" type="number" value="1">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>`).join("");
|
||||
|
||||
const overlay = document.createElement("div");
|
||||
overlay.className = "modal-overlay";
|
||||
overlay.innerHTML = `<div class="modal-box modal-wide"><h3><i class="fa-solid fa-bolt" style="color:var(--accent)"></i> Per-Field Bulk Edit</h3>
|
||||
<div style="padding:8px 12px;background:var(--bg-card);border-radius:var(--radius-sm);margin-bottom:12px;font-size:11px;color:var(--text-muted)">
|
||||
<i class="fa-solid fa-info-circle text-info"></i> Toggle switches ON for fields you want to edit. Each field gets its own mode and settings.
|
||||
<br><strong class="text-accent">Placeholders:</strong> <code class="font-mono text-accent">{i}</code> counter · <code class="font-mono text-accent">{idx}</code> index · <code class="font-mono text-accent">{total}</code> total · <code class="font-mono text-accent">{value}</code> current · <code class="font-mono text-accent">{field_name}</code> other field
|
||||
</div>
|
||||
<div id="fieldConfigList">${fieldsHtml}</div>
|
||||
<div class="modal-actions"><button class="btn btn-ghost" id="mdCancel">Cancel</button><button class="btn btn-primary" id="mdConfirm"><i class="fa-solid fa-check"></i> Apply Changes</button></div></div>`;
|
||||
document.body.appendChild(overlay);
|
||||
overlay.querySelector("#mdCancel").onclick = () => overlay.remove();
|
||||
overlay.addEventListener("click", e => { if (e.target === overlay) overlay.remove(); });
|
||||
overlay.querySelector("#mdConfirm").onclick = async () => {
|
||||
const fields = {};
|
||||
cols.forEach(c => {
|
||||
const el = document.getElementById("fc_" + c);
|
||||
const chk = el.querySelector("input[type=checkbox]");
|
||||
if (!chk.checked) return;
|
||||
const mode = document.getElementById("fc_mode_" + c).value;
|
||||
const cfg = { enabled: true, mode, value_type: document.getElementById("fc_type_" + c).value };
|
||||
if (mode === "set") cfg.new_value = document.getElementById("fc_val_" + c).value;
|
||||
else if (mode === "pattern") { cfg.new_value = document.getElementById("fc_pat_" + c).value; cfg.start_counter = document.getElementById("fc_patstart_" + c).value; }
|
||||
else if (mode === "sequence") cfg.start_counter = document.getElementById("fc_seqstart_" + c).value;
|
||||
else if (mode === "find_replace") { cfg.find_text = document.getElementById("fc_find_" + c).value; cfg.replace_text = document.getElementById("fc_repl_" + c).value; }
|
||||
else if (mode === "regex") { cfg.find_text = document.getElementById("fc_rxfind_" + c).value; cfg.replace_text = document.getElementById("fc_rxrepl_" + c).value; cfg.start_counter = document.getElementById("fc_rxstart_" + c).value; }
|
||||
else if (mode === "increment") cfg.increment_by = document.getElementById("fc_inc_" + c).value;
|
||||
else if (mode === "prefix_suffix") { cfg.prefix = document.getElementById("fc_pfx_" + c).value; cfg.suffix = document.getElementById("fc_sfx_" + c).value; }
|
||||
else if (mode === "conditional") { cfg.condition_field = document.getElementById("fc_condfield_" + c).value; cfg.condition_op = document.getElementById("fc_condop_" + c).value; cfg.condition_value = document.getElementById("fc_condval_" + c).value; cfg.new_value = document.getElementById("fc_condnew_" + c).value; cfg.start_counter = document.getElementById("fc_condstart_" + c).value; }
|
||||
fields[c] = cfg;
|
||||
});
|
||||
if (Object.keys(fields).length === 0) { toast("Enable at least one field", "error"); return; }
|
||||
const r = await api("/bulk/edit_multi_field", { path, fields });
|
||||
if (r.success) { loadNode(path); toast(`Edited ${r.modified} items`); overlay.remove(); }
|
||||
else toast(r.error, "error");
|
||||
};
|
||||
}
|
||||
|
||||
function toggleFieldConfig(field, checked) {
|
||||
const el = document.getElementById("fc_" + field);
|
||||
if (checked) { el.classList.add("active"); el.querySelector(".field-badge").textContent = "editing"; el.querySelector(".field-badge").style.color = "var(--success)"; }
|
||||
else { el.classList.remove("active"); el.querySelector(".field-badge").textContent = "unchanged"; el.querySelector(".field-badge").style.color = ""; }
|
||||
}
|
||||
|
||||
function updateFieldModeUI(field) {
|
||||
const mode = document.getElementById("fc_mode_" + field).value;
|
||||
const allModes = ["set", "pattern", "sequence", "find_replace", "regex", "increment", "prefix_suffix", "conditional"];
|
||||
allModes.forEach(m => { const el = document.getElementById("fc_" + m + "_" + field); if (el) el.style.display = m === mode ? "block" : "none"; });
|
||||
}
|
||||
|
||||
// ── Edit Item Modal ──
|
||||
function editItemModal(path, idx) {
|
||||
api("/node/get", { path }).then(data => {
|
||||
if (!data.items) return;
|
||||
const item = data.items[idx], cols = data.columns || Object.keys(item);
|
||||
let fields = cols.map(c => `<label class="label mt-sm">${c}</label><input class="input" id="mdEI_${c}" value="${item[c] !== null && item[c] !== undefined ? String(item[c]).replace(/"/g, """) : ""}">`).join("");
|
||||
showModal(`Edit Item #${idx}`, fields, async () => {
|
||||
const newItem = {}; cols.forEach(c => newItem[c] = document.getElementById(`mdEI_${c}`).value);
|
||||
const r = await api("/node/edit_item", { path, index: idx, item: newItem });
|
||||
if (r.success) { loadNode(path); toast("Updated"); } else toast(r.error, "error");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// ── Multi-Category Add Field + Append Modal ──
|
||||
let _mcFieldCounter = 0;
|
||||
async function showMultiCatAddFieldModal() {
|
||||
const discovery = await api("/discover_arrays", {});
|
||||
if (!discovery.success) { toast("No data loaded", "error"); return; }
|
||||
const catTree = discovery.category_tree || [];
|
||||
const allArrays = discovery.arrays || [];
|
||||
if (allArrays.length === 0) { toast("No arrays found in JSON", "error"); return; }
|
||||
_mcFieldCounter = 0;
|
||||
|
||||
function buildCatTreeHtml(nodes, indent) {
|
||||
let h = "";
|
||||
for (const n of nodes) {
|
||||
const isArr = n.is_array === true;
|
||||
const pad = indent * 18;
|
||||
const icon = isArr ? "fa-layer-group text-info" : "fa-folder text-warning";
|
||||
const badge = isArr ? `<span class="cat-count">${n.count}</span>` : "";
|
||||
const fields = (n.sample_fields || []).length ? `<span style="font-size:9px;color:var(--text-dim);margin-left:4px">[${n.sample_fields.slice(0,3).join(", ")}${n.sample_fields.length>3?"...":""}]</span>` : "";
|
||||
|
||||
if (isArr) {
|
||||
h += `<div class="cat-tree-item" style="margin-left:${pad}px">
|
||||
<input type="checkbox" class="mc-target" value="${n.path}" data-fields='${JSON.stringify(n.sample_fields||[])}'>
|
||||
<i class="fa-solid ${icon}" style="font-size:11px;width:14px"></i>
|
||||
<span style="flex:1">${n.key}</span>${badge}${fields}</div>`;
|
||||
} else {
|
||||
h += `<div class="cat-tree-item" style="margin-left:${pad}px;font-weight:700;color:var(--text-primary)">
|
||||
<input type="checkbox" class="mc-cat-toggle" onchange="toggleCatChildren(this)">
|
||||
<i class="fa-solid ${icon}" style="font-size:11px;width:14px"></i>
|
||||
<span style="flex:1">${n.key}</span></div>`;
|
||||
}
|
||||
if (n.children && n.children.length > 0) {
|
||||
h += `<div class="mc-cat-group">${buildCatTreeHtml(n.children, indent + 1)}</div>`;
|
||||
}
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
const treeHtml = buildCatTreeHtml(catTree, 0);
|
||||
|
||||
const overlay = document.createElement("div");
|
||||
overlay.className = "modal-overlay";
|
||||
overlay.innerHTML = `<div class="modal-box modal-wide" style="width:860px;max-width:96vw">
|
||||
<h3><i class="fa-solid fa-wand-magic-sparkles" style="color:var(--accent)"></i> Multi-Category Add Field + Append</h3>
|
||||
<div style="padding:8px 12px;background:var(--bg-card);border-radius:var(--radius-sm);margin-bottom:12px;font-size:11px;color:var(--text-muted)">
|
||||
<i class="fa-solid fa-info-circle text-info"></i> Select <strong>categories/subcategories</strong> to target, define fields to add/modify, then optionally append new items.
|
||||
<br><strong class="text-accent">Placeholders:</strong> <code class="font-mono text-accent">{i}</code> counter · <code class="font-mono text-accent">{idx}</code> index · <code class="font-mono text-accent">{total}</code> total
|
||||
</div>
|
||||
|
||||
<div style="display:grid;grid-template-columns:280px 1fr;gap:14px">
|
||||
<!-- Left: Category Tree -->
|
||||
<div>
|
||||
<div class="section-title"><i class="fa-solid fa-sitemap"></i> TARGET CATEGORIES</div>
|
||||
<div class="card" style="margin:0;max-height:380px;overflow-y:auto;padding:8px">
|
||||
<div class="cat-tree-item" style="border-bottom:1px solid var(--border);padding-bottom:6px;margin-bottom:4px">
|
||||
<input type="checkbox" id="mcSelAll" onchange="document.querySelectorAll('.mc-target,.mc-cat-toggle').forEach(c=>c.checked=this.checked);document.querySelectorAll('.mc-cat-toggle').forEach(c=>toggleCatChildren(c))">
|
||||
<strong>Select All</strong><span class="cat-count">${allArrays.length} arrays</span>
|
||||
</div>
|
||||
${treeHtml}
|
||||
</div>
|
||||
<div style="font-size:10px;color:var(--text-dim);margin-top:4px"><span id="mcSelectedCount">0</span> arrays selected</div>
|
||||
</div>
|
||||
|
||||
<!-- Right: Field Definitions -->
|
||||
<div>
|
||||
<div class="section-title"><i class="fa-solid fa-columns"></i> FIELDS TO ADD/MODIFY</div>
|
||||
<div id="mcFieldList" style="max-height:250px;overflow-y:auto"></div>
|
||||
<button class="btn btn-ghost btn-sm mt-sm" onclick="addMcField()"><i class="fa-solid fa-plus"></i> Add Field</button>
|
||||
|
||||
<div style="margin-top:14px;border-top:1px solid var(--border);padding-top:12px">
|
||||
<div class="section-title"><i class="fa-solid fa-plus-circle"></i> ALSO APPEND NEW ITEMS (optional)</div>
|
||||
<div style="display:flex;gap:10px;align-items:center">
|
||||
<label class="toggle-switch">
|
||||
<input type="checkbox" id="mcAppendToggle">
|
||||
<span class="toggle-slider"></span>
|
||||
</label>
|
||||
<span style="font-size:12px;font-weight:600">Append new items to each target</span>
|
||||
</div>
|
||||
<div id="mcAppendOpts" style="display:none;margin-top:8px">
|
||||
<label class="label-sm">Number of items to append per target</label>
|
||||
<input class="input input-sm" type="number" id="mcAppendCount" value="10" style="width:100px">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="modal-actions">
|
||||
<button class="btn btn-ghost" id="mcCancel">Cancel</button>
|
||||
<button class="btn btn-primary" id="mcApply"><i class="fa-solid fa-check"></i> Apply to Selected Targets</button>
|
||||
</div>
|
||||
</div>`;
|
||||
document.body.appendChild(overlay);
|
||||
|
||||
overlay.querySelector("#mcAppendToggle").onchange = function() {
|
||||
overlay.querySelector("#mcAppendOpts").style.display = this.checked ? "block" : "none";
|
||||
};
|
||||
|
||||
overlay.addEventListener("change", () => {
|
||||
const count = overlay.querySelectorAll(".mc-target:checked").length;
|
||||
overlay.querySelector("#mcSelectedCount").textContent = count;
|
||||
});
|
||||
|
||||
addMcField();
|
||||
|
||||
overlay.querySelector("#mcCancel").onclick = () => overlay.remove();
|
||||
overlay.addEventListener("click", e => { if (e.target === overlay) overlay.remove(); });
|
||||
|
||||
overlay.querySelector("#mcApply").onclick = async () => {
|
||||
const targetPaths = [];
|
||||
overlay.querySelectorAll(".mc-target:checked").forEach(cb => targetPaths.push(cb.value));
|
||||
if (targetPaths.length === 0) { toast("Select at least one target array", "error"); return; }
|
||||
|
||||
const fieldRows = document.querySelectorAll(".mc-field-row");
|
||||
const fieldDefs = [];
|
||||
for (const row of fieldRows) {
|
||||
const name = row.querySelector(".mc-f-name").value.trim();
|
||||
if (!name) continue;
|
||||
const distMode = row.querySelector(".mc-f-dist").value;
|
||||
const def = {
|
||||
name,
|
||||
type: row.querySelector(".mc-f-type").value,
|
||||
value: row.querySelector(".mc-f-val").value,
|
||||
use_pattern: row.querySelector(".mc-f-pattern").checked,
|
||||
start: parseInt(row.querySelector(".mc-f-start").value || 1),
|
||||
dist_mode: distMode
|
||||
};
|
||||
if (distMode === "random") {
|
||||
def.dist_rand_primary = row.querySelector(".mc-f-rand-prim").value;
|
||||
def.dist_rand_secondary = row.querySelector(".mc-f-rand-sec").value;
|
||||
def.dist_rand_pct = parseInt(row.querySelector(".mc-f-rand-pct").value || 50);
|
||||
} else if (distMode === "cyclic") {
|
||||
def.dist_pattern = row.querySelector(".mc-f-cyclic-pat").value;
|
||||
}
|
||||
fieldDefs.push(def);
|
||||
}
|
||||
if (fieldDefs.length === 0) { toast("Add at least one field", "error"); return; }
|
||||
|
||||
const targets = targetPaths.map(p => ({ path: p, fields: fieldDefs }));
|
||||
const body = { targets };
|
||||
if (document.getElementById("mcAppendToggle").checked) {
|
||||
body.append_items = true;
|
||||
body.append_count = parseInt(document.getElementById("mcAppendCount").value || 0);
|
||||
}
|
||||
|
||||
const r = await api("/bulk/add_field_multi", body);
|
||||
if (r.success) {
|
||||
toast(`Modified ${r.modified} fields across ${r.targets} targets` + (r.appended > 0 ? `, appended ${r.appended} items` : ""));
|
||||
refreshTree(r.tree);
|
||||
if (currentPath) loadNode(currentPath);
|
||||
overlay.remove();
|
||||
} else toast(r.error, "error");
|
||||
};
|
||||
}
|
||||
|
||||
function addMcField() {
|
||||
_mcFieldCounter++;
|
||||
const id = _mcFieldCounter;
|
||||
const row = document.createElement("div");
|
||||
row.className = "mc-field-row field-config active";
|
||||
row.style.cssText = "padding:8px 10px;margin-bottom:6px";
|
||||
row.innerHTML = `
|
||||
<div style="display:grid;grid-template-columns:1fr 80px 1fr 40px 60px 90px 28px;gap:6px;align-items:end">
|
||||
<div><label class="label-sm">Field Name</label><input class="input input-sm mc-f-name" placeholder="e.g. thumbnail"></div>
|
||||
<div><label class="label-sm">Type</label><select class="input input-sm mc-f-type"><option value="string">String</option><option value="number">Number</option><option value="boolean">Bool</option></select></div>
|
||||
<div><label class="label-sm">Value / Pattern</label><input class="input input-sm mc-f-val" placeholder="e.g. thumb_{i}.webp"></div>
|
||||
<div style="text-align:center"><label class="label-sm" title="Use pattern placeholders like {i}">{i}?</label><input type="checkbox" class="mc-f-pattern" checked style="margin-top:4px;accent-color:var(--accent)"></div>
|
||||
<div><label class="label-sm">Start #</label><input class="input input-sm mc-f-start" type="number" value="1"></div>
|
||||
<div><label class="label-sm">Dist</label><select class="input input-sm mc-f-dist" onchange="toggleMcFieldDist(this)"><option value="static">Static</option><option value="random">Random %</option><option value="cyclic">Cyclic</option></select></div>
|
||||
<div><button class="btn-icon btn-sm" onclick="this.closest('.mc-field-row').remove()" title="Remove"><i class="fa-solid fa-times" style="color:var(--danger)"></i></button></div>
|
||||
</div>
|
||||
<div class="mc-f-dist-opts" style="display:none;margin-top:6px;padding:6px 8px;background:var(--bg-input);border-radius:6px">
|
||||
<div class="mc-f-random-opts" style="display:none">
|
||||
<div style="display:grid;grid-template-columns:1fr 60px 1fr;gap:6px;align-items:end">
|
||||
<div><label class="label-sm">Primary</label><input class="input input-sm mc-f-rand-prim" value="free" placeholder="e.g. free"></div>
|
||||
<div><label class="label-sm">%</label><input class="input input-sm mc-f-rand-pct" type="number" value="70" style="text-align:center"></div>
|
||||
<div><label class="label-sm">Secondary</label><input class="input input-sm mc-f-rand-sec" value="paid" placeholder="e.g. paid"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mc-f-cyclic-opts" style="display:none">
|
||||
<label class="label-sm">Cyclic Pattern (comma-separated)</label>
|
||||
<input class="input input-sm mc-f-cyclic-pat" placeholder="free, free, paid" value="">
|
||||
</div>
|
||||
</div>`;
|
||||
document.getElementById("mcFieldList").appendChild(row);
|
||||
}
|
||||
|
||||
function toggleMcFieldDist(sel) {
|
||||
const row = sel.closest(".mc-field-row");
|
||||
const mode = sel.value;
|
||||
const optsWrap = row.querySelector(".mc-f-dist-opts");
|
||||
const randomOpts = row.querySelector(".mc-f-random-opts");
|
||||
const cyclicOpts = row.querySelector(".mc-f-cyclic-opts");
|
||||
|
||||
if (mode === "static") {
|
||||
optsWrap.style.display = "none";
|
||||
} else {
|
||||
optsWrap.style.display = "block";
|
||||
randomOpts.style.display = mode === "random" ? "block" : "none";
|
||||
cyclicOpts.style.display = mode === "cyclic" ? "block" : "none";
|
||||
}
|
||||
// When using distribution, disable pattern checkbox and value field
|
||||
const patternCb = row.querySelector(".mc-f-pattern");
|
||||
const valInput = row.querySelector(".mc-f-val");
|
||||
if (mode !== "static") {
|
||||
patternCb.checked = false;
|
||||
patternCb.disabled = true;
|
||||
valInput.disabled = true;
|
||||
valInput.style.opacity = "0.4";
|
||||
} else {
|
||||
patternCb.disabled = false;
|
||||
valInput.disabled = false;
|
||||
valInput.style.opacity = "1";
|
||||
}
|
||||
}
|
||||
|
||||
function toggleCatChildren(checkbox) {
|
||||
const group = checkbox.closest(".cat-tree-item").nextElementSibling;
|
||||
if (group && group.classList.contains("mc-cat-group")) {
|
||||
group.querySelectorAll(".mc-target").forEach(cb => cb.checked = checkbox.checked);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Multi-Category Append Panel ──
|
||||
let _apFieldCounter = 0;
|
||||
async function showAppendPanel(path) {
|
||||
const anal = await api("/analyze_category", { path });
|
||||
if (!anal.success) return;
|
||||
const fields = anal.fields, samples = anal.samples, inappOpts = anal.inapp_options;
|
||||
|
||||
const discovery = await api("/discover_arrays", {});
|
||||
const allArrays = discovery.success ? discovery.arrays : [];
|
||||
const catTree = discovery.success ? (discovery.category_tree || []) : [];
|
||||
_apFieldCounter = 0;
|
||||
|
||||
const wrap = document.getElementById("appendPanelWrap") || document.getElementById("editorArea");
|
||||
|
||||
// Build field rows HTML with per-field distribution
|
||||
let fHtml = "";
|
||||
fields.forEach((f, fi) => {
|
||||
_apFieldCounter++;
|
||||
const fid = _apFieldCounter;
|
||||
let v = samples[f] || "";
|
||||
if (f === "coordinates") v = "{line}";
|
||||
else if (typeof v === "string" && v.includes("http")) v = v.replace(/(\d+)(?=\.\w+$)/, "{i}");
|
||||
fHtml += _buildAppendFieldRow(fid, f, v, false);
|
||||
});
|
||||
|
||||
// Build hierarchical category tree for multi-target
|
||||
let catHtml = "";
|
||||
if (allArrays.length > 1) {
|
||||
function buildAppendTree(nodes, indent) {
|
||||
let h = "";
|
||||
for (const n of nodes) {
|
||||
const pad = indent * 16;
|
||||
if (n.is_array) {
|
||||
h += `<div class="cat-tree-item" style="margin-left:${pad}px">
|
||||
<input type="checkbox" class="ap-target" value="${n.path}" ${n.path === path ? "checked" : ""}>
|
||||
<i class="fa-solid fa-layer-group text-info" style="font-size:10px;width:14px"></i>
|
||||
<span>${n.key}</span><span class="cat-count">${n.count}</span></div>`;
|
||||
} else {
|
||||
h += `<div class="cat-tree-item" style="margin-left:${pad}px;font-weight:700;color:var(--text-primary)">
|
||||
<input type="checkbox" class="ap-cat-toggle" onchange="let g=this.closest('.cat-tree-item').nextElementSibling;if(g)g.querySelectorAll('.ap-target').forEach(c=>c.checked=this.checked)">
|
||||
<i class="fa-solid fa-folder text-warning" style="font-size:10px;width:14px"></i>
|
||||
<span>${n.key}</span></div>`;
|
||||
}
|
||||
if (n.children && n.children.length) {
|
||||
h += `<div class="ap-cat-group">${buildAppendTree(n.children, indent + 1)}</div>`;
|
||||
}
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
catHtml = `<div class="card" style="margin:0"><div class="section-title"><i class="fa-solid fa-sitemap"></i> TARGET ARRAYS (multi-select)</div>
|
||||
<div class="cat-tree" id="apCatTree">
|
||||
<div class="cat-tree-item" style="border-bottom:1px solid var(--border);padding-bottom:4px;margin-bottom:4px">
|
||||
<input type="checkbox" id="apSelAll" onchange="toggleAllTargets(this.checked)"><strong>Select All</strong><span class="cat-count">${allArrays.length}</span>
|
||||
</div>
|
||||
${buildAppendTree(catTree, 0)}
|
||||
</div></div>`;
|
||||
}
|
||||
|
||||
let panelHtml = `<div class="card slide-up" id="appendCard"><div class="card-header"><span class="card-title"><i class="fa-solid fa-bolt"></i> Bulk Append to ${path.split("/").pop()}</span>
|
||||
<button class="btn-icon btn-sm" onclick="document.getElementById('appendCard').remove()"><i class="fa-solid fa-times"></i></button></div>
|
||||
<div class="append-panel">
|
||||
<div class="card" style="margin:0"><div class="section-title"><i class="fa-solid fa-file-lines"></i> DATA FILE (optional)</div>
|
||||
<input type="file" class="input input-sm" id="apDataFile"><div id="apFileStatus" style="color:var(--success);font-size:11px;margin-top:4px"></div>
|
||||
<div class="section-title mt-md"><i class="fa-solid fa-sliders"></i> COUNT</div>
|
||||
<div style="display:flex;gap:8px"><div><label class="label-sm">Manual Count</label><input class="input input-sm" type="number" id="apManualCount" value="10"></div>
|
||||
<div><label class="label-sm">Limit</label><input class="input input-sm" type="number" id="apLimit" value="0" placeholder="0=all"></div></div>
|
||||
<div style="text-align:center;font-size:12px;margin-top:6px;color:var(--info)">Generating <strong id="apTotal">0</strong> items</div>
|
||||
</div>
|
||||
<div style="display:flex;flex-direction:column;gap:14px">
|
||||
${catHtml}
|
||||
<div class="card" style="margin:0"><div class="section-title"><i class="fa-solid fa-wand-magic-sparkles"></i> FIELD PATTERNS <span style="font-size:9px;color:var(--text-dim);font-weight:400">— per-field distribution supported</span></div>
|
||||
<div style="padding:4px 8px;background:var(--bg-card);border-radius:var(--radius-sm);margin-bottom:8px;font-size:10px;color:var(--text-muted)">
|
||||
Each field can be: <strong>Static</strong> (pattern with {i}), <strong>Random %</strong> (e.g. 70% free / 30% paid), or <strong>Cyclic</strong> (e.g. free, free, paid).
|
||||
</div>
|
||||
<div id="apFieldsContainer">${fHtml}</div>
|
||||
<button class="btn btn-ghost btn-sm mt-sm" onclick="addAppendField()"><i class="fa-solid fa-plus"></i> Add Column</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="full-width" style="display:flex;gap:8px"><button class="btn btn-primary" style="flex:1" onclick="doPreviewAppend('${path}')"><i class="fa-solid fa-eye"></i> Preview</button></div>
|
||||
<div class="full-width" id="apPreviewWrap" style="display:none"><div class="preview-box"><pre id="apPreviewJSON"></pre></div>
|
||||
<div style="display:flex;gap:8px;margin-top:8px"><button class="btn btn-success" style="flex:1" onclick="doCommitAppend('${path}')"><i class="fa-solid fa-check"></i> Append <span id="apPreviewCount"></span> items</button></div></div>
|
||||
</div></div>`;
|
||||
|
||||
if (document.getElementById("appendPanelWrap")) document.getElementById("appendPanelWrap").innerHTML = panelHtml;
|
||||
else wrap.innerHTML += panelHtml;
|
||||
|
||||
// Events
|
||||
document.getElementById("apDataFile").addEventListener("change", e => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = ev => {
|
||||
const lines = ev.target.result.split("\n").filter(l => l.trim() && !l.startsWith("#"));
|
||||
document.getElementById("apFileStatus").textContent = lines.length + " lines found";
|
||||
document.getElementById("apManualCount").value = lines.length;
|
||||
updateAppendTotal();
|
||||
};
|
||||
reader.readAsText(e.target.files[0]);
|
||||
});
|
||||
|
||||
const updateAppendTotal = () => {
|
||||
const mc = parseInt(document.getElementById("apManualCount").value || 0);
|
||||
const lim = parseInt(document.getElementById("apLimit").value || 0);
|
||||
document.getElementById("apTotal").textContent = lim > 0 ? Math.min(mc, lim) : mc;
|
||||
};
|
||||
document.getElementById("apManualCount").oninput = updateAppendTotal;
|
||||
document.getElementById("apLimit").oninput = updateAppendTotal;
|
||||
updateAppendTotal();
|
||||
}
|
||||
|
||||
function _buildAppendFieldRow(fid, fieldName, defaultPattern, isNew) {
|
||||
return `<div class="ap-field-row" data-field="${fieldName}" data-fid="${fid}" style="display:flex;flex-wrap:wrap;gap:6px;align-items:end;padding:6px 0;border-bottom:1px solid var(--border)">
|
||||
<span style="min-width:90px;font-weight:600;font-size:11px;color:var(--accent);font-family:'JetBrains Mono',monospace;align-self:center">${isNew ? `<input class="input input-sm ap-f-name" value="${fieldName}" placeholder="field_name" style="width:80px;font-weight:600;color:var(--accent)">` : fieldName}</span>
|
||||
<input class="input input-sm ap-f-pattern" name="pattern_${fieldName}" value="${defaultPattern}" style="flex:1;min-width:120px" placeholder="Pattern or value...">
|
||||
<input class="input input-sm ap-f-start" name="start_${fieldName}" type="number" value="1" style="width:55px;text-align:center">
|
||||
<select class="input input-sm ap-f-dist" style="width:90px" onchange="toggleApFieldDist(this)">
|
||||
<option value="static">Static</option>
|
||||
<option value="random">Random %</option>
|
||||
<option value="cyclic">Cyclic</option>
|
||||
</select>
|
||||
<button class="btn-icon btn-sm" onclick="this.closest('.ap-field-row').remove()" title="Remove"><i class="fa-solid fa-times" style="color:var(--danger)"></i></button>
|
||||
<div class="ap-f-dist-wrap" style="display:none;width:100%;padding:6px 8px;background:var(--bg-input);border-radius:6px;margin-top:2px">
|
||||
<div class="ap-f-random-opts" style="display:none">
|
||||
<div style="display:grid;grid-template-columns:1fr 55px 1fr;gap:6px;align-items:end">
|
||||
<div><label class="label-sm">Primary</label><input class="input input-sm ap-f-rand-prim" value="free" placeholder="e.g. free"></div>
|
||||
<div><label class="label-sm">%</label><input class="input input-sm ap-f-rand-pct" type="number" value="70" style="text-align:center"></div>
|
||||
<div><label class="label-sm">Secondary</label><input class="input input-sm ap-f-rand-sec" value="paid" placeholder="e.g. paid"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="ap-f-cyclic-opts" style="display:none">
|
||||
<label class="label-sm">Cyclic Pattern (comma-separated)</label>
|
||||
<input class="input input-sm ap-f-cyclic-pat" placeholder="free, free, paid" value="">
|
||||
</div>
|
||||
</div>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
function addAppendField() {
|
||||
_apFieldCounter++;
|
||||
const fid = _apFieldCounter;
|
||||
const container = document.getElementById("apFieldsContainer");
|
||||
const tmp = document.createElement("div");
|
||||
tmp.innerHTML = _buildAppendFieldRow(fid, "new_field", "", true);
|
||||
container.appendChild(tmp.firstElementChild);
|
||||
// Focus the name input
|
||||
const nameInp = container.lastElementChild.querySelector(".ap-f-name");
|
||||
if (nameInp) { nameInp.value = ""; nameInp.focus(); }
|
||||
}
|
||||
|
||||
function toggleApFieldDist(sel) {
|
||||
const row = sel.closest(".ap-field-row");
|
||||
const mode = sel.value;
|
||||
const wrap = row.querySelector(".ap-f-dist-wrap");
|
||||
const randomOpts = row.querySelector(".ap-f-random-opts");
|
||||
const cyclicOpts = row.querySelector(".ap-f-cyclic-opts");
|
||||
const patternInput = row.querySelector(".ap-f-pattern");
|
||||
|
||||
if (mode === "static") {
|
||||
wrap.style.display = "none";
|
||||
patternInput.disabled = false;
|
||||
patternInput.style.opacity = "1";
|
||||
} else {
|
||||
wrap.style.display = "block";
|
||||
randomOpts.style.display = mode === "random" ? "block" : "none";
|
||||
cyclicOpts.style.display = mode === "cyclic" ? "block" : "none";
|
||||
patternInput.disabled = true;
|
||||
patternInput.style.opacity = "0.4";
|
||||
}
|
||||
}
|
||||
|
||||
function toggleAllTargets(checked) {
|
||||
document.querySelectorAll(".ap-target").forEach(cb => cb.checked = checked);
|
||||
}
|
||||
|
||||
async function doPreviewAppend(path) {
|
||||
const fd = new FormData();
|
||||
const cf = document.getElementById("apDataFile").files[0];
|
||||
if (cf) fd.append("data_file", cf);
|
||||
|
||||
const mc = parseInt(document.getElementById("apManualCount").value || 10);
|
||||
const lim = parseInt(document.getElementById("apLimit").value || 0);
|
||||
fd.append("manual_count", mc);
|
||||
fd.append("limit_count", lim > 0 ? Math.min(mc, lim) : mc);
|
||||
|
||||
// Collect field order, patterns, starts, and per-field distributions
|
||||
const fieldRows = document.querySelectorAll("#apFieldsContainer .ap-field-row");
|
||||
const fieldOrder = [];
|
||||
const fieldDistributions = {};
|
||||
|
||||
fieldRows.forEach(row => {
|
||||
const nameEl = row.querySelector(".ap-f-name");
|
||||
const fieldName = nameEl ? nameEl.value.trim() : row.dataset.field;
|
||||
if (!fieldName) return;
|
||||
|
||||
// Update name attributes on dynamic fields
|
||||
const patInput = row.querySelector(".ap-f-pattern");
|
||||
const startInput = row.querySelector(".ap-f-start");
|
||||
if (patInput) patInput.name = `pattern_${fieldName}`;
|
||||
if (startInput) startInput.name = `start_${fieldName}`;
|
||||
|
||||
fieldOrder.push(fieldName);
|
||||
fd.append(`pattern_${fieldName}`, patInput ? patInput.value : "");
|
||||
fd.append(`start_${fieldName}`, startInput ? startInput.value : "1");
|
||||
|
||||
const distMode = row.querySelector(".ap-f-dist").value;
|
||||
if (distMode !== "static") {
|
||||
const distEntry = { mode: distMode };
|
||||
if (distMode === "random") {
|
||||
distEntry.rand_primary = row.querySelector(".ap-f-rand-prim").value;
|
||||
distEntry.rand_secondary = row.querySelector(".ap-f-rand-sec").value;
|
||||
distEntry.rand_pct = parseInt(row.querySelector(".ap-f-rand-pct").value || 50);
|
||||
} else if (distMode === "cyclic") {
|
||||
distEntry.pattern = row.querySelector(".ap-f-cyclic-pat").value;
|
||||
}
|
||||
fieldDistributions[fieldName] = distEntry;
|
||||
}
|
||||
});
|
||||
|
||||
fd.append("field_order", fieldOrder.join(","));
|
||||
fd.append("field_distributions", JSON.stringify(fieldDistributions));
|
||||
|
||||
// No longer need legacy dist fields — per-field handles it all
|
||||
fd.append("dist_field", "");
|
||||
fd.append("dist_mode", "static");
|
||||
|
||||
const r = await api("/preview_append", fd, true);
|
||||
if (r.success) {
|
||||
previewData = r.preview;
|
||||
document.getElementById("apPreviewWrap").style.display = "block";
|
||||
document.getElementById("apPreviewJSON").textContent = JSON.stringify(r.preview.slice(0, 5), null, 2) + (r.total_count > 5 ? "\n..." : "");
|
||||
document.getElementById("apPreviewCount").textContent = r.total_count;
|
||||
if (r.file_lines) document.getElementById("apFileStatus").textContent = r.file_lines + " lines loaded";
|
||||
} else toast(r.error, "error");
|
||||
}
|
||||
|
||||
async function doCommitAppend(path) {
|
||||
const targets = [];
|
||||
document.querySelectorAll(".ap-target:checked").forEach(cb => targets.push(cb.value));
|
||||
if (targets.length === 0) targets.push(path);
|
||||
|
||||
const r = await api("/commit_append", { path, target_paths: targets, items: previewData });
|
||||
if (r.success) {
|
||||
toast(`Appended ${r.count} items to ${r.targets} target(s)!`);
|
||||
refreshTree(r.tree);
|
||||
loadNode(path);
|
||||
const ac = document.getElementById("appendCard");
|
||||
if (ac) ac.remove();
|
||||
} else toast(r.error, "error");
|
||||
}
|
||||
|
||||
// ── Raw Editor ──
|
||||
async function toggleRawEditor() {
|
||||
rawMode = !rawMode;
|
||||
if (rawMode) {
|
||||
const r = await fetch("/raw/get").then(r => r.json());
|
||||
document.getElementById("rawTextarea").value = r.raw;
|
||||
document.getElementById("rawEditorArea").classList.remove("d-none");
|
||||
document.getElementById("editorArea").classList.add("d-none");
|
||||
document.getElementById("emptyState").style.display = "none";
|
||||
} else {
|
||||
const r = await api("/raw/set", { content: document.getElementById("rawTextarea").value });
|
||||
if (r.success) { refreshTree(r.tree); toast("JSON updated"); }
|
||||
else { toast(r.error, "error"); return; }
|
||||
document.getElementById("rawEditorArea").classList.add("d-none");
|
||||
document.getElementById("editorArea").classList.remove("d-none");
|
||||
if (currentPath) loadNode(currentPath);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Search ──
|
||||
let searchTimeout;
|
||||
function handleSearch(q) {
|
||||
clearTimeout(searchTimeout);
|
||||
if (!q.trim()) { renderTree(treeData); return; }
|
||||
searchTimeout = setTimeout(async () => {
|
||||
const r = await api("/search", { query: q });
|
||||
if (r.results && r.results.length) {
|
||||
const area = document.getElementById("editorArea");
|
||||
area.classList.remove("d-none");
|
||||
document.getElementById("emptyState").style.display = "none";
|
||||
let html = `<div class="card slide-up"><div class="card-header"><span class="card-title"><i class="fa-solid fa-search"></i> ${r.results.length} results</span></div>`;
|
||||
r.results.forEach(res => {
|
||||
html += `<div class="field-row" style="cursor:pointer" onclick="loadNode('${res.path.split("/").slice(0, -1).join("/")}')">
|
||||
<span class="field-type-badge type-${res.match_type === "key" ? "string" : "number"}">${res.match_type}</span>
|
||||
<span style="font-weight:600;font-size:11px;color:var(--accent);font-family:'JetBrains Mono',monospace">${res.path}</span>
|
||||
<span style="flex:1;font-size:11px;color:var(--text-muted)">${res.preview}</span></div>`;
|
||||
});
|
||||
html += "</div>";
|
||||
area.innerHTML = html;
|
||||
}
|
||||
}, 300);
|
||||
}
|
||||
|
||||
// ── Modal System ──
|
||||
function showModal(title, bodyHtml, onConfirm) {
|
||||
const overlay = document.createElement("div");
|
||||
overlay.className = "modal-overlay";
|
||||
overlay.innerHTML = `<div class="modal-box"><h3><i class="fa-solid fa-pen-to-square" style="color:var(--accent)"></i> ${title}</h3><div id="mdBody">${bodyHtml}</div>
|
||||
<div class="modal-actions"><button class="btn btn-ghost" id="mdCancel">Cancel</button><button class="btn btn-primary" id="mdConfirm">Confirm</button></div></div>`;
|
||||
document.body.appendChild(overlay);
|
||||
overlay.querySelector("#mdCancel").onclick = () => overlay.remove();
|
||||
overlay.querySelector("#mdConfirm").onclick = async () => { await onConfirm(); overlay.remove(); };
|
||||
overlay.addEventListener("click", e => { if (e.target === overlay) overlay.remove(); });
|
||||
setTimeout(() => { const inp = overlay.querySelector("input:not([type=hidden]):not([type=checkbox])"); if (inp) inp.focus(); }, 50);
|
||||
}
|
||||
|
||||
function renameFieldInline(el, path, key) {
|
||||
const orig = el.textContent;
|
||||
el.contentEditable = true; el.focus();
|
||||
const sel = window.getSelection(); sel.selectAllChildren(el);
|
||||
el.onblur = async () => {
|
||||
el.contentEditable = false;
|
||||
if (el.textContent !== orig) {
|
||||
const r = await api("/node/rename", { path: path + "/" + key, new_name: el.textContent });
|
||||
if (r.success) { refreshTree(r.tree); loadNode(path); toast("Renamed"); }
|
||||
else { el.textContent = orig; toast(r.error, "error"); }
|
||||
}
|
||||
};
|
||||
el.onkeydown = e => { if (e.key === "Enter") { e.preventDefault(); el.blur(); } if (e.key === "Escape") { el.textContent = orig; el.blur(); } };
|
||||
}
|
||||
|
||||
// ── Keyboard Shortcuts ──
|
||||
document.addEventListener("keydown", e => {
|
||||
if ((e.ctrlKey || e.metaKey) && e.key === "z") { e.preventDefault(); doUndo(); }
|
||||
if ((e.ctrlKey || e.metaKey) && e.key === "y") { e.preventDefault(); doRedo(); }
|
||||
if ((e.ctrlKey || e.metaKey) && e.key === "s") { e.preventDefault(); saveSession(); }
|
||||
if ((e.ctrlKey || e.metaKey) && e.key === "f") { e.preventDefault(); document.getElementById("searchInput").focus(); }
|
||||
});
|
||||
Reference in New Issue
Block a user