|
@@ -6,16 +6,44 @@ var items = {
|
6
|
6
|
};
|
7
|
7
|
|
8
|
8
|
var server = getServer();
|
9
|
|
-var xhrSend = new XMLHttpRequest();
|
10
|
|
-
|
11
|
9
|
var token = "";
|
12
|
|
-var xhrToken = new XMLHttpRequest();
|
13
|
|
-xhrToken.onreadystatechange = function() {
|
14
|
|
- if(xhrToken.readyState == XMLHttpRequest.DONE) {
|
15
|
|
- token = xhrToken.responseText;
|
16
|
|
- }
|
|
10
|
+var lang = {};
|
|
11
|
+
|
|
12
|
+var xhr = {
|
|
13
|
+ lang: new XMLHttpRequest(),
|
|
14
|
+ token: new XMLHttpRequest(),
|
|
15
|
+ send: new XMLHttpRequest()
|
17
|
16
|
}
|
18
|
17
|
|
|
18
|
+// XHR callbacks
|
|
19
|
+
|
|
20
|
+xhr.token.onreadystatechange = function() {
|
|
21
|
+ if(xhr.token.readyState == XMLHttpRequest.DONE) {
|
|
22
|
+ token = xhr.token.responseText;
|
|
23
|
+ }
|
|
24
|
+};
|
|
25
|
+
|
|
26
|
+xhr.lang.onreadystatechange = function() {
|
|
27
|
+ if(xhr.lang.readyState == XMLHttpRequest.DONE) {
|
|
28
|
+ lang = JSON.parse(xhr.lang.responseText);
|
|
29
|
+ }
|
|
30
|
+};
|
|
31
|
+
|
|
32
|
+xhr.send.onreadystatechange = function() {
|
|
33
|
+ if(xhr.send.readyState == XMLHttpRequest.DONE) {
|
|
34
|
+ let status = document.getElementById('form_status');
|
|
35
|
+ status.setAttribute('class', '');
|
|
36
|
+ if(xhr.send.status === 200) {
|
|
37
|
+ cleanForm();
|
|
38
|
+ status.setAttribute('class', 'success');
|
|
39
|
+ status.innerHTML = lang.send_status_success;
|
|
40
|
+ } else {
|
|
41
|
+ status.setAttribute('class', 'failure');
|
|
42
|
+ status.innerHTML = lang.send_status_failure;
|
|
43
|
+ }
|
|
44
|
+ }
|
|
45
|
+};
|
|
46
|
+
|
19
|
47
|
|
20
|
48
|
// Returns the server's base URI based on the user's script tag
|
21
|
49
|
// return: the SMAM server's base URI
|
|
@@ -40,6 +68,9 @@ function getServer() {
|
40
|
68
|
// id: HTML identifier of the document's block to create the form into
|
41
|
69
|
// return: nothing
|
42
|
70
|
function generateForm(id) {
|
|
71
|
+ // Get translated strings
|
|
72
|
+ getLangSync();
|
|
73
|
+
|
43
|
74
|
var el = document.getElementById(id);
|
44
|
75
|
|
45
|
76
|
// Set the form's behaviour
|
|
@@ -51,10 +82,10 @@ function generateForm(id) {
|
51
|
82
|
el.appendChild(status);
|
52
|
83
|
|
53
|
84
|
var input = {
|
54
|
|
- name: getField(items.name, 'Your name', false, 'input'), // TODO: configurable prefix
|
55
|
|
- addr: getField(items.addr, 'Your e-mail address', true, 'input'),
|
56
|
|
- subj: getField(items.subj, 'Your message\'s subject', false, 'input'),
|
57
|
|
- text: getField(items.text, 'Your message', false, 'textarea')
|
|
85
|
+ name: getField(items.name, lang.form_name_label, false, 'input'),
|
|
86
|
+ addr: getField(items.addr, lang.form_addr_label, true, 'input'),
|
|
87
|
+ subj: getField(items.subj, lang.form_subj_label, false, 'input'),
|
|
88
|
+ text: getField(items.text, lang.form_mesg_label, false, 'textarea')
|
58
|
89
|
};
|
59
|
90
|
|
60
|
91
|
// Adding nodes to document
|
|
@@ -66,24 +97,7 @@ function generateForm(id) {
|
66
|
97
|
|
67
|
98
|
// Adding submit button
|
68
|
99
|
|
69
|
|
- el.appendChild(getSubmitButton('form_subm', 'Send the mail'));
|
70
|
|
-
|
71
|
|
- // Setting the XHR callback
|
72
|
|
-
|
73
|
|
- xhrSend.onreadystatechange = function() {
|
74
|
|
- if(xhrSend.readyState == XMLHttpRequest.DONE) {
|
75
|
|
- let status = document.getElementById('form_status');
|
76
|
|
- status.setAttribute('class', '');
|
77
|
|
- if(xhrSend.status === 200) {
|
78
|
|
- cleanForm();
|
79
|
|
- status.setAttribute('class', 'success');
|
80
|
|
- status.innerHTML = 'Your message has been sent.';
|
81
|
|
- } else {
|
82
|
|
- status.setAttribute('class', 'failure');
|
83
|
|
- status.innerHTML = 'An error happened while sending your message, please retry later.';
|
84
|
|
- }
|
85
|
|
- }
|
86
|
|
- };
|
|
100
|
+ el.appendChild(getSubmitButton('form_subm', lang.form_subm_label));
|
87
|
101
|
|
88
|
102
|
// Retrieve the token from the server
|
89
|
103
|
|
|
@@ -177,11 +191,11 @@ function sendForm() {
|
177
|
191
|
// Clear status
|
178
|
192
|
let status = document.getElementById('form_status');
|
179
|
193
|
status.setAttribute('class', 'sending');
|
180
|
|
- status.innerHTML = 'Sending the e-mail';
|
|
194
|
+ status.innerHTML = lang.send_status_progress;
|
181
|
195
|
|
182
|
|
- xhrSend.open('POST', server + '/send');
|
183
|
|
- xhrSend.setRequestHeader('Content-Type', 'application/json');
|
184
|
|
- xhrSend.send(JSON.stringify(getFormData()));
|
|
196
|
+ xhr.send.open('POST', server + '/send');
|
|
197
|
+ xhr.send.setRequestHeader('Content-Type', 'application/json');
|
|
198
|
+ xhr.send.send(JSON.stringify(getFormData()));
|
185
|
199
|
|
186
|
200
|
// Get a new token
|
187
|
201
|
getToken();
|
|
@@ -211,9 +225,17 @@ function cleanForm() {
|
211
|
225
|
}
|
212
|
226
|
|
213
|
227
|
|
214
|
|
-// Ask the server for a token
|
|
228
|
+// Asks the server for a token
|
215
|
229
|
// return: nothing
|
216
|
230
|
function getToken() {
|
217
|
|
- xhrToken.open('GET', server + '/register');
|
218
|
|
- xhrToken.send();
|
|
231
|
+ xhr.token.open('GET', server + '/register');
|
|
232
|
+ xhr.token.send();
|
|
233
|
+}
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+// Asks the server for translated strings to display
|
|
237
|
+// return: notghing
|
|
238
|
+function getLangSync() {
|
|
239
|
+ xhr.lang.open('GET', server + '/lang', false);
|
|
240
|
+ xhr.lang.send();
|
219
|
241
|
}
|