-
Notifications
You must be signed in to change notification settings - Fork 1
/
form-submitter.js
executable file
·330 lines (254 loc) · 7.81 KB
/
form-submitter.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// ==========================================================================
// Project: Form Submitter
// Description: jQuery plugin used in GestiXi websites to handle form submission
// Copyright: ©2013-2016 GestiXi
// License: Licensed under the MIT license (see LICENCE)
// Version: 1.1
// Author: Nicolas BADIA
// ==========================================================================
!function($) { "use strict";
// ..........................................................
// FORM SUBMITTER PLUGIN DEFINITION
//
$.fn.formSubmitter = function( option ) {
return this.each(function() {
var $this = $(this),
$form = $this[0].tagName === 'FORM' ? $this.parent().find('form') : $this.find('form'),
data = $form.data('formSubmitter');
if (!data) {
var options = $.extend({}, $.fn.formSubmitter.defaults, typeof option == 'object' && option);
$form.data('formSubmitter', (data = new FormSubmitter(this, $form, options)));
$form.find("input[type=submit]").on('click', function() {
$form.find("input[type=submit]").removeAttr("clicked");
$(this).attr("clicked", "true");
});
$form.on('submit', function(evt) {
evt.preventDefault();
var canSubmit = options.willSend.call(data, evt);
if (canSubmit) {
var clickedButton = $("input[type=submit][clicked=true]");
clickedButton.prop('disabled', true);
data.clickedButton = clickedButton;
data.handler(evt);
options.didSend.call(data, evt);
}
});
}
else {
console.warn('formSubmitter is already handling the form');
}
})
}
$.fn.formSubmitter.defaults = {
/**
Delegate called before form submission.
Return `false` to prevent form submitting.
Example:
$('form').formSubmitter({
willSend: function() {
return true;
}
});
@type Function
@param {Event} evt
@returns {Boolean} true to submit the form
@since Version 1.0
*/
willSend: function(evt) {
return true;
},
/**
Delegate called to get the form data.
@type Function
@param {Event} evt
@returns {String} the data to send
@since Version 1.0
*/
formSettings: function(evt) {
var $form = this.$form,
settings = {
method: $form.attr("method"),
url: $form.attr("action"),
data: $form.serialize()
};
// FormData add input file support but is not supported in IE9
if (window.FormData !== undefined) {
settings.cache = false;
settings.contentType = false;
settings.processData = false;
settings.data = new FormData($form[0]);
}
return settings;
},
/**
Delegate called after form submission.
@type Function
@param {Event} evt
@since Version 1.0
*/
didSend: function(evt) {},
/**
Delegate called before the response handling.
@type Function
@param {String} response
@returns {String} the response to handle
@since Version 1.0
*/
willReceive: function(response) {
return JSON.parse(response);
},
/**
Delegate called after the response handling.
@type Function
@param {String} response
@since Version 1.0
*/
didReceive: function(response, result) {},
/**
Duration of the notifications in milliseconds.
@type String
@since Version 1.1
*/
notificationDelay: 8000,
/**
Layout of the notifications.
@type String
@since Version 1.0
*/
notificationLayout: 'position:fixed;top:10px;left:10px;width:350px;z-index:1000;'
}
// ..........................................................
// FORM SUBMITTER PUBLIC CLASS DEFINITION
//
var FormSubmitter = function(element, $form, options) {
this.element = element;
this.$form = $form;
this.options = options;
}
$.fn.formSubmitter.Constructor = FormSubmitter
FormSubmitter.prototype = {
constructor: FormSubmitter,
/**
The initial element.
@type DOM Element
*/
element: null,
/**
The jQuery form element.
@type jQuery Element
*/
$form: null,
/**
The passed options.
@type Object
*/
options: null,
/**
Will submit the form.
@type Function
@param {Event} evt
*/
handler: function(evt) {
var that = this,
options = this.options,
settings = options.formSettings.call(this, evt);
$.ajax(settings).done(function(response) {
var clickedButton = that.clickedButton;
if (clickedButton) clickedButton.prop('disabled', false);
var parsedResponse = options.willReceive.call(that, response);
if (parsedResponse) {
var result = that.handleResponse(parsedResponse);
options.didReceive.call(that, parsedResponse, result);
}
});
return false;
},
/**
Will handle the response.
@type Function
@param {String} response
*/
handleResponse: function(response) {
var $form = this.$form,
notification = response.notification,
helpInline = response.helpInline,
submitForm = response.submitForm,
redirectUrl = response.redirectUrl;
if (redirectUrl) {
document.location.href = redirectUrl;
}
if (submitForm) {
this.submitForm(submitForm);
}
$('.help-inline').html('');
$('.form-group').removeClass('has-error');
if (helpInline) {
helpInline = JSON.parse(helpInline);
for (var name in helpInline) {
var $helpInline = $form.find('.help-inline.'+name);
$helpInline.html(helpInline[name]);
$helpInline.closest('.form-group').addClass('has-error');
}
}
if (notification) this.notify(notification);
return response;
},
/**
Display a Bootstrap notification.
@type Function
@param {String} html
@since Version 1.0
*/
notify: function (notification) {
var options = this.options,
notificationDelay = options.notificationDelay;
if (!$('#notification-container').length) {
var layout = this.options.notificationLayout;
$('<div id="notification-container" style="'+layout+'"></div>').appendTo('body');
}
var $notification = $(notification);
$notification
.hide()
.appendTo('#notification-container')
.slideDown()
.delay(notificationDelay)
.fadeOut(400, function() {
$notification.remove();
});
},
/**
Helper used to submit a form.
The form must have the following format:
{
method: 'post',
action: 'https://www.gestixi.com/gx-handle-form',
inputs: [
{
type: 'text',
name: 'title',
value: 'example'
}
]
}
@type Function
@param {Object} form
@since Version 1.0
*/
submitForm: function (submitForm) {
var form = JSON.parse(submitForm),
formEl = document.createElement("form");
formEl.setAttribute("method", form.method || "post");
formEl.setAttribute("action", form.action);
for (var i = 0; i < form.inputs.length; i++) {
var input = form.inputs[i],
inputEl = document.createElement("input");
inputEl.setAttribute("type", input.type || "hidden");
inputEl.setAttribute("name", input.name);
inputEl.setAttribute("value", input.value);
formEl.appendChild(inputEl);
};
document.body.appendChild(formEl);
formEl.submit();
}
}
}(window.jQuery);