|
@@ -1,11 +1,49 @@
|
|
1
|
+var items = {
|
|
2
|
+ name: 'form_name',
|
|
3
|
+ addr: 'form_addr',
|
|
4
|
+ subj: 'form_subj',
|
|
5
|
+ text: 'form_text',
|
|
6
|
+};
|
|
7
|
+
|
|
8
|
+var server = getServer();
|
|
9
|
+var xhrSend = new XMLHttpRequest();
|
|
10
|
+xhrSend.onreadystatechange = function() {
|
|
11
|
+ if (xhrSend.readyState == XMLHttpRequest.DONE) {
|
|
12
|
+ console.log(xhrSend);
|
|
13
|
+ }
|
|
14
|
+};
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+// Returns the server's base URI based on the user's script tag
|
|
18
|
+// return: the SMAM server's base URI
|
|
19
|
+function getServer() {
|
|
20
|
+ var scripts = document.getElementsByTagName('script');
|
|
21
|
+ // Parsing all the <script> tags to find the URL to our file
|
|
22
|
+ for(var i = 0; i < scripts.length; i++) {
|
|
23
|
+ let script = scripts[i];
|
|
24
|
+ if(script.src) {
|
|
25
|
+ let url = script.src;
|
|
26
|
+ // This should be our script
|
|
27
|
+ if(url.match(/:[0-9]+\/form\.js$/)) {
|
|
28
|
+ // Port has been found
|
|
29
|
+ return url.match(/^(http:\/\/[^\/]+)/)[1];
|
|
30
|
+ }
|
|
31
|
+ }
|
|
32
|
+ }
|
|
33
|
+}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+// Creates a form
|
|
37
|
+// id: HTML identifier of the document's block to create the form into
|
|
38
|
+// return: nothing
|
1
|
39
|
function generateForm(id) {
|
2
|
40
|
var el = document.getElementById(id);
|
3
|
41
|
|
4
|
42
|
var input = {
|
5
|
|
- name: getField('form_name', 'Your name', false, 'input'), // TODO: configurable prefix
|
6
|
|
- addr: getField('form_addr', 'Your e-mail address', true, 'input'),
|
7
|
|
- subj: getField('form_subj', 'Your message\'s subject', false, 'input'),
|
8
|
|
- text: getField('form_text', 'Your message', false, 'textarea')
|
|
43
|
+ name: getField(items.name, 'Your name', false, 'input'), // TODO: configurable prefix
|
|
44
|
+ addr: getField(items.addr, 'Your e-mail address', true, 'input'),
|
|
45
|
+ subj: getField(items.subj, 'Your message\'s subject', false, 'input'),
|
|
46
|
+ text: getField(items.text, 'Your message', false, 'textarea')
|
9
|
47
|
};
|
10
|
48
|
|
11
|
49
|
// Adding nodes to document
|
|
@@ -100,4 +138,25 @@ function getSubmitButton(id, text) {
|
100
|
138
|
submit.appendChild(button);
|
101
|
139
|
|
102
|
140
|
return submit;
|
|
141
|
+}
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+// Send form data through the XHR object
|
|
145
|
+// return: nothing
|
|
146
|
+function sendForm() {
|
|
147
|
+ xhrSend.open('POST', server + '/send');
|
|
148
|
+ xhrSend.setRequestHeader('Content-Type', 'application/json');
|
|
149
|
+ xhrSend.send(JSON.stringify(getFormData()));
|
|
150
|
+}
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+// Fetch form inputs from HTML elements
|
|
154
|
+// return: an object containing all the user's input
|
|
155
|
+function getFormData() {
|
|
156
|
+ return {
|
|
157
|
+ name: document.getElementById(items.name + '_input').value,
|
|
158
|
+ addr: document.getElementById(items.addr + '_input').value,
|
|
159
|
+ subj: document.getElementById(items.subj + '_input').value,
|
|
160
|
+ text: document.getElementById(items.text + '_textarea').value
|
|
161
|
+ }
|
103
|
162
|
}
|