File size: 7,417 Bytes
4aa219b | 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 | /**
* FocusFriend — Custom JavaScript
* Handles: focus timer, breathing animation, break reminders, UI interactions
*/
// ============================================================
// Focus Timer
// ============================================================
let focusTimerId = null;
let focusRemainingSeconds = 0;
let focusTotalSeconds = 0;
function focusStartTimer(durationMinutes) {
focusTotalSeconds = durationMinutes * 60;
focusRemainingSeconds = focusTotalSeconds;
focusUpdateDisplay();
focusShowTimer();
if (focusTimerId) clearInterval(focusTimerId);
focusTimerId = setInterval(() => {
focusRemainingSeconds--;
focusUpdateDisplay();
if (focusRemainingSeconds <= 300) {
// 5-minute warning
const timerEl = document.getElementById('ff-focus-timer');
if (timerEl) timerEl.classList.add('warning');
}
if (focusRemainingSeconds <= 0) {
focusComplete();
}
}, 1000);
}
function focusPauseTimer() {
if (focusTimerId) {
clearInterval(focusTimerId);
focusTimerId = null;
}
}
function focusResumeTimer() {
if (focusRemainingSeconds > 0 && !focusTimerId) {
focusTimerId = setInterval(() => {
focusRemainingSeconds--;
focusUpdateDisplay();
if (focusRemainingSeconds <= 0) focusComplete();
}, 1000);
}
}
function focusStopTimer() {
if (focusTimerId) {
clearInterval(focusTimerId);
focusTimerId = null;
}
focusHideTimer();
}
function focusUpdateDisplay() {
const mins = Math.floor(focusRemainingSeconds / 60);
const secs = focusRemainingSeconds % 60;
const display = document.getElementById('ff-focus-timer');
if (display) {
display.textContent =
String(mins).padStart(2, '0') + ':' + String(secs).padStart(2, '0');
}
}
function focusShowTimer() {
const timer = document.getElementById('ff-focus-timer');
if (timer) timer.style.display = 'block';
}
function focusHideTimer() {
const timer = document.getElementById('ff-focus-timer');
if (timer) {
timer.style.display = 'none';
timer.classList.remove('warning');
}
}
function focusComplete() {
clearInterval(focusTimerId);
focusTimerId = null;
const artEl = document.querySelector('.ff-pip-art textarea');
if (artEl) {
artEl.classList.add('ff-celebrate');
setTimeout(() => artEl.classList.remove('ff-celebrate'), 1500);
}
// Trigger a Gradio event to notify the Python backend
const event = new CustomEvent('focusfriend:focus-complete', {
detail: { duration: focusTotalSeconds / 60 }
});
document.dispatchEvent(event);
}
function focusGetRemaining() {
return focusRemainingSeconds;
}
// ============================================================
// Breathing Animation
// ============================================================
let breatheIntervalId = null;
let breathePhaseIndex = 0;
let breatheSecondsInPhase = 0;
const BREATHE_PATTERNS = {
'4-7-8': [
{ duration: 4, phase: 'in', label: 'Inhale' },
{ duration: 7, phase: 'hold', label: 'Hold' },
{ duration: 8, phase: 'out', label: 'Exhale' },
],
'box': [
{ duration: 4, phase: 'in', label: 'Inhale' },
{ duration: 4, phase: 'hold', label: 'Hold' },
{ duration: 4, phase: 'out', label: 'Exhale' },
{ duration: 4, phase: 'hold', label: 'Hold' },
],
'simple': [
{ duration: 4, phase: 'in', label: 'Inhale' },
{ duration: 6, phase: 'out', label: 'Exhale' },
],
};
function breatheStart(technique) {
breatheStop();
const pattern = BREATHE_PATTERNS[technique] || BREATHE_PATTERNS['4-7-8'];
breathePhaseIndex = 0;
breatheSecondsInPhase = 0;
const pipEl = document.querySelector('.ff-pip-art textarea');
const guideEl = document.getElementById('ff-breathe-guide');
breatheIntervalId = setInterval(() => {
const phase = pattern[breathePhaseIndex % pattern.length];
if (breatheSecondsInPhase === 0) {
// Update Pip's expression
if (pipEl) {
pipEl.classList.remove('ff-breathing-in', 'ff-breathing-out');
if (phase.phase === 'in') {
pipEl.classList.add('ff-breathing-in');
} else if (phase.phase === 'out') {
pipEl.classList.add('ff-breathing-out');
}
}
// Update guide text
if (guideEl) {
guideEl.textContent = phase.label + ' (' + phase.duration + 's)';
}
}
breatheSecondsInPhase++;
if (breatheSecondsInPhase >= phase.duration) {
breatheSecondsInPhase = 0;
breathePhaseIndex++;
}
}, 1000);
}
function breatheStop() {
if (breatheIntervalId) {
clearInterval(breatheIntervalId);
breatheIntervalId = null;
}
const pipEl = document.querySelector('.ff-pip-art textarea');
if (pipEl) {
pipEl.classList.remove('ff-breathing-in', 'ff-breathing-out');
}
}
// ============================================================
// Break Reminder
// ============================================================
let breakReminderId = null;
let lastActivityTime = Date.now();
function breakReminderStart(intervalMinutes) {
breakReminderStop();
breakReminderId = setInterval(() => {
const idleMs = Date.now() - lastActivityTime;
const idleMinutes = idleMs / 60000;
if (idleMinutes >= intervalMinutes) {
const event = new CustomEvent('focusfriend:break-reminder', {
detail: { idleMinutes: Math.round(idleMinutes) }
});
document.dispatchEvent(event);
}
}, 60000); // Check every minute
}
function breakReminderStop() {
if (breakReminderId) {
clearInterval(breakReminderId);
breakReminderId = null;
}
}
function breakRecordActivity() {
lastActivityTime = Date.now();
}
// Track user activity
document.addEventListener('click', breakRecordActivity);
document.addEventListener('keydown', breakRecordActivity);
// ============================================================
// Pip Mood Animations
// ============================================================
function pipSetMood(mood) {
const artEl = document.querySelector('.ff-pip-art textarea');
if (!artEl) return;
// Remove all mood classes
artEl.classList.remove(
'ff-breathing-in', 'ff-breathing-out', 'ff-celebrate'
);
// Add mood-specific class
if (mood === 'celebrate') {
artEl.classList.add('ff-celebrate');
setTimeout(() => artEl.classList.remove('ff-celebrate'), 1500);
}
}
// ============================================================
// Page Load Initialization
// ============================================================
document.addEventListener('DOMContentLoaded', () => {
console.log('FocusFriend initialized ✦');
breakRecordActivity();
});
// Expose functions globally so Gradio can call them
window.focusfriend = {
focusStartTimer,
focusPauseTimer,
focusResumeTimer,
focusStopTimer,
focusGetRemaining,
breatheStart,
breatheStop,
breakReminderStart,
breakReminderStop,
pipSetMood,
};
|