Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
258 lines (229 loc) · 9.66 KB
/
Copy pathapp.js
File metadata and controls
258 lines (229 loc) · 9.66 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
// ========== מבנה התורה (לממשק) ==========
const TORAH_STRUCTURE = {
'בראשית': 50,
'שמות': 40,
'ויקרא': 27,
'במדבר': 36,
'דברים': 34
};
// ========== אלמנטים ==========
const bookSelect = document.getElementById('book-select');
const chapterSelect = document.getElementById('chapter-select');
const verseSelect = document.getElementById('verse-select');
const submitButton = document.getElementById('submit-button');
const form = document.getElementById('verse-form');
const aiHelperBtn = document.getElementById('ai-helper-btn');
const searchInput = document.getElementById('search-input');
const searchResults = document.getElementById('search-results');
// ========== אתחול ==========
document.addEventListener('DOMContentLoaded', function() {
initEventListeners();
loadStats();
loadPopularDvarim();
});
// ========== מאזיני אירועים ==========
function initEventListeners() {
bookSelect.addEventListener('change', function() {
const bookName = this.value;
resetSelect(chapterSelect, 'בחר פרק...');
resetSelect(verseSelect, 'בחר פסוק...');
submitButton.disabled = true;
if (!bookName) return;
const numChapters = TORAH_STRUCTURE[bookName];
populateSelect(chapterSelect, numChapters);
chapterSelect.disabled = false;
});
chapterSelect.addEventListener('change', function() {
resetSelect(verseSelect, 'בחר פסוק...');
submitButton.disabled = true;
if (!this.value) return;
const numVerses = 50; // קבוע להדגמה
populateSelect(verseSelect, numVerses);
verseSelect.disabled = false;
});
verseSelect.addEventListener('change', function() {
submitButton.disabled = !this.value;
});
form.addEventListener('submit', function(e) {
e.preventDefault();
const book = bookSelect.value;
const chapter = chapterSelect.value;
const verse = verseSelect.value;
if (book && chapter && verse) {
viewVerse(book, chapter, verse);
}
});
aiHelperBtn.addEventListener('click', showAIHelper);
searchInput.addEventListener('input', function() {
if (this.value.length >= 2) searchDvarim(this.value);
else clearSearchResults();
});
}
// ========== פונקציות עזר ==========
function resetSelect(select, defaultText) {
select.innerHTML = `<option value="">${defaultText}</option>`;
select.disabled = true;
}
function populateSelect(select, count) {
for (let i = 1; i <= count; i++) {
const option = document.createElement('option');
option.value = i;
option.textContent = i;
select.appendChild(option);
}
}
// ========== הצגת פסוק (Modal) ==========
function viewVerse(book, chapter, verse) {
fetch(`/api/verse/${book}/${chapter}/${verse}`)
.then(res => res.json())
.then(data => {
const modal = document.createElement('div');
modal.style.cssText = `
position: fixed; top:0; left:0; width:100%; height:100%;
background: rgba(0,0,0,0.8); display:flex; justify-content:center; align-items:center; z-index:10000;
`;
let contentHtml = data.length > 0
? data.map(d => `
<div class="card" style="margin:10px 0;">
<h3>${d.title || 'דבר תורה'}</h3>
<p>${d.content}</p>
<small>נכתב ע"י: ${d.user_name} | דירוג: ${d.rating}</small>
</div>`).join('')
: '<p>אין עדיין דברי תורה על פסוק זה.</p>';
modal.innerHTML = `
<div class="card" style="max-width:600px; width:90%; max-height:80vh; overflow-y:auto;">
<h2>${book} פרק ${chapter}, פסוק ${verse}</h2>
<div style="margin:20px 0;">${contentHtml}</div>
<button onclick="this.closest('div[style]').remove()" class="btn btn-primary">סגור</button>
</div>
`;
document.body.appendChild(modal);
});
}
// ========== סטטיסטיקות ==========
function loadStats() {
fetch('/api/stats')
.then(res => res.json())
.then(stats => {
document.getElementById('total-dvarim').textContent = stats.total_dvarim || 0;
document.getElementById('total-rating').textContent = stats.total_rating || 0;
document.getElementById('books-covered').textContent = stats.books_covered || 0;
})
.catch(() => {
document.getElementById('total-dvarim').textContent = '?';
document.getElementById('total-rating').textContent = '?';
document.getElementById('books-covered').textContent = '?';
});
}
// ========== פופולריים ==========
function loadPopularDvarim() {
fetch('/api/popular')
.then(res => res.json())
.then(data => {
const container = document.getElementById('popular-dvarim');
container.innerHTML = data.map(d => `
<div class="card hover-card">
<div class="card-header">
<h3>${d.book} ${d.chapter}:${d.verse}</h3>
<div class="rating">
<span class="rating-score">${d.rating}</span>
<i class="fas fa-star"></i>
</div>
</div>
<p class="dvar-preview">${d.content.substring(0, 100)}${d.content.length>100 ? '...' : ''}</p>
<button class="btn btn-secondary" onclick="viewVerse('${d.book}', ${d.chapter}, ${d.verse})">קרא עוד</button>
</div>
`).join('');
});
}
// ========== חיפוש ==========
function searchDvarim(query) {
const q = query || searchInput.value;
if (!q || q.length < 2) {
clearSearchResults();
return;
}
fetch(`/api/search?q=${encodeURIComponent(q)}`)
.then(res => res.json())
.then(results => {
if (results.length === 0) {
searchResults.innerHTML = `<p>לא נמצאו תוצאות עבור "${q}"</p>`;
return;
}
searchResults.innerHTML = `
<div class="cards-grid">
${results.map(d => `
<div class="card hover-card">
<h3>${d.title || d.book + ' ' + d.chapter + ':' + d.verse}</h3>
<p>${d.content}</p>
<small>${d.user_name} | ⭐ ${d.rating}</small>
</div>
`).join('')}
</div>
`;
});
}
function clearSearchResults() { searchResults.innerHTML = ''; }
// ========== AI Helper (קריאה לשרת) ==========
function showAIHelper() {
const book = bookSelect.value;
const chapter = chapterSelect.value;
const verse = verseSelect.value;
if (!book || !chapter || !verse) {
alert('⚠️ אנא בחר פסוק לפני השימוש בעוזר AI');
return;
}
// מראה למשתמש שאנחנו טוענים...
const modal = document.createElement('div');
modal.style.cssText = `
position: fixed; top:0; left:0; width:100%; height:100%;
background: rgba(0,0,0,0.5); display:flex; justify-content:center; align-items:center; z-index:10000;
`;
modal.innerHTML = `
<div class="card" style="max-width:500px; margin:20px; text-align:center;">
<h2><i class="fas fa-spinner fa-spin"></i> יוצר דבר תורה...</h2>
<p>אנא המתן בזמן ש-Gemini AI כותב עבורך.</p>
</div>
`;
document.body.appendChild(modal);
// קריאה לשרת שלנו שיפנה לגוגל
fetch('/api/ai/generate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ book: book, chapter: chapter, verse: verse })
})
.then(res => res.json())
.then(data => {
if(data.error) {
modal.innerHTML = `
<div class="card" style="max-width:500px; margin:20px;">
<h2><i class="fas fa-exclamation-triangle"></i> שגיאה</h2>
<p>${data.error}</p>
<button onclick="this.closest('div[style]').remove()" class="btn btn-primary mt-2">סגור</button>
</div>
`;
} else {
modal.innerHTML = `
<div class="card" style="max-width:500px; margin:20px; max-height:80vh; overflow-y:auto;">
<h2><i class="fas fa-robot"></i> עוזר AI - ${book} ${chapter}:${verse}</h2>
<p>${data.ai_dvar_torah.replace(/\n/g, '<br>')}</p>
<button onclick="this.closest('div[style]').remove()" class="btn btn-primary mt-2">סגור</button>
</div>
`;
}
})
.catch(err => {
modal.innerHTML = `
<div class="card" style="max-width:500px; margin:20px;">
<h2><i class="fas fa-exclamation-triangle"></i> שגיאת תקשורת</h2>
<p>לא הצלחנו להתחבר לשרת.</p>
<button onclick="this.closest('div[style]').remove()" class="btn btn-primary mt-2">סגור</button>
</div>
`;
});
}
// ========== Expose to HTML ==========
window.viewVerse = viewVerse;
window.searchDvarim = searchDvarim;