-
Notifications
You must be signed in to change notification settings - Fork 0
/
gemini.resetform.js
165 lines (138 loc) · 3.71 KB
/
gemini.resetform.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
/**
* @fileoverview
A Gemini plugin to reset forms to their default values.
### Notes
- The plugin uses ``data-reset`` attributes to find default values
### Features
- Here's a feature
*
* @namespace gemini.resetform
* @copyright Carpages.ca 2014
* @author Matt Rose <[email protected]>
*
* @requires gemini
*
* @example
<html>
<form id="js-form" action="#">
<select name="make" data-reset="dodge">
<option value="dodge" selected>Dodge</option>
<option value="ford">Ford</option>
</select>
<label>
<input type="radio" value="auto" name="transmission" data-reset="this" checked>
Automatic
</label>
<label>
<input type="radio" value="manual" name="transmission">
Manual
</label>
<label>
<input type="checkbox" value="bluetooth" name="features" data-reset="this" checked>
Bluetooth
</label>
<label>
<input type="checkbox" value="air" name="features">
Air-conditioning
</label>
<button data-reset="trigger">
Reset
</button>
</form>
</html>
*
* @example
G('#js-hook').resetform();
*/
( function( factory ) {
if ( typeof define === 'function' && define.amd ) {
// AMD. Register as an anonymous module.
define([ 'gemini' ], factory );
} else if ( typeof exports === 'object' ) {
// Node/CommonJS
module.exports = factory( require( 'gemini-loader' ));
} else {
// Browser globals
factory( G );
}
})( function( $ ) {
$.boiler( 'resetform', {
init: function() {
var plugin = this;
// Bind reset form
plugin.$el.bind( 'reset', function() {
plugin.reset();
});
// Bind click event
plugin.$el.find( '[data-reset="trigger"]' ).click( function( e ) {
e.preventDefault();
plugin.$el.trigger( 'reset' );
});
},
/**
* Reset the form to its defaults
*
* @method
* @name gemini.resetform#reset
**/
reset: function() {
var plugin = this;
// Select boxes
plugin.selectReset();
// Checkboxes
plugin.checkboxReset();
// Radio Buttons
plugin.radioReset();
},
/**
* Reset all of the select dropdowns
*
* @method
* @name gemini.resetform#seletReset
**/
selectReset: function() {
var plugin = this;
var $select = plugin.$el.find( 'select[data-reset]' );
$select.each( function() {
var $this = $( this );
var $toSelect = $this.find( '[value=' + $this.data( 'reset' ) + ']' );
if ( $toSelect.length <= 0 ) $toSelect = $this.find( 'option:first' );
// Deselect
$this.find( 'option:selected' ).prop( 'selected', false );
// Select
$toSelect.prop( 'selected', true );
});
},
/**
* Reset all of the checkboxes
*
* @method
* @name gemini.resetform#checkboxReset
**/
checkboxReset: function() {
var plugin = this;
var $checkbox = plugin.$el.find( '[type="checkbox"]' );
// Uncheck them all
$checkbox.prop( 'checked', false );
// Check the default items
$checkbox.filter( '[data-reset="this"]' ).prop( 'checked', true );
},
/**
* Reset all of the radio buttons
*
* @method
* @name gemini.resetform#radioReset
**/
radioReset: function() {
var plugin = this;
var $radio = plugin.$el.find( '[type="radio"]' );
// Uncheck them all
$radio.prop( 'checked', false );
// Check the default items
$radio.filter( '[data-reset="this"]' ).prop( 'checked', true );
}
});
// Return the jquery object
// This way you don't need to require both jquery and the plugin
return $;
});