-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
584 lines (490 loc) · 20 KB
/
script.js
File metadata and controls
584 lines (490 loc) · 20 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
// Wait for DOM to load
document.addEventListener('DOMContentLoaded', function() {
// Mobile menu toggle
const mobileMenuToggle = document.getElementById('mobileMenuToggle');
const navMenu = document.getElementById('navMenu');
mobileMenuToggle.addEventListener('click', function() {
this.classList.toggle('active');
navMenu.classList.toggle('active');
});
// Close mobile menu when clicking on a link
const navLinks = document.querySelectorAll('.nav-link');
navLinks.forEach(link => {
link.addEventListener('click', () => {
mobileMenuToggle.classList.remove('active');
navMenu.classList.remove('active');
});
});
// Header scroll effect
const header = document.getElementById('header');
let lastScroll = 0;
window.addEventListener('scroll', () => {
const currentScroll = window.pageYOffset;
if (currentScroll > 100) {
header.classList.add('scrolled');
} else {
header.classList.remove('scrolled');
}
lastScroll = currentScroll;
});
// Active navigation link
const sections = document.querySelectorAll('section[id]');
function setActiveLink() {
const scrollY = window.pageYOffset;
sections.forEach(section => {
const sectionHeight = section.offsetHeight;
const sectionTop = section.offsetTop - 100;
const sectionId = section.getAttribute('id');
const correspondingLink = document.querySelector(`.nav-link[href="#${sectionId}"]`);
if (scrollY > sectionTop && scrollY <= sectionTop + sectionHeight) {
navLinks.forEach(link => link.classList.remove('active'));
if (correspondingLink) {
correspondingLink.classList.add('active');
}
}
});
}
window.addEventListener('scroll', setActiveLink);
// Smooth scroll for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
// FAQ Accordion
const faqItems = document.querySelectorAll('.faq-item');
faqItems.forEach(item => {
const question = item.querySelector('.faq-question');
question.addEventListener('click', () => {
const isActive = item.classList.contains('active');
// Close all FAQ items
faqItems.forEach(faq => {
faq.classList.remove('active');
});
// Open clicked item if it wasn't active
if (!isActive) {
item.classList.add('active');
}
});
});
// Contact form handling
const contactForm = document.getElementById('contactForm');
if (contactForm) {
contactForm.addEventListener('submit', function(e) {
e.preventDefault();
// Get form data
const formData = new FormData(this);
const data = Object.fromEntries(formData);
// Form data prepared for submission
// Send data to server
fetch('/contact', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(result => {
if (result.success) {
alert('תודה על פנייתך! נחזור אליך בהקדם.');
this.reset();
} else {
alert('שגיאה בשליחת הטופס: ' + result.message);
}
})
.catch(error => {
console.error('Error sending form:', error);
alert('שגיאה בשליחת הטופס. נסה שוב מאוחר יותר.');
});
});
}
// Scroll animations - optimized for faster loading
const observerOptions = {
threshold: 0.15, // Trigger earlier for faster response
rootMargin: '0px 0px -50px 0px' // Reduced margin for earlier trigger
};
// Make observer global for reuse
window.scrollObserver = new IntersectionObserver(function(entries) {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.style.opacity = '1';
entry.target.style.transform = 'translateY(0)';
window.scrollObserver.unobserve(entry.target);
}
});
}, observerOptions);
// Function to setup animations for elements
function setupAnimations() {
const animatedElements = document.querySelectorAll('.service-card, .project-card, .testimonial-card, .faq-item');
animatedElements.forEach((el, index) => {
// Show content immediately, then animate
el.style.opacity = '0.3'; // Show content with low opacity first
el.style.transform = 'translateY(20px)';
el.style.transition = `opacity 0.4s ease ${index * 0.05}s, transform 0.4s ease ${index * 0.05}s`;
window.scrollObserver.observe(el);
});
}
// Setup animations immediately
setupAnimations();
// Parallax effect for hero background shapes
const shapes = document.querySelectorAll('.geometric-shape');
window.addEventListener('scroll', () => {
const scrolled = window.pageYOffset;
shapes.forEach((shape, index) => {
const speed = 0.5 + (index * 0.1);
const yPos = -(scrolled * speed);
shape.style.transform = `translateY(${yPos}px)`;
});
});
// Form validation
const inputs = contactForm ? contactForm.querySelectorAll('input, select, textarea') : [];
inputs.forEach(input => {
input.addEventListener('blur', function() {
if (this.hasAttribute('required') && !this.value) {
this.classList.add('error');
this.style.borderColor = '#FF3B30';
} else {
this.classList.remove('error');
this.style.borderColor = '';
}
});
input.addEventListener('focus', function() {
this.classList.remove('error');
this.style.borderColor = '';
});
});
// Email validation
const emailInput = document.getElementById('email');
if (emailInput) {
emailInput.addEventListener('blur', function() {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (this.value && !emailRegex.test(this.value)) {
this.classList.add('error');
this.style.borderColor = '#FF3B30';
}
});
}
// Phone validation
const phoneInput = document.getElementById('phone');
if (phoneInput) {
phoneInput.addEventListener('input', function() {
// Remove non-numeric characters
this.value = this.value.replace(/[^0-9-]/g, '');
});
}
// Add loading state to submit button
if (contactForm) {
contactForm.addEventListener('submit', function() {
const submitButton = this.querySelector('.submit-button');
submitButton.textContent = 'שולח...';
submitButton.disabled = true;
// Reset button after 2 seconds (simulating server response)
setTimeout(() => {
submitButton.textContent = 'שלח פנייה';
submitButton.disabled = false;
}, 2000);
});
}
// Lazy loading for project images
const projectImages = document.querySelectorAll('.project-image');
const imageObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
// Here you would normally load the actual image
entry.target.style.opacity = '1';
imageObserver.unobserve(entry.target);
}
});
}, { threshold: 0.1 });
projectImages.forEach(img => {
img.style.opacity = '0';
img.style.transition = 'opacity 0.6s ease';
imageObserver.observe(img);
});
// Add hover effect to project cards
const projectCards = document.querySelectorAll('.project-card');
projectCards.forEach(card => {
card.addEventListener('mouseenter', function() {
this.style.transform = 'translateY(-10px) scale(1.02)';
});
card.addEventListener('mouseleave', function() {
this.style.transform = '';
});
});
// Service cards hover effect
const serviceCards = document.querySelectorAll('.service-card');
serviceCards.forEach(card => {
card.addEventListener('mouseenter', function() {
const icon = this.querySelector('.service-icon');
icon.style.transform = 'scale(1.1) rotate(5deg)';
});
card.addEventListener('mouseleave', function() {
const icon = this.querySelector('.service-icon');
icon.style.transform = '';
});
});
// Load dynamic content from admin data
loadDynamicContent();
// Set initial styles for hero animations - faster and smoother
const heroTitle = document.querySelector('.hero-title');
const heroSubtitle = document.querySelector('.hero-subtitle');
const ctaButton = document.querySelector('.cta-button');
if (heroTitle) {
heroTitle.style.opacity = '0';
heroTitle.style.transform = 'translateY(20px)';
heroTitle.style.transition = 'opacity 0.5s ease, transform 0.5s ease';
}
if (heroSubtitle) {
heroSubtitle.style.opacity = '0';
heroSubtitle.style.transform = 'translateY(20px)';
heroSubtitle.style.transition = 'opacity 0.5s ease, transform 0.5s ease';
}
if (ctaButton) {
ctaButton.style.opacity = '0';
ctaButton.style.transform = 'translateY(20px)';
ctaButton.style.transition = 'opacity 0.5s ease, transform 0.5s ease';
}
// Initialize animations on page load - much faster
window.addEventListener('load', () => {
document.body.classList.add('loaded');
// Animate hero content with shorter delays
setTimeout(() => {
if (heroTitle) {
heroTitle.style.opacity = '1';
heroTitle.style.transform = 'translateY(0)';
}
}, 100);
setTimeout(() => {
if (heroSubtitle) {
heroSubtitle.style.opacity = '1';
heroSubtitle.style.transform = 'translateY(0)';
}
}, 200);
setTimeout(() => {
if (ctaButton) {
ctaButton.style.opacity = '1';
ctaButton.style.transform = 'translateY(0)';
}
}, 300);
});
// Load dynamic content when page loads
loadDynamicContent();
});
// Load dynamic content from server
async function loadDynamicContent() {
console.log('loadDynamicContent called');
try {
// Fetch data from server
console.log('Fetching site data...');
const response = await fetch('/site-data');
if (!response.ok) throw new Error('Failed to fetch site data');
console.log('Site data response:', response.status);
const data = await response.json();
console.log('Site data loaded:', data);
// Site data loaded successfully from server
updatePageContent(data);
} catch (error) {
console.error('Error loading site data from server:', error);
// No fallback to localStorage - we want to rely on server data only
}
}
// Update page content with data
function updatePageContent(data) {
if (!data) return;
// Update hero section
if (data.hero) {
const heroTitle = document.querySelector('.hero-title');
const heroSubtitle = document.querySelector('.hero-subtitle');
if (heroTitle && data.hero.headline) {
heroTitle.textContent = data.hero.headline;
}
if (heroSubtitle && data.hero.subtitle) {
heroSubtitle.textContent = data.hero.subtitle;
}
}
// Update logo
if (data.settings) {
updateSiteLogo(data.settings.logo);
}
// Update projects dynamically from JSON with proper ordering
if (data.projects) {
console.log('Updating projects with data:', data.projects);
const projectsGrid = document.querySelector('.projects-grid');
if (projectsGrid) {
// Clear existing static projects
projectsGrid.innerHTML = '';
// Sort projects by order field (for drag & drop reordering)
const sortedProjects = data.projects
.filter(project => project.active)
.sort((a, b) => {
const orderA = a.order !== undefined ? a.order : a.id;
const orderB = b.order !== undefined ? b.order : b.id;
return orderA - orderB;
});
// Create project cards dynamically from JSON
sortedProjects.forEach((project) => {
const projectCard = document.createElement('div');
projectCard.className = 'project-card';
projectCard.dataset.projectId = project.id; // For drag & drop
projectCard.innerHTML = `
<div class="project-image">
${project.image ? `<img src="${project.image}" alt="${project.name}" style="width: 100%; height: 100%; object-fit: cover;">` : ''}
</div>
<div class="project-content">
<h3>${project.name}</h3>
<p>${project.description}</p>
${project.url ? `<a href="${project.url}" target="_blank" class="project-link">צפה בפרויקט</a>` : ''}
</div>
`;
projectsGrid.appendChild(projectCard);
console.log(`Added project: ${project.name} with image: ${project.image ? 'YES' : 'NO'} at order: ${project.order || 0}`);
});
console.log(`Total projects displayed: ${sortedProjects.length}`);
}
}
// Update testimonials
if (data.testimonials) {
updateTestimonials(data.testimonials);
}
// Update FAQ
if (data.faq) {
updateFAQ(data.faq);
}
}
// Function to update from admin panel
function updateFromAdmin(data) {
if (data.hero) {
const heroTitle = document.querySelector('.hero-title');
const heroSubtitle = document.querySelector('.hero-subtitle');
if (heroTitle && data.hero.headline) {
heroTitle.textContent = data.hero.headline;
}
if (heroSubtitle && data.hero.subtitle) {
heroSubtitle.textContent = data.hero.subtitle;
}
}
// Update projects
if (data.projects) {
const projectCards = document.querySelectorAll('.project-card');
data.projects.forEach((project, index) => {
if (projectCards[index]) {
const projectName = projectCards[index].querySelector('h3');
const projectDesc = projectCards[index].querySelector('p');
const projectImage = projectCards[index].querySelector('.project-image');
if (projectName) projectName.textContent = project.name;
if (projectDesc) projectDesc.textContent = project.description;
if (projectImage && project.image) {
projectImage.innerHTML = `<img src="${project.image}" alt="${project.name}" style="width: 100%; height: 100%; object-fit: cover;">`;
}
}
});
}
}
// Listen for messages from admin panel
window.addEventListener('message', function(e) {
if (e.data && e.data.type === 'dataUpdate') {
// Received data update notification from admin panel
loadDynamicContent();
} else if (e.data && e.data.type === 'logoUpdate') {
// Received logo update from admin panel
updateSiteLogo(e.data.logoUrl);
}
});
// Function to update site logo
function updateSiteLogo(logoUrl) {
const siteLogo = document.getElementById('siteLogo');
const siteTitle = document.getElementById('siteTitle');
if (siteLogo && siteTitle) {
if (logoUrl) {
// Force reload to bypass cache
siteLogo.onload = function() {
this.style.display = 'block';
siteTitle.style.display = 'none';
};
siteLogo.src = logoUrl;
} else {
siteLogo.style.display = 'none';
siteTitle.style.display = 'block';
}
}
}
// Function to update testimonials dynamically
function updateTestimonials(testimonials) {
const testimonialsGrid = document.querySelector('.testimonials-grid');
if (!testimonialsGrid) return;
testimonialsGrid.innerHTML = testimonials.map(testimonial => `
<div class="testimonial-card">
<div class="testimonial-content">
<p>"${testimonial.text}"</p>
<div class="testimonial-author">
<strong>${testimonial.name}</strong>
<span>${testimonial.role}${testimonial.company ? ` - ${testimonial.company}` : ''}</span>
</div>
</div>
</div>
`).join('');
// Re-setup animations for new content
const newCards = testimonialsGrid.querySelectorAll('.testimonial-card');
newCards.forEach((card, index) => {
card.style.opacity = '0.3';
card.style.transform = 'translateY(20px)';
card.style.transition = `opacity 0.4s ease ${index * 0.05}s, transform 0.4s ease ${index * 0.05}s`;
window.scrollObserver.observe(card);
});
}
// Function to update FAQ dynamically
function updateFAQ(faq) {
const faqList = document.querySelector('.faq-list');
if (!faqList) return;
faqList.innerHTML = faq.map(item => `
<div class="faq-item">
<button class="faq-question">
<span>${item.question}</span>
<svg class="faq-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="6 9 12 15 18 9"></polyline>
</svg>
</button>
<div class="faq-answer">
<p>${item.answer}</p>
</div>
</div>
`).join('');
// Re-setup animations for new FAQ items
const newItems = faqList.querySelectorAll('.faq-item');
newItems.forEach((item, index) => {
item.style.opacity = '0.3';
item.style.transform = 'translateY(20px)';
item.style.transition = `opacity 0.4s ease ${index * 0.05}s, transform 0.4s ease ${index * 0.05}s`;
window.scrollObserver.observe(item);
});
// Re-initialize FAQ accordion functionality
initializeFAQAccordion();
}
// Function to initialize FAQ accordion
function initializeFAQAccordion() {
const faqItems = document.querySelectorAll('.faq-item');
faqItems.forEach(item => {
const question = item.querySelector('.faq-question');
question.addEventListener('click', () => {
const isActive = item.classList.contains('active');
// Close all FAQ items
faqItems.forEach(faq => {
faq.classList.remove('active');
});
// Open clicked item if it wasn't active
if (!isActive) {
item.classList.add('active');
}
});
});
}
// Make updateFromAdmin available globally for iframe communication
window.updateFromAdmin = updateFromAdmin;