Implement sync_from_state methods for Area, Brand, Item, RoomFunction, and Room models; enhance entity management and foreign key resolution in settings
This commit is contained in:
parent
8a5c5db9e0
commit
7833c4828b
9 changed files with 359 additions and 186 deletions
|
@ -59,7 +59,19 @@ submit_button=True
|
|||
{% set room_editor %}
|
||||
const roomEditor = new bootstrap.Modal(document.getElementById('roomEditor'));
|
||||
const roomNameInput = document.getElementById('roomName');
|
||||
roomNameInput.value = document.getElementById('room-input').value;
|
||||
const input = document.getElementById('room-input');
|
||||
const name = input.value.trim();
|
||||
const existingOption = Array.from(document.getElementById('room-list').options)
|
||||
.find(opt => opt.textContent.trim() === name);
|
||||
|
||||
roomNameInput.value = name;
|
||||
document.getElementById('roomId').value = existingOption?.value ?? ''; // this will be the ID or temp ID
|
||||
|
||||
if (existingOption?.dataset.sectionId)
|
||||
document.getElementById('roomSection').value = existingOption.dataset.sectionId;
|
||||
|
||||
if (existingOption?.dataset.functionId)
|
||||
document.getElementById('roomFunction').value = existingOption.dataset.functionId;
|
||||
|
||||
roomEditor.show();
|
||||
|
||||
|
@ -67,11 +79,12 @@ submit_button=True
|
|||
{% endset %}
|
||||
<div class="col">
|
||||
{{ combos.render_combobox(
|
||||
id='room',
|
||||
options=rooms,
|
||||
label='Rooms',
|
||||
placeholder='Add a new room',
|
||||
onAdd=room_editor
|
||||
id='room',
|
||||
options=rooms,
|
||||
label='Rooms',
|
||||
placeholder='Add a new room',
|
||||
onAdd=room_editor,
|
||||
onEdit=room_editor
|
||||
) }}
|
||||
</div>
|
||||
</div>
|
||||
|
@ -89,6 +102,7 @@ submit_button=True
|
|||
<div class="col">
|
||||
<label for="roomName" class="form-label">Room Name</label>
|
||||
<input type="text" class="form-input" id="roomName" placeholder="Enter room name">
|
||||
<input type="hidden" id="roomId">
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
|
@ -134,16 +148,20 @@ submit_button=True
|
|||
function buildFormState() {
|
||||
function extractOptions(id) {
|
||||
const select = document.getElementById(`${id}-list`);
|
||||
return Array.from(select.options).map(opt => ({ name: opt.textContent.trim(), id: parseInt(opt.value) || undefined }));
|
||||
return Array.from(select.options).map(opt => ({
|
||||
name: opt.textContent.trim(),
|
||||
id: opt.value || undefined
|
||||
}));
|
||||
}
|
||||
|
||||
const roomOptions = Array.from(document.getElementById('room-list').options);
|
||||
const rooms = roomOptions.map(opt => {
|
||||
const data = opt.dataset;
|
||||
return {
|
||||
id: opt.value || undefined,
|
||||
name: opt.textContent.trim(),
|
||||
section_id: data.sectionId ? parseInt(data.sectionId) : null,
|
||||
function_id: data.functionId ? parseInt(data.functionId) : null
|
||||
section_id: data.sectionId ?? null,
|
||||
function_id: data.functionId ?? null,
|
||||
};
|
||||
});
|
||||
|
||||
|
@ -192,44 +210,48 @@ submit_button=True
|
|||
const name = document.getElementById('roomName').value.trim();
|
||||
const sectionVal = document.getElementById('roomSection').value;
|
||||
const funcVal = document.getElementById('roomFunction').value;
|
||||
|
||||
const section = sectionVal !== "" ? parseInt(sectionVal) : null;
|
||||
const func = funcVal !== "" ? parseInt(funcVal) : null;
|
||||
|
||||
let idRaw = document.getElementById('roomId').value;
|
||||
|
||||
if (!name) {
|
||||
alert('Please enter a room name.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Avoid duplicate visible names
|
||||
|
||||
const roomList = document.getElementById('room-list');
|
||||
const exists = Array.from(roomList.options).some(opt => opt.textContent.trim() === name);
|
||||
if (exists) {
|
||||
alert(`Room "${name}" already exists.`);
|
||||
return;
|
||||
let existingOption = Array.from(roomList.options).find(opt => opt.value === idRaw);
|
||||
|
||||
// If it's a brand new ID, generate one (string-based!)
|
||||
if (!idRaw) {
|
||||
idRaw = ComboBoxWidget.createTempId("room");
|
||||
}
|
||||
|
||||
// Add to select box visibly
|
||||
const option = ComboBoxWidget.createOption(name);
|
||||
|
||||
if (section !== null) {
|
||||
option.dataset.sectionId = section;
|
||||
|
||||
if (!existingOption) {
|
||||
existingOption = ComboBoxWidget.createOption(name, idRaw);
|
||||
roomList.appendChild(existingOption);
|
||||
}
|
||||
if (func !== null) {
|
||||
option.dataset.functionId = func;
|
||||
}
|
||||
|
||||
roomList.appendChild(option);
|
||||
|
||||
existingOption.textContent = name;
|
||||
existingOption.value = idRaw;
|
||||
existingOption.dataset.sectionId = sectionVal;
|
||||
existingOption.dataset.functionId = funcVal;
|
||||
|
||||
ComboBoxWidget.sortOptions(roomList);
|
||||
|
||||
// Track in state object
|
||||
formState.rooms.push({
|
||||
|
||||
// Update formState.rooms
|
||||
const index = formState.rooms.findIndex(r => r.id === idRaw);
|
||||
const payload = {
|
||||
id: idRaw,
|
||||
name,
|
||||
section_id: section,
|
||||
function_id: func
|
||||
});
|
||||
|
||||
section_id: sectionVal !== "" ? sectionVal : null,
|
||||
function_id: funcVal !== "" ? funcVal : null
|
||||
};
|
||||
|
||||
if (index >= 0) {
|
||||
formState.rooms[index] = payload;
|
||||
} else {
|
||||
formState.rooms.push(payload);
|
||||
}
|
||||
|
||||
bootstrap.Modal.getInstance(modal).hide();
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue