-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventsModule.js
More file actions
185 lines (130 loc) · 6.51 KB
/
eventsModule.js
File metadata and controls
185 lines (130 loc) · 6.51 KB
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
var eventsModule = (function(dModule, uModule, cModule, wModule){
var addEventListeners = function(){
//enter click event
uModule.getDOMElements().textInput.addEventListener('keydown', function(event){
console.log(event);
//if the test ended, do nothing
if(dModule.testEnded()){
return;
}
//check if the user pressed Enter
var key = event.keyCode;
if(key == 13){
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 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();
//dModule.returnData();
//update results (UI module)
uModule.updateResults(results);
//check if we have time left
if(dModule.timeLeft()){
//reduce time by one sec: data Module
var timeLeft = dModule.reduceTime();
//update time remaining in UI
uModule.updateTimeLeft(timeLeft);
}else{
//end the test: data module
clearInterval(b);
dModule.endTest();
dModule.returnData();
//fill modal
uModule.fillModal(results.wpm);
//show modal
uModule.showModal();
}
}, 1000);
}
//get typed word: UI module
var typedWord = uModule.getTypedWord();
//update current word: data module
dModule.updateCurrentWord(typedWord);
//format the active word
var currentWord = dModule.getCurrentWord();
uModule.formatWord(currentWord);
//check if the user pressed space or enter
if(uModule.spacePressed(event) || uModule.enterPressed(dModule.getLineReturn())){
//empty text input
uModule.emptyInput();
//deactivate current word
uModule.deactivateCurrentWord();
//move to a new word: data Module
dModule.moveToNewWord();
//set active Word: UI Module
var index = dModule.getCurrentWordIndex();
uModule.setActiveWord(index);
//format the active word: UI Module
var 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();
console.log(certificateData);
certificateModule.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: data Module
var words = wModule.getWords(textNumber);
dModule.fillListOfTestWords(textNumber, words);
//fill the list of test words: UI Module
var lineReturn = dModule.getLineReturn();
var testWords = dModule.getListofTestWords();
uModule.fillContent(testWords, lineReturn);
//set the total test time: data Module
dModule.setTestTime(duration);
//update time left: data Module
dModule.initializeTimeLeft();
//update time left: UI module
var timeLeft = dModule.getTimeLeft();
uModule.updateTimeLeft(timeLeft);
//move to a new word: data Module
dModule.moveToNewWord();
//set active Word: UI Module
var index = dModule.getCurrentWordIndex();
uModule.setActiveWord(index);
//format the active word: UI Module
var currentWord = dModule.getCurrentWord();
uModule.formatWord(currentWord);
//focus on text input: UI Module
uModule.inputFocus();
//add avent listeners
addEventListeners();
}
};
})(dataModule, UIModule, certificateModule, wordsModule);