From 069d3c1c81d71042706372df153254487a765dfc Mon Sep 17 00:00:00 2001 From: historia <[not public]> Date: Mon, 29 Jun 2026 22:31:11 -0400 Subject: feat: wip web admin for mapping and crud --- internal/admin/static/editor.js | 174 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 internal/admin/static/editor.js (limited to 'internal/admin/static/editor.js') diff --git a/internal/admin/static/editor.js b/internal/admin/static/editor.js new file mode 100644 index 0000000..d9ec36f --- /dev/null +++ b/internal/admin/static/editor.js @@ -0,0 +1,174 @@ +var editorType = ''; +var editorFields = []; +var currentID = null; +var allIDs = []; + +function initEditor(type, fields) { + editorType = type; + editorFields = fields; + loadList(); + var hash = window.location.hash.substring(1); + if (hash) { loadItem(hash); } +} + +function loadList() { + API.get('/api/' + editorType).then(function(data) { + allIDs = data.ids || data || []; + renderList(''); + }).catch(function(e) { + notify('Failed to load list: ' + e.message, 'error'); + }); +} + +function renderList(filter) { + var el = $('#listEntries'); + var f = filter.toLowerCase(); + var filtered = allIDs.filter(function(id) { return !f || String(id).toLowerCase().indexOf(f) >= 0; }); + el.innerHTML = filtered.map(function(id) { + return '
Failed to load: ' + esc(e.message) + '
'; + }); +} + +function renderEditor(id, data) { + var obj = data; + var html = 'Deleted
'; + loadList(); + }).catch(function(e) { notify('Delete failed: ' + e.message, 'error'); }); +} + +function createNew() { + var name = prompt(editorType + ' ID:'); + if (!name) return; + API.post('/api/' + editorType, {id: name}).then(function() { + notify(editorType + ' ' + name + ' created', 'success'); + updateUndoBar(); + loadList(); + loadItem(name); + }).catch(function(e) { notify('Create failed: ' + e.message, 'error'); }); +} + +function switchTab(e, tab) { + e.target.parentElement.querySelectorAll('.tab').forEach(function(t) { t.classList.remove('active'); }); + e.target.classList.add('active'); + var tabs = $('#editorMain').querySelectorAll('.tab-content'); + tabs.forEach(function(t) { t.style.display = 'none'; }); + var target = document.getElementById('tab' + tab.charAt(0).toUpperCase() + tab.slice(1)); + if (target) target.style.display = 'block'; +} + +function getNested(obj, path) { + return path.split('.').reduce(function(o, k) { return o && o[k] !== undefined ? o[k] : undefined; }, obj); +} + +function setNested(obj, path, val) { + var keys = path.split('.'); + var last = keys.pop(); + var target = keys.reduce(function(o, k) { + if (!o[k]) o[k] = {}; + return o[k]; + }, obj); + target[last] = val; +} + +function esc(s) { return String(s || '').replace(/&/g,'&').replace(//g,'>').replace(/"/g,'"'); } +function escAttr(s) { return String(s || '').replace(/&/g,'&').replace(/"/g,'"'); } + +window.refreshPage = function() { loadList(); if (currentID) loadItem(currentID); }; -- cgit v1.2.3