Update web/templates/entry_add.html
This commit is contained in:
parent
20bdd92cf7
commit
415f410e83
@ -81,4 +81,60 @@
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
(function () {
|
||||
// Gracefully load the mapping, then wire up behavior
|
||||
const talksUrl = "{% static 'talks.json' %}";
|
||||
|
||||
function wireAutofill(talkMap) {
|
||||
const numberEl = document.getElementById("id_talk_number");
|
||||
const titleEl = document.getElementById("id_talk_title");
|
||||
if (!numberEl || !titleEl) return;
|
||||
|
||||
// If the user types anything, we mark the field "dirty" so we won't overwrite it.
|
||||
let userTyped = false;
|
||||
titleEl.addEventListener("input", () => {
|
||||
// If the user cleared the field, allow auto-fill again.
|
||||
userTyped = titleEl.value.trim().length > 0;
|
||||
if (!userTyped) titleEl.dataset.autofilled = "0";
|
||||
});
|
||||
|
||||
function maybeAutofill() {
|
||||
const n = numberEl.value;
|
||||
const mapped = talkMap && talkMap[n] ? talkMap[n] : "";
|
||||
|
||||
// Only set if the user hasn't typed their own value
|
||||
if (!userTyped) {
|
||||
if (mapped) {
|
||||
titleEl.value = mapped;
|
||||
titleEl.dataset.autofilled = "1";
|
||||
} else if (titleEl.dataset.autofilled === "1") {
|
||||
// Clear if the previous value was auto-filled and the new number has no mapping
|
||||
titleEl.value = "";
|
||||
titleEl.dataset.autofilled = "0";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// On change
|
||||
numberEl.addEventListener("change", maybeAutofill);
|
||||
|
||||
// Initial fill on page load if title is empty
|
||||
if (titleEl.value.trim() === "") {
|
||||
userTyped = false;
|
||||
maybeAutofill();
|
||||
} else {
|
||||
// User already has a value; don't overwrite
|
||||
userTyped = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Try to fetch the mapping; if it fails, we just silently skip auto-fill
|
||||
fetch(talksUrl, {cache: "no-store"})
|
||||
.then(r => r.ok ? r.json() : {})
|
||||
.then(map => wireAutofill(map))
|
||||
.catch(() => wireAutofill({}));
|
||||
})();
|
||||
</script>
|
||||
{% endblock %}
|
||||
Loading…
Reference in New Issue
Block a user