From 25a8de02b54c00be62c29afc81b4e9c3239f6c59 Mon Sep 17 00:00:00 2001 From: Valentin Korenjak Date: Sat, 21 Mar 2026 16:21:22 +0100 Subject: [PATCH] Edit poljubne in ustvari novo. #11 --- web/server.py | 39 +++++++++++++++--- web/static/script.js | 88 ++++++++++++++++++++++++++++++++++++++-- web/templates/index.html | 31 +++++++++++++- 3 files changed, 148 insertions(+), 10 deletions(-) diff --git a/web/server.py b/web/server.py index 3ae0e7e..f8a4d9b 100644 --- a/web/server.py +++ b/web/server.py @@ -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() diff --git a/web/static/script.js b/web/static/script.js index c828a7a..601ceeb 100644 --- a/web/static/script.js +++ b/web/static/script.js @@ -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.'); - menuDropdown.classList.remove('show'); + 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; diff --git a/web/templates/index.html b/web/templates/index.html index 7f48d72..4eb7edb 100644 --- a/web/templates/index.html +++ b/web/templates/index.html @@ -33,7 +33,7 @@