gefiwek187
commited on
Create js/script.js
Browse files- static/js/script.js +173 -0
static/js/script.js
ADDED
@@ -0,0 +1,173 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
document.addEventListener('DOMContentLoaded', () => {
|
2 |
+
const generateBtn = document.getElementById('generate-btn');
|
3 |
+
const topicInput = document.getElementById('topic');
|
4 |
+
const loadingSpinner = document.getElementById('loading');
|
5 |
+
const storyContainer = document.getElementById('story-container');
|
6 |
+
const storyContent = document.getElementById('story-content');
|
7 |
+
const storyTitle = document.getElementById('story-title');
|
8 |
+
|
9 |
+
// Initialize counters
|
10 |
+
let reads = 0;
|
11 |
+
let votes = 0;
|
12 |
+
let comments = 0;
|
13 |
+
|
14 |
+
// Animate counter function
|
15 |
+
const animateCounter = (element, start, end, duration) => {
|
16 |
+
let startTimestamp = null;
|
17 |
+
const step = (timestamp) => {
|
18 |
+
if (!startTimestamp) startTimestamp = timestamp;
|
19 |
+
const progress = Math.min((timestamp - startTimestamp) / duration, 1);
|
20 |
+
const current = Math.floor(progress * (end - start) + start);
|
21 |
+
element.textContent = current;
|
22 |
+
if (progress < 1) {
|
23 |
+
window.requestAnimationFrame(step);
|
24 |
+
}
|
25 |
+
};
|
26 |
+
window.requestAnimationFrame(step);
|
27 |
+
};
|
28 |
+
|
29 |
+
// Add hover effect to story container
|
30 |
+
storyContainer.addEventListener('mouseenter', () => {
|
31 |
+
storyContainer.style.transform = 'translateY(-5px)';
|
32 |
+
storyContainer.style.boxShadow = '0 6px 25px rgba(0, 0, 0, 0.15)';
|
33 |
+
});
|
34 |
+
|
35 |
+
storyContainer.addEventListener('mouseleave', () => {
|
36 |
+
storyContainer.style.transform = 'translateY(0)';
|
37 |
+
storyContainer.style.boxShadow = '0 4px 20px rgba(0, 0, 0, 0.1)';
|
38 |
+
});
|
39 |
+
|
40 |
+
// Handle story generation
|
41 |
+
generateBtn.addEventListener('click', async () => {
|
42 |
+
const topic = topicInput.value.trim();
|
43 |
+
if (!topic) {
|
44 |
+
topicInput.classList.add('shake');
|
45 |
+
setTimeout(() => topicInput.classList.remove('shake'), 500);
|
46 |
+
return;
|
47 |
+
}
|
48 |
+
|
49 |
+
// Disable button and show loading
|
50 |
+
generateBtn.disabled = true;
|
51 |
+
generateBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Generating...';
|
52 |
+
loadingSpinner.style.display = 'block';
|
53 |
+
storyContainer.style.display = 'none';
|
54 |
+
|
55 |
+
try {
|
56 |
+
const response = await fetch('/generate', {
|
57 |
+
method: 'POST',
|
58 |
+
headers: {
|
59 |
+
'Content-Type': 'application/json',
|
60 |
+
},
|
61 |
+
body: JSON.stringify({ topic }),
|
62 |
+
});
|
63 |
+
|
64 |
+
const data = await response.json();
|
65 |
+
|
66 |
+
if (response.ok) {
|
67 |
+
// Format and display story
|
68 |
+
storyTitle.textContent = topic.charAt(0).toUpperCase() + topic.slice(1);
|
69 |
+
storyContent.innerHTML = formatStoryText(data.story);
|
70 |
+
|
71 |
+
// Show story with animation
|
72 |
+
storyContainer.style.display = 'block';
|
73 |
+
storyContainer.style.opacity = '0';
|
74 |
+
storyContainer.style.transform = 'translateY(20px)';
|
75 |
+
|
76 |
+
// Trigger reflow
|
77 |
+
storyContainer.offsetHeight;
|
78 |
+
|
79 |
+
// Add animation
|
80 |
+
storyContainer.style.transition = 'all 0.5s ease-out';
|
81 |
+
storyContainer.style.opacity = '1';
|
82 |
+
storyContainer.style.transform = 'translateY(0)';
|
83 |
+
|
84 |
+
// Animate counters
|
85 |
+
reads += Math.floor(Math.random() * 50) + 10;
|
86 |
+
votes += Math.floor(Math.random() * 20) + 5;
|
87 |
+
comments += Math.floor(Math.random() * 10) + 2;
|
88 |
+
|
89 |
+
animateCounter(document.querySelector('.reads .count'), 0, reads, 1500);
|
90 |
+
animateCounter(document.querySelector('.votes .count'), 0, votes, 1500);
|
91 |
+
animateCounter(document.querySelector('.comments .count'), 0, comments, 1500);
|
92 |
+
|
93 |
+
// Scroll to story
|
94 |
+
storyContainer.scrollIntoView({ behavior: 'smooth', block: 'start' });
|
95 |
+
} else {
|
96 |
+
throw new Error(data.error || 'Failed to generate story');
|
97 |
+
}
|
98 |
+
} catch (error) {
|
99 |
+
showError('Error generating story: ' + error.message);
|
100 |
+
} finally {
|
101 |
+
loadingSpinner.style.display = 'none';
|
102 |
+
generateBtn.disabled = false;
|
103 |
+
generateBtn.innerHTML = '<span class="btn-text">Generate Story</span><i class="fas fa-magic"></i>';
|
104 |
+
}
|
105 |
+
});
|
106 |
+
|
107 |
+
// Handle action buttons
|
108 |
+
document.querySelector('.vote-btn').addEventListener('click', function() {
|
109 |
+
this.classList.toggle('voted');
|
110 |
+
if (this.classList.contains('voted')) {
|
111 |
+
votes++;
|
112 |
+
this.style.backgroundColor = '#FF8F00';
|
113 |
+
this.style.color = 'white';
|
114 |
+
} else {
|
115 |
+
votes--;
|
116 |
+
this.style.backgroundColor = 'white';
|
117 |
+
this.style.color = '#333';
|
118 |
+
}
|
119 |
+
document.querySelector('.votes .count').textContent = votes;
|
120 |
+
});
|
121 |
+
|
122 |
+
document.querySelector('.share-btn').addEventListener('click', () => {
|
123 |
+
// Create a temporary input to copy the URL
|
124 |
+
const input = document.createElement('input');
|
125 |
+
input.value = window.location.href;
|
126 |
+
document.body.appendChild(input);
|
127 |
+
input.select();
|
128 |
+
document.execCommand('copy');
|
129 |
+
document.body.removeChild(input);
|
130 |
+
|
131 |
+
// Show copied notification
|
132 |
+
const shareBtn = document.querySelector('.share-btn');
|
133 |
+
const originalText = shareBtn.innerHTML;
|
134 |
+
shareBtn.innerHTML = '<i class="fas fa-check"></i> Copied!';
|
135 |
+
setTimeout(() => {
|
136 |
+
shareBtn.innerHTML = originalText;
|
137 |
+
}, 2000);
|
138 |
+
});
|
139 |
+
|
140 |
+
// Format story text
|
141 |
+
function formatStoryText(text) {
|
142 |
+
return text
|
143 |
+
.split('\n')
|
144 |
+
.filter(para => para.trim())
|
145 |
+
.map(para => `<p class="story-paragraph">${para}</p>`)
|
146 |
+
.join('');
|
147 |
+
}
|
148 |
+
|
149 |
+
// Show error message
|
150 |
+
function showError(message) {
|
151 |
+
const errorDiv = document.createElement('div');
|
152 |
+
errorDiv.className = 'error-message';
|
153 |
+
errorDiv.innerHTML = `<i class="fas fa-exclamation-circle"></i> ${message}`;
|
154 |
+
document.querySelector('.input-section').appendChild(errorDiv);
|
155 |
+
setTimeout(() => errorDiv.remove(), 5000);
|
156 |
+
}
|
157 |
+
|
158 |
+
// Enable enter key to generate story
|
159 |
+
topicInput.addEventListener('keypress', (e) => {
|
160 |
+
if (e.key === 'Enter') {
|
161 |
+
generateBtn.click();
|
162 |
+
}
|
163 |
+
});
|
164 |
+
|
165 |
+
// Add input animations
|
166 |
+
topicInput.addEventListener('focus', () => {
|
167 |
+
document.querySelector('.input-wrapper').style.transform = 'scale(1.02)';
|
168 |
+
});
|
169 |
+
|
170 |
+
topicInput.addEventListener('blur', () => {
|
171 |
+
document.querySelector('.input-wrapper').style.transform = 'scale(1)';
|
172 |
+
});
|
173 |
+
});
|