spotnotes.app/static/assets/js/actions.js

123 lines
4.6 KiB
JavaScript
Raw Normal View History

2026-05-12 23:58:18 +00:00
document.addEventListener('DOMContentLoaded', () => {
initModals();
});
let activeNotepadId = null; // Tracks which notepad is currently selected
function initModals() {
// --- Create Notepad Logic ---
const modalPad = document.getElementById('modal-create-pad');
const btnOpenPad = document.getElementById('btn-create-pad');
const formPad = document.getElementById('form-create-pad');
if(btnOpenPad) {
btnOpenPad.addEventListener('click', () => {
modalPad.classList.remove('hidden');
});
}
if(formPad) {
formPad.addEventListener('submit', async (e) => {
e.preventDefault();
const name = document.getElementById('pad-name').value;
const isPublic = document.getElementById('pad-public').checked;
const color = document.getElementById('pad-color').value;
2026-05-12 23:58:18 +00:00
try {
const res = await fetch('/api/notepads', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, is_public: isPublic, color: color })
2026-05-12 23:58:18 +00:00
});
if (res.ok) {
const newPad = await res.json();
modalPad.classList.add('hidden');
formPad.reset();
// Transition to the new notepad
if (typeof window.SpotNotes.loadNotesForPad === 'function') window.SpotNotes.loadNotesForPad(newPad.id, newPad.name, newPad.is_public, newPad.owner_id);
2026-05-12 23:58:18 +00:00
} else {
alert('Failed to create notepad');
}
} catch (err) {
console.error(err);
}
});
}
// --- Create Note Logic ---
const modalNote = document.getElementById('modal-create-note');
const formNote = document.getElementById('form-create-note');
if(formNote) {
formNote.addEventListener('submit', async (e) => {
e.preventDefault();
if (!activeNotepadId) {
alert("No notepad selected!");
return;
}
const title = document.getElementById('note-title').value;
const content = document.getElementById('note-content').value;
// Use current map center
const center = window.SpotNotes.map.getCenter();
2026-05-12 23:58:18 +00:00
try {
const res = await fetch('/api/notes', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
title,
content,
lat: center.lat,
lng: center.lng,
notepad_id: activeNotepadId
})
});
if (res.ok) {
modalNote.classList.add('hidden');
formNote.reset();
// Refresh notes
if (typeof window.SpotNotes.loadNotesForPad === 'function') window.SpotNotes.loadNotesForPad(activeNotepadId);
2026-05-12 23:58:18 +00:00
} else {
alert('Failed to create note');
}
} catch (err) {
console.error(err);
}
});
}
// Close buttons
document.querySelectorAll('.close-modal').forEach(btn => {
btn.addEventListener('click', (e) => {
e.target.closest('.modal').classList.add('hidden');
});
});
}
// Helper to open edit notepad modal (called from index.js)
window.openEditPadModal = function(id, currentName, currentPublic, currentColor = '#3388ff') {
2026-05-12 23:58:18 +00:00
document.getElementById('pad-id-edit').value = id;
document.getElementById('pad-name-edit').value = currentName;
document.getElementById('pad-public-edit').checked = currentPublic;
document.getElementById('pad-color-edit').value = currentColor;
2026-05-12 23:58:18 +00:00
document.getElementById('modal-edit-pad').classList.remove('hidden');
};
// Helper to open edit note modal (called from index.js)
window.openEditNoteModal = function(id, currentTitle, currentContent) {
document.getElementById('note-id-edit').value = id;
document.getElementById('note-title-edit').value = currentTitle;
document.getElementById('note-content-edit').value = currentContent;
document.getElementById('modal-edit-note').classList.remove('hidden');
};
// Helper to open note modal (called from index.js)
2026-06-02 04:22:59 +00:00
window.openCreateNoteModal = function(notepadId) {
2026-05-12 23:58:18 +00:00
activeNotepadId = notepadId;
document.getElementById('modal-create-note').classList.remove('hidden');
2026-06-02 04:22:59 +00:00
};