Make edit.html responsive with sticky action bar, Ctrl+S shortcut, and safe save handlers

This commit is contained in:
pongpon pitisuk
2026-09-12 15:12:32 +07:00
parent 0e09259c9d
commit 1609078a9c
2 changed files with 169 additions and 55 deletions
+112 -31
View File
@@ -2,7 +2,7 @@
<html lang="th"> <html lang="th">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>แก้ไขแบบสอบถาม - TPU Survey</title> <title>แก้ไขแบบสอบถาม - TPU Survey</title>
<link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
@@ -26,79 +26,160 @@
</script> </script>
<link rel="stylesheet" href="css/style.css?v=6"> <link rel="stylesheet" href="css/style.css?v=6">
<style> <style>
body { background-color: var(--bg-color); font-family: 'Noto Sans Thai', sans-serif; } * { box-sizing: border-box; }
.editor-container { body {
max-width: 1000px; background-color: #f8fafc;
margin: 0 auto; font-family: 'Noto Sans Thai', sans-serif;
padding: 100px 20px 60px; margin: 0;
padding: 0;
overflow-x: hidden;
} }
.editor-header { .editor-header {
position: fixed; position: fixed;
top: 0; top: 0;
left: 0; left: 0;
right: 0; right: 0;
height: var(--header-height); min-height: 64px;
background: rgba(255, 255, 255, 0.95); background: rgba(255, 255, 255, 0.98);
backdrop-filter: blur(12px); backdrop-filter: blur(12px);
border-bottom: 3px solid #fbbf24; border-bottom: 3px solid #fbbf24;
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: space-between; justify-content: space-between;
padding: 0 40px; padding: 8px 20px;
z-index: 1000; z-index: 1000;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05); box-shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
gap: 12px;
} }
.editor-title { .editor-title {
font-size: 1.25rem; font-size: 1.1rem;
font-weight: 800; font-weight: 800;
color: #1e3a8a; color: #1e3a8a;
white-space: nowrap; white-space: nowrap;
overflow: hidden; overflow: hidden;
text-overflow: ellipsis; text-overflow: ellipsis;
max-width: 50%; max-width: 450px;
}
.editor-container {
max-width: 1000px;
width: 100%;
margin: 0 auto;
padding: 84px 16px 120px;
} }
.editor-actions { .editor-actions {
display: flex; display: flex;
align-items: center;
gap: 8px;
flex-shrink: 0;
}
.sticky-bottom-bar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
background: rgba(255, 255, 255, 0.98);
backdrop-filter: blur(12px);
border-top: 1px solid #e2e8f0;
padding: 12px 20px;
display: flex;
align-items: center;
justify-content: space-between;
z-index: 999;
box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.08);
gap: 12px; gap: 12px;
} }
.section-category-badge { .dirty-badge {
font-size: 10px; display: inline-flex;
text-transform: uppercase; align-items: center;
letter-spacing: 0.5px; gap: 6px;
padding: 2px 8px; font-size: 0.8rem;
border-radius: 4px; color: #d97706;
font-weight: 700; font-weight: 600;
margin-bottom: 8px; }
display: inline-block; @media (max-width: 768px) {
.editor-header {
padding: 8px 12px;
}
.editor-title {
max-width: 160px;
font-size: 0.95rem;
}
.editor-container {
padding: 76px 10px 140px;
}
.editor-actions .btn-preview-text {
display: none;
}
.sticky-bottom-bar {
flex-direction: column;
gap: 8px;
padding: 10px 14px;
}
.sticky-bottom-bar .bottom-actions {
width: 100%;
display: flex;
gap: 8px;
}
.sticky-bottom-bar .bottom-actions button {
flex: 1;
}
} }
</style> </style>
</head> </head>
<body> <body>
<!-- Top Header -->
<header class="editor-header"> <header class="editor-header">
<div class="d-flex items-center gap-16"> <div class="flex items-center gap-3 overflow-hidden">
<button class="btn btn-ghost btn-sm" onclick="closeEditor()">✕ ปิด</button> <button class="btn btn-ghost btn-sm text-slate-600 hover:text-slate-900" onclick="closeEditor()" title="ปิดหน้าแก้ไข">
<i class="fas fa-times"></i> <span class="hidden sm:inline">ปิด</span>
</button>
<div class="editor-title" id="surveyTitleDisplay">กำลังโหลด...</div> <div class="editor-title" id="surveyTitleDisplay">กำลังโหลด...</div>
</div> </div>
<div class="editor-actions"> <div class="editor-actions">
<button class="btn btn-ghost" onclick="previewSurvey()">👁️ พรีวิว</button> <button class="btn btn-ghost btn-sm text-blue-600 hover:bg-blue-50 font-bold border border-blue-200" onclick="previewSurvey()">
<button class="btn btn-primary" onclick="saveSurvey()">💾 บันทึกการเปลี่ยนแปลง</button> 👁️ <span class="btn-preview-text">พรีวิว</span>
</button>
<button id="saveSurveyBtnTop" class="btn btn-primary btn-sm font-bold shadow-md bg-blue-600 hover:bg-blue-700 text-white" onclick="saveSurvey()">
💾 บันทึกการแก้ไข
</button>
</div> </div>
</header> </header>
<!-- Main Content -->
<main class="editor-container"> <main class="editor-container">
<div id="editorContent"> <div id="editorContent">
<div class="loading" style="padding: 100px 0;"> <div class="loading" style="padding: 100px 0; text-align:center;">
<div class="spinner"></div> <div class="spinner"></div>
<p>กำลังโหลดข้อมูลแบบสอบถาม...</p> <p class="text-slate-500 mt-4 font-bold">กำลังโหลดข้อมูลแบบสอบถาม...</p>
</div> </div>
</div> </div>
<div class="text-center mt-40"> <div class="text-center mt-32 mb-40">
<button class="btn btn-secondary" onclick="addSection()">➕ เพิ่มโพลล์ส่วนใหม่</button> <button class="btn btn-secondary font-bold px-6 py-3 shadow-sm hover:shadow" onclick="addSection()">
➕ เพิ่มส่วนแบบสอบถามใหม่
</button>
</div> </div>
</main> </main>
<script src="js/app.js?v=5"></script> <!-- Sticky Bottom Bar -->
<script src="js/editor.js?v=5"></script> <div class="sticky-bottom-bar">
<div class="flex items-center gap-4">
<span id="saveStatusIndicator" class="dirty-badge" style="display:none;">
<span class="w-2.5 h-2.5 rounded-full bg-amber-500 animate-pulse"></span>
มีการแก้ไขที่ยังไม่ได้บันทึก (กด Ctrl+S เพื่อบันทึก)
</span>
</div>
<div class="bottom-actions flex items-center gap-3">
<button class="btn btn-secondary font-bold text-sm px-4 py-2" onclick="previewSurvey()">
👁️ พรีวิวแบบสอบถาม
</button>
<button id="saveSurveyBtnBottom" class="btn btn-primary font-bold text-sm px-6 py-2.5 shadow-lg bg-blue-600 hover:bg-blue-700 text-white" onclick="saveSurvey()">
💾 บันทึกการเปลี่ยนแปลง
</button>
</div>
</div>
<script src="js/app.js?v=6"></script>
<script src="js/editor.js?v=6"></script>
</body> </body>
</html> </html>
+57 -24
View File
@@ -346,26 +346,32 @@ function toggleOptions(select) {
async function saveSurvey() { async function saveSurvey() {
if (!currentSurvey) return; if (!currentSurvey) return;
const title = document.getElementById('edit-survey-title').value; const titleEl = document.getElementById('edit-survey-title');
const description = document.getElementById('edit-survey-desc').value; const title = titleEl ? titleEl.value.trim() : currentSurvey.title;
const hasCertificate = document.getElementById('edit-survey-cert').checked ? 1 : 0; if (!title) {
Toast.error('กรุณาระบุชื่อแบบสอบถาม');
return;
}
const description = document.getElementById('edit-survey-desc')?.value || '';
const hasCertificate = document.getElementById('edit-survey-cert')?.checked ? 1 : 0;
const sections = []; const sections = [];
document.querySelectorAll('[data-section]').forEach((sectionEl, si) => { document.querySelectorAll('[data-section]').forEach((sectionEl, si) => {
const sTitle = sectionEl.querySelector('.section-title-input')?.value || `ส่วนที่ ${si+1}`; const sTitle = sectionEl.querySelector('.section-title-input')?.value?.trim() || `ส่วนที่ ${si+1}`;
const sCategory = sectionEl.querySelector('.section-category-select')?.value || 'rating'; const sCategory = sectionEl.querySelector('.section-category-select')?.value || 'rating';
// Collect headings and their questions // Collect headings and their questions
const headings = []; const headings = [];
sectionEl.querySelectorAll('.heading-block').forEach((headingEl, hi) => { sectionEl.querySelectorAll('.heading-block').forEach((headingEl, hi) => {
const hTitle = headingEl.querySelector('.heading-title-input')?.value || ''; const hTitle = headingEl.querySelector('.heading-title-input')?.value?.trim() || '';
const hDesc = headingEl.querySelector('.heading-desc-input')?.value || ''; const hDesc = headingEl.querySelector('.heading-desc-input')?.value?.trim() || '';
const hQuestions = collectQuestions(headingEl.querySelector('.heading-questions-list')); const hQuestions = collectQuestions(headingEl.querySelector('.heading-questions-list'));
if (hTitle.trim()) { if (hTitle) {
headings.push({ headings.push({
title: hTitle.trim(), title: hTitle,
description: hDesc.trim() || null, description: hDesc || null,
order_num: hi, order_num: hi,
questions: hQuestions questions: hQuestions
}); });
@@ -384,14 +390,22 @@ async function saveSurvey() {
}); });
}); });
try { const setSaveButtonsLoading = (loading) => {
const btn = document.querySelector('.btn-primary'); const btns = [document.getElementById('saveSurveyBtnTop'), document.getElementById('saveSurveyBtnBottom')];
btn.disabled = true; btns.forEach(btn => {
btn.textContent = '⌛ กำลังบันทึก...'; if (btn) {
btn.disabled = loading;
btn.innerHTML = loading ? '<i class="fas fa-spinner fa-spin"></i> กำลังบันทึก...' : '💾 บันทึกการเปลี่ยนแปลง';
}
});
};
const certificateTemplate = document.getElementById('edit-survey-cert-template-path').value || null; try {
const certNameY = parseFloat(document.getElementById('edit-survey-cert-name-y').value || 82.5); setSaveButtonsLoading(true);
const certCodeY = parseFloat(document.getElementById('edit-survey-cert-code-y').value || 190.5);
const certificateTemplate = document.getElementById('edit-survey-cert-template-path')?.value || null;
const certNameY = parseFloat(document.getElementById('edit-survey-cert-name-y')?.value || 82.5);
const certCodeY = parseFloat(document.getElementById('edit-survey-cert-code-y')?.value || 190.5);
await API.post('surveys.php?action=update', { await API.post('surveys.php?action=update', {
id: currentSurvey.id, id: currentSurvey.id,
@@ -404,21 +418,25 @@ async function saveSurvey() {
sections sections
}); });
Toast.success('บันทึกสำเร็จ'); Toast.success('บันทึกการแก้ไขเรียบร้อยแล้ว');
isDirty = false; isDirty = false;
const dirtyIndicator = document.getElementById('saveStatusIndicator');
if (dirtyIndicator) dirtyIndicator.style.display = 'none';
// Update survey title display in header
const headerTitleEl = document.getElementById('surveyTitleDisplay');
if (headerTitleEl) headerTitleEl.textContent = title;
} catch (err) { } catch (err) {
Toast.error(err.message); Toast.error(err.message || 'บันทึกล้มเหลว');
} finally { } finally {
const btn = document.querySelector('.btn-primary'); setSaveButtonsLoading(false);
btn.disabled = false;
btn.textContent = '💾 บันทึกการเปลี่ยนแปลง';
} }
} }
function collectQuestions(container) { function collectQuestions(container) {
if (!container) return []; if (!container) return [];
const questions = []; const questions = [];
container.querySelectorAll(':scope > .question-builder').forEach((qEl, qi) => { container.querySelectorAll('.question-builder').forEach((qEl, qi) => {
const qText = qEl.querySelector('.q-text-input')?.value || ''; const qText = qEl.querySelector('.q-text-input')?.value || '';
const qDescription = qEl.querySelector('.q-desc-input')?.value || ''; const qDescription = qEl.querySelector('.q-desc-input')?.value || '';
const qType = qEl.querySelector('.q-type-select')?.value || 'radio'; const qType = qEl.querySelector('.q-type-select')?.value || 'radio';
@@ -440,24 +458,39 @@ function collectQuestions(container) {
} }
/* ============================================================ /* ============================================================
UTILITY FUNCTIONS UTILITY FUNCTIONS & SHORTCUTS
============================================================ */ ============================================================ */
function markDirty() { function markDirty() {
isDirty = true; isDirty = true;
const dirtyIndicator = document.getElementById('saveStatusIndicator');
if (dirtyIndicator) dirtyIndicator.style.display = 'inline-flex';
} }
function closeEditor() { function closeEditor() {
if (isDirty) { if (isDirty) {
if (!confirm('มีการเปลี่ยนแปลงที่ยังไม่ได้บันทึก ยืนยันปิดหรือไม่?')) return; if (!confirm('มีการเปลี่ยนแปลงที่ยังไม่ได้บันทึก ยืนยันปิดหรือไม่?')) return;
} }
if (window.history.length > 1) {
window.history.back();
} else {
window.close(); window.close();
} }
}
function previewSurvey() { function previewSurvey() {
if (!currentSurvey) return; if (!currentSurvey) return;
window.open(`respond.php?link=${currentSurvey.unique_link}`, '_blank'); const basePath = window.location.pathname.includes('/survey') ? '/survey' : '';
window.open(`${basePath}/respond.php?link=${currentSurvey.unique_link}`, '_blank');
} }
// Keyboard shortcut: Ctrl+S / Cmd+S to save
document.addEventListener('keydown', (e) => {
if ((e.ctrlKey || e.metaKey) && e.key === 's') {
e.preventDefault();
saveSurvey();
}
});
// Warn before leave if dirty // Warn before leave if dirty
window.addEventListener('beforeunload', (e) => { window.addEventListener('beforeunload', (e) => {
if (isDirty) { if (isDirty) {