// JavaScript for Bookmarkly document.addEventListener('DOMContentLoaded', function() { // Handle Edit Category Modal const editCategoryModal = document.getElementById('editCategoryModal'); if (editCategoryModal) { editCategoryModal.addEventListener('show.bs.modal', function (event) { const button = event.relatedTarget; const id = button.getAttribute('data-bs-id'); const name = button.getAttribute('data-bs-name'); const idInput = editCategoryModal.querySelector('#edit-category-id'); const nameInput = editCategoryModal.querySelector('#edit-category-name'); idInput.value = id; nameInput.value = name; }); } // --- Bookmark Modals --- const editBookmarkModal = document.getElementById('editBookmarkModal'); if (editBookmarkModal) { editBookmarkModal.addEventListener('show.bs.modal', function (event) { const button = event.relatedTarget; const id = button.getAttribute('data-bs-id'); const title = button.getAttribute('data-bs-title'); const url = button.getAttribute('data-bs-url'); const category = button.getAttribute('data-bs-category'); const icon = button.getAttribute('data-bs-icon'); const iconSrc = button.getAttribute('data-bs-icon-src'); const favorite = button.getAttribute('data-bs-favorite'); const modalTitle = editBookmarkModal.querySelector('.modal-title'); const idInput = editBookmarkModal.querySelector('#edit-bookmark-id'); const titleInput = editBookmarkModal.querySelector('#edit-bookmark-title'); const urlInput = editBookmarkModal.querySelector('#edit-bookmark-url'); const categoryInput = editBookmarkModal.querySelector('#edit-bookmark-category'); const iconUrlInput = editBookmarkModal.querySelector('#edit-bookmark-icon-url'); const favoriteInput = editBookmarkModal.querySelector('#edit-bookmark-favorite'); const currentIconPreview = document.getElementById('current-icon-preview'); const currentIconContainer = document.getElementById('current-icon-container'); const removeCustomIconBtn = document.getElementById('remove-custom-icon'); const removeIconInput = document.getElementById('remove-icon-input'); modalTitle.textContent = 'Edit ' + title; idInput.value = id; titleInput.value = title; urlInput.value = url; categoryInput.value = category; favoriteInput.checked = (favorite == '1'); // Handle icon display logic currentIconContainer.style.display = 'none'; // Hide by default // Handle text input for icon URL/domain if (icon && icon.startsWith('data/images/custom/')) { iconUrlInput.value = ''; // Clear if it's a custom icon } else { // For domains or full URLs, just show the value in the input. iconUrlInput.value = icon; } // Handle current icon preview and remove button if (icon && icon.startsWith('data/images/custom/')) { currentIconPreview.src = iconSrc; currentIconContainer.style.display = 'block'; removeIconInput.value = '0'; } else { currentIconContainer.style.display = 'none'; } removeCustomIconBtn.onclick = function() { removeIconInput.value = '1'; currentIconContainer.style.display = 'none'; document.getElementById('edit-bookmark-icon-upload').value = ''; }; }); // Reset remove icon flag when modal is hidden editBookmarkModal.addEventListener('hidden.bs.modal', function () { document.getElementById('remove-icon-input').value = '0'; document.getElementById('edit-bookmark-icon-upload').value = ''; // Clear file input }); } // --- Favicon Fetching --- function fetchFavicon(url, targetInputId) { if (!url) return; try { // We only want to store the domain, not the full generated URL const domain = new URL(url).hostname; const targetInput = document.getElementById(targetInputId); if (targetInput) { targetInput.value = domain; } } catch (e) { console.error('Invalid URL for favicon fetching:', e); const targetInput = document.getElementById(targetInputId); if (targetInput) { targetInput.value = ''; } } } // Event listener for fetching favicon in ADD modal const fetchButtonAdd = document.getElementById('fetch-favicon-add'); const urlInputAdd = document.getElementById('bookmark-url'); if (fetchButtonAdd && urlInputAdd) { // Auto-fetch on change urlInputAdd.addEventListener('change', function() { fetchFavicon(urlInputAdd.value, 'bookmark-icon-url'); }); // Manual fetch on button click fetchButtonAdd.addEventListener('click', function() { fetchFavicon(urlInputAdd.value, 'bookmark-icon-url'); }); } // Event listener for fetching favicon in EDIT modal const fetchButtonEdit = document.getElementById('fetch-favicon-edit'); const urlInputEdit = document.getElementById('edit-bookmark-url'); if (fetchButtonEdit && urlInputEdit) { urlInputEdit.addEventListener('change', function() { const iconContainer = document.getElementById('current-icon-container'); // Only fetch favicon if there is no custom icon currently set if (iconContainer && iconContainer.style.display === 'none') { fetchFavicon(urlInputEdit.value, 'edit-bookmark-icon-url'); } }); fetchButtonEdit.addEventListener('click', function() { fetchFavicon(urlInputEdit.value, 'edit-bookmark-icon-url'); }); } // --- Pexels API --- const pexelsSearchButton = document.getElementById('pexels_search_button'); const pexelsSearchQuery = document.getElementById('pexels_search_query'); const pexelsResultsContainer = document.getElementById('pexels_results'); const bgUrlInput = document.getElementById('background_image_url'); if (pexelsSearchButton && pexelsSearchQuery && pexelsResultsContainer && bgUrlInput) { const performSearch = () => { const query = pexelsSearchQuery.value; if (!query) { alert('Please enter a search term.'); return; } pexelsResultsContainer.innerHTML = '