-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsubbscribe.js
301 lines (188 loc) · 8.72 KB
/
subbscribe.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
* Subbscribe.js (http://www.subbscribe.com)
* Copyright (c) 2014 (v2.0) Shlomi Nissan, 1ByteBeta (http://www.1bytebeta.com)
* Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
*/
(function($) {
$.fn.subbscribe = function( options ) {
var obj = this;
// Default settings
var settings = $.extend({
list : 'MailChimp',
url : '',
title : 'Never miss a post!',
text : 'Get our latest posts and announcements in your inbox. You won\'t regret it!',
name : 'Subbscribe',
color : '#ee6262',
thumbnail : 'https://s3-ap-southeast-2.amazonaws.com/subbscribe/img/avatar.png',
emailonly : false,
cm_mail_field : '',
delay : 0,
}, options);
// Make sure a URL has been passed through
if ( settings.url == '' ) {
console.log('Subbscribe Error: You must provide a valid MailChimp form URL.');
return;
};
//make sure the cm_mail_field is set when using Campaign Monitor
if( settings.list === 'CampaignMonitor' && !settings.cm_mail_field.length ){
console.log('You must provide the mail input name. Found in the form code from Campaign Monitor');
return;
}
var _name = '';
var _email = '';
var _url = '';
// Make sure list is either set to MailChimp or CampaignMonitor
// Change field names if yours don’t match
if( settings.list == 'MailChimp' ) {
_name = 'NAME';
_email = 'EMAIL';
_action = settings.url.replace('/post?', '/post-json?').concat('&c=?');
}
else if ( settings.list == 'CampaignMonitor' ) {
_name = 'cm-name';
_email = settings.cm_mail_field;
_action = settings.url + "?callback=?";
}
else {
console.log('Subbscribe Error: list value must be set to MailChimp or CampaignMonitor');
return;
}
// Separate the input fields from the HTML
// if emailonly is set, nameInput should be blank
var nameInput = '';
var emailInput = '<input type="email" name="' + _email + '" id="subb-EMAIL" placeholder="Email Address" />';
if( !settings.emailonly ) {
nameInput = ' <input type="text" name="' + _name + '" id="subb-NAME" placeholder="Name" />';
}
// HTML
var html = '<div id="subbscribe" style="display: none"><div class="subb-title">' + settings.title + ' <img class="close-x" src="https://s3-ap-southeast-2.amazonaws.com/subbscribe/img/close.svg" /> </div> <div class="subb-body"> <div class="subb-hidden"> <div class="subb-thumbnail"> <img style="width: 40px; height: 40px;" src="' + settings.thumbnail + '" /> </div> <div class="subb-hidden"> <div class="subb-site"> ' + settings.name + ' </div> <button class="subb-button show-form">Subscribe</button> </div> </div> <div class="subb-form" style="display: none"> <p>' + settings.text + '</p> <form id="mc-embedded-subbscribe-form" method="post" action="' + settings.url + '"> <div class="subbscribe-alert subbscribe-error" style="display: none">Oops! Check your details and try again.</div> <div class="subbscribe-alert subbscribe-success" style="display: none">Thanks! Check your email for confirmation.</div> <div class="text-input"> ' + nameInput + ' </div> <div class="text-input"> ' + emailInput + ' </div> <button class="subb-button submit-form" type="submit" style="width: 100%; margin-bottom: 10px;">Subscribe</button></form> <div class="footer">Powered by <a href="http://www.subbscribe.com" target="_blank">Subbscribe.com</a></div> </div> </div> </div>';
if(getCookie('subbscribe-hidden') != 1) {
this.append(html);
setTimeout(function(){
$('#subbscribe').css('display', 'block');
$('#subbscribe').css('width', $('.subb-site').width() + 200 );
$('#subbscribe').addClass('animated slideInRight');
}, settings.delay * 1000);
}
// Update CSS classes
$('#subbscribe .subb-button').css('background-color', settings.color);
/*
===============================================================================
Events
===============================================================================
*/
$('#subbscribe .close-x').click(function(){
$('#subbscribe').toggleClass('slideInRight fadeOut');
$('#subbscribe').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
$('#subbscribe').remove();
setCookie('subbscribe-hidden', 1, 1); // Hide for a day
});
settings.onClose.call();
});
$('#subbscribe .show-form').click(function(){
$('#subbscribe .subb-hidden').hide();
$('#subbscribe .subb-form').show();
});
$('#mc-embedded-subbscribe-form').submit(function(e){
e.preventDefault();
if( formValidation() ) {
$('#subbscribe .subbscribe-error').slideUp();
$('#subbscribe .submit-form').attr('disabled', 'disabled');
$.ajax({
url: _action,
type: 'post',
data: $(this).serialize(),
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (data) {
if ( isError(data) ) {
console.log('Subbscribe Error: submission failed.');
}
else {
//SUCCESS
resetFormFields()
$('.subbscribe-success').slideDown();
setTimeout(function(){ $('#subbscribe').addClass('animated fadeOut'); }, 2000);
$('#subbscribe').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
$('#subbscribe').remove();
setCookie('subbscribe-hidden', 1, 365); // Hide for a year
if(typeof settings.onSubbscribe === 'function'){
settings.onSubbscribe.call();
}
});
}
}
});
} else {
$('#subbscribe .subbscribe-error').slideDown();
}
});
/*
===============================================================================
Helpers
===============================================================================
*/
function isError(data) {
console.log( data );
if ( settings.list == 'MailChimp' ) {
if( data['result'] != "success" ) {
return true;
}
return false;
}
else if ( settings.list == 'CampaignMonitor' ) {
if( data.Status === 400 ) {
return true;
}
return false;
}
return true;
}
function resetFormFields() {
$('#subbscribe input').each(function(){
$(this).val('');
});
}
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
function formValidation() {
var valid = true;
var name = $('#subb-NAME');
var email = $('#subb-EMAIL');
if( !settings.emailonly ) {
if( name.val().length < 2 ) {
valid = false;
name.addClass('error');
} else {
name.removeClass('error');
}
}
if ( !validateEmail( email.val() ) ) {
valid = false;
email.addClass('error');
} else {
email.removeClass('error');
}
return valid;
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires + "; path=/";
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
}
}(jQuery));