diff options
Diffstat (limited to 'internal/admin/static/map.js')
| -rw-r--r-- | internal/admin/static/map.js | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/internal/admin/static/map.js b/internal/admin/static/map.js index 6acec47..740edf3 100644 --- a/internal/admin/static/map.js +++ b/internal/admin/static/map.js @@ -135,6 +135,13 @@ function loadMap() { } renderMap(data); renderDisconnectedList(data); + var newRoomBtn = $('#newRoomBtn'); + var newRoomSep = $('#newRoomSep'); + if (newRoomBtn && newRoomSep) { + var empty = !data.rooms || data.rooms.length === 0; + newRoomBtn.style.display = empty ? '' : 'none'; + newRoomSep.style.display = empty ? '' : 'none'; + } if (wasDirChange && data.seed) { selectRoom(data.seed); } @@ -1716,4 +1723,116 @@ function updateDirSelect() { } } +function showRenameDir() { + if (currentDir === '') { notify('Cannot rename root', 'error'); return; } + hideNewDir(); + $('#renameDirBtn').classList.add('active'); + var form = $('#renameDirForm'); + form.style.display = 'flex'; + var base = currentDir.includes('/') ? currentDir.split('/').pop() : currentDir; + var input = $('#renameDirInput'); + input.placeholder = base; + input.value = ''; + input.focus(); +} + +function cancelRenameDir() { + $('#renameDirForm').style.display = 'none'; + $('#renameDirBtn').classList.remove('active'); +} + +function confirmRenameDir() { + var newName = $('#renameDirInput').value.trim(); + if (!newName) { notify('Enter a directory name', 'error'); return; } + if (newName.indexOf('/') !== -1 || newName.indexOf('\\') !== -1 || newName === '.' || newName === '..') { notify('Invalid directory name', 'error'); return; } + API.post('/api/room-dirs/rename', {dir: currentDir, new_name: newName}).then(function(r) { + if (r.error) { notify(r.error, 'error'); return; } + notify('Renamed to ' + newName, 'success'); + cancelRenameDir(); + currentDir = r.new_dir; + reloadRoomDirs(function() { + changeDir(r.new_dir); + }); + }).catch(function(e) { notify('Rename failed: ' + e.message, 'error'); }); +} + +function showNewDir() { + hideRenameDir(); + $('#newDirBtn').classList.add('active'); + var form = $('#newDirForm'); + form.style.display = 'flex'; + var input = $('#newDirInput'); + input.value = ''; + input.focus(); +} + +function cancelNewDir() { + $('#newDirForm').style.display = 'none'; + $('#newDirBtn').classList.remove('active'); +} + +function confirmNewDir() { + var name = $('#newDirInput').value.trim(); + if (!name) { notify('Enter a directory name', 'error'); return; } + if (name.indexOf('/') !== -1 || name.indexOf('\\') !== -1 || name === '.' || name === '..') { notify('Invalid directory name', 'error'); return; } + API.post('/api/room-dirs/create', {name: name}).then(function(r) { + if (r.error) { notify(r.error, 'error'); return; } + notify('Created ' + name, 'success'); + cancelNewDir(); + currentDir = name; + reloadRoomDirs(function() { + changeDir(name); + }); + }).catch(function(e) { notify('Create failed: ' + e.message, 'error'); }); +} + +function onDirInputKey(event, type) { + if (event.key === 'Enter') { + event.preventDefault(); + if (type === 'rename') confirmRenameDir(); + else if (type === 'new') confirmNewDir(); + } else if (event.key === 'Escape') { + event.preventDefault(); + if (type === 'rename') cancelRenameDir(); + else if (type === 'new') cancelNewDir(); + } +} + +function createEmptyRoom() { + API.post('/api/rooms/new-empty', {dir: currentDir}).then(function(r) { + if (r.error) { notify(r.error, 'error'); return; } + notify('Created room ' + r.room.id, 'success'); + loadMap().then(function() { + if (r.room && r.room.id) selectRoom(r.room.id); + }); + }).catch(function(e) { notify('Create room failed: ' + e.message, 'error'); }); +} + +function hideRenameDir() { + $('#renameDirForm').style.display = 'none'; + $('#renameDirBtn').classList.remove('active'); +} + +function hideNewDir() { + $('#newDirForm').style.display = 'none'; + $('#newDirBtn').classList.remove('active'); +} + +function reloadRoomDirs(cb) { + API.get('/api/room-dirs').then(function(dirs) { + var sel = $('#dirSelect'); + if (sel) { + sel.innerHTML = ''; + dirs.forEach(function(d) { + var opt = document.createElement('option'); + opt.value = d; + opt.textContent = d || '(root)'; + sel.appendChild(opt); + }); + } + updateDirSelect(); + if (cb) cb(dirs); + }); +} + document.addEventListener('DOMContentLoaded', function() { loadRoomDirs(function() { loadMap(); }); initPanelResize(); }); |
