11// fetch params in url for quick settings
2+ import { updateTimer } from "./utils.mjs" ;
3+
4+ // Lecture du paramètre "duration" dans l'URL (en secondes)
25const params = new URLSearchParams ( globalThis . location . search ) ;
36const div = document . getElementById ( 'expandingDiv' ) ;
47const timer = document . getElementById ( 'timer' ) ;
58const startMessage = document . getElementById ( 'startMessage' ) ;
6- const settingsModal = document . getElementById ( "settingsModal" ) ;
79const settingsForm = document . getElementById ( "settingsForm" ) ;
10+ const settingsModal = document . getElementById ( "settingsModal" ) ;
11+ const closeSettings = document . getElementById ( "closeSettings" ) ;
12+ const resetBtn = document . getElementById ( "resetBtn" ) ;
13+ const submitBtn = document . getElementById ( "submitBtn" ) ;
14+ const showSettingsBtn = document . getElementById ( "showSettings" ) ;
15+ const startAnimationBtn = document . getElementById ( "startAnimation" ) ;
816const totalHeight = window . innerHeight ;
917
1018const defaultSettings = {
11- "durationInSeconds" : 600 ,
19+ "durationInSeconds" : 600 ,
1220 "showTimer" : true ,
1321 "colorScheme" : "zenika-colors" ,
1422 "firstThreshold" : 0.8 ,
@@ -19,7 +27,7 @@ const defaultSettings = {
1927}
2028
2129let settings = {
22- "durationInSeconds" : parseDuration ( params . get ( "duration" ) ) ,
30+ "durationInSeconds" : parseDuration ( params . get ( "duration" ) ) ,
2331 "showTimer" : true ,
2432 "colorScheme" : "zenika-colors" ,
2533 "firstThreshold" : 0.8 ,
@@ -86,7 +94,7 @@ function submitSettings() {
8694}
8795
8896function applySettings ( ) {
89- updateTimer ( settings . durationInSeconds ) ;
97+ timer . textContent = updateTimer ( settings . durationInSeconds ) ;
9098 document . documentElement . className = settings . colorScheme ;
9199
92100 if ( settings . showTimer === false ) {
@@ -120,16 +128,6 @@ function resetDefaultSettings() {
120128}
121129
122130// ------------- Animation ----------------
123-
124- function updateTimer ( secRemaining ) {
125- const sec = Math . max ( 0 , Math . ceil ( secRemaining ) ) ;
126- const minutes = Math . floor ( sec / 60 ) ;
127- const remainingSeconds = sec % 60 ;
128-
129- // format into "mm:ss" padded with 0 if needed
130- timer . textContent = `${ String ( minutes ) . padStart ( 2 , '0' ) } :${ String ( remainingSeconds ) . padStart ( 2 , '0' ) } ` ;
131- }
132-
133131function getClassByProgress ( p ) {
134132 // p = percentage between 0 and 1
135133 if ( p < settings . firstThreshold ) return 'start' ;
@@ -138,7 +136,6 @@ function getClassByProgress(p) {
138136 return 'ending' ;
139137}
140138
141-
142139function startAnimation ( ) {
143140 requestWakeLock ( ) ;
144141 startMessage . style . opacity = 0 ;
@@ -150,8 +147,9 @@ function startAnimation() {
150147 const elapsed = time - startTime ;
151148 const progress = Math . min ( elapsed / ( settings . durationInSeconds * 1000 ) , 1 ) ; // 0 → 1
152149 const currentHeight = Math . floor ( totalHeight * progress ) ;
150+
153151 const remaining = settings . durationInSeconds - ( elapsed / 1000 ) ;
154- updateTimer ( remaining ) ;
152+ timer . textContent = updateTimer ( remaining ) ;
155153
156154 div . style . height = `${ currentHeight } px` ;
157155 div . classList = getClassByProgress ( progress ) ;
@@ -207,11 +205,24 @@ function playBeep() {
207205 oscillator . stop ( audioCtx . currentTime + 0.3 ) ; // play for 0.3s
208206}
209207
210- // Wakelock: réactiver si la page revient au premier plan
211- document . addEventListener ( 'visibilitychange' , ( ) => {
212- if ( wakeLock !== null && document . visibilityState === 'visible' ) {
213- requestWakeLock ( ) ;
214- }
215- } ) ;
216208
217- applySettings ( ) ;
209+ export function init ( ) {
210+ // Wakelock: réactiver si la page revient au premier plan
211+ document . addEventListener ( 'visibilitychange' , ( ) => {
212+ if ( wakeLock !== null && document . visibilityState === 'visible' ) {
213+ requestWakeLock ( ) ;
214+ }
215+ } ) ;
216+
217+ settingsForm . addEventListener ( 'submit' , submitSettings ) ;
218+ closeSettings . addEventListener ( 'click' , closeSettings ) ;
219+ resetBtn . addEventListener ( 'click' , resetDefaultSettings ) ;
220+ submitBtn . addEventListener ( 'click' , submitSettings ) ;
221+ startAnimationBtn . addEventListener ( 'click' , startAnimation ) ;
222+ showSettingsBtn . addEventListener ( 'click' , showSettings ) ;
223+
224+
225+
226+ applySettings ( ) ;
227+ }
228+
0 commit comments