-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
289 lines (221 loc) · 7.9 KB
/
app.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
let default_language = "en";
let languages = [];
$("#new-language").on('submit', function(event) {
event.preventDefault();
let language = $(event.target).serializeArray()[0].value;
$(event.target).find('input')[0].value = ""
addLanguage(language);
})
$(document).ready(function() {
// Ajout d'une ligne
$("[data-add-row]").on('click', function(event) {
addRow()
})
// Modification de la langue par défaut
$("[data-default-language]").on('input', function(event) {
let language = $(event.target).val();
setDefaultLocal(language)
})
$("#import-data").on('submit', function(event) {
event.preventDefault();
let file = $("#imported-data")[0].files[0];
let reader = new FileReader();
reader.onload = function(event){
let datas = JSON.parse(event.target.result);
initFixture(datas);
};
reader.readAsText(file);
})
// initFixture()
});
function suggeredLanguage(language) {
$("#new-language").find('input')[0].value = language
}
function setDefaultLocal(language){
$(`[data-language="${default_language}"]`).each(function() {
$(this).attr('data-language', language);
if($(this).attr('scope') == 'col') {
$(this).text(`${language} (default)`);
}
})
default_language = language;
}
function resetWebsite(){
languages.forEach((language, index) => {
deleteLanguage(language, true);
if(index == languages.length - 1) {
languages = [];
}
})
$(".table-tr").each(function(index) {
if(index != 0) {
$(this).remove();
} else {
$(this).children().each(function() {
$(this).children().first().val("");
})
}
})
setDefaultLocal("en");
$("[data-default-language]").val("en");
}
// Quand on modifie une clef de la langue par défaut
function onDefaultKeyInput(parent) {
let key = $(parent).val();
const result = $(`input[data-language="${default_language}"]`).filter((index, input) => { return $(input).val() == key && input != parent && key != ""});
if(result.length >= 1){
$(parent).addClass('is-invalid');
$(parent).parent().find('.invalid-feedback').show();
} else {
if($(parent).hasClass('is-invalid')) {
$(parent).removeClass('is-invalid');
$(parent).parent().find('.invalid-feedback').hide();
}
}
}
// Quand on rajoute une langue
function addLanguage(language){
if(languages.indexOf(language) > -1) return;
languages.push(language);
$("#table-header").append(`<th scop="col" data-language="${language}">
<div class="d-flex aic jcc gap-5">
<span data-editable-col="${language}">${language}</span>
<div class="d-flex gap-1 aic">
<span class="material-icons-sharp pointer" onclick="editLanguage('${language}')">edit</span>
<span class="material-icons-sharp pointer text-danger" onclick="deleteLanguage('${language}')">delete</span>
</div>
</div>
</th>`);
$(".table-tr").each(function(index) {
$(this).append(`<td><input type="text" class="form-control" data-row="${index + 1}" data-language="${language}"></td>`);
});
$(`[data-editable-col="${language}"]`).on('keydown', function(event) {
if(event.keyCode == 32) {
event.preventDefault();
}
if(event.keyCode == 13){
event.preventDefault();
let new_language = $(this).text();
$(this).attr('contenteditable', 'false');
$(`[data-language="${language}"]`).each(function() {
$(this).attr('data-language', new_language);
})
$(this).blur();
}
})
}
function addRow(){
let count = $("tbody").children().length;
let html = `<tr class="table-tr"><td class="text-center"><button class="btn btn-danger" onclick="deleteRow(this)">Delete row</button></td>`
html += `<td>
<input type="text" onblur="onDefaultKeyInput(this)" onkeyup="onDefaultKeyInput(this)" data-row="${count}" data-language="${default_language}" class="form-control" aria-describedby="row-${count}-default">
<div id="row-${count}-default" class="invalid-feedback">
This key is already register.
</div>
</td>`;
languages.forEach((language, index) => {
html += `<td>
<input type="text" class="form-control" data-row="${count}" data-language="${language}">
</td>`;
});
html += `</tr>`;
$(html).insertBefore("[data-button-parent]");
}
function deleteRow(e) {
$(e).closest('tr.table-tr').remove();
}
function editLanguage(language) {
$(`[data-editable-col="${language}"]`).attr('contenteditable', 'true');
$(`[data-editable-col="${language}"]`).on('blur', function(event) {
let new_language = $(this).text();
$(this).attr('contenteditable', 'false');
$(`[data-language="${language}"]`).each(function() {
$(this).attr('data-language', new_language);
})
})
}
function deleteLanguage(language, keep = false) {
$(`th[data-language="${language}"]`).remove()
$(`input[data-language="${language}"]`).each(function() {
$(this).parent(0).remove();
})
if(!keep){
languages.splice(languages.indexOf(language), 1);
}
}
function exportData(){
let datas = extractData();
downloadFile("extracted-data.json", JSON.stringify(datas))
}
function download(){
let json = extractData();
Object.entries(json).forEach(([key, value]) => {
const parsedKey = key.trim().replace(' ', '_');
if(!parsedKey) return alert("Some languages have no key");
let content = "";
Object.entries(value).forEach(([language, value]) => {
content += `${language}: ${value} \n`;
})
downloadFile(`messages.${key}.yaml`, content);
})
}
function extractData(){
let json = {};
let cp_languages = [default_language];
cp_languages = cp_languages.concat(languages);
cp_languages.forEach((language) => {
json[language] = {};
$(`input[data-language="${language}"]`).each(function() {
let key = $(`input[data-language="${default_language}"][data-row="${$(this).attr('data-row')}"]`).val();
let value = $(this).val();
json[language][key] = value;
})
})
return json;
}
function downloadFile(fileName, fileContent){
$("body").append(`<a id="download-btn" class="d-none" target="_blank" download="${fileName}" href="data:text/plain;charset=utf-8,${encodeURIComponent(fileContent)}"></a>`);
console.log($(`#download-btn`));
$("#download-btn").get(0).click();
$("#download-btn").remove()
}
function initFixture(fixtures){
resetWebsite()
fixtures = fixtures ?? {
"en": {
"Hello":"Hello",
"World":"World",
"Car":"Car",
"Game":"Game"
},
"fr":{
"Hello":"Bonjour",
"World":"Monde",
"Car":"Voiture",
"Game":"Jeux"
},
"es": {
"Hello":"Hola",
"World":"Mundo",
"Car":"Coche",
"Game":"Juegos"
},
"al": {
"Hello":"Hallo",
"World":"Welt",
"Car":"Auto",
"Game":"Spiel"
}
};
for (let index = 1; index <= Object.entries(fixtures).length - 1; index++) {
addRow();
}
Object.entries(fixtures).forEach(([language, value]) => {
if(language != default_language){
addLanguage(language);
};
for (let index = 1; index <= Object.entries(value).length; index++) {
$(`input[data-language="${language}"][data-row="${index}"]`).val(Object.values(value)[index - 1]);
}
})
}