ソースを参照

Adding the labels setting

Brendan Abolivier 8 年 前
コミット
1acd9578a1
署名者: Brendan Abolivier <contact@brendanabolivier.com> GPGキーID: 8EF1500759F70623
共有4 個のファイルを変更した30 個の追加9 個の削除を含む
  1. 5
    2
      README.md
  2. 9
    4
      front/form.js
  3. 14
    2
      server.js
  4. 2
    1
      settings.example.json

+ 5
- 2
README.md ファイルの表示

@@ -79,7 +79,8 @@ First, you must rename the `settings.example.conf` into `settings.conf`, and edi
79 79
         "someone.else@example.com"
80 80
     ],
81 81
     "formOrigin": "https://example.tld",
82
-    "language": "en"
82
+    "language": "en",
83
+    "labels": true
83 84
 }
84 85
 ```
85 86
 
@@ -89,7 +90,9 @@ The `recipients` section is an array containing the e-mail addresses any message
89 90
 
90 91
 The `formOrigin` part is a string containing the origin of the page you'll include the contact form into. This allows SMAM to work with the [Cross-Origin Resource Sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) security most browser use. For more info on how to fill this field, and what is an origin, please give a look at [the MDN's definition](https://developer.mozilla.org/en-US/docs/Glossary/origin).
91 92
 
92
-Finally, the `language` string tells SMAM in which language you want your form served. Possible values are all the files in `locales/`'s names (without the `.json`). To create your own translation, please read the section below.
93
+The `language` string tells SMAM in which language you want your form served. Possible values are all the files in `locales/`'s names (without the `.json`). To create your own translation, please read the section below.
94
+
95
+Finally, the `labels` setting is a boolean to set whether or not labels will be displayed in the `<form></form>` block. If set to `false`, the form will still display the front-end strings ("Your name", "Your e-mail address"...), but only as placeholders in the text fields. If set to true, the said strings will appear as text fields' placeholders but also as labels outside of the fields. If not set, defaults to `true`.
93 96
 
94 97
 ## Templating
95 98
 

+ 9
- 4
front/form.js ファイルの表示

@@ -6,8 +6,9 @@ var items = {
6 6
 };
7 7
 
8 8
 var server  = getServer();
9
-var token = "";
10
-var lang = {};
9
+var token   = "";
10
+var labels  = true;
11
+var lang    = {};
11 12
 
12 13
 var xhr = {
13 14
     lang: new XMLHttpRequest(),
@@ -25,7 +26,9 @@ xhr.token.onreadystatechange = function() {
25 26
 
26 27
 xhr.lang.onreadystatechange = function() {
27 28
     if(xhr.lang.readyState == XMLHttpRequest.DONE) {
28
-        lang = JSON.parse(xhr.lang.responseText);
29
+        let response = JSON.parse(xhr.lang.responseText);
30
+        lang = response.translations;
31
+        labels = response.labels;
29 32
     }
30 33
 };
31 34
 
@@ -115,7 +118,9 @@ function getField(id, placeholder, email, type) {
115 118
     var field = document.createElement('div');
116 119
     
117 120
     field.setAttribute('id', id); // TODO: configurable prefix
118
-    field.appendChild(getLabel(id, placeholder, type));
121
+    if(labels) {
122
+        field.appendChild(getLabel(id, placeholder, type));
123
+    }
119 124
     field.appendChild(getInputField(id, placeholder, email, type));
120 125
     
121 126
     return field;

+ 14
- 2
server.js ファイルの表示

@@ -121,11 +121,23 @@ app.post('/send', function(req, res, next) {
121 121
 
122 122
 
123 123
 // A request on /lang sends translated strings (according to the locale set in
124
-// the app settings)
124
+// the app settings), alongside the boolean for the display of labels in the
125
+// form block.
125 126
 app.get('/lang', function(req, res, next) {
126 127
     // Response will be JSON
127 128
     res.header('Access-Control-Allow-Headers', 'Content-Type');
128
-    res.status(200).send(locale.client);
129
+
130
+    // Preventing un-updated settings file
131
+    let labels = true;
132
+    if(settings.labels !== undefined) {
133
+        labels = settings.labels;
134
+    }
135
+    
136
+    // Send the infos
137
+    res.status(200).send({
138
+        'labels': labels,
139
+        'translations': locale.client
140
+    });
129 141
 });
130 142
 
131 143
 

+ 2
- 1
settings.example.json ファイルの表示

@@ -14,5 +14,6 @@
14 14
         "someone.else@example.com"
15 15
     ],
16 16
     "formOrigin": "https://example.tld",
17
-    "language": "en"
17
+    "language": "en",
18
+    "labels": true
18 19
 }