-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.formidable.js
More file actions
171 lines (138 loc) · 4.48 KB
/
jquery.formidable.js
File metadata and controls
171 lines (138 loc) · 4.48 KB
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
/*
* jQuery formidable plug-in 0.2
*
* http://bassistance.de/jquery-plugins/jquery-plugin-validation/
* http://docs.jquery.com/Plugins/Validation
*
* Copyright (c) 2011 Daniel J Carper
*
* $Id: jquery.formidable.js 6403 2011-06-06 14:27:16Z dan.carper $
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
// HIDE ANYTHING IN THE SUBMIT DIV THEN INSERT A SPINNER
// ALSO DISABLE INPUTS BY DEFAULT
//
$.fn.disableSubmission = function(options) {
options = options || {disableInputs: true}; // this is bad
if (options.disableInputs)
{ this.disableInputs() }; // DISABLE INPUTS
this.hideSubmitButtons(); // HIDE SUBMIT BUTTON, SHOW SPINNER
this.attr('disabled', 'disabled'); // DISABLE THE FORM
return this;
}
// DISABLE INPUTS
//
$.fn.disableInputs = function() {
this.find('input').attr('disabled', 'disabled');
return this;
}
// ENABLE THE INPUTS
//
$.fn.enableInputs = function() {
this.find('input').removeAttr('disabled');
return this;
}
// HIDE THE SUBMIT BUTTON, SHOW THE SPINNER
//
// BASED ON THE CURRENT PATTERN OF PRELOADING A SPINNER GIF
// WITH CLASS OFF IN THE SUBMIT DIV
//
$.fn.hideSubmitButtons = function() {
this.toggleOffables();
}
// TOGGLE THE CLASS OFF ON THE DIRECT CHILDREN OF THE OFF DIV
//
$.fn.toggleOffibles = function() {
this.find("div#submit").children().toggleClass('off')
}
// RESETS EVERY FIELD WITH A DEFAULT VALUE TO THAT DEFAULT
//
// BASED ON THE ATTRIBUTE data-default
//
$.fn.reset = function() {
defaultables = this.find('[data-default]');
defaultables.each(function(){
$(this).attr('value', $(this).attr('data-default'));
});
return this;
}
// REMOVE THE SPINNER THEN SHOW EVERYTHING IN THE #SUBMIT DIV
//
$.fn.enableSubmission = function(options) {
this.removeAttr('disabled');
options = options || {reset: true};
if (!options.disableInputs) // ENABLE THE INPUTS BY DEFAULT
{ this.enableInputs() }
if(options.reset) // RESET ANY DEFAULT FIELDS
{ this.reset() }
this.toggleOffables();
return this;
}
// DISABLE THE SUBMIT BUTTONS AND LET ANY FORM INTERACTION REACTIVATE THEM
//
$.fn.disableSubmitters = function() {
$submitters = this.find('input[type=submit]'); // disable any submit buttons
$submitters.attr('disabled', 'disabled');
$(this).find('input, textarea, select').one('keyup', function() { // enable them after submission
$submitters.removeAttr('disabled');
})
$(this).find('input, textarea').one('click', function() { // enable them after submission
$submitters.removeAttr('disabled');
})
$(this).find('select').one('change', function() { // enable them after submission
$submitters.removeAttr('disabled');
})
$(this).find('#baby_bracket_state, #baby_bracket_due_date').one('click', function() { // enable them after submission
$submitters.removeAttr('disabled');
});
return this;
}
// RETURN TRUE IF THE FORM CAN SUBMIT
//
$.fn.submissionEnabled = function() {
return this.attr('disabled') != 'disabled'
}
// A jquery.validate METHOD TO PREVENT SUBMISSION OF DEFAULT VALUES
//
jQuery.validator.addMethod("not_default", function(value, element) {
return !( value == $(element).attr('data-default'))
});
// RETURNS TRUE IF THE FIELD CONTAINS IT'S DEFAULT VALUE
//
$.fn.isDefaultValue = function() {
return (this.attr("value") == this.attr('data-default'))
}
// RETURNS TRUE IF THE FIELD IS BLANK
//
$.fn.valueIsBlank = function() {
return (this.attr('value') == '');
}
// DRIVE THE DEFAULT FORM FIELD BEHAVIOR
//
// If THE FIELD IS FOCUSED AND IS EQUAL TO IT'S DEFAULT VALUE, THE FIELD CLEARS
// IF THE FOCUS IS LOST ON A FIELD AND THE FIELD IS BLANK, RESET IT'S DEFAULT VALUE
//
// AS IT STANDS NOW, YOU CANNOT SUBMIT DEFAULT VALUES
//
$.fn.enableDefaults = function () {
var defaultables = this.find('[data-default]'); // FIND THE DEFAULTABLES
defaultables.focus(function(){ // CLEAR WHEN APPROPRIATE
if( $(this).isDefaultValue() )
{ $(this).attr('value', '') }});
defaultables.focusout(function(){ // RESET WHEN APPROPRIATE
if ( $(this).valueIsBlank() )
{ $(this).attr('value', $(this).attr('data-default')) }});
defaultables.rules('add', { not_default: true }); // DO NOT SUBMIT DEFAULT VALUES
return this;
}
// FOR REGULAR HTTP SUBMITS. DISABLE THE FORM AND SUBMIT
//
$.fn.submitOnce = function () {
$(this).submit(function() {
$(this).disableSubmission();
return true;
})
}