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;
|
2026-06-02 03:49:38 +00:00
|
|
|
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' },
|
2026-06-02 03:49:38 +00:00
|
|
|
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
|
2026-06-02 03:49:38 +00:00
|
|
|
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
|
2026-06-02 03:49:38 +00:00
|
|
|
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
|
2026-06-02 03:49:38 +00:00
|
|
|
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)
|
2026-06-02 03:49:38 +00:00
|
|
|
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;
|
2026-06-02 03:49:38 +00:00
|
|
|
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)
|
|
|
|
|
function openCreateNoteModal(notepadId) {
|
|
|
|
|
activeNotepadId = notepadId;
|
|
|
|
|
document.getElementById('modal-create-note').classList.remove('hidden');
|
|
|
|
|
}
|
2026-06-02 03:49:38 +00:00
|
|
|
en');
|
|
|
|
|
}
|