forked from vebersol/jQuery-Custom-Forms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.custom.forms-0.5.js
More file actions
227 lines (184 loc) · 5.44 KB
/
jquery.custom.forms-0.5.js
File metadata and controls
227 lines (184 loc) · 5.44 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
jQuery Custom Forms Plugin
Version: 0.5
Site: http://vebersol.net/demos/jquery-custom-forms/
Author: Vinícius Ebersol
License: MIT License/GPL License
*/
(function($){
var CustomForm = function(element, options) {
this.element = element;
this.options = options;
this.formElements = this.element.find('input, select');
this.init();
};
CustomForm.prototype = {
init: function(options) {
this.setup();
this.bind();
},
setup: function() {
var _this = this;
this.formElements.each(function() {
_this.create($(this));
});
},
bind: function() {
var _this = this;
$(document).bind('toggleFormElement', function(ev, element, fakeElement, className) {
if (_this.isRadio(element)) {
_this.toggleRadio(element, fakeElement, className);
}
else {
_this.toggleCheckbox(element, fakeElement, className);
}
});
$(document).bind('changeSelectValue', function(ev, element, fakeSelect) {
//var optionName = $(element).children('option[value="'+element.get(0).value+'"]').html();
var optionName = $(element).find('option:selected').html();
fakeSelect.html(optionName);
});
},
create: function(element) {
if (element.hasClass(this.options.keyClass)) {
if (element.get(0).nodeName == 'INPUT') {
this.createCustomInput(element);
return;
}
this.createCustomSelect(element);
}
},
createCustomSelect: function(element) {
var _this = this;
var fakeSelect = this.createFakeElement(element, 'select');
var newValue = function() {
$(document).trigger('changeSelectValue', [element, fakeSelect]);
};
element.before(fakeSelect);
element.on({
change: function() {
newValue();
},
focus: function() {
fakeSelect.addClass(_this.setClass('focused'));
newValue();
},
blur: function() {
fakeSelect.removeClass(_this.setClass('focused'));
},
keyup: function() {
newValue();
},
mousedown: function() {
newValue();
}
});
element.css({
opacity: 0,
position: 'relative',
zIndex: 10,
width: fakeSelect.outerWidth(),
height: fakeSelect.outerHeight()
})
},
createCustomInput: function(element) {
var _this = this;
var fakeElement = this.createFakeElement(element, element.get(0).type);
var className = this.toSlug(element.attr('name'));
fakeElement.addClass(className);
element.addClass(className);
element.before(fakeElement);
element.css({
opacity:0,
position: 'absolute',
left: -99999
});
var toggleFormElement = function() {
$(document).trigger('toggleFormElement', [element, fakeElement, className]);
};
if (element.attr('checked')) {
fakeElement.addClass(this.setClass('checked'));
}
element.on({
focus: function() {
fakeElement.addClass(_this.setClass('focused'));
if (_this.isRadio(element)) {
toggleFormElement();
}
},
blur: function() {
fakeElement.removeClass(_this.setClass('focused'));
},
keypress: function(ev) {
if (!ev.which && ((ev.charCode || ev.charCode === 0) ? ev.charCode: ev.keyCode)) {
ev.which = ev.charCode || ev.keyCode;
}
if (_this.isRadio(element)) {
toggleFormElement();
}
},
change: function() {
fakeElement.addClass(_this.setClass('focused'));
if (_this.isRadio(element)) {
toggleFormElement();
} else {
fakeElement.toggleClass(_this.setClass('checked'));
}
}
});
fakeElement.on({
click: function() {
if (_this.isRadio(element)) {
toggleFormElement();
}
else {
_this.toggleCheckbox(element, fakeElement, className);
}
}
});
},
toggleRadio: function(element, fakeElement, className) {
$('input.' + className).attr('checked', false);
$('span.' + className).removeClass(this.setClass('checked')).removeClass(this.setClass('focused'));
fakeElement.addClass(this.setClass('checked')).addClass(this.setClass('focused'));
fakeElement.next('input.' + className).attr('checked', true);
},
toggleCheckbox: function(element, fakeElement, className) {
if (element.attr('checked')) {
fakeElement.next('input.' + className).attr('checked', false);
fakeElement.removeClass(this.setClass('checked'));
}
else {
fakeElement.next('input.' + className).attr('checked', true);
fakeElement.addClass(this.setClass('checked'));
}
},
createFakeElement: function(element, type) {
//var value = (type == 'select') ? $(element).children('option[value="'+element.get(0).value+'"]').html() : '';
var value = (type == 'select') ? $(element).find('option:selected').html() : '';
var disabled = element.attr('disabled') !== undefined
var fakeElement = $('<span class="'+ this.setClass(type) +'">'+ value +'</span>');
if (element.attr('disabled') !== undefined) {
fakeElement.addClass(this.setClass('disabled'));
}
return fakeElement;
},
isRadio: function(element) {
return element.get(0).type == 'radio';
},
setClass: function(name) {
return this.options.prefix + name;
},
toSlug: function(text) {
return text.toLowerCase().replace(/ /g,'-').replace(/[^\w-]+/g,'');
}
}
var defaultOptions = {
prefix: 'custom-form-',
keyClass: 'cform'
}
$.fn.customForm = function(options) {
options = $.extend(defaultOptions, options);
return new CustomForm(this, options);
};
})(jQuery);