Update static/script.js
Browse files- static/script.js +13 -265
static/script.js
CHANGED
|
@@ -1,190 +1,20 @@
|
|
| 1 |
-
//
|
| 2 |
-
async function
|
| 3 |
-
const text = document.getElementById('textInput').value;
|
| 4 |
-
if (!text.trim()) {
|
| 5 |
-
showError('Пожалуйста, введите текст для анализа');
|
| 6 |
-
return;
|
| 7 |
-
}
|
| 8 |
-
|
| 9 |
-
showLoading('analysisResult', 'Анализируем эмоции...');
|
| 10 |
-
|
| 11 |
try {
|
| 12 |
-
const response = await fetch('/
|
| 13 |
-
method: 'POST',
|
| 14 |
-
headers: {
|
| 15 |
-
'Content-Type': 'application/json',
|
| 16 |
-
},
|
| 17 |
-
body: JSON.stringify({
|
| 18 |
-
text: text,
|
| 19 |
-
type: 'emotion'
|
| 20 |
-
})
|
| 21 |
-
});
|
| 22 |
-
|
| 23 |
const data = await response.json();
|
| 24 |
|
| 25 |
-
if (data.
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
}
|
| 29 |
-
|
| 30 |
-
displayEmotionAnalysis(data);
|
| 31 |
} catch (error) {
|
| 32 |
-
|
| 33 |
}
|
| 34 |
}
|
| 35 |
|
| 36 |
-
|
| 37 |
-
const text = document.getElementById('textInput').value;
|
| 38 |
-
if (!text.trim()) {
|
| 39 |
-
showError('Пожалуйста, введите текст для анализа');
|
| 40 |
-
return;
|
| 41 |
-
}
|
| 42 |
-
|
| 43 |
-
showLoading('analysisResult', 'Анализируем паттерны мышления...');
|
| 44 |
-
|
| 45 |
-
try {
|
| 46 |
-
const response = await fetch('/analyze', {
|
| 47 |
-
method: 'POST',
|
| 48 |
-
headers: {
|
| 49 |
-
'Content-Type': 'application/json',
|
| 50 |
-
},
|
| 51 |
-
body: JSON.stringify({
|
| 52 |
-
text: text,
|
| 53 |
-
type: 'personality'
|
| 54 |
-
})
|
| 55 |
-
});
|
| 56 |
-
|
| 57 |
-
const data = await response.json();
|
| 58 |
-
|
| 59 |
-
if (data.error) {
|
| 60 |
-
showError(data.error, 'analysisResult');
|
| 61 |
-
return;
|
| 62 |
-
}
|
| 63 |
-
|
| 64 |
-
displayPersonalityAnalysis(data);
|
| 65 |
-
} catch (error) {
|
| 66 |
-
showError('Ошибка при анализе: ' + error.message, 'analysisResult');
|
| 67 |
-
}
|
| 68 |
-
}
|
| 69 |
-
|
| 70 |
-
async function saveToJournal() {
|
| 71 |
-
const text = document.getElementById('textInput').value;
|
| 72 |
-
if (!text.trim()) {
|
| 73 |
-
showError('Пожалуйста, введите запись для дневника');
|
| 74 |
-
return;
|
| 75 |
-
}
|
| 76 |
-
|
| 77 |
-
try {
|
| 78 |
-
const response = await fetch('/journal', {
|
| 79 |
-
method: 'POST',
|
| 80 |
-
headers: {
|
| 81 |
-
'Content-Type': 'application/json',
|
| 82 |
-
},
|
| 83 |
-
body: JSON.stringify({
|
| 84 |
-
entry: text
|
| 85 |
-
})
|
| 86 |
-
});
|
| 87 |
-
|
| 88 |
-
const data = await response.json();
|
| 89 |
-
|
| 90 |
-
if (data.error) {
|
| 91 |
-
showError(data.error);
|
| 92 |
-
return;
|
| 93 |
-
}
|
| 94 |
-
|
| 95 |
-
// Очищаем поле ввода
|
| 96 |
-
document.getElementById('textInput').value = '';
|
| 97 |
-
|
| 98 |
-
// Добавляем запись в дневник
|
| 99 |
-
addJournalEntry(data.entry);
|
| 100 |
-
|
| 101 |
-
showSuccess('Запись успешно сохранена в дневник!');
|
| 102 |
-
} catch (error) {
|
| 103 |
-
showError('Ошибка при сохранении: ' + error.message);
|
| 104 |
-
}
|
| 105 |
-
}
|
| 106 |
-
|
| 107 |
-
async function getInsights() {
|
| 108 |
-
showLoading('insightsResult', 'Генерируем инсайты...');
|
| 109 |
-
|
| 110 |
-
try {
|
| 111 |
-
const response = await fetch('/insights');
|
| 112 |
-
const data = await response.json();
|
| 113 |
-
|
| 114 |
-
if (data.error) {
|
| 115 |
-
showError(data.error, 'insightsResult');
|
| 116 |
-
return;
|
| 117 |
-
}
|
| 118 |
-
|
| 119 |
-
displayInsights(data.insights);
|
| 120 |
-
} catch (error) {
|
| 121 |
-
showError('Ошибка при получении инсайтов: ' + error.message, 'insightsResult');
|
| 122 |
-
}
|
| 123 |
-
}
|
| 124 |
-
|
| 125 |
-
// Функции отображения результатов
|
| 126 |
-
function displayEmotionAnalysis(data) {
|
| 127 |
-
const container = document.getElementById('analysisResult');
|
| 128 |
-
let html = '<h3>Эмоциональный анализ:</h3>';
|
| 129 |
-
|
| 130 |
-
if (data.dominant_emotion) {
|
| 131 |
-
html += `<div class="dominant-emotion">
|
| 132 |
-
<strong>Преобладающая эмоция:</strong> ${data.dominant_emotion.label}
|
| 133 |
-
(${(data.dominant_emotion.score * 100).toFixed(1)}%)
|
| 134 |
-
</div>`;
|
| 135 |
-
}
|
| 136 |
-
|
| 137 |
-
html += '<h4>Все обнаруженные эмоции:</h4>';
|
| 138 |
-
data.emotions.forEach(emotion => {
|
| 139 |
-
const percentage = (emotion.score * 100).toFixed(1);
|
| 140 |
-
html += `
|
| 141 |
-
<div class="emotion-item">
|
| 142 |
-
<span>${emotion.label}</span>
|
| 143 |
-
<span class="emotion-score">${percentage}%</span>
|
| 144 |
-
</div>
|
| 145 |
-
`;
|
| 146 |
-
});
|
| 147 |
-
|
| 148 |
-
container.innerHTML = html;
|
| 149 |
-
}
|
| 150 |
-
|
| 151 |
-
function displayPersonalityAnalysis(data) {
|
| 152 |
-
const container = document.getElementById('analysisResult');
|
| 153 |
-
let html = '<h3>Анализ паттернов:</h3>';
|
| 154 |
-
|
| 155 |
-
if (data.traits) {
|
| 156 |
-
html += `
|
| 157 |
-
<div class="trait-item">
|
| 158 |
-
<strong>Эмоциональная стабильность:</strong>
|
| 159 |
-
${(data.traits.emotional_stability * 100).toFixed(1)}%
|
| 160 |
-
</div>
|
| 161 |
-
<div class="trait-item">
|
| 162 |
-
<strong>Позитивность:</strong>
|
| 163 |
-
${(data.traits.positivity * 100).toFixed(1)}%
|
| 164 |
-
</div>
|
| 165 |
-
<div class="trait-item">
|
| 166 |
-
<strong>Сложность мышления:</strong>
|
| 167 |
-
${(data.traits.complexity * 100).toFixed(1)}%
|
| 168 |
-
</div>
|
| 169 |
-
<div class="trait-item">
|
| 170 |
-
<strong>Последовательность:</strong>
|
| 171 |
-
${(data.traits.consistency * 100).toFixed(1)}%
|
| 172 |
-
</div>
|
| 173 |
-
`;
|
| 174 |
-
}
|
| 175 |
-
|
| 176 |
-
container.innerHTML = html;
|
| 177 |
-
}
|
| 178 |
-
|
| 179 |
-
function displayInsights(insights) {
|
| 180 |
-
const container = document.getElementById('insightsResult');
|
| 181 |
-
container.innerHTML = `
|
| 182 |
-
<div class="insight-text">
|
| 183 |
-
"${insights}"
|
| 184 |
-
</div>
|
| 185 |
-
`;
|
| 186 |
-
}
|
| 187 |
-
|
| 188 |
function addJournalEntry(entry) {
|
| 189 |
const container = document.getElementById('journalEntries');
|
| 190 |
const entryElement = document.createElement('div');
|
|
@@ -196,7 +26,7 @@ function addJournalEntry(entry) {
|
|
| 196 |
if (entry.emotions && entry.emotions.length > 0) {
|
| 197 |
emotionsHtml = '<div class="entry-emotions">';
|
| 198 |
entry.emotions.forEach(emotion => {
|
| 199 |
-
emotionsHtml += `<span class="emotion-tag">${emotion.label}</span>`;
|
| 200 |
});
|
| 201 |
emotionsHtml += '</div>';
|
| 202 |
}
|
|
@@ -210,90 +40,8 @@ function addJournalEntry(entry) {
|
|
| 210 |
container.insertBefore(entryElement, container.firstChild);
|
| 211 |
}
|
| 212 |
|
| 213 |
-
//
|
| 214 |
-
function showLoading(containerId, message = 'Загрузка...') {
|
| 215 |
-
document.getElementById(containerId).innerHTML = `
|
| 216 |
-
<div class="loading">${message}</div>
|
| 217 |
-
`;
|
| 218 |
-
}
|
| 219 |
-
|
| 220 |
-
function showError(message, containerId = null) {
|
| 221 |
-
if (containerId) {
|
| 222 |
-
document.getElementById(containerId).innerHTML = `
|
| 223 |
-
<div class="error">${message}</div>
|
| 224 |
-
`;
|
| 225 |
-
} else {
|
| 226 |
-
// Показываем временное уведомление
|
| 227 |
-
const notification = document.createElement('div');
|
| 228 |
-
notification.className = 'error';
|
| 229 |
-
notification.style.position = 'fixed';
|
| 230 |
-
notification.style.top = '20px';
|
| 231 |
-
notification.style.right = '20px';
|
| 232 |
-
notification.style.zIndex = '1000';
|
| 233 |
-
notification.textContent = message;
|
| 234 |
-
|
| 235 |
-
document.body.appendChild(notification);
|
| 236 |
-
|
| 237 |
-
setTimeout(() => {
|
| 238 |
-
document.body.removeChild(notification);
|
| 239 |
-
}, 5000);
|
| 240 |
-
}
|
| 241 |
-
}
|
| 242 |
-
|
| 243 |
-
function showSuccess(message) {
|
| 244 |
-
const notification = document.createElement('div');
|
| 245 |
-
notification.style.cssText = `
|
| 246 |
-
position: fixed;
|
| 247 |
-
top: 20px;
|
| 248 |
-
right: 20px;
|
| 249 |
-
background: #48bb78;
|
| 250 |
-
color: white;
|
| 251 |
-
padding: 15px 20px;
|
| 252 |
-
border-radius: 8px;
|
| 253 |
-
z-index: 1000;
|
| 254 |
-
box-shadow: 0 4px 12px rgba(0,0,0,0.3);
|
| 255 |
-
`;
|
| 256 |
-
notification.textContent = message;
|
| 257 |
-
|
| 258 |
-
document.body.appendChild(notification);
|
| 259 |
-
|
| 260 |
-
setTimeout(() => {
|
| 261 |
-
document.body.removeChild(notification);
|
| 262 |
-
}, 3000);
|
| 263 |
-
}
|
| 264 |
-
|
| 265 |
-
// Загрузка данных при старте
|
| 266 |
document.addEventListener('DOMContentLoaded', function() {
|
| 267 |
-
|
| 268 |
loadProgress();
|
| 269 |
-
});
|
| 270 |
-
|
| 271 |
-
async function loadProgress() {
|
| 272 |
-
try {
|
| 273 |
-
const response = await fetch('/progress');
|
| 274 |
-
const data = await response.json();
|
| 275 |
-
|
| 276 |
-
if (data.message) {
|
| 277 |
-
document.getElementById('progressContent').innerHTML =
|
| 278 |
-
`<p>${data.message}</p>`;
|
| 279 |
-
return;
|
| 280 |
-
}
|
| 281 |
-
|
| 282 |
-
let html = `
|
| 283 |
-
<div class="progress-stats">
|
| 284 |
-
<div class="stat-item">
|
| 285 |
-
<div class="stat-value">${data.total_entries}</div>
|
| 286 |
-
<div class="stat-label">Всего записей</div>
|
| 287 |
-
</div>
|
| 288 |
-
<div class="stat-item">
|
| 289 |
-
<div class="stat-value">${(data.consistency_score * 100).toFixed(0)}%</div>
|
| 290 |
-
<div class="stat-label">Регулярность</div>
|
| 291 |
-
</div>
|
| 292 |
-
</div>
|
| 293 |
-
`;
|
| 294 |
-
|
| 295 |
-
document.getElementById('progressContent').innerHTML = html;
|
| 296 |
-
} catch (error) {
|
| 297 |
-
console.error('Ошибка загрузки прогресса:', error);
|
| 298 |
-
}
|
| 299 |
-
}
|
|
|
|
| 1 |
+
// Добавьте эту функцию в script.js
|
| 2 |
+
async function loadJournalEntries() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
try {
|
| 4 |
+
const response = await fetch('/journal_entries');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
const data = await response.json();
|
| 6 |
|
| 7 |
+
if (data.entries) {
|
| 8 |
+
data.entries.forEach(entry => {
|
| 9 |
+
addJournalEntry(entry);
|
| 10 |
+
});
|
| 11 |
}
|
|
|
|
|
|
|
| 12 |
} catch (error) {
|
| 13 |
+
console.error('Ошибка загрузки записей:', error);
|
| 14 |
}
|
| 15 |
}
|
| 16 |
|
| 17 |
+
// Обновите функцию addJournalEntry для обработки уже существующих записей
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
function addJournalEntry(entry) {
|
| 19 |
const container = document.getElementById('journalEntries');
|
| 20 |
const entryElement = document.createElement('div');
|
|
|
|
| 26 |
if (entry.emotions && entry.emotions.length > 0) {
|
| 27 |
emotionsHtml = '<div class="entry-emotions">';
|
| 28 |
entry.emotions.forEach(emotion => {
|
| 29 |
+
emotionsHtml += `<span class="emotion-tag">${emotion.label} (${(emotion.score * 100).toFixed(0)}%)</span>`;
|
| 30 |
});
|
| 31 |
emotionsHtml += '</div>';
|
| 32 |
}
|
|
|
|
| 40 |
container.insertBefore(entryElement, container.firstChild);
|
| 41 |
}
|
| 42 |
|
| 43 |
+
// Обновите обработчик DOMContentLoaded
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
document.addEventListener('DOMContentLoaded', function() {
|
| 45 |
+
loadJournalEntries();
|
| 46 |
loadProgress();
|
| 47 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|