From 1273fee1519176b75f95a3eec0d58cbba4c8009c Mon Sep 17 00:00:00 2001 From: helamanl0424 Date: Wed, 3 Apr 2024 11:08:43 -0600 Subject: [PATCH] Fix search function --- samples/index.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/samples/index.js b/samples/index.js index b197f65..6617a97 100644 --- a/samples/index.js +++ b/samples/index.js @@ -6,7 +6,7 @@ document.addEventListener("DOMContentLoaded", async function() { // Set default photo storageURL = await getStorage(); - setCurrentPath(''); + setCurrentPath(storageURL); // Show all items in the root storage URL fetchAndDisplayItems(); @@ -176,7 +176,7 @@ async function fetchAndDisplayItems(folderPath = storageURL, goBack = false) { } // Function to create HTML for an item -function createItemHTML(itemName, url) { +function createItemHTML(itemName, dataURL) { const itemType = getItemType(itemName); let src = ''; let altText = ''; @@ -189,9 +189,9 @@ function createItemHTML(itemName, url) { onClickAttribute = `onclick="fetchAndDisplayItems('${getCurrentPath() + itemName}')"`; break; case 'photo': - src = url; + src = dataURL; altText = 'Photo'; - onClickAttribute = `onclick="displayFullSizePhoto('${url}', '${itemName}')"`; + onClickAttribute = `onclick="displayFullSizePhoto('${dataURL}', '${getCurrentPath() + itemName}')"`; break; case 'other': src = 'file.png'; @@ -205,11 +205,11 @@ function createItemHTML(itemName, url) { `; } -function displayFullSizePhoto(url, itemName) { +function displayFullSizePhoto(dataURL, pathURL) { const folderDiv = document.querySelector('.folder'); - folderDiv.innerHTML = ``; + folderDiv.innerHTML = ``; lastURL.push(getCurrentPath()); - setCurrentPath(getCurrentPath() + itemName); + setCurrentPath(pathURL); displayCurrentPath(getCurrentPath()); toggleControlPanel(false); } @@ -595,7 +595,8 @@ async function search() { if (path == null) { alert("File not found! Please make sure the file name is spelled correctly and the file extension is correct.") } else { - displayFullSizePhoto(path, path); + const dataURL = await getDataURL(path); + displayFullSizePhoto(dataURL, path); } } catch (error) { console.log("error in search: " + error); @@ -621,9 +622,9 @@ async function addFolder(folderName) { }); } -async function getDataURL(item) { +async function getDataURL(url) { try { - const event = `${getPicoURL()}1556/sample_app/fetch_file?fileURL=${getCurrentPath() + item}`; + const event = `${getPicoURL()}1556/sample_app/fetch_file?fileURL=${url}`; const response = await fetch(event); if (!response.ok) { throw new Error(`Fetch file failed: ${response.status}`); @@ -638,7 +639,7 @@ async function getDataURL(item) { async function prefetchDataURLs(items) { const urlPromises = items.map(item => { if (getItemType(item) === 'photo') { - return getDataURL(item).then(url => ({ itemName: item, url })); + return getDataURL(getCurrentPath() + item).then(url => ({ itemName: item, url })); } return Promise.resolve({ itemName: item, url: undefined }); });