-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathjquery.safeform.js
67 lines (53 loc) · 1.48 KB
/
jquery.safeform.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
(function($) {
var getTime = function() { return new Date().getTime(); };
var SafeForm = function(element, options) {
this.initialize(element, options);
}
SafeForm.prototype = {
constructor: SafeForm,
initialize: function(element, options) {
this.$element = $(element);
this.options = $.extend({}, $.fn.safeform.defaults, options);
this.disabled = false;
this.submittedAt = getTime();
this.$element.submit($.proxy(this.prevenDoubleSubmit, this));
},
disable: function() {
this.disabled = true;
},
complete: function() {
this.disabled = false;
},
submit: function() {
this.$element.submit();
},
isAutoRefreshed: function() {
var o = this.options,
now = getTime();
return o.timeout && now - this.submittedAt > o.timeout;
},
prevenDoubleSubmit: function(event) {
var o = this.options;
if (this.disabled && !this.isAutoRefreshed()) return false;
this.disable();
this.submittedAt = getTime();
if ($.isFunction(o.submit)) {
return o.submit.call(this.$element, event);
}
}
};
$.fn.safeform = function(option) {
return this.each(function () {
var $this = $(this),
data = $this.data('safeform'),
options = typeof option == 'object' && option;
if (!data) $this.data('safeform', (data = new SafeForm(this, options)));
if (typeof option == 'string') data[option]();
});
};
$.fn.safeform.Constructor = SafeForm;
$.fn.safeform.defaults = {
timeout: null,
submit: null
};
})(window.jQuery);