-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.customFormElements.js
164 lines (133 loc) · 3.94 KB
/
jquery.customFormElements.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
/**
* jQuery Custom Form Elements Plugin 1.3.0
*
* https://github.com/jmatc/custom-form-elements
* https://github.com/jmatc/custom-form-elements/blob/master/README.md
*
* (c) 2013 Jonathan Calvin
* Released under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*/
;(function($){
var methods = {
namespace : 'customFormElements',
settings : {
beforeUnChecked : function (){},
afterUnChecked : function (){},
beforeChecked : function (){},
afterChecked : function (){}
},
init : function (options) {
var $this = $(this),
data = $this.data(methods.namespace);
if (!data) {
var opts = $.extend( {}, methods.settings, options );
opts.checkboxWrap = $('<div class="checkbox-wrapper"/>');
opts.radioWrap = $('<div class="radio-wrapper"/>');
$this.data(methods.namespace, opts);
if ($this.is('input[type="checkbox"]')) {
methods.styleCheckbox.apply($this);
} else if ($this.is('input[type="radio"]')) {
methods.styleRadio.apply($this);
}
}
},
styleCheckbox : function () {
var $this = this,
data = $this.data(methods.namespace);
$this.wrap(data.checkboxWrap).parents('.input-box').addClass('select-child');
if ($this.prop('checked')) {
$this.parent().addClass('active');
}
$this.on('change', function(evt) {
var $chkBox = $(this);
if ($chkBox.prop('checked')) {
if (typeof data.beforeUnChecked() == "function") {
data.beforeUnChecked.apply($chkBox);
}
$chkBox.parent().addClass('active');
if (typeof data.afterUnChecked() == "function") {
data.afterUnChecked.apply($chkBox);
}
} else {
if (typeof data.beforeChecked() == "function") {
data.beforeChecked.apply($chkBox);
}
$chkBox.parent().removeClass('active');
if (typeof data.afterChecked() == "function") {
data.afterChecked.apply($chkBox);
}
}
});
},
styleRadio : function () {
var $this = this,
data = $this.data(methods.namespace);
$this.wrap(data.radioWrap).parents('.input-box').addClass('select-child');
if ($this.prop('checked')) {
$this.parent().addClass('active');
}
$this.on('click', function(evt) {
var $radio = $(this);
var radioGroup = $this.prop('name');
if (typeof data.beforeChecked() === "function") {
data.beforeChecked.apply( $this );
}
$('[name="' + radioGroup + '"]').parent().removeClass('active');
$radio.parent().addClass('active');
if (typeof data.afterChecked() === "function") {
data.afterChecked.apply( $this );
}
});
},
autoCheck : function() {
var $this = $(this),
data = $this.data(methods.namespace);
if (!data) {
methods.init.apply($this);
return;
}
if (!$this.prop('checked')) {
$this.prop('checked', true);
$this.parent().addClass('active');
}
},
reset : function() {
var $this = $(this),
data = $this.data(methods.namespace);
if (!data) {
methods.init.apply($this);
return;
}
$this.parent().removeClass('active');
if ($this.prop('checked')) {
if ($this.is('input[type="radio"]')) {
$('[name="' + $this.prop('name') + '"]').parent().removeClass('active');
}
$this.parent().addClass('active');
}
},
remove : function() {
var $this = $(this),
data = $this.data(methods.namespace);
if (!data) {
return;
}
var $input = $this;
$this.parent().before($input).remove();
$this.data(methods.namespace, null);
}
};
$.fn.customFormElements = function(option) {
var args = arguments;
return this.each(function() {
if (typeof option === 'object' || typeof option === 'undefined') {
methods.init.apply(this, [option]);
} else if (typeof option === 'string' && methods[option]) {
methods[option].apply(this, Array.prototype.slice.call(args, 1));
} else {
$.error('Method ' + option + ' does not exist in ' + methods.namespace);
}
});
};
})(window.jQuery);