-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
170 lines (138 loc) · 5.91 KB
/
script.js
File metadata and controls
170 lines (138 loc) · 5.91 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
// 3D Background
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('bg-canvas'), alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
const geometry = new THREE.TorusGeometry(10, 3, 16, 100);
const material = new THREE.MeshBasicMaterial({ color: 0x0a1929, wireframe: true });
const torus = new THREE.Mesh(geometry, material);
scene.add(torus);
camera.position.z = 30;
function animate() {
requestAnimationFrame(animate);
torus.rotation.x += 0.01;
torus.rotation.y += 0.005;
renderer.render(scene, camera);
}
animate();
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Theme switching
const themeToggle = document.getElementById('theme-toggle');
const body = document.body;
themeToggle.addEventListener('click', () => {
body.classList.toggle('light-theme');
const icon = themeToggle.querySelector('i');
icon.classList.toggle('fa-moon');
icon.classList.toggle('fa-sun');
});
// Language switching
const langToggle = document.getElementById('lang-toggle');
let currentLang = 'dv';
langToggle.addEventListener('click', () => {
currentLang = currentLang === 'dv' ? 'en' : 'dv';
updateLanguage();
});
function updateLanguage() {
document.querySelectorAll('[data-dv]').forEach(el => {
el.textContent = el.getAttribute(`data-${currentLang}`);
});
langToggle.textContent = currentLang === 'dv' ? 'English' : 'ދިވެހި';
}
// Smooth scrolling for navigation links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
document.querySelector(this.getAttribute('href')).scrollIntoView({
behavior: 'smooth'
});
});
});
// Chat functionality
const chatMessages = document.getElementById('chat-messages');
const chatForm = document.getElementById('chat-form');
const userInput = document.getElementById('user-input');
function addMessage(content, isUser = false) {
const messageDiv = document.createElement('div');
messageDiv.classList.add('message');
messageDiv.classList.add(isUser ? 'user-message' : 'ai-message');
messageDiv.textContent = content;
chatMessages.appendChild(messageDiv);
chatMessages.scrollTop = chatMessages.scrollHeight;
}
// Initial AI message
addMessage('ހާދަ ރީތި ދުވަހެއް! އަހަރެންނަކީ އަދަބީ. ކިހިނެއްތޯ އަހަރެންނަށް އެހީތެރިވެވޭނީ؟');
chatForm.addEventListener('submit', async (e) => {
e.preventDefault();
const userMessage = userInput.value.trim();
if (userMessage) {
addMessage(userMessage, true);
userInput.value = '';
try {
const response = await fetch('/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ message: userMessage, lang: currentLang }),
});
const data = await response.json();
addMessage(data.response);
} catch (error) {
console.error('Error:', error);
addMessage(' !މަޢާފުކުރައްވާ، މިވަގުތު އަހަރެންނަށް ޖަވާބެއް ނުދެވޭނެ. އަހަރެންގެ ޤާބިލިއްޔަތު އިތުރަށް ތަރައްޤީ ކުރެވެމުންދަނީ. ޑިސްކޯރޑް ސާރވާރ އަށް ޖޮއިންކޮށްލައްވައިގެން އަދަބީ ބޭނުން ކުރައްވާ');
}
}
});
// Initialize language
updateLanguage();
// ... (keep the existing code for background, theme switching, and language switching)
// Chat functionality
function initChat() {
const chatMessages = document.getElementById('chat-messages');
const chatForm = document.getElementById('chat-form');
const userInput = document.getElementById('user-input');
if (!chatForm) return;
function addMessage(content, isUser = false) {
const messageDiv = document.createElement('div');
messageDiv.classList.add('message');
messageDiv.classList.add(isUser ? 'user-message' : 'ai-message');
messageDiv.textContent = content;
chatMessages.appendChild(messageDiv);
chatMessages.scrollTop = chatMessages.scrollHeight;
}
// Initial AI message
addMessage('ހާދަ ރީތި ދުވަހެއް! އަހަރެންނަކީ އަދަބީ. ކިހިނެއްތޯ އަހަރެންނަށް އެހީތެރިވެވޭނީ؟');
chatForm.addEventListener('submit', async (e) => {
e.preventDefault();
const userMessage = userInput.value.trim();
if (userMessage) {
addMessage(userMessage, true);
userInput.value = '';
try {
const response = await fetch('/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ message: userMessage, lang: document.documentElement.lang }),
});
const data = await response.json();
addMessage(data.response);
} catch (error) {
console.error('Error:', error);
addMessage('މަޢާފުކުރައްވާ، މިވަގުތު އަހަރެންނަށް ޖަވާބެއް ނުދެވޭނެ. އަހަރެންގެ ޤާބިލިއްޔަތު އިތުރަށް ތަރައްޤީ ކުރެވެމުންދަނީ.');
}
}
});
}
// Initialize everything
document.addEventListener('DOMContentLoaded', () => {
initBackground();
initThemeSwitch();
initLanguageSwitch();
initChat();
});