-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflashcards.js
234 lines (193 loc) · 7.81 KB
/
flashcards.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
var myDict = {};
// states:
// 0: starting a new game
// 1: showing a card
// 2: giving feedback
// 3: showing result, game is pending
/*global alert: false, confirm: false, console: false, Debug: false, opera: false, prompt: false, WSH: false, "_":true */
(function() {
"use strict";
var localStorageKeys = {
statistics : {
known: "statistics_known",
notSure: "statistics_notSure",
unknown: "statistics_unknown"
},
showingShortcutTable : "show_shortcut_table",
currentState : "current_state",
currentWordPrimaryLanguage : "current_word_hun",
currentWordSecondaryLanguage : "current_word_en"
};
var state = {
initial : "initial_state",
showingWord : "show_word_state",
showingMeaning : "show_meaning_state"
};
var Storage = {
get statisticsKnown() {
return localStorage.getItem(localStorageKeys.statistics.known);
},
set statisticsKnown(value) {
localStorage.setItem(localStorageKeys.statistics.known, value.toString());
},
get statisticsNotSure() {
return localStorage.getItem(localStorageKeys.statistics.notSure);
},
set statisticsNotSure(value) {
localStorage.setItem(localStorageKeys.statistics.notSure, value.toString());
},
get statisticsUnknown() {
return localStorage.getItem(localStorageKeys.statistics.unknown);
},
set statisticsUnknown(value) {
return localStorage.setItem(localStorageKeys.statistics.unknown, value.toString());
},
get currentState() {
return localStorage.getItem(localStorageKeys.currentState);
},
set currentState(value) {
return localStorage.setItem(localStorageKeys.currentState, value.toString());
},
increaseCounter: function(counterName) {
var newScore = 1;
if (localStorage.getItem(counterName)) {
newScore = parseInt(localStorage.getItem(counterName), 10) + 1;
}
localStorage.setItem(counterName, newScore.toString());
},
get wordFromPrimaryLanguage() {
return localStorage.getItem(localStorageKeys.currentWordPrimaryLanguage);
},
set wordFromPrimaryLanguage(value) {
return localStorage.setItem(localStorageKeys.currentWordPrimaryLanguage, value);
},
get wordFromSecondaryLanguage() {
return localStorage.getItem(localStorageKeys.currentWordSecondaryLanguage);
},
set wordFromSecondaryLanguage(value) {
return localStorage.setItem(localStorageKeys.currentWordSecondaryLanguage, value);
}
};
function startNewGame() {
Storage.statisticsKnown = 0;
Storage.statisticsNotSure = 0;
Storage.statisticsUnknown = 0;
showWord();
}
function showWord() {
Storage.currentState = state.showingWord;
pickItemFromDictionary();
$("#myFrame").html("<h1>" + Storage.wordFromPrimaryLanguage +"</h1>");
$("#startGame, #feedback").hide();
$("#show, #myFrame").show();
}
function showMeaning() {
Storage.currentState = state.showingMeaning;
$("#myFrame").html("<h1>" + Storage.wordFromSecondaryLanguage +"</h1>");
$("#feedback, #myFrame").show();
$("#show").hide();
}
function showResult() {
$("#statsKnown").text(Storage.statisticsKnown);
$("#statsNotSure").text(Storage.statisticsNotSure);
$("#statsUnknown").text(Storage.statisticsUnknown);
$("#myResult, #restartOrContinue").show();
}
function pickItemFromDictionary() {
var id = Math.floor((Math.random() * myDict.length));
Storage.wordFromPrimaryLanguage = myDict[id].hun;
Storage.wordFromSecondaryLanguage = myDict[id].en;
}
var handlers = {};
handlers[state.initial] = function(){};
handlers[state.showingWord] = showWord;
handlers[state.showingMeaning] = showMeaning;
function loadBundles(lang) {
$.i18n.properties({
name: 'Messages',
path: 'bundle/',
mode: 'both',
language: lang,
callback: function() {
$("#header-text").text(header_text);
$("#game_tab").text(game_tab_title);
$("#results_tab").text(results_tab_title);
$("#cheatsheet_tab").text(cheatsheet_tab_title);
$("#iKnow").text(i_know_button);
$("#almostKnow").text(not_sure_button);
$("#dontKnow").text(did_not_know_button);
$("#show").text(show_meaning_button);
$("#restart").text(restart_button);
$("#newGameShortcut").text(new_game_shortcut_text);
$("#showMeaningShortcut").text(new_game_shortcut_text);
$("#iKnewShortcut").text(i_knew_shortcut_text);
$("#almostKnewShortcut").text(almost_knew_shortcut_text);
$("#didNotKnowShortcut").text(did_not_know_shortcut_text);
$("#knownInTable").text(i_know_result_table);
$("#notSureInTable").text(not_sure_result_table);
$("#unknownInTable").text(did_not_know_result_table);
$("#languageMenu").text(language_menu);
}
});}
$(document).ready(function(){
var lng = $.i18n.browserLang();//"hu";
loadBundles(lng);
// configure language combo box
$('#lang').change(function() {
var selection = $('#lang option:selected').val();
loadBundles(selection !== 'browser' ? selection : $.i18n.browserLang());
});
if (Storage.currentState) {
$.get("data/hun-en.json", function(data, status) {
myDict = data;
console.log("status is " + Storage.currentState);
handlers[Storage.currentState]();
});
} else {
console.log("status was nil.");
Storage.currentState = state.initial;
}
$("#startGame").click(function(){
$.get("data/hun-en.json", function(data, status) {
myDict = data;
startNewGame();
});
});
$("#show").click(showMeaning);
$("")
var showWordAndUpdateStats = _.compose(showWord, Storage.increaseCounter);
$("#iKnow").click(function(){
showWordAndUpdateStats(localStorageKeys.statistics.known);
});
$("#almostKnow").click(function(){
showWordAndUpdateStats(localStorageKeys.statistics.notSure);
});
$("#dontKnow").click(function(){
showWordAndUpdateStats(localStorageKeys.statistics.unknown);
});
$("#restart").click(startNewGame);
$("#backToGame").click(showWord);
$('a[href="#results"]').click(showResult);
jQuery(document).bind('keydown', 'w', function() {
if (Storage.currentState === state.showingWord) {
showMeaning();
}
});
jQuery(document).bind('keydown', 'a', function() {
if (Storage.currentState === state.showingMeaning) {
showWordAndUpdateStats(localStorageKeys.statistics.known);
}
});
jQuery(document).bind('keydown', 's', function() {
if (Storage.currentState === state.showingMeaning) {
showWordAndUpdateStats(localStorageKeys.statistics.notSure);
}
});
jQuery(document).bind('keydown', 'd', function() {
if (Storage.currentState === state.showingMeaning) {
showWordAndUpdateStats(localStorageKeys.statistics.unknown);
}
});
jQuery(document).bind('keydown', 'n', startNewGame);
});
}());