-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventsModule.js
189 lines (138 loc) · 6.81 KB
/
eventsModule.js
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
var eventsModule = (function(dModule, uModule, cModule, wModule){
var addEventListeners = function(){
// the button "Enter" click event
uModule.getDOMElements().textInput.addEventListener('keydown', function(event){
// if the test ended, do nothing
if (dModule.testEnded()){
return;
}
// check if the user pressed "Enter"
var key = event.keyCode;
if(key == 13){// 13 is the the button "enter" keyCode
uModule.getDOMElements().textInput.value += dModule.getLineReturn()+' ';
// create a new 'input' event
var inputEvent = new Event('input');
// dispatch it
uModule.getDOMElements().textInput.dispatchEvent(inputEvent);
}
});
// character typing event listener
uModule.getDOMElements().textInput.addEventListener('input', function(event){
// if the test ended, do nothing
if (dModule.testEnded()){
return;
}
// if the test has not started yet, start the test and countdown
if (!dModule.testStarted()){
// start the test: data Module
dModule.startTest();
// start a counter
var b = setInterval(function(){
// calculate the results: data Module
var results = {};
// update wpm, wpmChange
[results.wpm, results.wpmChange] = dModule.calculateWpm();
// update cpm, cpmChange
[results.cpm, results.cpmChange] = dModule.calculateCpm();
//update accuracy, accuracyChange
[results.accuracy, results.accuracyChange] = dModule.calculateAccuracy();
// update results (UI module)
uModule.updateResults(results);
// update time left
// check if we have time left
if (dModule.timeLeft()){
//yes:
// reduce time by one sec in data module by 1 second
var timeLeft = dModule.reduceTime();
// update time remaining in UI module
uModule.updateTimeLeft(timeLeft);
}else {
//no:
// end the test: data module
clearInterval(b); // stop the set interval function which update the results every second
dModule.endsTest();
// fill modal
uModule.fillModal(results.wpm);
// show modal
uModule.showModal();
}
}, 1000);
}
// get typed word: UI module
var typedWord = uModule.getTypeWord();
// update current word: data module
dModule.updateCurrentWord(typedWord);
// format the active word
// format the active word: UIModule
// get the current word value
var currentWord = dModule.getCurrentWord();
uModule.formatWord(currentWord);
// check if the user pressed space or enter
if(uModule.spacePressed(event) || uModule.enterPressed(dModule.getLineReturn())){
// empty the text input
uModule.emptyInput();
// deactivate (un-highlight) the current word
uModule.deactivateCurrentWord();
// move to a new word: dataModule
dModule.moveToNewWord();
// set active word: UIModule
// get the correct current word index
var index = dModule.getCurrentWordIndex();
uModule.setActiveWord(index);
// format the active word: UIModule
// get the current word value
currentWord = dModule.getCurrentWord();
uModule.formatWord(currentWord);
// scroll word into the middle view
uModule.scroll();
}
});
// click on download button event listener
uModule.getDOMElements().download.addEventListener('click', function(event){
if(uModule.isNameEmpty()){
uModule.flagNameInput();
}else{
var certificateData = dModule.getCertificateData();
cModule.generateCertificate(certificateData);
}
});
};
// scroll active word into middle view on window resize
window.addEventListener('resize', uModule.scroll);
return {
// init function, initializes the test before start
init: function(duration, textNumber){
// fill the list of test words: dataModule
// get the words from the wordsModule using the getWords method and the textNumber to select the list of words out of 3
var words = wModule.getWords(textNumber);
dModule.fillListOfTestWords(textNumber, words);
// fill the list of test words: UIModule
// get the array with the test words
var testWords = dModule.getListofTestWords();
// get the line return variable
var lineReturn = dModule.getLineReturn();
uModule.fillContent(testWords, lineReturn);
// set the total test time: data Module
dModule.setTestTime(duration);
// update time left: dataModule
dModule.initializeTimeLeft();
// update time left: UIModule
var timeLeft = dModule.getTimeLeft();
uModule.updateTimeLeft(timeLeft);
// move to a new word: dataModule
dModule.moveToNewWord();
// set active word: UIModule
// get the correct current word index
var index = dModule.getCurrentWordIndex();
uModule.setActiveWord(index);
// format the active word: UIModule
// get the current word value
currentWord = dModule.getCurrentWord();
uModule.formatWord(currentWord);
// focus on text input: UIModule
uModule.inputFocus();
// add event listeners
addEventListeners();
}
};
})(dataModule, UIModule, certificateModule, wordsModule);