127 lines
6 KiB
JavaScript
127 lines
6 KiB
JavaScript
window.SpotNotes = window.SpotNotes || {};
|
|
|
|
window.SpotNotes.initSidebar = function() {
|
|
const sidebar = document.getElementById('sidebar');
|
|
const toggleBtn = document.getElementById('sidebar-toggle');
|
|
|
|
toggleBtn.addEventListener('click', () => {
|
|
sidebar.classList.toggle('collapsed');
|
|
// Change arrow direction based on state
|
|
if (sidebar.classList.contains('collapsed')) {
|
|
const sections = document.querySelectorAll('.list-section');
|
|
sections.forEach(section => {
|
|
const content = section.querySelector('.list-section-content');
|
|
if (content) content.style.display = 'none';
|
|
});
|
|
toggleBtn.innerHTML = '<'; // Point left (open)
|
|
} else {
|
|
toggleBtn.innerHTML = '>'; // Point right (close)
|
|
}
|
|
});
|
|
|
|
const centerBtn = document.getElementById('btn-center-user');
|
|
if (centerBtn) {
|
|
centerBtn.addEventListener('click', () => {
|
|
window.SpotNotes.isTracking = true;
|
|
window.SpotNotes.updateTrackingUI();
|
|
if (window.SpotNotes.userLocation) {
|
|
window.SpotNotes.map.setView([window.SpotNotes.userLocation.lat, window.SpotNotes.userLocation.lng], 16);
|
|
}
|
|
});
|
|
}
|
|
};
|
|
|
|
window.SpotNotes.setupPublicToggle = function() {
|
|
const btn = document.getElementById('btn-toggle-public');
|
|
const list = document.getElementById('public-notepads-list');
|
|
|
|
if (!btn || !list) return;
|
|
|
|
btn.addEventListener('click', async () => {
|
|
if (list.style.display === 'none') {
|
|
list.style.display = 'block';
|
|
btn.innerHTML = 'Public Notepads ▾';
|
|
|
|
// Lazy load if empty
|
|
if (list.innerHTML === '') {
|
|
list.innerHTML = '<p style="padding:10px; color:#666;">Loading...</p>';
|
|
try {
|
|
const res = await fetch('/api/notepads?type=public');
|
|
const pads = await res.json();
|
|
window.SpotNotes.renderNotepadList(pads, 'public-notepads-list');
|
|
} catch (err) {
|
|
list.innerHTML = '<p style="color:red; padding:10px;">Error loading public pads.</p>';
|
|
}
|
|
}
|
|
} else {
|
|
list.style.display = 'none';
|
|
btn.innerHTML = 'Public Notepads ▸';
|
|
}
|
|
});
|
|
};
|
|
|
|
window.SpotNotes.renderNotepadList = function(pads, containerId, isFavorite) {
|
|
const container = document.getElementById(containerId);
|
|
if (!container) return;
|
|
container.innerHTML = '';
|
|
|
|
if (pads.length === 0) {
|
|
container.innerHTML = '<p style="padding:10px; color:#999; font-style:italic;">No notepads found.</p>';
|
|
return;
|
|
}
|
|
|
|
pads.forEach(pad => {
|
|
const item = document.createElement('div');
|
|
item.className = 'notepad-item';
|
|
item.style.padding = '10px';
|
|
item.style.borderBottom = '1px solid #eee';
|
|
item.style.cursor = 'pointer';
|
|
|
|
let icon = pad.is_public ? '🌍' : '🔒';
|
|
|
|
let favButton = '';
|
|
const editBtn = (window.SpotNotes.currentUser && pad.owner_id === window.SpotNotes.currentUser.id) ?
|
|
`<button onclick="event.stopPropagation(); openEditPadModal(${pad.id}, '${window.SpotNotes.escapeHtml(pad.name)}', ${pad.is_public}, '${pad.color || '#3388ff'}')" title="Edit Notepad" style="border:none; background:none; cursor:pointer; padding:0 5px;">✏️</button>`
|
|
: '';
|
|
|
|
const isOwner = window.SpotNotes.currentUser && pad.owner_id === window.SpotNotes.currentUser.id;
|
|
const manageShareBtn = isOwner ?
|
|
`<button onclick="event.stopPropagation(); openShareSettingsModal(${pad.id}, '${window.SpotNotes.escapeHtml(pad.name)}')" title="Manage Sharing" style="border:none; background:none; cursor:pointer; padding:0 5px;">⚙️</button>`
|
|
: '';
|
|
|
|
if(window.SpotNotes.isLoggedIn){
|
|
const action = isFavorite ? 'unfavorite' : 'favorite';
|
|
const label = isFavorite ? '♥' : '♡';
|
|
favButton = `<button onclick="event.stopPropagation(); window.SpotNotes.toggleFavorite(${pad.id}, '${action}')" style="color:#dc3545; border:none; background:none; cursor:pointer; font-weight:bold; padding:0 5px;">${label}</button>`;
|
|
}
|
|
|
|
const deleteBtn = isOwner ?
|
|
`<button onclick="event.stopPropagation(); window.SpotNotes.deleteNotepad(${pad.id})" style="color:#dc3545; border:none; background:none; cursor:pointer; font-weight:bold; padding:0 5px;">×</button>`
|
|
: '';
|
|
|
|
const shareToken = pad.share_token || '';
|
|
const shareBtn = `<button onclick="event.stopPropagation(); window.SpotNotes.shareNotepad(${pad.id}, '${shareToken}')" title="Share Notepad" style="border:none; background:none; cursor:pointer; padding:0 5px;">🔗</button>`;
|
|
|
|
item.innerHTML = `
|
|
<div style="display:flex; justify-content:space-between; align-items:center;">
|
|
<div style="font-weight:bold;">${icon} ${window.SpotNotes.escapeHtml(pad.name)}</div>
|
|
<div style="display: flex; align-items: center;">
|
|
${editBtn} ${manageShareBtn} ${shareBtn} ${favButton}
|
|
${deleteBtn}
|
|
</div>
|
|
<div style="font-size:0.8rem; color:#666;">${pad.note_count || 0} notes</div>
|
|
</div>
|
|
`;
|
|
|
|
item.addEventListener('click', () => window.SpotNotes.loadNotesForPad(pad.id, pad.name, pad.is_public, pad.owner_id, false, null, pad.can_write));
|
|
container.appendChild(item);
|
|
});
|
|
};
|
|
|
|
window.SpotNotes.toggleSection = function(event) {
|
|
const section = event.target.closest('.list-section');
|
|
const content = section.querySelector('.list-section-content');
|
|
const isCollapsed = content.style.display === 'none';
|
|
content.style.display = isCollapsed ? 'block' : 'none';
|
|
event.target.innerHTML = isCollapsed ? event.target.innerHTML.replace('▸', '▾') : event.target.innerHTML.replace('▾', '▸');
|
|
};
|