@@ -2,6 +2,15 @@ import Util from '@services/util.js';
22import PanelList from '@components/panel-list/panel-list.js' ;
33import { VIEW_STATES } from '@scripts/h5p-discrete-option-multi-choice' ;
44
5+ /** @constant {number} DISTRACTION_CHANCE Chance of distraction. */
6+ const DISTRACTION_CHANCE = 0.5 ;
7+
8+ /** @constant {number} JUMPING_PREVENTION_TIMEOUT_MS Timeout to prevent jumping. */
9+ const JUMPING_PREVENTION_TIMEOUT_MS = 10 ;
10+
11+ /** @constant {number} OFFSET_FACTOR Factor for offset calculations. */
12+ const OFFSET_FACTOR = 2 ;
13+
514/**
615 * Main DOM component incl. main controller.
716 */
@@ -19,7 +28,7 @@ export default class Main {
1928
2029 this . callbacks = Util . extend ( {
2130 onAnswered : ( ) => { } ,
22- onGameOver : ( ) => { }
31+ onGameOver : ( ) => { } ,
2332 } , callbacks ) ;
2433
2534 this . dom = document . createElement ( 'div' ) ;
@@ -127,7 +136,7 @@ export default class Main {
127136 }
128137
129138 // Randomly add one more option on correct answer to distract
130- if ( scoreDelta === 1 && Math . random ( ) < 0.5 ) {
139+ if ( scoreDelta === 1 && Math . random ( ) < DISTRACTION_CHANCE ) {
131140 this . callbacks . onGameOver ( { quiet : params . quiet } ) ; // Done
132141 return ;
133142 }
@@ -145,7 +154,7 @@ export default class Main {
145154 if ( ! params . quiet ) {
146155 const message = Util . stripHTML (
147156 this . params . dictionary . get ( 'a11y.panelAdded' )
148- . replace ( / @ o p t i o n / , this . answerOptions [ this . currentPanelIndex ] . text )
157+ . replace ( / @ o p t i o n / , this . answerOptions [ this . currentPanelIndex ] . text ) ,
149158 ) ;
150159
151160 this . params . globals . get ( 'read' ) ( message ) ;
@@ -155,7 +164,7 @@ export default class Main {
155164 if ( params . focus ) {
156165 window . setTimeout ( ( ) => {
157166 this . panelList . focus ( this . currentPanelIndex , true ) ;
158- } , 10 ) ; // Prevent jumping before iframe resize and let screenreader read
167+ } , JUMPING_PREVENTION_TIMEOUT_MS ) ; // Prevent jumping before iframe resize and let screenreader read
159168 }
160169 }
161170
@@ -168,7 +177,7 @@ export default class Main {
168177
169178 this . scorePoints = this . scorePoints || new H5P . Question . ScorePoints ( ) ;
170179 this . panelList . showResults (
171- params . showScores ? this . scorePoints : null
180+ params . showScores ? this . scorePoints : null ,
172181 ) ;
173182 }
174183
@@ -177,7 +186,7 @@ export default class Main {
177186 */
178187 showFeedback ( ) {
179188 this . panelList . showFeedback (
180- { selected : this . answerOptions . map ( ( option ) => option . userAnswer ) }
189+ { selected : this . answerOptions . map ( ( option ) => option . userAnswer ) } ,
181190 ) ;
182191 }
183192
@@ -206,10 +215,10 @@ export default class Main {
206215 appendResultsMessage ( ) {
207216 this . resultsMessage = document . createElement ( 'p' ) ;
208217 this . resultsMessage . classList . add (
209- 'h5p-discrete-option-multi-choice-message'
218+ 'h5p-discrete-option-multi-choice-message' ,
210219 ) ;
211220 this . resultsMessage . innerText = this . params . dictionary . get (
212- 'l10n.yourResults'
221+ 'l10n.yourResults' ,
213222 ) ;
214223
215224 this . dom . append ( this . resultsMessage ) ;
@@ -262,7 +271,7 @@ export default class Main {
262271 */
263272 reset ( params = { } ) {
264273 params = Util . extend ( {
265- previousState : { }
274+ previousState : { } ,
266275 } , params ) ;
267276
268277 this . dom . innerHTML = '' ;
@@ -296,7 +305,7 @@ export default class Main {
296305 for ( let i = 0 ; i < values . length ; i ++ ) {
297306 selector . options . push ( {
298307 value : values [ i ] ,
299- label : labels ?. [ i ] ?? values [ i ]
308+ label : labels ?. [ i ] ?? values [ i ] ,
300309 } ) ;
301310 }
302311 }
@@ -321,21 +330,21 @@ export default class Main {
321330 {
322331 dictionary : this . params . dictionary ,
323332 globals : this . params . globals ,
324- options : this . answerOptions
333+ options : this . answerOptions ,
325334 } ,
326335 {
327336 onAnswered : ( index , userAnswer ) => {
328337 this . handleAnswered ( {
329338 index : index ,
330339 userAnswer : userAnswer ,
331340 quiet : false ,
332- focus : true
341+ focus : true ,
333342 } ) ;
334343 } ,
335344 onConfidenceChanged : ( index , confidenceIndex ) => {
336345 this . handleConfidenceChanged ( index , confidenceIndex ) ;
337- }
338- }
346+ } ,
347+ } ,
339348 ) ;
340349
341350 this . panelList . reset ( { previousState : params . previousState . answers } ) ;
@@ -367,10 +376,10 @@ export default class Main {
367376 answers : this . answerOptions . map ( ( option ) => {
368377 return ( {
369378 userAnswer : option . userAnswer ,
370- confidenceIndex : option . confidenceIndex
379+ confidenceIndex : option . confidenceIndex ,
371380 } ) ;
372381 } ) ,
373- isOvertime : this . isOvertime
382+ isOvertime : this . isOvertime ,
374383 } ;
375384 }
376385
@@ -404,11 +413,11 @@ export default class Main {
404413 return this . getScoredAnswerOptions ( )
405414 . map ( ( option , index ) => {
406415 if ( ( option . userAnswer === true ) ) {
407- return 2 * index ;
416+ return OFFSET_FACTOR * index ;
408417 }
409418
410419 if ( ( option . userAnswer === false ) ) {
411- return 2 * index + 1 ;
420+ return OFFSET_FACTOR * index + 1 ;
412421 }
413422
414423 return null ;
@@ -426,17 +435,17 @@ export default class Main {
426435 . reduce ( ( choices , choice , index ) => {
427436 choices . push (
428437 {
429- id : ( index * 2 ) . toString ( ) ,
438+ id : ( index * OFFSET_FACTOR ) . toString ( ) ,
430439 description : {
431- 'en-US' : `${ Util . stripHTML ( choice . text ) } (@correct)`
432- }
440+ 'en-US' : `${ Util . stripHTML ( choice . text ) } (@correct)` ,
441+ } ,
433442 } ,
434443 {
435- id : ( index * 2 + 1 ) . toString ( ) ,
444+ id : ( index * OFFSET_FACTOR + 1 ) . toString ( ) ,
436445 description : {
437- 'en-US' : `${ Util . stripHTML ( choice . text ) } (@incorrect)`
438- }
439- }
446+ 'en-US' : `${ Util . stripHTML ( choice . text ) } (@incorrect)` ,
447+ } ,
448+ } ,
440449 ) ;
441450
442451 return choices ;
@@ -452,15 +461,15 @@ export default class Main {
452461 this . getScoredAnswerOptions ( )
453462 . reduce ( ( correct , option , index ) => {
454463 if ( option . correct ) {
455- correct . push ( ( 2 * index ) . toString ( ) ) ;
464+ correct . push ( ( OFFSET_FACTOR * index ) . toString ( ) ) ;
456465 }
457466 else {
458- correct . push ( ( 2 * index + 1 ) . toString ( ) ) ;
467+ correct . push ( ( OFFSET_FACTOR * index + 1 ) . toString ( ) ) ;
459468 }
460469
461470 return correct ;
462471 } , [ ] )
463- . join ( '[,]' )
472+ . join ( '[,]' ) ,
464473 ] ;
465474 }
466475}
0 commit comments