Refactor settings template to improve structure and enhance readability of room editor functionality

This commit is contained in:
Yaro Kasear 2025-07-17 14:04:28 -05:00
parent 34cdc17eee
commit 7fb117a95e

View file

@ -112,13 +112,19 @@
{% endblock %} {% endblock %}
{% block content %} {% block content %}
<form id="settingsForm">
<div class="container"> <div class="container">
<div class="card mb-3"> <ul class="nav nav-tabs">
<div class="card-body"> <li class="nav-item">
<h5 class="card-title"> <button class="nav-link active" id="inventory-tab" data-bs-toggle="tab" data-bs-target="#inventory-tab-pane"
Inventory Settings type="button">Inventory</button>
</h5> </li>
<li class="nav-item">
<button class="nav-link" id="location-tab" data-bs-toggle="tab" data-bs-target="#location-tab-pane"
type="button">Location</button>
</li>
</ul>
<div class="tab-content" id="tabContent">
<div class="tab-pane fade show active border border-top-0 p-3" id="inventory-tab-pane">
<div class="row"> <div class="row">
<div class="col"> <div class="col">
{{ combos.render_combobox( {{ combos.render_combobox(
@ -138,10 +144,7 @@
</div> </div>
</div> </div>
</div> </div>
</div> <div class="tab-pane fade border border-top-0 p-3" id="location-tab-pane">
<div class="card">
<div class="card-body">
<h5 class="card-title">Location Settings</h5>
<div class="row"> <div class="row">
<div class="col"> <div class="col">
{{ combos.render_combobox( {{ combos.render_combobox(
@ -196,127 +199,126 @@
</div> </div>
</div> </div>
</div> </div>
</div> <div class="modal fade" id="roomEditor" data-bs-backdrop="static" tabindex="-1">
<div class="modal fade" id="roomEditor" data-bs-backdrop="static" tabindex="-1"> <div class="modal-dialog">
<div class="modal-dialog"> <div class="modal-content">
<div class="modal-content"> <div class="modal-header">
<div class="modal-header"> <h1 class="modal-title fs-5" id="roomEditorLabel">Room Editor</h1>
<h1 class="modal-title fs-5" id="roomEditorLabel">Room Editor</h1>
</div>
<div class="modal-body">
<div class="row">
<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>
<div class="row"> <div class="modal-body">
<div class="col"> <div class="row">
<label for="roomSection" class="form-label">Section</label> <div class="col">
<select id="roomSection" class="form-select"> <label for="roomName" class="form-label">Room Name</label>
<option value="">Select a section</option> <input type="text" class="form-input" id="roomName" placeholder="Enter room name">
{% for section in sections %} <input type="hidden" id="roomId">
</div>
</div>
<div class="row">
<div class="col">
<label for="roomSection" class="form-label">Section</label>
<select id="roomSection" class="form-select">
<option value="">Select a section</option>
{% for section in sections %}
<option value="{{ section.id }}">{{ section.name }}</option> <option value="{{ section.id }}">{{ section.name }}</option>
{% endfor %} {% endfor %}
</select> </select>
</div> </div>
<div class="col"> <div class="col">
<label class="form-label">Function</label> <label class="form-label">Function</label>
<select id="roomFunction" class="form-select"> <select id="roomFunction" class="form-select">
<option value="">Select a function</option> <option value="">Select a function</option>
{% for function in functions %} {% for function in functions %}
<option value="{{ function.id }}">{{ function.name }}</option> <option value="{{ function.id }}">{{ function.name }}</option>
{% endfor %} {% endfor %}
</select> </select>
</div>
</div> </div>
</div> </div>
</div> <div class="modal-footer">
<div class="modal-footer"> <button type="button" class="btn btn-danger" data-bs-dismiss="modal" id="roomEditorCancelButton">{{
<button type="button" class="btn btn-danger" data-bs-dismiss="modal" icons.render_icon('x-lg', 16) }}</button>
id="roomEditorCancelButton">{{ icons.render_icon('x-lg', 16) }}</button> {% set editorSaveLogic %}
{% set editorSaveLogic %} const modal = document.getElementById('roomEditor');
const modal = document.getElementById('roomEditor'); const name = document.getElementById('roomName').value.trim();
const name = document.getElementById('roomName').value.trim(); const sectionVal = document.getElementById('roomSection').value;
const sectionVal = document.getElementById('roomSection').value; const funcVal = document.getElementById('roomFunction').value;
const funcVal = document.getElementById('roomFunction').value; let idRaw = document.getElementById('roomId').value;
let idRaw = document.getElementById('roomId').value;
if (!name) { if (!name) {
alert('Please enter a room name.'); alert('Please enter a room name.');
return; return;
} }
const roomList = document.getElementById('room-list'); const roomList = document.getElementById('room-list');
let existingOption = Array.from(roomList.options).find(opt => opt.value === idRaw); let existingOption = Array.from(roomList.options).find(opt => opt.value === idRaw);
if (!idRaw) { if (!idRaw) {
idRaw = ComboBoxWidget.createTempId("room"); idRaw = ComboBoxWidget.createTempId("room");
} }
if (!existingOption) { if (!existingOption) {
existingOption = ComboBoxWidget.createOption(name, idRaw); existingOption = ComboBoxWidget.createOption(name, idRaw);
roomList.appendChild(existingOption); roomList.appendChild(existingOption);
} }
existingOption.textContent = name; existingOption.textContent = name;
existingOption.value = idRaw; existingOption.value = idRaw;
existingOption.dataset.sectionId = sectionVal || ""; existingOption.dataset.sectionId = sectionVal || "";
existingOption.dataset.functionId = funcVal || ""; existingOption.dataset.functionId = funcVal || "";
ComboBoxWidget.sortOptions(roomList); ComboBoxWidget.sortOptions(roomList);
bootstrap.Modal.getInstance(modal).hide(); bootstrap.Modal.getInstance(modal).hide();
{% endset %} {% endset %}
{{ buttons.render_button( {{ buttons.render_button(
id='editorSave', id='editorSave',
icon='floppy', icon='floppy',
logic=editorSaveLogic logic=editorSaveLogic
) }} ) }}
</div>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
</form>
{% endblock %} {% endblock %}
{% block script %} {% block script %}
const modal = document.getElementById('roomEditor'); const modal = document.getElementById('roomEditor');
const cancelButton = document.getElementById('roomEditorCancelButton'); const cancelButton = document.getElementById('roomEditorCancelButton');
const form = document.getElementById('settingsForm'); const form = document.getElementById('settingsForm');
modal.addEventListener('roomEditor:prepare', (event) => { modal.addEventListener('roomEditor:prepare', (event) => {
const { id, name, sectionId, functionId } = event.detail; const { id, name, sectionId, functionId } = event.detail;
document.getElementById('roomId').value = id; document.getElementById('roomId').value = id;
document.getElementById('roomName').value = name; document.getElementById('roomName').value = name;
// Populate dropdowns before assigning selection // Populate dropdowns before assigning selection
const modalSections = document.getElementById('roomSection'); const modalSections = document.getElementById('roomSection');
const modalFunctions = document.getElementById('roomFunction'); const modalFunctions = document.getElementById('roomFunction');
const pageSections = document.getElementById('section-list'); const pageSections = document.getElementById('section-list');
const pageFunctions = document.getElementById('function-list'); const pageFunctions = document.getElementById('function-list');
modalSections.innerHTML = '<option value="">Select a section</option>'; modalSections.innerHTML = '<option value="">Select a section</option>';
modalFunctions.innerHTML = '<option value="">Select a function</option>'; modalFunctions.innerHTML = '<option value="">Select a function</option>';
Array.from(pageSections.options).forEach(opt => { Array.from(pageSections.options).forEach(opt => {
const option = new Option(opt.textContent, opt.value); const option = new Option(opt.textContent, opt.value);
if (opt.value === sectionId) { if (opt.value === sectionId) {
option.selected = true; option.selected = true;
} }
modalSections.appendChild(option); modalSections.appendChild(option);
});
Array.from(pageFunctions.options).forEach(opt => {
const option = new Option(opt.textContent, opt.value);
if (opt.value === functionId) {
option.selected = true;
}
modalFunctions.appendChild(option);
});
}); });
cancelButton.addEventListener('click', () => { Array.from(pageFunctions.options).forEach(opt => {
bootstrap.Modal.getInstance(modal).hide(); const option = new Option(opt.textContent, opt.value);
if (opt.value === functionId) {
option.selected = true;
}
modalFunctions.appendChild(option);
}); });
});
cancelButton.addEventListener('click', () => {
bootstrap.Modal.getInstance(modal).hide();
});
{% endblock %} {% endblock %}