This repository has been archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
jquery.ajaxSubmit.js
246 lines (204 loc) · 6.29 KB
/
jquery.ajaxSubmit.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
//
// jquery.ajaxSubmit.js - Effortlessly submit forms using AJAX and JSON
//
// Developed by Cory LaViska for A Beautiful Site, LLC
//
// Licensed under the MIT license: http://opensource.org/licenses/MIT
//
if(jQuery) (function($) {
'use strict';
// Defaults
$.ajaxSubmit = {
defaults: {
// Request options
url: function() {
return $(this).attr('action');
},
method: function() {
return $(this).attr('method');
},
headers: undefined,
data: function() {
return $(this).serialize();
},
after: function() {},
before: function() {},
error: function() {},
success: function() {},
// UI options
loader: '.form-loader',
message: '.form-message',
messageErrorClasses: 'alert alert-danger',
messageSuccessClasses: 'alert alert-success',
hideInvalid: function(input) {
$(input).removeClass('is-invalid');
},
showInvalid: function(input) {
$(input).addClass('is-invalid');
}
}
};
// Create the plugin
$.extend($.fn, {
ajaxSubmit: function(method, options) {
if(typeof method === 'object') options = method;
// Public API
switch(method) {
case 'busy':
return $(this).each(options === false ? unbusy : busy);
case 'destroy':
return $(this).each(destroy);
case 'disable':
return $(this).each(options === false ? enable : disable);
case 'reset':
return $(this).each(reset);
default:
return $(this).each(function() {
create.call(this, $.extend({}, $.ajaxSubmit.defaults, options));
});
}
}
});
// Make the form busy
function busy() {
var form = this;
var options = $(form).data('options.ajaxSubmit');
$(form)
.addClass('ajaxSubmit-busy')
.find(options.loader).prop('hidden', false);
}
// Create (initialize) it
function create(options) {
$(this)
.data('options.ajaxSubmit', options)
.on('submit.ajaxSubmit', submit);
}
// Destroy it
function destroy() {
$(this)
.removeData('options.ajaxSubmit')
.off('.ajaxSubmit');
}
// Disable all form elements
function disable() {
$(this)
.addClass('ajaxSubmit-disabled')
.find(':input').prop('disabled', true);
}
// Enable all form elements
function enable() {
$(this)
.removeClass('ajaxSubmit-disabled')
.find(':input').prop('disabled', false);
}
// Hide invalid field errors
function hideInvalid() {
var form = this;
var options = $(form).data('options.ajaxSubmit');
// Loop through each invalid field and run `hideInvalid`
$(form).find('.ajaxSubmit-invalid').each(function() {
var input = this;
$(input).removeClass('ajaxSubmit-invalid');
options.hideInvalid.call(form, input);
});
}
// Hide the form message
function hideMessage() {
var form = this;
var options = $(form).data('options.ajaxSubmit');
$(form).find(options.message)
.text('')
.prop('hidden', true);
}
// Reset the form
function reset() {
unbusy.call(this);
hideInvalid.call(this);
hideMessage.call(this);
this.reset();
}
// Show invalid field errors
function showInvalid(fields) {
var form = this;
var options = $(form).data('options.ajaxSubmit');
// Loop through each invalid field and run `showInvalid`
$.each(fields, function(index, value) {
var input = $(form).find(':input[name="' + value + '"]').get(0);
$(input).addClass('ajaxSubmit-invalid');
options.showInvalid.call(form, input);
});
}
// Show the form message
function showMessage(message, success) {
var form = this;
var options = $(form).data('options.ajaxSubmit');
$(form).find(options.message)
.removeClass(success ? options.messageErrorClasses : options.messageSuccessClasses)
.addClass(success ? options.messageSuccessClasses : options.messageErrorClasses)
.text(message)
.prop('hidden', false);
}
// Handle form submission
function submit(event) {
var form = this;
var options = $(form).data('options.ajaxSubmit');
event.preventDefault();
// Don't allow submission if the form is busy
if($(form).is('.ajaxSubmit-busy')) return;
// Run the before callback (returning false here will prevent submission)
if(options.before.call(form) === false) return;
// Make the form busy and hide invalid fields/messages
hideMessage.call(form);
hideInvalid.call(form);
busy.call(form);
// Send the request
$.ajax({
url: typeof options.url === 'function' ? options.url.call(form) : options.url,
type: typeof options.method === 'function' ? options.method.call(form) : options.method,
data: typeof options.data === 'function' ? options.data.call(form) : options.data,
headers: options.headers,
dataType: 'json'
})
.done(function(res) {
// Remove busy state
unbusy.call(form);
// Show the message if `res.message` exists
if(res && res.message) {
showMessage.call(form, res.message, true);
}
// Show invalid fields if `res.invalid` exists
if(res && res.invalid && res.invalid.length) {
showInvalid.call(form, res.invalid);
}
// Run the success callback
options.success.call(form, res);
// Run the after callback
options.after.call(form, res);
})
.fail(function(jqXHR, textStatus, errorThrown) {
var res = jqXHR.responseJSON;
// Remove busy state
unbusy.call(form);
// Show the message if `res.message` exists
if(res && res.message) {
showMessage.call(form, res.message, false);
}
// Show invalid fields if `res.invalid` exists
if(res && res.invalid && res.invalid.length) {
showInvalid.call(form, res.invalid);
}
// Run the error callback
options.error.call(form, res, errorThrown);
// Run the after callback
options.after.call(form, res);
});
}
// Remove the form's busy state
function unbusy() {
var form = this;
var options = $(form).data('options.ajaxSubmit');
$(form)
.removeClass('ajaxSubmit-busy')
.find(options.loader).prop('hidden', true);
}
})(jQuery);