1+ /* ==========================================================================
2+
3+ jQuery FormChimp - v1.2.1
4+ A customizable MailChimp ajax plugin for jQuery
5+ Fabio Quarantini - @febba
6+ http://www.fabioquarantini.com/formchimp/
7+
8+ ========================================================================== */
9+
10+
11+ ; ( function ( $ , window , document , undefined ) {
12+
13+ $ . fn . formchimp = function ( settings ) {
14+
15+ var $form = $ ( this ) ;
16+ var $body = $ ( 'body' ) ;
17+ var actionUrl = $form . attr ( 'action' ) . replace ( '/post?' , '/post-json?' ) . concat ( '&c=?' ) ;
18+ var $button = $form . find ( '[type="submit"]' ) ;
19+ var defaults = {
20+ 'appendElement' : $form , // Declare where the new element, containing the messages from Mailchimp will be appended to.
21+ 'buttonSelector' : $button , // Set the button selector.
22+ 'buttonText' : '' , // The message to be written on the submit button after a successful subscription.
23+ 'debug' : false , // Activate debug message in console.
24+ 'errorMessage' : '' , // Set custom error message given when return an error.
25+ 'onMailChimpSuccess' : function ( ) { } , // Callback that fires on success.
26+ 'onMailChimpError' : function ( ) { } , // Callback that fires on errors.
27+ 'responseClass' : "mc-response" , // Declare custom element in page for message output. (Set different classes for multiple sign-up forms)
28+ 'successMessage' : '' , // Set a custom success message.
29+ 'url' : actionUrl , // The mailchip list subscription url, to get the JSONP address just change `post` to `post-json` and append `&c=?` at the end.
30+ } ;
31+ var originalButtonText = defaults . buttonSelector . text ( ) ;
32+
33+ // Merge default whith settings
34+ $ . extend ( defaults , settings ) ;
35+
36+ // On submit
37+ $ ( $form ) . on ( 'submit' , function ( event ) {
38+
39+ // Disable default action of submit
40+ event . preventDefault ( ) ;
41+
42+ // Remove status class and add the loading
43+ $body . removeClass ( 'mc-success mc-error' ) . addClass ( 'mc-loading' ) ;
44+
45+ // If the response container does not exists
46+ if ( $ ( "." + defaults . responseClass ) . length === 0 ) {
47+
48+ // Add response container to append element
49+ $responseContainer = $ ( '<div/>' ) . addClass ( defaults . responseClass ) . appendTo ( defaults . appendElement ) ;
50+
51+ } else {
52+
53+ // Remove old message
54+ $responseContainer . html ( '' ) ;
55+
56+ }
57+
58+ // Perform an Ajax request
59+ $ . ajax ( {
60+
61+ url : defaults . url ,
62+ data : $form . serialize ( ) ,
63+ dataType : 'jsonp'
64+
65+ } ) . done ( function ( data ) {
66+
67+ // If debug is active
68+ if ( defaults . debug ) {
69+
70+ // Log in cosole the Mailchimp data
71+ console . log ( JSON . stringify ( data ) ) ;
72+ }
73+
74+ // Save the Mailchimp data
75+ var responseMessage = data . msg ;
76+
77+ // If the message start with a number and contains "-"
78+ if ( ! isNaN ( responseMessage . charAt ( 0 ) ) && responseMessage . charAt ( 2 ) === '-' ) {
79+
80+ // Remove first 3 characters
81+ responseMessage = responseMessage . substring ( 3 ) ;
82+
83+ }
84+
85+ // Add status class and remove the loading class
86+ $body . addClass ( 'mc-' + data . result ) . removeClass ( 'mc-loading' ) ;
87+
88+ // If the Mailchimp result is success
89+ if ( data . result === 'success' ) {
90+
91+ // If success message parameter is not empty
92+ if ( defaults . successMessage !== '' ) {
93+
94+ // Replace the default success message with parameter
95+ responseMessage = defaults . successMessage ;
96+
97+ }
98+
99+ // If button text parameter is not empty
100+ if ( defaults . buttonText !== '' ) {
101+
102+ // Replace the default button text with parameter
103+ defaults . buttonSelector . text ( defaults . buttonText ) ;
104+
105+ }
106+
107+ // Add event on error
108+ $ ( document ) . trigger ( 'mailChimpSuccess' ) ;
109+
110+ // Run callback
111+ defaults . onMailChimpSuccess . call ( ) ;
112+
113+ } else { // If there is an error
114+
115+ // If error message parameter is not empty
116+ if ( defaults . errorMessage !== '' ) {
117+
118+ // Replace the default error message with parameter
119+ responseMessage = defaults . errorMessage ;
120+
121+ }
122+
123+ // If button text parameter is not empty
124+ if ( defaults . buttonText !== '' ) {
125+
126+ // Replace the default button text with the original text
127+ defaults . buttonSelector . text ( originalButtonText ) ;
128+
129+ }
130+
131+ // Add event on error
132+ $ ( document ) . trigger ( 'mailChimpError' ) ;
133+
134+ // Run callback
135+ defaults . onMailChimpError . call ( ) ;
136+
137+ }
138+
139+ // Show the message
140+ $responseContainer . html ( responseMessage ) ;
141+
142+ } ) ;
143+
144+ } ) ;
145+
146+ } ;
147+
148+ } ) ( jQuery , window , document ) ;
0 commit comments