-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunitform.js
330 lines (218 loc) · 8.66 KB
/
unitform.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*--------------------------------------------------------------------------------------------------------------
JQuery Unitform Plug-in (v2.1)
by Chris Lawes
--------------------------------------------------------------------------------------------------------------*/
$.fn.unitform = function(options)
{
/*----------------------------------------------------------------------------------------------------------
SETTINGS
----------------------------------------------------------------------------------------------------------*/
var $self = $(this.selector);
// defaults (fallback values if nothing is passed in from defining plugin)
var defaults = {
fileHelperText : 'No File Selected.',
fileHelperButton : 'Upload'
};
// use config.var to use user option with defult fallback
var config = $.extend({}, defaults, options);
/*----------------------------------------------------------------------------------------------------------
GLOBAL HELPERS
----------------------------------------------------------------------------------------------------------*/
var HELPER = {
// global helper - check if this selector is checked and change parent class
// @param {array} selector - jquery selector
// called on radios and checkboxes
ifChecked: function(selector)
{
if ($(selector).is(':checked'))
{
$(selector).parent('span').addClass('checked');
// if this element is inside a "label" tag,
// add a class to that label
if ($(selector).parents('label').length == 1)
{
$(selector).parents('label').addClass('unitform_label');
}
}
else
{
$(selector).parent('span').removeClass('checked');
// if this element is inside a "label" tag,
// remove a class to that label
if ($(selector).parents('label').length == 1)
{
$(selector).parents('label').removeClass('unitform_label');
}
}
},
// global helper for focus class on form elements
onFocus: function(selector)
{
$(selector).parent('div').addClass('unitform_focus');
},
// global helper to remove focus class on form elements
onBlur: function(selector)
{
$(selector).parent('div').removeClass('unitform_focus');
},
// pinch any class names on the select
// remove, and put them on the new 'unitform_select' wrapper
moveClass: function(selector)
{
// grab the class to carry up
var thisSelectClass = $(selector).attr('class');
// if we have something to carry
if (thisSelectClass)
{
// assume we're in a div (.unitform_*)
var wrapperType = 'div';
// if it's inside a label, add them to that
if ($(selector).parents('label').length == 1)
{
wrapperType = 'label';
}
// remove from this, add to something else
$(selector).removeClass( thisSelectClass ).parent().closest( wrapperType ).addClass( thisSelectClass );
}
}
};
/*----------------------------------------------------------------------------------------------------------
FORM FUNCTIONS
----------------------------------------------------------------------------------------------------------*/
/**
* Form functions
* [*]_markup, [*]_onChange - for each (select, radio, check, file)
*/
var FORM = {
/*------------------------------------------------------------------------------------------------------
SELECT
------------------------------------------------------------------------------------------------------*/
// change selectbox markup; add wrapper, span for value (+ span for arrow?)
// @param {array} selector - jquery selector
select_markup: function(selector)
{
// add wrapper and spans for value (and em for arrow)
$(selector).wrap('<div class="unitform_select"></div>').parent().append('<span>' + $(selector).find('option:selected').text() + '</span> <em>Arrow</em>');
// keep element class (move from form element, to html wrapper)
HELPER.moveClass(selector);
// move disabled attribute if present
if($(selector).attr('disabled'))
{
$(selector).parent().addClass('unitform_disabled');
}
},
// on change (selectbox) - change class and 1st span content
// @param {array} selector - jquery selector
select_onChange: function(selector)
{
// find new value, pass to parents span
$(selector).parent().find('span').html( $(selector).find('option:selected').text() );
},
/*------------------------------------------------------------------------------------------------------
RADIO
------------------------------------------------------------------------------------------------------*/
// change radio markup function
// use: check if already checked (on load) and toggle class
// use: bind to click to toggle active class
// @param {array} selector - jquery selector
radio_markup: function(selector)
{
// add wrapper
$(selector).wrap('<div class="unitform_radio"><span></span></div>');
// if it's already checked, add checked class
HELPER.ifChecked(selector);
// keep element class (move from form element, to html wrapper)
HELPER.moveClass(selector);
},
// on change (radio) - change class of this if the thing (passed in) un/checked
// remove checked class from all other radios in this group
// @param {array} selector - jquery selector
radio_onChange: function(selector)
{
// find the group this radio belongs to (via their shared name)
var thisGroupName = $(selector).attr('name');
// if this radio is in a group
if (thisGroupName.length)
{
// loop all radios with this name, uncheck and remove active class
$('input:radio[name=' + thisGroupName +']').each(function()
{
$(this)
.prop('checked', false).parent('span').removeClass('checked');
if ($(this).parents('label').length == 1)
{
$(this).parents('label').removeClass('unitform_label');
}
});
}
// add active class to the one clicked
$(selector)
.prop('checked', true).parent('span').addClass('checked');
if ($(selector).parents('label').length == 1)
{
$(selector).parents('label').addClass('unitform_label');
}
},
/*------------------------------------------------------------------------------------------------------
CHECKBOX
------------------------------------------------------------------------------------------------------*/
checkbox_markup: function(selector){
// add wrapper
$(selector).wrap('<div class="unitform_checkbox"><span></span></div>');
// if it's already checked, add checked class
HELPER.ifChecked(selector);
// keep element class (move from form element, to html wrapper)
HELPER.moveClass(selector);
},
// on change (checkbox) - change class of this if the thing (passed in) un/checked
checkbox_onChange: function(selector)
{
// run if checked helper function
HELPER.ifChecked(selector);
},
/*------------------------------------------------------------------------------------------------------
FILE
------------------------------------------------------------------------------------------------------*/
// add unitform wrapper, text holder (span) and dummy button
file_markup: function(selector){
$(selector).wrap('<div class="unitform_file"></div>').parent().append('<span>' + config.fileHelperText + '</span> <em>' + config.fileHelperButton + '</em>');
},
// update new span tag with value (file name) when one is added
file_onChange: function(selector){
var uploadFileName = $(selector).val().split('/').pop(); // get this file name, without "fake/path/"
$(selector).parent().find('span').text( uploadFileName );
}
};
/*----------------------------------------------------------------------------------------------------------
MAIN PLUG-IN
Bind helper/form functions to actions on each form element
----------------------------------------------------------------------------------------------------------*/
// don't run if none of the elements are on this page
if ($self.length)
{
// for each form element passed in
$self.each(function()
{
// get element type (select or radio or file ...etc)
var type = this.type || this.tagName.toLowerCase(),
typeName = type.split('-')[0];
// change markup form function
FORM[typeName + '_markup'](this);
// bind change form function to this
$(this).bind('change', function()
{
FORM[typeName + '_onChange'](this); // find helper using the name of this element
});
// bind focus helper function to this
$(this).bind('focus', function()
{
HELPER.onFocus(this);
});
// bind blur helper function to this
$(this).bind('blur', function()
{
HELPER.onBlur(this);
});
}); // end; each element
} // end; if length
}; // End; unitform plug-in