-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
340 lines (290 loc) · 10.5 KB
/
script.js
File metadata and controls
340 lines (290 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
require([
"esri/Map",
"esri/views/MapView",
"esri/Graphic",
"esri/layers/GraphicsLayer"
], function(Map, MapView, Graphic, GraphicsLayer) {
const map = new Map({
basemap: "topo-vector" // Choose a basemap style
});
const graphicsLayer = new GraphicsLayer();
map.add(graphicsLayer);
const view = new MapView({
container: "viewDiv",
map: map,
center: [138.2529, 36.2048], // Longitude, latitude of Japan
zoom: 5 // Initial zoom level
});
// Make view globally accessible
window.mapView = view;
// Add the addPin function
window.addPin = function(longitude, latitude, title, shouldCenter = false) {
// Create a point
const point = {
type: "point",
longitude: longitude,
latitude: latitude
};
// Create a symbol for rendering the point
const markerSymbol = {
type: "simple-marker",
color: [226, 119, 40], // Orange
outline: {
color: [255, 255, 255], // White
width: 2
}
};
// Create a graphic with popup template
const pointGraphic = new Graphic({
geometry: point,
symbol: markerSymbol,
attributes: {
title: title
},
popupTemplate: {
title: title
}
});
graphicsLayer.add(pointGraphic);
// Only center the map if specifically requested
if (shouldCenter) {
view.goTo({
center: [longitude, latitude],
zoom: 12
});
}
};
});
function handleClick(button) {
const buttons = document.querySelectorAll('.button');
buttons.forEach(btn => btn.classList.remove('active'));
button.classList.add('active');
}
document.getElementById('sign-in-button').addEventListener('click', function() {
showPopup('loginPopup');
});
document.getElementById('register-button').addEventListener('click', function() {
showPopup('registerPopup');
});
function showPopup(popupId) {
document.getElementById(popupId).style.display = 'block';
}
function closePopup(popupId) {
document.getElementById(popupId).style.display = 'none';
}
document.addEventListener('DOMContentLoaded', () => {
const divider = document.getElementById('dividerBetweenSections');
const contentGrid = document.getElementById('contentGrid');
let isDragging = false;
window.addEventListener('mousemove', (e) => {
if (isDragging) {
const newWidth = e.clientX;
// Adjust the grid template columns to resize the panels
contentGrid.style.gridTemplateColumns = `${newWidth}px auto 1fr`;
}
});
window.addEventListener('mouseup', () => {
if (isDragging) {
isDragging = false;
document.body.style.cursor = 'default';
}
});
});
// Event listener for the send button
document.getElementById("sendButton").addEventListener("click", sendMessage);
// Event listener for pressing 'Enter' in the text input
document.getElementById("chatInput").addEventListener("keypress", function(event) {
if (event.key === "Enter") {
event.preventDefault(); // Prevent default 'Enter' key behavior
sendMessage();
}
});
// Toggle the display of the dropdown menu when the profile icon is clicked
document.querySelector('.profile-icon').addEventListener('click', function() {
const dropdown = document.getElementById('profileDropdown');
dropdown.style.display = dropdown.style.display === 'block' ? 'none' : 'block';
});
// Close the dropdown if clicked outside
window.addEventListener('click', function(event) {
if (!event.target.matches('.profile-icon')) {
const dropdown = document.getElementById('profileDropdown');
if (dropdown.style.display === 'block') {
dropdown.style.display = 'none';
}
}
});
document.addEventListener('DOMContentLoaded', () => {
const numberGrid = document.getElementById('numberGrid');
const submitButton = document.getElementById('submitButton');
const warningMessage = document.getElementById('warningMessage');
let selectedNumber = null;
// Create 30 number boxes
for (let i = 1; i <= 30; i++) {
const numberBox = document.createElement('div');
numberBox.classList.add('number-box');
numberBox.textContent = i;
numberBox.addEventListener('click', () => {
// Remove the 'selected' class from any previously selected box
document.querySelectorAll('.number-box').forEach(box => box.classList.remove('selected'));
// Mark this box as selected
numberBox.classList.add('selected');
selectedNumber = i;
});
numberGrid.appendChild(numberBox);
}
submitButton.addEventListener('click', () => {
if (!selectedNumber) {
warningMessage.style.display = 'block';
} else {
warningMessage.style.display = 'none';
alert(`Preferences submitted successfully! Selected number: ${selectedNumber}`);
// Add any further actions here
}
});
});
const resizeHandle = document.getElementById('resizeHandle');
const chatPane = document.getElementById('chat');
let isResizing = false;
resizeHandle.addEventListener('mousedown', (e) => {
isResizing = true;
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('mouseup', () => {
isResizing = false;
document.removeEventListener('mousemove', onMouseMove);
});
});
function onMouseMove(e) {
if (isResizing) {
const newHeight = window.innerHeight - e.clientY;
chatPane.style.height = `${newHeight}px`;
}
}
window.addEventListener('resize', function() {
if (window.innerWidth <= 768) { // Match the media query max-width
const chatSection = document.getElementById('chat');
if (chatSection) {
chatSection.scrollIntoView({ behavior: 'smooth' });
}
}
});
document.getElementById("newChatButton").addEventListener("click", function() {
currentConversationId = null; // Reset conversation ID
const chatContent = document.getElementById("chatContent");
chatContent.innerHTML = ''; // Clear chat content
// Add initial greeting
const greetingBubble = document.createElement("div");
greetingBubble.classList.add("chat-bubble", "responder-bubble");
greetingBubble.textContent = "Hello! How can I help you plan your trip to Japan?";
chatContent.appendChild(greetingBubble);
});
// REGISTERING NEW USERS
document.querySelector('.popup-submit-register').addEventListener('click', function(event) {
event.preventDefault(); // Prevent the default form submission
const username = document.getElementById('registerUsername').value;
const password = document.getElementById('registerPassword').value;
// Send the data to the Python server
fetch('http://10.146.103.162/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ username: username, password: password })
})
.then(response => response.json())
.then(data => {
console.log(data); // Log the entire JSON response
alert(data.message); // Show a message to the user
})
.catch(error => {
console.error('Error:', error);
alert('There was an error with registration.');
});
});
let currentConversationId = null;
async function sendMessage() {
console.log("Sending message...");
const chatInput = document.getElementById("chatInput");
const chatContent = document.getElementById("chatContent");
const userMessage = chatInput.value.trim();
if (!userMessage) return;
// Create and append user message bubble
const userBubble = document.createElement("div");
userBubble.classList.add("chat-bubble", "user-bubble");
userBubble.textContent = userMessage;
chatContent.appendChild(userBubble);
// Clear input field
chatInput.value = "";
try {
// Get selected preferences
const selectedBudget = document.querySelector('.button.active')?.textContent || '$';
const selectedDays = document.querySelector('.number-box.selected')?.textContent || '1';
const response = await fetch('http://127.0.0.1:5001/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
message: userMessage,
conversation_id: currentConversationId,
preferences: {
budget: selectedBudget,
days: selectedDays
}
})
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
// Store the conversation ID if it's a new conversation
if (!currentConversationId) {
currentConversationId = data.conversation_id;
}
// Create and append AI response bubble
const aiBubble = document.createElement("div");
aiBubble.classList.add("chat-bubble", "responder-bubble");
aiBubble.textContent = data.response;
// chatContent.appendChild(aiBubble);
await parseResponse(data.response);
chatContent.appendChild(aiBubble);
} catch (error) {
console.error('Error:', error);
// Create error message bubble
const errorBubble = document.createElement("div");
errorBubble.classList.add("chat-bubble", "responder-bubble", "error");
errorBubble.textContent = "Sorry, I encountered an error while processing your request.";
chatContent.appendChild(errorBubble);
}
// Scroll to bottom of chat
chatContent.scrollTop = chatContent.scrollHeight;
}
// Event listeners for the send button and Enter key
document.getElementById("sendButton").addEventListener("click", sendMessage);
document.getElementById("chatInput").addEventListener("keypress", function(event) {
if (event.key === "Enter") {
event.preventDefault();
sendMessage();
}
});
async function parseResponse(response) {
const response2 = await fetch('http://127.0.0.1:5001/parse', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
text: response
})
});
const data = await response2.json();
console.log(data);
if (data.locations) {
data.locations.forEach((location, index) => {
// Swap the coordinates order since they come as [lat, lon] but we need [lon, lat]
const longitude = location.coordinates[1]; // Get longitude from second position
const latitude = location.coordinates[0]; // Get latitude from first position
const shouldCenter = index === 0;
addPin(longitude, latitude, location.location, shouldCenter);
});
}
}