Edit poljubne in ustvari novo. #11

This commit is contained in:
2026-03-21 16:21:22 +01:00
parent 67fa26a0b1
commit 25a8de02b5
3 changed files with 148 additions and 10 deletions

View File

@@ -156,15 +156,25 @@ def search_songs():
@app.route('/api/get_song_details', methods=['GET'])
def get_song_details():
"""Vrne podrobnosti trenutno naložene pesmi"""
"""Vrne podrobnosti trenutno naložene pesmi ali pesmi po ID-ju"""
if _projector_app is None:
return jsonify({'status': 'error', 'message': 'Aplikacija ni inicijalizirana'})
if not _projector_app.song_number_last:
song_id_param = request.args.get('id')
if song_id_param:
try:
song_id = int(song_id_param)
except ValueError:
return jsonify({'status': 'error', 'message': 'Neveljaven ID pesmi'})
elif _projector_app.song_number_last:
try:
song_id = int(_projector_app.song_number_last)
except ValueError:
return jsonify({'status': 'error', 'message': 'Neveljavna številka naložene pesmi'})
else:
return jsonify({'status': 'error', 'message': 'Nobena pesem ni naložena'})
try:
song_id = int(_projector_app.song_number_last)
_projector_app.cursor.execute("SELECT id, title, lyrics FROM songs WHERE id=?", (song_id,))
result = _projector_app.cursor.fetchone()
if result:
@@ -177,14 +187,14 @@ def get_song_details():
}
})
else:
return jsonify({'status': 'error', 'message': 'Pesem ni najdena v bazi'})
return jsonify({'status': 'error', 'message': f'Pesem s številko {song_id} ni najdena v bazi'})
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)})
@app.route('/api/update_song', methods=['POST'])
def update_song():
"""Posodobi naslov in besedilo pesmi"""
"""Posodobi naslov in besedilo pesmi ali ustvari novo"""
if _projector_app is None:
return jsonify({'status': 'error', 'message': 'Aplikacija ni inicijalizirana'})
@@ -197,6 +207,25 @@ def update_song():
return jsonify({'status': 'error', 'message': 'Manjkajoči podatki'})
try:
if song_id == 'new':
# Pridobi prvo naslednjo prosto številko
_projector_app.cursor.execute("SELECT MAX(id) FROM songs")
max_id = _projector_app.cursor.fetchone()[0]
new_id = (max_id or 0) + 1
_projector_app.cursor.execute(
"INSERT INTO songs (id, title, lyrics) VALUES (?, ?, ?)",
(new_id, title, lyrics)
)
_projector_app.conn.commit()
# Naloži novo pesem
_projector_app.song_number = str(new_id)
_projector_app.load_song()
return jsonify({'status': 'ok', 'new_id': new_id})
# Obstoječa pesem
_projector_app.cursor.execute("UPDATE songs SET title=?, lyrics=? WHERE id=?", (title, lyrics, song_id))
_projector_app.conn.commit()

View File

@@ -22,6 +22,19 @@ const editTitleInput = document.getElementById('edit-title');
const editLyricsInput = document.getElementById('edit-lyrics');
const saveEditBtn = document.getElementById('save-edit-btn');
const cancelEditBtn = document.getElementById('cancel-edit-btn');
const editSongNumberDisplay = document.getElementById('edit-song-number-display');
// Prompt Modal elementi
const promptModal = document.getElementById('prompt-modal');
const promptInput = document.getElementById('prompt-input');
const promptEditBtn = document.getElementById('prompt-edit-btn');
const promptCancelBtn = document.getElementById('prompt-cancel-btn');
const promptNewBtn = document.getElementById('prompt-new-btn');
// Info Modal elementi
const infoModal = document.getElementById('info-modal');
const infoMessage = document.getElementById('info-message');
const infoOkBtn = document.getElementById('info-ok-btn');
let capsMode = false;
let wakeLock = null;
@@ -225,20 +238,38 @@ async function toggleCaps() {
}
// Odpri urejevalnik
async function openEditor() {
async function openEditor(songId = null) {
try {
const response = await fetch('/api/get_song_details');
if (songId === 'new') {
editTitleInput.value = '';
editLyricsInput.value = '';
editModal.dataset.songId = 'new';
editSongNumberDisplay.textContent = '';
editModal.style.display = 'block';
menuDropdown.classList.remove('show');
return;
}
const url = songId ? `/api/get_song_details?id=${songId}` : '/api/get_song_details';
const response = await fetch(url);
const data = await response.json();
if (data.status === 'ok') {
editTitleInput.value = data.song.title;
editLyricsInput.value = data.song.lyrics;
editModal.dataset.songId = data.song.id;
editSongNumberDisplay.textContent = data.song.id;
editModal.style.display = 'block';
menuDropdown.classList.remove('show');
} else {
alert(data.message || 'Za urejanje najprej naložite besedilo.');
if (!songId) {
// Če ni naložene pesmi, pokaži prompt
promptModal.style.display = 'block';
promptInput.value = '';
menuDropdown.classList.remove('show');
} else {
alert(data.message || 'Napaka pri pridobivanju podatkov.');
}
}
} catch (error) {
console.error('Napaka pri pridobivanju podatkov:', error);
@@ -246,6 +277,11 @@ async function openEditor() {
}
}
function showInfo(message) {
infoMessage.textContent = message;
infoModal.style.display = 'block';
}
// Zapri urejevalnik
function closeEditor() {
editModal.style.display = 'none';
@@ -276,6 +312,9 @@ async function saveSongEdit() {
if (data.status === 'ok') {
closeEditor();
if (data.new_id) {
showInfo(`Nova pesem je bila shranjena pod številko: ${data.new_id}`);
}
await updateState(true);
} else {
alert('Napaka pri shranjevanju: ' + data.message);
@@ -376,6 +415,39 @@ if (saveEditBtn) {
});
}
// Prompt Modal dogodki
if (promptEditBtn) {
promptEditBtn.addEventListener('click', () => {
const id = promptInput.value.trim();
if (id) {
promptModal.style.display = 'none';
openEditor(id);
} else {
alert('Vnesite številko pesmi.');
}
});
}
if (promptCancelBtn) {
promptCancelBtn.addEventListener('click', () => {
promptModal.style.display = 'none';
});
}
if (promptNewBtn) {
promptNewBtn.addEventListener('click', () => {
promptModal.style.display = 'none';
openEditor('new');
});
}
// Info Modal dogodki
if (infoOkBtn) {
infoOkBtn.addEventListener('click', () => {
infoModal.style.display = 'none';
});
}
// Hamburger menu toggle
if (menuToggle) {
menuToggle.addEventListener('click', (e) => {
@@ -430,6 +502,14 @@ document.addEventListener('keydown', (e) => {
return;
}
// Če smo v promptu za številko, ne procesiraj bližnjic za numerično tipkovnico
if (promptModal && promptModal.style.display === 'block') {
if (e.key === 'Escape') {
promptModal.style.display = 'none';
}
return;
}
// na telefonu ni potrebe; na velikih ekranih pa naj dela
if (window.innerWidth < 901) return;

View File

@@ -33,7 +33,7 @@
<!-- Edit Modal -->
<div id="edit-modal" class="modal">
<div class="modal-content">
<h2 id="edit-modal-title">Uredi besedilo</h2>
<h2 id="edit-modal-title">Uredi besedilo <span id="edit-song-number-display" style="float: right; color: var(--text-muted);"></span></h2>
<div class="form-group">
<label for="edit-title">Naslov:</label>
<input type="text" id="edit-title" class="form-control">
@@ -49,6 +49,35 @@
</div>
</div>
<!-- Prompt Modal -->
<div id="prompt-modal" class="modal">
<div class="modal-content">
<h2 id="prompt-title">Uredi besedilo</h2>
<p id="prompt-message">Nobena pesem ni naložena. Vnesite številko pesmi za urejanje ali ustvarite novo.</p>
<div class="form-group">
<input type="number" id="prompt-input" class="form-control" placeholder="Številka pesmi">
</div>
<div class="modal-footer" style="justify-content: space-between;">
<button id="prompt-new-btn" class="btn-secondary" style="background-color: #c28b24; color: white; border: none;">Ustvari novo</button>
<div style="display: flex; gap: 10px;">
<button id="prompt-cancel-btn" class="btn-secondary">Prekliči</button>
<button id="prompt-edit-btn" class="btn-primary">Uredi</button>
</div>
</div>
</div>
</div>
<!-- Info Modal -->
<div id="info-modal" class="modal">
<div class="modal-content">
<h2 id="info-title">Informacija</h2>
<p id="info-message"></p>
<div class="modal-footer">
<button id="info-ok-btn" class="btn-primary">V redu</button>
</div>
</div>
</div>
<div class="content">
<div id="display-area" class="lyrics-display">
<span class="status-message">Pripravljeno. Vpiši številko pesmi ali drugega besedila.</span>