-
Notifications
You must be signed in to change notification settings - Fork 0
/
form-generator.js
142 lines (130 loc) · 4.8 KB
/
form-generator.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
var fs = require("fs");
const open = require('open');
function fieldType(field) {
var x = `<input type="text" class="form-control" id="${field}" name="${field}" placeholder="${fieldLabel(field)}" required>`;
if(field.includes("message")){
x = `<textarea type="text" class="form-control" id="${field}" name="${field}" placeholder="${fieldLabel(field)}" required></textarea>`;
}
else if(field.includes("email")){
x = `<input type="email" class="form-control" id="${field}" name="${field}" placeholder="${fieldLabel(field)}" required>`;
}
else if(field.includes("number")){
x = `<input type="number" class="form-control" id="${field}" name="${field}" placeholder="${fieldLabel(field)}" required>`;
}
return x;
}
function fieldLabel(field) {
var str = field.replace(/-/g, ' ').replace(/_/g, ' ');
return str;
}
exports.script = function formGenerator(url) {
let rawdata = fs.readFileSync('variables.json');
obj = JSON.parse(rawdata);
const fields = obj.formFields;
var formBody = ''
for(let f of fields) {
formBody += `
<label for="${f}" class="small mb-0">${fieldLabel(f)}</label>
${fieldType(f)}
`;
};
var fieldVars = '"id": "",';
for(let fv of fields) {
fieldVars += `"${fv}": $('#${fv}').val(),`;
};
var jqVars= fieldVars.substring(0, (fieldVars.length -1));
var htmlForm = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="A serverless contact form for your website in less than a minute." content="">
<meta name="Torus" content="">
<title>Super Easy From</title>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body class="p-5 bg-light">
<!-- CONTACT SECTION -->
<section class="bg-white py-4">
<div class="container">
<div class="row">
<div class="col-md-6">
<h4 class="h2 pb-2">Contact Us</h4>
<!-- FORM BY SUPER EASY FORMS -->
<form id="super-easy-form" alt="Form by Super Easy Forms" class="pb-3" action="#">
${formBody}
<h5 id="contact-status" class="text-secondary" style="display:none;">Thanks for Contacting Us</h5>
<button class="btn btn-primary mt-3" type="submit" id="super-easy-btn">Send</button>
</form>
<small>made with <a href="http://supereasyforms.com">super easy forms</a> <small> <!-- it's bad etiquette to use free stuff without giving some cred to the creators :) -->
</div>
</div>
</div>
</section>
<!--Javascript-->
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous">
</script>
<script>
$(function(){
$('#super-easy-form').submit(function(e){
e.preventDefault();
var formdata = toJSONString(this);
console.log(formdata);
$.ajax({
type: "POST",
url: "${url}",
dataType: "json",
contentType: "application/json",
data: JSON.stringify( { ${jqVars} } ),
beforeSend: function(data) {
$('#super-easy-btn').prop('disabled', true);
$('#super-easy-form :input').prop('disabled', true);
$('#contact-status').html('Sending...').show();
},
success: function(data) {
console.log(data);
$('#contact-status').text("We'll get back to you soon").show();
$('#super-easy-form :input').removeProp('disabled');
$('#super-easy-btn').removeProp('disabled');
},
error: function(jqXHR, textStatus, errorThrown) {
$('#contact-status').text('Error. Please try again soon.').show();
$('#super-easy-form :input').removeProp('disabled');
$('#super-easy-btn').removeProp('disabled');
}
});
});
function toJSONString (form) {
var obj = {};
var elements = form.querySelectorAll("input, select, textarea");
for(var i = 0; i < elements.length; ++i) {
var element = elements[i];
var name = element.name;
var value = element.value;
if(name) {
obj[name] = value;
}
}
return JSON.stringify(obj);
}
});
</script>
</body>
</html>
`;
fs.writeFile("super-easy-form.html", htmlForm, function(err) {
if(err) {
console.log(err);
}
else {
console.log('\x1b[32m', 'Your form was succesfully saved as super-easy-form.html', '\x1b[0m');
console.log('\x1b[32m', "Wasn't that Super Easy?", '\x1b[0m');
open('super-easy-form.html');
}
});
}