/
app-1.0.js
148 lines (116 loc) · 4.04 KB
/
app-1.0.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
var APP = (function($) {
"use strict";
var WHITESPACE = 32;
var init = function(options) {
$(window).on("load", function() {
$("#keyboardModal").modal("show");
$("#userInput")
.attr("unselectable", "on")
.css("user-select", "none")
.on("selectstart dragstart", false);
});
var lesson = options.lesson;
var totalWords = lesson.length;
var wordCount = 0;
var inputWordLen = 0;
var index = 0;
var noOfWordsMatched = 0;
var timer = true;
var start = 0;
var stop = 0;
var os = navigator.appVersion.includes('Mac') ? "mac" : "other";
var keyboard = "arabic-keyboard-" + os + '.svg';
$('#keyboardLayout').attr('src', '../' + keyboard);
var $lessonBlock = $("#lessonBlock");
$.each(lesson, function(index, value) {
var id = "word" + index;
$("<label>", { id: id, text: value }).appendTo($lessonBlock);
});
var lessonNumber = options.lessonNumber;
var $buttonBlock = $("#buttonBlock");
var $buttonLeft = $('<div>', {class: 'col'});
if (lessonNumber != 1) {
var $prev = $('<a>', {href: 'lesson' + (lessonNumber - 1) + '.html',
class: 'btn btn-light'})
.append($('<img>', {src: '../prev.svg', title: 'Prev Lesson'}));
$prev.appendTo($buttonLeft);
}
var $reset = $('<button>', {id: 'reset', class: 'btn btn-light'})
.append($('<img>', {src: '../reset.svg', title: 'Reset'}));
$reset.appendTo($buttonLeft);
var $showKeyboard = $('<button>', {id: 'showKeyboard', class: 'btn btn-light'})
.attr('data-toggle', 'modal')
.attr('data-target', '#keyboardModal');
var $keyboardImage = $('<img>', {src: '../keyboard.svg', title: 'Show Keyboard'});
$keyboardImage.appendTo($showKeyboard);
$showKeyboard.appendTo($buttonLeft);
$buttonLeft.appendTo($buttonBlock);
if (!options.isFinalLesson) {
var $next = $('<div>', {class: 'col text-right'})
.append($('<a>', {class: 'btn btn-light', href: 'lesson' + (lessonNumber + 1) + '.html'})
.append($('<img>', {src: '../next.svg', title: 'Next Lesson'})));
$next.appendTo($buttonBlock);
}
$("#wordsMatched").html("Score: " + noOfWordsMatched + "/" + totalWords);
$("#timer").html("Speed: 0 w/s");
$("#userInput").keydown(function(e) {
if (timer) {
start = new Date().getTime();
timer = false;
}
if (
wordCount >= totalWords ||
e.which < 32 ||
(e.which > 32 && e.which < 48)
) {
e.preventDefault();
return false;
} else if (e.which === WHITESPACE) {
var wordId = "#word" + wordCount;
var color = "red";
var value = $("#userInput")
.val()
.substr(index, index + inputWordLen);
if (value === lesson[wordCount]) {
color = "green";
noOfWordsMatched++;
}
$(wordId).css("border-bottom", "6px solid " + color);
$(wordId).css("color", "#ccc");
$("#wordsMatched").html(
"Score: " + noOfWordsMatched + "/" + totalWords
);
index += inputWordLen + 1;
inputWordLen = 0;
wordCount++;
if (wordCount >= totalWords) {
stop = new Date().getTime();
var diff = stop - start;
var wordPerSecond = Math.round((noOfWordsMatched / diff) * 6000, 2);
$("#timer").html("Speed: " + wordPerSecond + " w/s");
}
} else {
inputWordLen++;
}
});
$("#reset").click(function() {
index = 0;
wordCount = 0;
inputWordLen = 0;
noOfWordsMatched = 0;
timer = true;
$("#wordsMatched").html("Score: " + noOfWordsMatched + "/" + totalWords);
$("#timer").html("Speed: 0 w/s");
$("#userInput").val("");
$("#lessonBlock label").css("border-bottom", "6px solid #ccc");
$("#lessonBlock label").css("color", "black");
$("#userInput").focus();
});
$("#keyboardModal").on("hide.bs.modal", function(e) {
$("#userInput").focus();
});
};
return {
init: init
};
})(jQuery);