11const fs = require ( 'fs' ) ;
22const path = require ( 'path' ) ;
33const BrowserWindow = electron . remote . BrowserWindow ;
4+
45class Question {
56 private correctAnswer : boolean ;
67 private answer : boolean ;
78 private questionImage : string ;
89 private type : string ;
910
10- constructor ( questionImage : string , type : string , correctAnswer : boolean ) {
11+ constructor (
12+ questionImage : string ,
13+ type : string ,
14+ correctAnswer : boolean = true
15+ ) {
1116 this . questionImage = questionImage ;
1217 this . correctAnswer = correctAnswer ;
1318 this . type = type ;
1419 }
15- public getQuestionImage ( ) {
20+ public getImage ( ) {
1621 return this . questionImage ;
1722 }
23+
24+ public getType ( ) {
25+ return this . type ;
26+ }
1827}
1928
20- let timeHandle : number ;
21- let restartTimeout : number ;
29+ const timeouts : { [ key : string ] : ( ) => number } = {
30+ review : ( ) => 10000 ,
31+ prose : ( ) => 5000 ,
32+ fixation : ( ) => 1000 * Math . floor ( Math . random ( ) * 6 ) + 2
33+ } ;
34+
35+ let timeHandle : any ;
2236let questions : Question [ ] = this . createQuestions ( ) ;
2337function startCarousel ( ) : void {
38+ clearTimeout ( timeHandle ) ;
2439 function nextPicture ( qs : Question [ ] ) {
2540 const picture = qs . shift ( ) ;
2641 if ( picture ) {
2742 replaceImage ( picture ) ;
28- this . timeHandle = setTimeout ( ( ) => nextPicture ( qs ) , 5000 ) ;
29- } else {
30- const el = document . querySelector ( 'img' ) ;
31- el . setAttribute ( 'src' , './assets/questions/fixation.jpg' ) ;
32- const btnDiv = document . getElementById ( 'buttons' ) ;
33- btnDiv . style . visibility = 'hidden' ;
34- this . restartTimeout = setTimeout (
35- restart ,
36- 1000 * Math . floor ( Math . random ( ) * 6 ) + 2
37- ) ;
43+ const type = picture . getType ( ) ;
44+ timeHandle = setTimeout ( ( ) => nextPicture ( qs ) , timeouts [ type ] ( ) ) ;
45+ console . log ( 'replace image' ) ;
46+ console . log ( timeHandle ) ;
3847 }
3948 }
4049 nextPicture ( questions ) ;
4150}
42- function restart ( ) {
43- console . log ( 'restarting' ) ;
44- clearTimeout ( this . restartTimeout ) ;
45- const btnDiv = document . getElementById ( 'buttons' ) ;
46- btnDiv . style . visibility = 'visible' ;
47- this . questions = this . createQuestions ( ) ;
48- startCarousel ( ) ;
49- }
51+
5052function nextQuestion ( ) {
5153 console . log ( 'nextQuestion' ) ;
52- clearTimeout ( this . timeHandle ) ;
54+ clearTimeout ( timeHandle ) ;
55+ console . log ( timeHandle ) ;
5356 startCarousel ( ) ;
5457}
5558
5659function replaceImage ( obj : Question ) {
60+ const div = document . getElementById ( 'buttons' ) ;
5761 const el = document . querySelector ( 'img' ) ;
58- el . setAttribute ( 'src' , `./assets/questions/review/${ obj . getQuestionImage ( ) } ` ) ;
62+ if ( obj . getType ( ) === 'fixation' ) {
63+ div . style . visibility = 'hidden' ;
64+ } else {
65+ div . style . visibility = 'visible' ;
66+ }
67+
68+ el . setAttribute (
69+ 'src' ,
70+ `./assets/questions/${ obj . getType ( ) } /${ obj . getImage ( ) } `
71+ ) ;
5972}
6073
6174function createQuestions ( ) : Question [ ] {
@@ -64,23 +77,54 @@ function createQuestions(): Question[] {
6477 const questionArray : Question [ ] = [ ] ;
6578 fs
6679 . readdirSync ( questionFolder )
67- . sort ( ( ) => 0.5 - Math . random ( ) )
68- . slice ( 0 , 10 )
80+ // .sort(() => 0.5 - Math.random())
81+ // .slice(0, 10)
6982 . forEach ( ( file : any ) => {
70- const q = new Question ( file , type , true ) ;
83+ const q = new Question ( file , type ) ;
7184 questionArray . push ( q ) ;
7285 } ) ;
7386 return questionArray ;
7487 }
7588 const qReview = createQuestionOfType ( 'review' ) ;
7689 const qProse = createQuestionOfType ( 'prose' ) ;
77- return qReview ;
90+ const block1 : Question [ ] = qReview
91+ . splice ( 0 , 3 )
92+ . concat ( qProse . splice ( 0 , 6 ) )
93+ . sort ( ( ) => 0.5 - Math . random ( ) ) ;
94+
95+ const block2 : Question [ ] = qReview
96+ . splice ( 0 , 3 )
97+ . concat ( qProse . splice ( 0 , 6 ) )
98+ . sort ( ( ) => 0.5 - Math . random ( ) ) ;
99+
100+ const block3 : Question [ ] = qReview
101+ . splice ( 0 , 3 )
102+ . concat ( qProse . splice ( 0 , 6 ) )
103+ . sort ( ( ) => 0.5 - Math . random ( ) ) ;
104+
105+ const block4 : Question [ ] = qReview
106+ . splice ( 0 , 3 )
107+ . concat ( qProse . splice ( 0 , 6 ) )
108+ . sort ( ( ) => 0.5 - Math . random ( ) ) ;
109+
110+ const fixation = new Question ( 'fixation.jpg' , 'fixation' ) ;
111+ const out : Question [ ] = block1 . concat (
112+ fixation ,
113+ block2 ,
114+ fixation ,
115+ block3 ,
116+ fixation ,
117+ block4
118+ ) ;
119+ console . log ( out ) ;
120+
121+ return out ;
78122}
79123
80124function createQuestionWindow ( e : any ) : void {
81125 e . preventDefault ( ) ;
82126 const carousel = path . join ( 'file://' , __dirname , 'carousel.html' ) ;
83- let win = new BrowserWindow ( { width : 500 , height : 500 } ) ;
127+ let win = new BrowserWindow ( { width : 800 , height : 800 } ) ;
84128 win . on ( 'close' , ( ) => ( win = null ) ) ;
85129 win . loadURL ( carousel ) ;
86130}
0 commit comments