allow using "?" in ID as a placeholder for next available number

This commit is contained in:
jomo 2022-08-11 02:51:11 +02:00
parent 196269aa38
commit ade2219469
2 changed files with 14 additions and 3 deletions

View file

@ -10,7 +10,7 @@
<body> <body>
<h1><a href="../">in?</a></h1> <h1><a href="../">in?</a></h1>
<form id="form"> <form id="form">
<label for="id">ID</label> <label for="id">ID</label> (use ? as placeholder for next available number)
<input id="id" type="text" placeholder="loading..."> <input id="id" type="text" placeholder="loading...">
<label for="is_in">Is in</label> <label for="is_in">Is in</label>

View file

@ -65,8 +65,8 @@ function knownTypes() {
} }
function save() { function save() {
item = { const item = {
id: document.getElementById('id').value || null, id: document.getElementById('id').value || '',
is_in: document.getElementById('is_in').value || null, is_in: document.getElementById('is_in').value || null,
coords_bl: document.getElementById('coords_bl').value || null, coords_bl: document.getElementById('coords_bl').value || null,
coords_tr: document.getElementById('coords_tr').value || null, coords_tr: document.getElementById('coords_tr').value || null,
@ -77,6 +77,17 @@ function save() {
hidden: document.getElementById('hidden').checked, hidden: document.getElementById('hidden').checked,
} }
if (item.id.includes('?')) {
let n = 1;
while (true) {
const candidate = item.id.replace('?', n++);
if (!Object.keys(items).includes(candidate)) {
item.id = candidate;
break;
}
}
}
if (items[item.id]) { if (items[item.id]) {
if (!id || (id && id != item.id)) { if (!id || (id && id != item.id)) {
alert(`ID ${item.id} exists. Please chose a different ID or edit the existing ${item.id} item!`); alert(`ID ${item.id} exists. Please chose a different ID or edit the existing ${item.id} item!`);