-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
78 lines (67 loc) · 1.81 KB
/
main.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
var value = "";
var lang = "es";
$(document).ready(function() {
generateButton = $("#generateButton");
usernameBox = $("#usernameBox");
$("input").keyup(function() { value = $(this).val(); })
generateButton.click(generateCardFile);
$(".flag-icon").click(selectFlag);
});
function generateCardFile() {
showSpinner();
//var lang = $("#langselection option:selected").attr("value");
getKnownWords(value, lang, function(words) {
getTranslations(words, lang, "en", function(data) {
fileURL = makeFile(generateCsvRows(data));
hideSpinner();
}, handleError);
}, handleError);
}
function handleError(message) {
hideSpinner(true);
if (message) {
swal({
title: 'Error!',
text: message,
type: 'error',
confirmButtonText: 'Okay',
});
}
else {
alert('failed');
}
}
function showSpinner() {
$("#genButtonText").text('');
$("#dotContainer").removeAttr('style');
$("#downloadInfo").attr('style', 'display: none');
}
function hideSpinner(didFail) {
$("#genButtonText").text('Generate');
$("#dotContainer").attr('style', 'display: none');
if (!didFail) {
$("#downloadLink").attr('href', fileURL);
$("#downloadInfo").removeAttr('style');
}
}
function selectFlag() {
var re = new RegExp('flag-icon flag-icon-(\\w\\w) flag-icon.*');
$(".flag-icon").removeClass("selected");
$(".flag-icon").addClass("unselected");
$(this).removeClass("unselected");
$(this).addClass("selected");
lang = re.exec($(this).attr('class'))[1];
}
// wordObj is {"word": ["transl0", "transl1"]}
function generateCsvRows(wordObj){
rows = "";
for (var from in wordObj) {
row = from + ",";
for (var i = 0; i < wordObj[from].length; i++) {
row += " {to}<br>".formatUnicorn({"to": wordObj[from][i]});
}
row += "\n";
rows += row;
}
return rows;
}