-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.ajaxchimp.min.js
120 lines (108 loc) · 4.71 KB
/
jquery.ajaxchimp.min.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
(function ($) {
'use strict';
$.ajaxChimp = {
responses: {
'We have sent you a confirmation email' : 0,
'Please enter a valid email' : 1,
'An email address must contain a single @' : 2,
'The domain portion of the email address is invalid (the portion after the @: )' : 3,
'The username portion of the email address is invalid (the portion before the @: )' : 4,
'This email address looks fake or invalid. Please enter a real email address' : 5
},
translations: {
'en': null
},
init: function (selector, options) {
$(selector).ajaxChimp(options);
}
};
$.fn.ajaxChimp = function (options) {
$(this).each(function(i, elem) {
var form = $(elem);
var email = form.find('input[type=email]');
var label = form.find('.info');
var settings = $.extend({
'url': form.attr('action'),
'language': 'en'
}, options);
var url = settings.url.replace('/post?', '/post-json?').concat('&c=?');
form.attr('novalidate', 'true');
email.attr('name', 'EMAIL');
form.submit(function () {
var msg;
function successCallback(resp) {
if (resp.result === 'success') {
msg = 'We have sent you a confirmation email';
label.removeClass('error').addClass('valid');
email.removeClass('error').addClass('valid');
} else {
email.removeClass('valid').addClass('error');
label.removeClass('valid').addClass('error');
var index = -1;
try {
var parts = resp.msg.split(' - ', 2);
if (parts[1] === undefined) {
msg = resp.msg;
} else {
var i = parseInt(parts[0], 10);
if (i.toString() === parts[0]) {
index = parts[0];
msg = parts[1];
} else {
index = -1;
msg = resp.msg;
}
}
}
catch (e) {
index = -1;
msg = resp.msg;
}
}
// Translate and display message
if (
settings.language !== 'en'
&& $.ajaxChimp.responses[msg] !== undefined
&& $.ajaxChimp.translations
&& $.ajaxChimp.translations[settings.language]
&& $.ajaxChimp.translations[settings.language][$.ajaxChimp.responses[msg]]
) {
msg = $.ajaxChimp.translations[settings.language][$.ajaxChimp.responses[msg]];
}
label.html(msg);
label.show(2000);
if (settings.callback) {
settings.callback(resp);
}
}
var data = {};
var dataArray = form.serializeArray();
$.each(dataArray, function (index, item) {
data[item.name] = item.value;
});
$.ajax({
url: url,
data: data,
success: successCallback,
dataType: 'jsonp',
error: function (resp, text) {
console.log('mailchimp ajax submit error: ' + text);
}
});
// Translate and display submit message
var submitMsg = 'Submitting...';
if(
settings.language !== 'en'
&& $.ajaxChimp.translations
&& $.ajaxChimp.translations[settings.language]
&& $.ajaxChimp.translations[settings.language]['submit']
) {
submitMsg = $.ajaxChimp.translations[settings.language]['submit'];
}
label.html(submitMsg).show(2000);
return false;
});
});
return this;
};
})(jQuery);