-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
242 lines (224 loc) · 12.9 KB
/
Copy pathscript.js
File metadata and controls
242 lines (224 loc) · 12.9 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
// ===== GLOBAL VARIABLES =====
let timeLeft = 25 * 60, totalTime = 25 * 60, timerInterval = null, isRunning = false, currentMode = 'focus';
let audioElement = null, currentTrack = 0, isMusicPlaying = false, musicProgressInterval = null;
let todaySessions = parseInt(localStorage.getItem('todaySessions')) || 0;
let totalFocusTime = parseInt(localStorage.getItem('totalFocusTime')) || 0;
let goalSessions = parseInt(localStorage.getItem('goalSessions')) || 4;
let goalMinutes = parseInt(localStorage.getItem('goalMinutes')) || 120;
let lastDate = localStorage.getItem('lastDate') || new Date().toDateString();
// Reset harian
if (lastDate !== new Date().toDateString()) {
todaySessions = 0; totalFocusTime = 0;
lastDate = new Date().toDateString();
localStorage.setItem('lastDate', lastDate);
localStorage.setItem('todaySessions', '0');
localStorage.setItem('totalFocusTime', '0');
}
const playlist = [
{ title: "In Dreamland", artist: "Chillpeach", url: "assetsmusic/song1.mp3" },
{ title: "2:00 AM", artist: "Chillpeach", url: "assetsmusic/song2.mp3" },
{ title: "Taiyaki", artist: "Chillpeach", url: "assetsmusic/song3.mp3" },
{ title: "Gameplay", artist: "Chillpeach", url: "assetsmusic/song4.mp3" },
{ title: "Purple", artist: "Chillpeach", url: "assetsmusic/song5.mp3" },
{ title: "Loading", artist: "Chillpeach", url: "assetsmusic/song6.mp3" },
{ title: "Dreamy Mode", artist: "Chillpeach", url: "assetsmusic/song7.mp3" },
{ title: "On The Top", artist: "Chillpeach", url: "assetsmusic/song8.mp3" },
{ title: "Tomato Farm", artist: "Chillpeach", url: "assetsmusic/song9.mp3" },
{ title: "Little Break", artist: "Chillpeach", url: "assetsmusic/song10.mp3" }
];
const quotes = [
{ text: "The only way to do great work is to love what you do.", author: "Steve Jobs" },
{ text: "Believe you can and you're halfway there.", author: "Theodore Roosevelt" },
{ text: "Success is not final, failure is not fatal: it is the courage to continue that counts.", author: "Winston Churchill" },
{ text: "Don't watch the clock; do what it does. Keep going.", author: "Sam Levenson" },
{ text: "The future belongs to those who believe in the beauty of their dreams.", author: "Eleanor Roosevelt" }
];
// ===== INIT =====
document.addEventListener('DOMContentLoaded', () => {
audioElement = new Audio(); audioElement.volume = 0.7;
audioElement.addEventListener('ended', nextTrack);
updateTimerDisplay(); renderPlaylist(); loadNewQuote(); loadSettings();
setupEventListeners(); preloadDurations(); updateStatsDisplay(); setupStatsEditing();
});
// ===== SETTINGS & UI =====
function loadSettings() {
['focusTime','restTime','longBreak'].forEach(id => {
const val = localStorage.getItem(id);
if(val) document.getElementById(id).value = val;
});
}
function setupEventListeners() {
document.addEventListener('keydown', e => {
if(e.code === 'Space' && e.target.tagName !== 'INPUT') { e.preventDefault(); isRunning ? pauseTimer() : startTimer(); }
if(e.key === 'Escape') { const m = document.getElementById('settingsModal'); if(m.classList.contains('show')) toggleSettings(); }
});
}
function toggleSettings() {
const m = document.getElementById('settingsModal'), b = document.getElementById('settingsBackdrop');
m.classList.toggle('show'); b.classList.toggle('show');
}
function toggleTheme() {
const overlay = document.getElementById('themeOverlay');
const isDark = document.body.classList.contains('dark-mode');
// Reset overlay
overlay.className = 'theme-overlay';
void overlay.offsetWidth; // Force reflow biar animasi jalan ulang
if (!isDark) {
overlay.classList.add('light-to-dark', 'animating');
document.body.classList.add('dark-mode');
} else {
overlay.classList.add('dark-to-light', 'animating');
document.body.classList.remove('dark-mode');
}
// Hapus class animasi setelah selesai
setTimeout(() => {
overlay.className = 'theme-overlay';
}, 550);
}
function loadNewQuote() {
const q = quotes[Math.floor(Math.random() * quotes.length)];
document.getElementById('quoteText').textContent = `"${q.text}"`;
document.getElementById('quoteAuthor').textContent = `- ${q.author}`;
}
function newQuote() { loadNewQuote(); }
// ===== TIMER =====
function updateTimerDisplay() {
const m = Math.floor(timeLeft/60), s = timeLeft%60;
document.getElementById('timerDisplay').textContent = `${m.toString().padStart(2,'0')}:${s.toString().padStart(2,'0')}`;
}
function startTimer() {
if(isRunning) return; isRunning = true;
document.getElementById('startBtn').style.display = 'none';
document.getElementById('pauseBtn').style.display = 'inline-block';
timerInterval = setInterval(() => {
timeLeft--; updateTimerDisplay();
if(timeLeft <= 0) { clearInterval(timerInterval); playNotificationSound(); switchMode(); }
}, 1000);
}
function pauseTimer() {
if(!isRunning) return; isRunning = false; clearInterval(timerInterval);
document.getElementById('startBtn').style.display = 'inline-block';
document.getElementById('pauseBtn').style.display = 'none';
document.getElementById('startBtn').textContent = 'Resume';
}
function resetTimer() {
pauseTimer();
const id = currentMode === 'focus' ? 'focusTime' : currentMode === 'rest' ? 'restTime' : 'longBreak';
timeLeft = document.getElementById(id).value * 60; totalTime = timeLeft;
document.getElementById('startBtn').textContent = 'Start'; updateTimerDisplay();
}
function switchMode() {
if(currentMode === 'focus') trackSession(parseInt(document.getElementById('focusTime').value));
if(currentMode === 'focus') {
currentMode = 'rest'; timeLeft = document.getElementById('restTime').value * 60;
document.getElementById('timerLabel').textContent = 'Rest Time';
document.getElementById('modeIndicator').textContent = '😌 Rest Mode';
document.getElementById('modeIndicator').style.background = 'var(--pastel-cyan)';
} else {
currentMode = 'focus'; timeLeft = document.getElementById('focusTime').value * 60;
document.getElementById('timerLabel').textContent = 'Focus Time';
document.getElementById('modeIndicator').textContent = '🍅 Focus Mode';
document.getElementById('modeIndicator').style.background = 'var(--pastel-peach)';
}
totalTime = timeLeft; updateTimerDisplay(); isRunning = false;
document.getElementById('startBtn').style.display = 'inline-block';
document.getElementById('pauseBtn').style.display = 'none';
document.getElementById('startBtn').textContent = 'Start';
}
function playNotificationSound() {
try {
const ctx = new (window.AudioContext || window.webkitAudioContext)();
const osc = ctx.createOscillator(), gain = ctx.createGain();
osc.connect(gain); gain.connect(ctx.destination); osc.frequency.value = 800;
gain.gain.setValueAtTime(0.3, ctx.currentTime); gain.gain.exponentialRampToValueAtTime(0.01, ctx.currentTime + 0.5);
osc.start(); osc.stop(ctx.currentTime + 0.5);
} catch(e) {}
}
// ===== STATS & EDITING =====
function trackSession(mins) { todaySessions++; totalFocusTime += mins; localStorage.setItem('todaySessions', todaySessions); localStorage.setItem('totalFocusTime', totalFocusTime); updateStatsDisplay(); }
function updateStatsDisplay() {
const sess = document.getElementById('sessionsInput'), goalS = document.getElementById('goalSessionsInput');
const timeD = document.getElementById('totalTimeDisplay'), prog = document.getElementById('dailyProgress'), pct = document.getElementById('progressPercent');
if(sess) sess.value = todaySessions; if(goalS) goalS.value = goalSessions;
if(timeD) timeD.textContent = `${Math.floor(totalFocusTime/60)}h ${totalFocusTime%60}m`;
const p = Math.min((totalFocusTime / goalMinutes) * 100, 100);
if(prog) prog.style.width = `${p}%`; if(pct) pct.textContent = `${Math.round(p)}%`;
}
function setupStatsEditing() {
document.getElementById('sessionsInput')?.addEventListener('change', e => { todaySessions = Math.max(0, parseInt(e.target.value)||0); localStorage.setItem('todaySessions', todaySessions); updateStatsDisplay(); });
document.getElementById('goalSessionsInput')?.addEventListener('change', e => { goalSessions = Math.max(1, parseInt(e.target.value)||1); localStorage.setItem('goalSessions', goalSessions); updateStatsDisplay(); });
}
function addManualTime(mins) { totalFocusTime += mins; localStorage.setItem('totalFocusTime', totalFocusTime); updateStatsDisplay(); }
function resetTimeStat() { if(confirm('Reset waktu?')) { totalFocusTime = 0; localStorage.setItem('totalFocusTime', 0); updateStatsDisplay(); } }
function resetStats() { if(confirm('Reset semua stats?')) { todaySessions=0; totalFocusTime=0; localStorage.setItem('todaySessions','0'); localStorage.setItem('totalFocusTime','0'); updateStatsDisplay(); } }
function toggleStats() {
const t = document.getElementById('timerView'), s = document.getElementById('statsPanel');
if(s.style.display === 'block') { s.style.display='none'; t.style.display='block'; }
else { t.style.display='none'; s.style.display='block'; updateStatsDisplay(); }
}
// ===== MUSIC =====
function formatDuration(sec) { if(!sec||isNaN(sec)) return "0:00"; return `${Math.floor(sec/60)}:${Math.floor(sec%60).toString().padStart(2,'0')}`; }
function preloadDurations() {
let loaded = 0; playlist.forEach(t => {
const a = new Audio(); a.src = t.url;
a.addEventListener('loadedmetadata', () => { t.duration = a.duration; if(++loaded === playlist.length) renderPlaylist(); });
a.addEventListener('error', () => { t.duration = 0; if(++loaded === playlist.length) renderPlaylist(); });
});
}
function renderPlaylist() {
const c = document.getElementById('playlist'); if(!c) return; c.innerHTML = '';
playlist.forEach((t, i) => {
const d = document.createElement('div'); d.className = 'playlist-item' + (i===currentTrack?' active':'');
d.onclick = () => selectTrack(i);
d.innerHTML = `<div class="playlist-item-number">${i+1}</div><div style="flex:1"><div style="font-weight:600;font-size:14px">${t.title}</div><div style="font-size:12px;opacity:0.7">${t.artist}</div></div><div style="font-size:12px;font-weight:600;color:var(--pastel-magenta);min-width:35px;text-align:right">${formatDuration(t.duration)}</div>`;
c.appendChild(d);
});
}
function selectTrack(i) {
currentTrack = i; renderPlaylist();
document.getElementById('nowPlaying').textContent = playlist[i].title;
audioElement.src = playlist[i].url; audioElement.load();
if(isMusicPlaying) { audioElement.play().catch(()=>{isMusicPlaying=false;document.getElementById('playMusicBtn').textContent='▶';}); startMusicProgress(); }
}
function toggleMusic() {
if(!audioElement.src) selectTrack(currentTrack);
isMusicPlaying = !isMusicPlaying; const b = document.getElementById('playMusicBtn');
if(isMusicPlaying) { audioElement.play().then(()=>{b.textContent='⏸';startMusicProgress();}).catch(()=>{isMusicPlaying=false;b.textContent='▶';}); }
else { audioElement.pause(); b.textContent='▶'; stopMusicProgress(); }
}
function startMusicProgress() {
stopMusicProgress();
musicProgressInterval = setInterval(() => {
if(!isMusicPlaying||!audioElement.duration) return;
document.getElementById('musicProgress').style.width = `${(audioElement.currentTime/audioElement.duration)*100}%`;
}, 500);
}
function stopMusicProgress() { if(musicProgressInterval) { clearInterval(musicProgressInterval); musicProgressInterval=null; } }
function nextTrack() { currentTrack=(currentTrack+1)%playlist.length; selectTrack(currentTrack); }
function previousTrack() { currentTrack=(currentTrack-1+playlist.length)%playlist.length; selectTrack(currentTrack); }
// ===== KONFIRMASI SETTINGS =====
function confirmSettings() {
// 1. Ambil nilai dari input
const f = document.getElementById('focusTime').value;
const r = document.getElementById('restTime').value;
const l = document.getElementById('longBreak').value;
// 2. Simpan ke LocalStorage
localStorage.setItem('focusTime', f);
localStorage.setItem('restTime', r);
localStorage.setItem('longBreak', l);
// 3. Update Timer Sekarang (Kalau lagi Pause/Stop)
if (!isRunning) {
if (currentMode === 'focus') timeLeft = f * 60;
else if (currentMode === 'rest') timeLeft = r * 60;
else timeLeft = l * 60;
totalTime = timeLeft;
updateTimerDisplay();
}
// 4. Tutup Modal
toggleSettings();
}
// ===== EXPORTS =====
window.startTimer=startTimer; window.pauseTimer=pauseTimer; window.resetTimer=resetTimer;
window.toggleSettings=toggleSettings; window.toggleTheme=toggleTheme; window.newQuote=newQuote;
window.toggleMusic=toggleMusic; window.nextTrack=nextTrack; window.previousTrack=previousTrack;
window.toggleStats=toggleStats; window.resetStats=resetStats; window.addManualTime=addManualTime; window.resetTimeStat=resetTimeStat;