-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContactStorm.js
83 lines (61 loc) · 1.85 KB
/
ContactStorm.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
function ContactStorm(username, provider, formName) {
this.username = username;
this.provider = provider;
this.formName = formName;
this._form = false;
this.onSubmit = function(cb) {
this.cbSubmit = cb;
return this;
};
this.onError = function(cb) {
this.cbError = cb;
return this;
};
this.onSuccess = function(cb) {
this.cbSuccess = cb;
return this;
};
this.cbSubmit = function() {};
this.cbError = function(error) {};
this.cbSuccess = function(obj) {};
this.submit = function(ev) {
ev.preventDefault();
ev.stopPropagation();
// Call submission handler
this.cbSubmit();
let payload = {};
// Load form inputs
let formElements = this._form.querySelectorAll("input, textarea, select");
formElements.forEach(function(element) {
let key = element.name || element.id;
payload[key] = element.value;
}.bind(this));
emailjs.send(this.provider, this.formName, payload)
.then(function (response) {
this.cbSuccess(response);
}.bind(this))
.catch(function(error) {
this.cbError(error);
}.bind(this))
}
this.init = function() {
try {
emailjs.init(this.username);
// Initialize contact form
this._form = document.getElementById(this.formName);
if (this._form) {
this._form.addEventListener(
"submit",
this.submit.bind(this),
false
);
}
else {
throw "Could not find the contact form.";
}
}
catch (ex) {
this.cbError(ex);
}
};
};