Spaces:
Running
Running
Update main.js
Browse files
main.js
CHANGED
@@ -1,31 +1,11 @@
|
|
1 |
document.addEventListener('DOMContentLoaded', () => {
|
2 |
-
// --- SETTINGS MODAL LOGIC ---
|
3 |
-
|
4 |
-
// Listen for a click on the main settings gear icon
|
5 |
-
settingsBtn.addEventListener('click', () => {
|
6 |
-
settingsModal.classList.remove('hidden');
|
7 |
-
});
|
8 |
-
|
9 |
-
// Listen for a click on the "X" button inside the modal
|
10 |
-
closeSettingsModal.addEventListener('click', () => {
|
11 |
-
settingsModal.classList.add('hidden');
|
12 |
-
});
|
13 |
-
|
14 |
-
// Bonus: Also close the modal if the user clicks on the dark background overlay
|
15 |
-
settingsModal.addEventListener('click', (event) => {
|
16 |
-
// Check if the click was on the dark background itself, not the white modal content
|
17 |
-
if (event.target === settingsModal) {
|
18 |
-
settingsModal.classList.add('hidden');
|
19 |
-
}
|
20 |
-
});
|
21 |
// --- CONFIGURATION ---
|
22 |
const config = {
|
23 |
-
|
24 |
-
backendUrl: 'https://ar-city-explorer-backend.aiagents.workers.dev'
|
25 |
};
|
26 |
|
27 |
// --- GLOBAL STATE ---
|
28 |
-
let poisData = [];
|
29 |
let arActive = false;
|
30 |
|
31 |
// --- DOM ELEMENT SELECTORS ---
|
@@ -40,12 +20,12 @@ settingsModal.addEventListener('click', (event) => {
|
|
40 |
const objectDescription = document.getElementById('objectDescription');
|
41 |
const objectImage = document.getElementById('objectImage');
|
42 |
const closeObjectModal = document.getElementById('closeObjectModal');
|
43 |
-
|
44 |
const settingsBtn = document.getElementById('settingsBtn');
|
45 |
const settingsModal = document.getElementById('settingsModal');
|
46 |
const closeSettingsModal = document.getElementById('closeSettingsModal');
|
47 |
|
48 |
-
// --- CORE FUNCTIONS ---
|
49 |
|
50 |
function toggleARView(showAR) {
|
51 |
arActive = showAR;
|
@@ -53,7 +33,6 @@ settingsModal.addEventListener('click', (event) => {
|
|
53 |
normalView.classList.add('hidden');
|
54 |
arViewport.classList.remove('hidden');
|
55 |
arToggle.innerHTML = '<i class="fas fa-times mr-1"></i> Exit AR';
|
56 |
-
// Fetch data only if it hasn't been fetched yet
|
57 |
if (poisData.length === 0) {
|
58 |
fetchPoisAndCreateAREntities();
|
59 |
}
|
@@ -65,73 +44,41 @@ settingsModal.addEventListener('click', (event) => {
|
|
65 |
}
|
66 |
|
67 |
function fetchPoisAndCreateAREntities() {
|
68 |
-
console.log('Fetching POIs from backend...');
|
69 |
fetch(`${config.backendUrl}/api/pois`)
|
70 |
.then(response => {
|
71 |
if (!response.ok) throw new Error(`Network error: ${response.statusText}`);
|
72 |
return response.json();
|
73 |
})
|
74 |
.then(pois => {
|
75 |
-
|
76 |
-
poisData = pois; // Store the data globally
|
77 |
const scene = document.querySelector('a-scene');
|
78 |
if (!scene) return console.error('A-Frame scene not found!');
|
79 |
-
|
80 |
pois.forEach(poi => {
|
81 |
const entity = document.createElement('a-entity');
|
82 |
-
entity.setAttribute('gps-new-entity-place', {
|
83 |
-
latitude: poi.latitude,
|
84 |
-
longitude: poi.longitude
|
85 |
-
});
|
86 |
-
|
87 |
const box = document.createElement('a-box');
|
88 |
box.setAttribute('material', 'color: red; opacity: 0.7;');
|
89 |
box.setAttribute('scale', '10 10 10');
|
90 |
box.setAttribute('position', '0 5 0');
|
91 |
-
box.setAttribute('data-poi-id', poi.id);
|
92 |
entity.appendChild(box);
|
93 |
-
|
94 |
const text = document.createElement('a-text');
|
95 |
text.setAttribute('value', poi.name);
|
96 |
text.setAttribute('look-at', '[gps-new-camera]');
|
97 |
text.setAttribute('scale', '50 50 50');
|
98 |
text.setAttribute('position', '0 15 0');
|
99 |
entity.appendChild(text);
|
100 |
-
|
101 |
scene.appendChild(entity);
|
102 |
});
|
103 |
})
|
104 |
.catch(error => {
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
// Make the error box visible and print all error details inside it
|
111 |
-
debugLog.style.display = 'block';
|
112 |
-
debugLog.textContent = `
|
113 |
-
A critical error occurred.
|
114 |
-
|
115 |
-
Error Name:
|
116 |
-
${error.name}
|
117 |
-
|
118 |
-
Error Message:
|
119 |
-
${error.message}
|
120 |
-
|
121 |
-
This usually happens for one of two reasons:
|
122 |
-
1. The 'backendUrl' in main.js has a typo.
|
123 |
-
2. Your browser is blocking the request for security reasons (CORS policy).
|
124 |
-
|
125 |
-
Full Technical Details:
|
126 |
-
${error.stack}
|
127 |
-
`;
|
128 |
-
|
129 |
-
// Switch back to the normal view so we can see the error box
|
130 |
-
toggleARView(false);
|
131 |
-
});
|
132 |
}
|
133 |
|
134 |
-
// ---
|
135 |
|
136 |
function showObjectInfo(poiId) {
|
137 |
const poi = poisData.find(p => p.id === poiId);
|
@@ -161,17 +108,11 @@ settingsModal.addEventListener('click', (event) => {
|
|
161 |
if (!searchTerm) return;
|
162 |
addUserMessage(searchTerm);
|
163 |
userInput.value = '';
|
164 |
-
|
165 |
-
if (poisData.length === 0) {
|
166 |
-
addAIMessage("I'm still loading location data. Please try again in a moment.");
|
167 |
-
return;
|
168 |
-
}
|
169 |
-
|
170 |
const results = poisData.filter(poi =>
|
171 |
poi.name.toLowerCase().includes(searchTerm.toLowerCase()) ||
|
172 |
(poi.description && poi.description.toLowerCase().includes(searchTerm.toLowerCase()))
|
173 |
);
|
174 |
-
|
175 |
if (results.length > 0) {
|
176 |
let response = `I found ${results.length} result(s):<ul>`;
|
177 |
results.forEach(poi => { response += `<li class="mt-2 list-disc list-inside">${poi.name}</li>`; });
|
@@ -184,8 +125,10 @@ settingsModal.addEventListener('click', (event) => {
|
|
184 |
|
185 |
// --- EVENT LISTENERS ---
|
186 |
|
|
|
187 |
arToggle.addEventListener('click', () => toggleARView(!arActive));
|
188 |
|
|
|
189 |
document.querySelector('a-scene').addEventListener('click', (event) => {
|
190 |
if (event.target.hasAttribute('data-poi-id')) {
|
191 |
const poiId = parseInt(event.target.getAttribute('data-poi-id'), 10);
|
@@ -193,13 +136,29 @@ settingsModal.addEventListener('click', (event) => {
|
|
193 |
}
|
194 |
});
|
195 |
|
|
|
196 |
closeObjectModal.addEventListener('click', () => objectModal.classList.add('hidden'));
|
197 |
|
|
|
198 |
sendBtn.addEventListener('click', handleSearch);
|
199 |
userInput.addEventListener('keypress', (e) => {
|
200 |
if (e.key === 'Enter') handleSearch();
|
201 |
});
|
202 |
|
203 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
fetchPoisAndCreateAREntities();
|
205 |
});
|
|
|
1 |
document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
// --- CONFIGURATION ---
|
3 |
const config = {
|
4 |
+
backendUrl: 'https://ar-city-explorer-backend.aiagents.workers.dev' // Ensure this is your correct URL
|
|
|
5 |
};
|
6 |
|
7 |
// --- GLOBAL STATE ---
|
8 |
+
let poisData = [];
|
9 |
let arActive = false;
|
10 |
|
11 |
// --- DOM ELEMENT SELECTORS ---
|
|
|
20 |
const objectDescription = document.getElementById('objectDescription');
|
21 |
const objectImage = document.getElementById('objectImage');
|
22 |
const closeObjectModal = document.getElementById('closeObjectModal');
|
23 |
+
// Selectors for the Settings Modal
|
24 |
const settingsBtn = document.getElementById('settingsBtn');
|
25 |
const settingsModal = document.getElementById('settingsModal');
|
26 |
const closeSettingsModal = document.getElementById('closeSettingsModal');
|
27 |
|
28 |
+
// --- CORE FUNCTIONS (for AR and Data) ---
|
29 |
|
30 |
function toggleARView(showAR) {
|
31 |
arActive = showAR;
|
|
|
33 |
normalView.classList.add('hidden');
|
34 |
arViewport.classList.remove('hidden');
|
35 |
arToggle.innerHTML = '<i class="fas fa-times mr-1"></i> Exit AR';
|
|
|
36 |
if (poisData.length === 0) {
|
37 |
fetchPoisAndCreateAREntities();
|
38 |
}
|
|
|
44 |
}
|
45 |
|
46 |
function fetchPoisAndCreateAREntities() {
|
|
|
47 |
fetch(`${config.backendUrl}/api/pois`)
|
48 |
.then(response => {
|
49 |
if (!response.ok) throw new Error(`Network error: ${response.statusText}`);
|
50 |
return response.json();
|
51 |
})
|
52 |
.then(pois => {
|
53 |
+
poisData = pois;
|
|
|
54 |
const scene = document.querySelector('a-scene');
|
55 |
if (!scene) return console.error('A-Frame scene not found!');
|
|
|
56 |
pois.forEach(poi => {
|
57 |
const entity = document.createElement('a-entity');
|
58 |
+
entity.setAttribute('gps-new-entity-place', { latitude: poi.latitude, longitude: poi.longitude });
|
|
|
|
|
|
|
|
|
59 |
const box = document.createElement('a-box');
|
60 |
box.setAttribute('material', 'color: red; opacity: 0.7;');
|
61 |
box.setAttribute('scale', '10 10 10');
|
62 |
box.setAttribute('position', '0 5 0');
|
63 |
+
box.setAttribute('data-poi-id', poi.id);
|
64 |
entity.appendChild(box);
|
|
|
65 |
const text = document.createElement('a-text');
|
66 |
text.setAttribute('value', poi.name);
|
67 |
text.setAttribute('look-at', '[gps-new-camera]');
|
68 |
text.setAttribute('scale', '50 50 50');
|
69 |
text.setAttribute('position', '0 15 0');
|
70 |
entity.appendChild(text);
|
|
|
71 |
scene.appendChild(entity);
|
72 |
});
|
73 |
})
|
74 |
.catch(error => {
|
75 |
+
console.error('Failed to load POIs:', error);
|
76 |
+
alert('Could not load city data. Check connection and backend URL.');
|
77 |
+
toggleARView(false);
|
78 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
}
|
80 |
|
81 |
+
// --- UI FUNCTIONS (Modals, Chat, Search) ---
|
82 |
|
83 |
function showObjectInfo(poiId) {
|
84 |
const poi = poisData.find(p => p.id === poiId);
|
|
|
108 |
if (!searchTerm) return;
|
109 |
addUserMessage(searchTerm);
|
110 |
userInput.value = '';
|
111 |
+
if (poisData.length === 0) return addAIMessage("Location data is still loading. Please try again in a moment.");
|
|
|
|
|
|
|
|
|
|
|
112 |
const results = poisData.filter(poi =>
|
113 |
poi.name.toLowerCase().includes(searchTerm.toLowerCase()) ||
|
114 |
(poi.description && poi.description.toLowerCase().includes(searchTerm.toLowerCase()))
|
115 |
);
|
|
|
116 |
if (results.length > 0) {
|
117 |
let response = `I found ${results.length} result(s):<ul>`;
|
118 |
results.forEach(poi => { response += `<li class="mt-2 list-disc list-inside">${poi.name}</li>`; });
|
|
|
125 |
|
126 |
// --- EVENT LISTENERS ---
|
127 |
|
128 |
+
// AR Mode Toggle
|
129 |
arToggle.addEventListener('click', () => toggleARView(!arActive));
|
130 |
|
131 |
+
// Clicking on AR objects
|
132 |
document.querySelector('a-scene').addEventListener('click', (event) => {
|
133 |
if (event.target.hasAttribute('data-poi-id')) {
|
134 |
const poiId = parseInt(event.target.getAttribute('data-poi-id'), 10);
|
|
|
136 |
}
|
137 |
});
|
138 |
|
139 |
+
// Info Modal Close Button
|
140 |
closeObjectModal.addEventListener('click', () => objectModal.classList.add('hidden'));
|
141 |
|
142 |
+
// Search Bar
|
143 |
sendBtn.addEventListener('click', handleSearch);
|
144 |
userInput.addEventListener('keypress', (e) => {
|
145 |
if (e.key === 'Enter') handleSearch();
|
146 |
});
|
147 |
|
148 |
+
// Settings Modal Open/Close
|
149 |
+
settingsBtn.addEventListener('click', () => {
|
150 |
+
settingsModal.classList.remove('hidden');
|
151 |
+
});
|
152 |
+
closeSettingsModal.addEventListener('click', () => {
|
153 |
+
settingsModal.classList.add('hidden');
|
154 |
+
});
|
155 |
+
settingsModal.addEventListener('click', (event) => {
|
156 |
+
if (event.target === settingsModal) {
|
157 |
+
settingsModal.classList.add('hidden');
|
158 |
+
}
|
159 |
+
});
|
160 |
+
|
161 |
+
// --- INITIALIZATION ---
|
162 |
+
// Fetch data as soon as the app loads
|
163 |
fetchPoisAndCreateAREntities();
|
164 |
});
|