Spaces:
Running
Running
Update main.js
Browse files
main.js
CHANGED
@@ -1,19 +1,18 @@
|
|
1 |
document.addEventListener('DOMContentLoaded', () => {
|
2 |
// --- CONFIGURATION ---
|
3 |
const config = {
|
4 |
-
backendUrl: 'https://ar-city-explorer-backend.aiagents.workers.dev' //
|
5 |
};
|
6 |
|
7 |
// --- GLOBAL STATE ---
|
8 |
let poisData = [];
|
9 |
let arActive = false;
|
10 |
-
// Add this object to hold our settings
|
11 |
let userSettings = {
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
};
|
17 |
|
18 |
// --- DOM ELEMENT SELECTORS ---
|
19 |
const arToggle = document.getElementById('arToggle');
|
@@ -27,14 +26,11 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
27 |
const objectDescription = document.getElementById('objectDescription');
|
28 |
const objectImage = document.getElementById('objectImage');
|
29 |
const closeObjectModal = document.getElementById('closeObjectModal');
|
30 |
-
// Selectors for the Settings Modal
|
31 |
const settingsBtn = document.getElementById('settingsBtn');
|
32 |
-
console.log('The settings button element is:', settingsBtn); // <-- ADD THIS LINE
|
33 |
const settingsModal = document.getElementById('settingsModal');
|
34 |
const closeSettingsModal = document.getElementById('closeSettingsModal');
|
35 |
|
36 |
-
|
37 |
-
// --- CORE FUNCTIONS (for AR and Data) ---
|
38 |
|
39 |
function toggleARView(showAR) {
|
40 |
arActive = showAR;
|
@@ -86,8 +82,6 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
86 |
toggleARView(false);
|
87 |
});
|
88 |
}
|
89 |
-
|
90 |
-
// --- UI FUNCTIONS (Modals, Chat, Search) ---
|
91 |
|
92 |
function showObjectInfo(poiId) {
|
93 |
const poi = poisData.find(p => p.id === poiId);
|
@@ -113,87 +107,63 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
113 |
}
|
114 |
|
115 |
function handleSearch() {
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
const results = poisData.filter(poi =>
|
134 |
-
poi.name.toLowerCase().includes(searchTerm.toLowerCase()) ||
|
135 |
-
(poi.description && poi.description.toLowerCase().includes(searchTerm.toLowerCase()))
|
136 |
-
);
|
137 |
-
|
138 |
-
if (results.length > 0) {
|
139 |
-
let responseMessage = `I found ${results.length} local result(s):<ul>`;
|
140 |
-
results.forEach(poi => { responseMessage += `<li class="mt-2 list-disc list-inside">${poi.name}</li>`; });
|
141 |
-
responseMessage += "</ul>";
|
142 |
-
addAIMessage(responseMessage);
|
143 |
-
} else {
|
144 |
-
addAIMessage(`Sorry, I couldn't find any local places matching "${searchTerm}".`);
|
145 |
-
}
|
146 |
-
|
147 |
-
} else {
|
148 |
-
// --- NEW LOGIC: ASK THE GEMINI AI ---
|
149 |
-
// 1. Show a temporary "thinking" message for better UX
|
150 |
-
addAIMessage("AI is thinking...");
|
151 |
-
|
152 |
-
// 2. Call your backend's new "/api/ask" endpoint
|
153 |
-
fetch(`${config.backendUrl}/api/ask`, {
|
154 |
-
method: 'POST',
|
155 |
-
headers: {
|
156 |
-
'Content-Type': 'application/json'
|
157 |
-
},
|
158 |
-
body: JSON.stringify({ prompt: searchTerm }) // Send the user's message
|
159 |
-
})
|
160 |
-
.then(response => {
|
161 |
-
if (!response.ok) {
|
162 |
-
throw new Error('Network response was not ok');
|
163 |
-
}
|
164 |
-
return response.json();
|
165 |
-
})
|
166 |
-
.then(data => {
|
167 |
-
// 3. Find the "AI is thinking..." message and replace its content with the real response
|
168 |
-
const chatContainer = aiAssistant.querySelector('.flex-col');
|
169 |
-
const thinkingMessage = Array.from(chatContainer.querySelectorAll('.assistant-message')).pop();
|
170 |
-
|
171 |
-
if (thinkingMessage && thinkingMessage.textContent.includes("AI is thinking...")) {
|
172 |
-
thinkingMessage.querySelector('p').innerHTML = data.response.replace(/\n/g, '<br>'); // Update the message and format newlines
|
173 |
} else {
|
174 |
-
addAIMessage(
|
175 |
}
|
176 |
-
}
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
|
183 |
-
if (thinkingMessage && thinkingMessage.textContent.includes("AI is thinking...")) {
|
184 |
-
thinkingMessage.querySelector('p').innerHTML = "Sorry, I had trouble connecting to the AI. Please try again.";
|
185 |
-
} else {
|
186 |
-
addAIMessage("Sorry, I had trouble connecting to the AI. Please try again.");
|
187 |
-
}
|
188 |
-
});
|
189 |
-
}
|
190 |
-
|
191 |
// --- EVENT LISTENERS ---
|
192 |
|
193 |
-
// AR Mode Toggle
|
194 |
arToggle.addEventListener('click', () => toggleARView(!arActive));
|
195 |
|
196 |
-
// Clicking on AR objects
|
197 |
document.querySelector('a-scene').addEventListener('click', (event) => {
|
198 |
if (event.target.hasAttribute('data-poi-id')) {
|
199 |
const poiId = parseInt(event.target.getAttribute('data-poi-id'), 10);
|
@@ -201,16 +171,13 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
201 |
}
|
202 |
});
|
203 |
|
204 |
-
// Info Modal Close Button
|
205 |
closeObjectModal.addEventListener('click', () => objectModal.classList.add('hidden'));
|
206 |
|
207 |
-
// Search Bar
|
208 |
sendBtn.addEventListener('click', handleSearch);
|
209 |
userInput.addEventListener('keypress', (e) => {
|
210 |
if (e.key === 'Enter') handleSearch();
|
211 |
});
|
212 |
|
213 |
-
// Settings Modal Open/Close
|
214 |
settingsBtn.addEventListener('click', () => {
|
215 |
settingsModal.classList.remove('hidden');
|
216 |
});
|
@@ -222,40 +189,21 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
222 |
settingsModal.classList.add('hidden');
|
223 |
}
|
224 |
});
|
225 |
-
// --- Settings Toggles Functionality ---
|
226 |
-
// Select all checkboxes that have a "data-setting" attribute
|
227 |
-
const settingToggles = settingsModal.querySelectorAll('input[data-setting]');
|
228 |
-
|
229 |
-
settingToggles.forEach(toggle => {
|
230 |
-
// On page load, set the checkbox's state from our userSettings object
|
231 |
-
const settingName = toggle.dataset.setting;
|
232 |
-
if (userSettings[settingName] !== undefined) {
|
233 |
-
toggle.checked = userSettings[settingName];
|
234 |
-
}
|
235 |
-
|
236 |
-
// Add a listener that fires whenever a toggle is changed
|
237 |
-
toggle.addEventListener('change', (event) => {
|
238 |
-
const changedSettingName = event.target.dataset.setting;
|
239 |
-
const newValue = event.target.checked;
|
240 |
-
|
241 |
-
// Update our settings object with the new value
|
242 |
-
userSettings[changedSettingName] = newValue;
|
243 |
-
|
244 |
-
// For debugging, log the entire updated settings object to the console
|
245 |
-
// --- Replace the console.log line with this: ---
|
246 |
|
247 |
-
const
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
|
|
|
|
|
|
255 |
});
|
256 |
-
});
|
257 |
|
258 |
// --- INITIALIZATION ---
|
259 |
-
// Fetch data as soon as the app loads
|
260 |
fetchPoisAndCreateAREntities();
|
261 |
-
|
|
|
|
1 |
document.addEventListener('DOMContentLoaded', () => {
|
2 |
// --- CONFIGURATION ---
|
3 |
const config = {
|
4 |
+
backendUrl: 'https://ar-city-explorer-backend.aiagents.workers.dev' // This is the user's provided URL
|
5 |
};
|
6 |
|
7 |
// --- GLOBAL STATE ---
|
8 |
let poisData = [];
|
9 |
let arActive = false;
|
|
|
10 |
let userSettings = {
|
11 |
+
showHistorical: true,
|
12 |
+
showMenus: true,
|
13 |
+
showNavigation: false,
|
14 |
+
voiceResponses: true
|
15 |
+
};
|
16 |
|
17 |
// --- DOM ELEMENT SELECTORS ---
|
18 |
const arToggle = document.getElementById('arToggle');
|
|
|
26 |
const objectDescription = document.getElementById('objectDescription');
|
27 |
const objectImage = document.getElementById('objectImage');
|
28 |
const closeObjectModal = document.getElementById('closeObjectModal');
|
|
|
29 |
const settingsBtn = document.getElementById('settingsBtn');
|
|
|
30 |
const settingsModal = document.getElementById('settingsModal');
|
31 |
const closeSettingsModal = document.getElementById('closeSettingsModal');
|
32 |
|
33 |
+
// --- CORE & UI FUNCTIONS ---
|
|
|
34 |
|
35 |
function toggleARView(showAR) {
|
36 |
arActive = showAR;
|
|
|
82 |
toggleARView(false);
|
83 |
});
|
84 |
}
|
|
|
|
|
85 |
|
86 |
function showObjectInfo(poiId) {
|
87 |
const poi = poisData.find(p => p.id === poiId);
|
|
|
107 |
}
|
108 |
|
109 |
function handleSearch() {
|
110 |
+
const searchTerm = userInput.value.trim();
|
111 |
+
if (!searchTerm) return;
|
112 |
+
addUserMessage(searchTerm);
|
113 |
+
userInput.value = '';
|
114 |
+
const searchKeywords = ['search', 'find', 'show', 'where is', 'landmark', 'park', 'beach', 'hollywood', 'map', 'navigate'];
|
115 |
+
const isLocalSearch = searchKeywords.some(keyword => searchTerm.toLowerCase().includes(keyword));
|
116 |
+
if (isLocalSearch) {
|
117 |
+
if (poisData.length === 0) return addAIMessage("Location data is still loading...");
|
118 |
+
const results = poisData.filter(poi =>
|
119 |
+
poi.name.toLowerCase().includes(searchTerm.toLowerCase()) ||
|
120 |
+
(poi.description && poi.description.toLowerCase().includes(searchTerm.toLowerCase()))
|
121 |
+
);
|
122 |
+
if (results.length > 0) {
|
123 |
+
let responseMessage = `I found ${results.length} local result(s):<ul>`;
|
124 |
+
results.forEach(poi => { responseMessage += `<li class="mt-2 list-disc list-inside">${poi.name}</li>`; });
|
125 |
+
responseMessage += "</ul>";
|
126 |
+
addAIMessage(responseMessage);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
} else {
|
128 |
+
addAIMessage(`Sorry, I couldn't find any local places matching "${searchTerm}".`);
|
129 |
}
|
130 |
+
} else {
|
131 |
+
addAIMessage("AI is thinking...");
|
132 |
+
fetch(`${config.backendUrl}/api/ask`, {
|
133 |
+
method: 'POST',
|
134 |
+
headers: { 'Content-Type': 'application/json' },
|
135 |
+
body: JSON.stringify({ prompt: searchTerm })
|
136 |
+
})
|
137 |
+
.then(response => {
|
138 |
+
if (!response.ok) throw new Error('Network response was not ok');
|
139 |
+
return response.json();
|
140 |
+
})
|
141 |
+
.then(data => {
|
142 |
+
const chatContainer = aiAssistant.querySelector('.flex-col');
|
143 |
+
const thinkingMessage = Array.from(chatContainer.querySelectorAll('.assistant-message')).pop();
|
144 |
+
if (thinkingMessage && thinkingMessage.textContent.includes("AI is thinking...")) {
|
145 |
+
thinkingMessage.querySelector('p').innerHTML = data.response.replace(/\n/g, '<br>');
|
146 |
+
} else {
|
147 |
+
addAIMessage(data.response.replace(/\n/g, '<br>'));
|
148 |
+
}
|
149 |
+
})
|
150 |
+
.catch(error => {
|
151 |
+
console.error('Error asking AI:', error);
|
152 |
+
const chatContainer = aiAssistant.querySelector('.flex-col');
|
153 |
+
const thinkingMessage = Array.from(chatContainer.querySelectorAll('.assistant-message')).pop();
|
154 |
+
if (thinkingMessage && thinkingMessage.textContent.includes("AI is thinking...")) {
|
155 |
+
thinkingMessage.querySelector('p').innerHTML = "Sorry, I had trouble connecting to the AI.";
|
156 |
+
} else {
|
157 |
+
addAIMessage("Sorry, I had trouble connecting to the AI.");
|
158 |
+
}
|
159 |
+
});
|
160 |
+
}
|
161 |
+
} // <-- THIS CLOSING BRACE WAS MISSING
|
162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
// --- EVENT LISTENERS ---
|
164 |
|
|
|
165 |
arToggle.addEventListener('click', () => toggleARView(!arActive));
|
166 |
|
|
|
167 |
document.querySelector('a-scene').addEventListener('click', (event) => {
|
168 |
if (event.target.hasAttribute('data-poi-id')) {
|
169 |
const poiId = parseInt(event.target.getAttribute('data-poi-id'), 10);
|
|
|
171 |
}
|
172 |
});
|
173 |
|
|
|
174 |
closeObjectModal.addEventListener('click', () => objectModal.classList.add('hidden'));
|
175 |
|
|
|
176 |
sendBtn.addEventListener('click', handleSearch);
|
177 |
userInput.addEventListener('keypress', (e) => {
|
178 |
if (e.key === 'Enter') handleSearch();
|
179 |
});
|
180 |
|
|
|
181 |
settingsBtn.addEventListener('click', () => {
|
182 |
settingsModal.classList.remove('hidden');
|
183 |
});
|
|
|
189 |
settingsModal.classList.add('hidden');
|
190 |
}
|
191 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
|
193 |
+
const settingToggles = settingsModal.querySelectorAll('input[data-setting]');
|
194 |
+
settingToggles.forEach(toggle => {
|
195 |
+
const settingName = toggle.dataset.setting;
|
196 |
+
if (userSettings[settingName] !== undefined) {
|
197 |
+
toggle.checked = userSettings[settingName];
|
198 |
+
}
|
199 |
+
toggle.addEventListener('change', (event) => {
|
200 |
+
const changedSettingName = event.target.dataset.setting;
|
201 |
+
userSettings[changedSettingName] = event.target.checked;
|
202 |
+
console.log('Settings updated:', userSettings);
|
203 |
+
});
|
204 |
});
|
|
|
205 |
|
206 |
// --- INITIALIZATION ---
|
|
|
207 |
fetchPoisAndCreateAREntities();
|
208 |
+
|
209 |
+
}); // <-- THIS CLOSING BRACE AND PARENTHESIS WERE MISSING
|