This repository was archived by the owner on Sep 6, 2022. It is now read-only.
forked from laurens94/jquery-emailMandrill
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs-client-mail.js
More file actions
153 lines (131 loc) · 4.85 KB
/
Copy pathjs-client-mail.js
File metadata and controls
153 lines (131 loc) · 4.85 KB
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
143
144
145
146
147
148
149
150
151
152
153
(function( $ ) {
// SETTINGS:
var pluginName = "jsClientMail",
defaults = {
api: '',
labelAttribute: 'data-name',
submitSelector: 'input[type="submit"]',
success: function () {
$('body').removeClass('js-client-mail-waiting');
$('body').addClass('js-client-mail-success');
console.log('Message sent!');
},
wait: function () {
$('body').removeClass('js-client-mail-error');
$('body').removeClass('js-client-mail-success');
$('body').addClass('js-client-mail-waiting');
},
error: function (errors) {
$('body').removeClass('js-client-mail-waiting');
$('body').addClass('js-client-mail-error');
if (errors.length > 1) {
console.log(errors.length + ' messages could not be sent, reasons: ' + errors);
} else {
console.log(errors.length + ' message could not be sent, reason: ' + errors);
}
},
};
// The actual plugin constructor
function Plugin( element, options ) {
this.element = element;
this.options = $.extend( {}, defaults, options);
this.temp = {
errors: [],
total: 0
};
this._defaults = defaults;
this._name = pluginName;
this.init();
}
Plugin.prototype = {
init: function() {
var that = this;
// Skip init if the parent is not a form.
if ($(this.element)[0].nodeName != 'FORM') {
console.log(pluginName + ' can not handle ' + $(this.element)[0].nodeName + '. Use a FORM instead.');
return false;
}
$(this.element).submit(function (e) {
e.preventDefault();
// reset errors:
that.temp.errors = [];
that.waitingHandler();
var values = that.parseValues();
var result = that.callApi(values.valuesOrdered);
return false;
})
},
callApi: function (values) {
var that = this
$.ajax({
type: "POST",
url: that.options.api,
data: JSON.stringify(values),
contentType: "application/json; charset=utf-8",
dataType: "json",
headers: {
"Content-Type": "application/json"
},
success: function(success){
console.log(success)
that.options.success(success);
},
error: function(error) {
that.options.error(error);
}
});
},
waitingHandler: function () {
var that = this;
$(that.element).find(that.options.submitSelector).prop('disabled', true);
that.options.wait();
},
successHandler: function (status, response) {
var that = this;
if (status == 'error') {
that.temp.errors.push(response.message);
}
that.temp.total -= 1;
if (that.temp.total == 0) {
if (that.temp.errors.length > 0) {
that.options.error(that.temp.errors);
$(that.element).find(that.options.submitSelector).prop('disabled', false);
} else {
that.options.success();
}
}
},
parseValues: function () {
var that = this;
var output = {
valuesOrdered: [],
valuesDirect: {}
}
var data = $(that.element).serializeArray();
$.each(data, function (delta, formItem) {
var input = $('[name="' + formItem.name + '"]');
var label = input.attr(that.options.labelAttribute);
var value = formItem.value;
// Use name as label when no labelAttribute is present:
if(label == undefined || label == '') {
label = formItem.name;
}
output.valuesOrdered.push({
'label': label,
'value': value
});
output.valuesDirect[formItem.name] = value; // Used because of direct referencing in settings.
})
return output;
}
}
// jQuery plugin constructor
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName,
new Plugin( this, options ));
}
});
};
}( jQuery ));