feat(frontend): Vue (CDN) UI matching templates design; reuse /static CSS and Bootstrap; mount /static in FastAPI

This commit is contained in:
Danamir
2025-11-10 08:45:25 +03:00
parent 779c256e7b
commit 3f91dc91ec
3 changed files with 115 additions and 97 deletions

View File

@@ -30,7 +30,8 @@ def ping():
return {"message": "pong"} return {"message": "pong"}
# Serve static frontend # Serve static assets and frontend
app.mount("/static", StaticFiles(directory="static"), name="static")
app.mount("/app", StaticFiles(directory="frontend", html=True), name="frontend") app.mount("/app", StaticFiles(directory="frontend", html=True), name="frontend")
@app.get("/") @app.get("/")

View File

@@ -1,3 +1,5 @@
const { createApp } = Vue;
const api = { const api = {
auds: "/auditories/", auds: "/auditories/",
oboruds: (audId) => `/oboruds/?aud_id=${encodeURIComponent(audId)}`, oboruds: (audId) => `/oboruds/?aud_id=${encodeURIComponent(audId)}`,
@@ -9,68 +11,49 @@ async function fetchJSON(url) {
return res.json(); return res.json();
} }
function setStatus(msg, type = "info") { createApp({
const el = document.getElementById("status"); data() {
el.textContent = msg || ""; return {
el.className = `status ${type}`; view: 'byAud',
} auditories: [],
selectedAudId: '',
async function loadAuditories() { oboruds: [],
setStatus("Загрузка аудиторий…"); status: '',
error: '',
};
},
methods: {
async loadAuditories() {
this.status = 'Загрузка аудиторий…';
this.error = '';
try { try {
const data = await fetchJSON(api.auds); this.auditories = await fetchJSON(api.auds);
const select = document.getElementById("aud-select"); this.status = '';
select.innerHTML = '<option value="">— выберите аудиторию —</option>';
data.forEach((a) => {
const opt = document.createElement("option");
opt.value = a.id;
opt.textContent = `${a.id}${a.audnazvanie}`;
select.appendChild(opt);
});
setStatus("");
} catch (e) { } catch (e) {
console.error(e); console.error(e);
setStatus("Не удалось загрузить аудитории", "error"); this.error = 'Не удалось загрузить аудитории';
this.status = '';
} }
} },
async loadOboruds() {
function renderOboruds(items) { if (!this.selectedAudId) {
const tbody = document.querySelector("#ob-table tbody"); this.error = '';
tbody.innerHTML = ""; this.status = 'Выберите аудиторию';
items.forEach((it) => {
const tr = document.createElement("tr");
tr.innerHTML = `
<td>${it.id}</td>
<td>${it.invNumber ?? ""}</td>
<td>${it.nazvanie ?? ""}</td>
<td>${it.raspologenie ?? ""}</td>
<td>${it.kolichestvo ?? ""}</td>
<td>${it.type?.name ?? ""}</td>
`;
tbody.appendChild(tr);
});
}
async function loadOborudsForSelected() {
const select = document.getElementById("aud-select");
const audId = select.value;
if (!audId) {
setStatus("Выберите аудиторию", "warn");
return; return;
} }
setStatus("Загрузка оборудования…"); this.status = 'Загрузка оборудования…';
this.error = '';
try { try {
const data = await fetchJSON(api.oboruds(audId)); this.oboruds = await fetchJSON(api.oboruds(this.selectedAudId));
renderOboruds(data); this.status = '';
setStatus("");
} catch (e) { } catch (e) {
console.error(e); console.error(e);
setStatus("Не удалось загрузить оборудование", "error"); this.error = 'Не удалось загрузить оборудование';
this.status = '';
} }
} },
},
document.addEventListener("DOMContentLoaded", () => { mounted() {
document.getElementById("load-btn").addEventListener("click", loadOborudsForSelected); this.loadAuditories();
loadAuditories(); }
}); }).mount('#app');

View File

@@ -3,45 +3,79 @@
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<title>ASU Inventory — Фронт</title> <title>АСУ Инвентаризация</title>
<link rel="stylesheet" href="/app/styles.css" /> <link rel="stylesheet" href="/static/css/bootstrap.min.css" />
<link rel="stylesheet" href="/static/css/index.css" />
</head> </head>
<body> <body>
<header> <header>
<h1>ASU Inventory</h1> <h1>
<nav> <a href="/app/">АСУ Инвентаризация</a>
<a href="/docs" target="_blank">API Docs</a> </h1>
</nav> <h2>Учет оборудования. Демоверсия</h2>
</header> </header>
<main> <div class="row no-print">
<section class="panel"> <nav class="no-print navbar navbar-expand-lg navbar-light">
<h2>Оборудование по аудитории</h2> <div class="container-fluid">
<div class="controls"> <a class="navbar-brand" href="/app/">Главная</a>
<label for="aud-select">Аудитория:</label> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<select id="aud-select"> <span class="navbar-toggler-icon"></span>
<option value="">— выберите аудиторию —</option> </button>
</select> <div class="collapse navbar-collapse" id="navbarNav">
<button id="load-btn">Показать</button> <ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item"><a class="nav-link" href="#" @click="view='byAud'">По аудитории</a></li>
<li class="nav-item"><a class="nav-link" href="/docs" target="_blank">API Docs</a></li>
</ul>
</div> </div>
<div id="status" class="status"></div> </div>
<table id="ob-table"> </nav>
</div>
<div id="app" class="container">
<div v-if="view==='byAud'" class="row">
<div class="card col-md-10 col-10">
<div class="card-body">
<h3 class="card-title">Оборудование по аудитории</h3>
<div class="mb-2 d-flex align-items-center gap-2">
<label for="aud-select" class="me-2">Аудитория:</label>
<select id="aud-select" class="form-select w-auto" v-model="selectedAudId">
<option value="">— выберите аудиторию —</option>
<option v-for="a in auditories" :key="a.id" :value="a.id">{{ a.id }} — {{ a.audnazvanie }}</option>
</select>
<button class="btn btn-primary" @click="loadOboruds">Показать</button>
</div>
<div class="status" :class="{error: !!error}">{{ status }}</div>
<div class="table-responsive">
<table class="table datatable">
<thead> <thead>
<tr> <tr>
<th>ID</th> <th scope="col">ID</th>
<th>Инв. номер</th> <th scope="col">Инв. номер</th>
<th>Название</th> <th scope="col">Название</th>
<th>Расположение</th> <th scope="col">Расположение</th>
<th>Кол-во</th> <th scope="col">Кол-во</th>
<th>Тип</th> <th scope="col">Тип</th>
</tr> </tr>
</thead> </thead>
<tbody></tbody> <tbody>
<tr v-for="it in oboruds" :key="it.id">
<td>{{ it.id }}</td>
<td class="inv">{{ it.invNumber ?? '' }}</td>
<td>{{ it.nazvanie ?? '' }}</td>
<td class="rasp">{{ it.raspologenie ?? '' }}</td>
<td>{{ it.kolichestvo ?? '' }}</td>
<td>{{ it.type?.name ?? '' }}</td>
</tr>
</tbody>
</table> </table>
</section> </div>
</main> </div>
</div>
</div>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<script src="/app/app.js" defer></script> <script src="/app/app.js" defer></script>
</body> </body>
</html> </html>