-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.onetime.js
99 lines (80 loc) · 2.31 KB
/
jquery.onetime.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
/**
* Onetime: disable buttons on submit
* Copyright 2012 Eugene Kenny <[email protected]>
* Released under the MIT license (http://www.opensource.org/licenses/MIT)
*/
(function($) {
// jQuery method
$.extend($.fn, {
onetime: function(formOptions, buttonOptions) {
// Only attach to form elements
var forms = this.filter('form');
forms.each(function(i, form) {
var onetime = new $.onetime(form, formOptions, buttonOptions);
$(this).submit(function(event) {
if (!event.isDefaultPrevented()) {
onetime.disableButtons();
onetime.triggerButtonEvent();
}
});
});
return this;
}
});
// Constructor
$.onetime = function(form, formOptions, buttonOptions) {
this.form = form;
this.formSettings = $.extend(true, {}, formOptions);
this.buttonSettings = $.extend(true, {}, buttonOptions);
this.init();
};
// Internal methods
$.extend($.onetime, {
prototype: {
init: function() {
var onetime = this;
var otherButtons = this.buttons();
jQuery.each(this.buttonSettings, function(selector, settings) {
var buttons = onetime.buttons(selector);
settings = $.extend(true, {}, onetime.formSettings, settings);
onetime.setupCallbacks(buttons, settings);
otherButtons = otherButtons.not(buttons);
});
this.setupCallbacks(otherButtons, this.formSettings);
},
buttons: function(selector) {
var buttons = $(this.form).find('input:submit, input[type=image]');
if (selector !== undefined)
{
buttons = buttons.filter(selector);
}
return buttons;
},
setupCallbacks: function(elements, settings) {
if (settings.hasOwnProperty('disable')) {
elements.bind('onetime-submit', settings.disable);
}
if (settings.hasOwnProperty('text')) {
elements.bind('onetime-submit', function() {
$(this).val(settings.text);
});
}
if (settings.hasOwnProperty('loader')) {
elements.bind('onetime-submit', function() {
$(onetime.form).find(settings.loader).show();
});
}
var onetime = this;
elements.click(function() {
onetime.submitButton = this;
});
},
disableButtons: function() {
this.buttons().attr('disabled', 'disabled');
},
triggerButtonEvent: function() {
$(this.submitButton).triggerHandler('onetime-submit');
}
}
});
})(jQuery);