-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlabelHolders.js
218 lines (195 loc) · 9.07 KB
/
labelHolders.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
(function($) {
/*
// labelHolders.js jQuery Plugin version 1.8
// Copyright 2015 Marek Brzechwa-White
// Allows for proper semantic markup of labels for form inputs with the design trend of inline Labels/Placeholders for cleaner looking forms.
// Does not interfere with actual Placeholder text when present.
*/
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
$.fn.labelHolders = function(options) {
var defaults = {
input: ':input:not(:checkbox, :radio, :button, :submit, :reset)', // input types to apply logic to. DEFAULT all inputs except buttons, check boxes and radio buttons
offset: {left: 0, top: 0}, // offset of label when input has value/placeholder
active_class: 'label', // css after animation for active state
inactive_class: 'placeholder', // css after animation for inactive state
scale: 1, // label scale difference
speed: 150, // speed of animation
position: 'top-center', // active label position (top-left, bottom-right, etc.)
align: 'left', // active label text-alignment
hideOnBlur: false, // hide label on blur when in active state
hideOnFocus: false, // why are you even using this plugin?? Oh yeah, proper semantic markup and it works in IE...
disableDate: false // disable labelHolders on date inputs?
};
var stored = $(this).data('labelHolders') || '';
var conf = $.extend(defaults, options, stored);
var filterDate = conf.disableDate ? ':not([type=date])' : '*';
// Test if the Firefox Legend positioning bug is an issue
var testForm = $('<form><fieldset><legend>testing</legend><input type="text"/></fieldset></form>');
$('body').append(testForm);
testForm.css({'position':'absolute','left':'-10000em','top':'0'});
testForm.find('input').css({'top':0,'position':'absolute'});
var legendAdjust = testForm.find('input').position().top > 0;
testForm.remove();
$(this).data('labelHolders',conf);
function setState(form,input,state,speedOveride) {
var label = form.find('label[for='+input.attr('id')+']'),
iheight = parseInt(input.height(),10),
iwidth = parseInt(input.width(),10),
osize = parseInt(input.css('font-size'),10),
nsize = osize * conf.scale,
offset = input.position();
if(speedOveride && state == 'active') {
label.css({
'font-size' : nsize,
'line-height' : input.css('line-height'),
'white-space' : 'nowrap'
});
}
var lheight = parseInt(label.height(),10),
lwidth = parseInt(label.width(),10),
margin = {top: parseInt(input.css('marginTop'),10)||0, bottom: parseInt(input.css('marginBottom'),10)||0,left: parseInt(input.css('marginLeft'),10)||0,right: parseInt(input.css('marginRight'),10)||0},
padding = {top: parseInt(input.css('padding-top'),10)||0, bottom: parseInt(input.css('padding-bottom'),10)||0,left: parseInt(input.css('padding-left'),10)||0,right: parseInt(input.css('padding-right'),10)||0},
border = {top: parseInt(input.css('border-top-width'),10)||0, bottom: parseInt(input.css('border-bottom-width'),10)||0, left: parseInt(input.css('border-left-width'),10)||0,right: parseInt(input.css('border-right-width'),10)||0},
adjust = legendAdjust && input.offsetParent().is('fieldset') && input.offsetParent().find('legend').length > 0 ? input.offsetParent().find('legend').outerHeight() : 0,
start = {top: offset.top + padding.top + margin.top - adjust, left: offset.left + padding.left + margin.left},
top = conf.position.indexOf('top') != -1 ? start.top - lheight - conf.offset.top : conf.position.indexOf('bottom') != -1 ? start.top + padding.bottom + border.bottom + border.top + iheight + conf.offset.top - adjust : start.top + conf.offset.top,
left = conf.position.indexOf('left') != -1 ? start.left - lwidth - conf.offset.left : conf.position.indexOf('right') != -1 ? start.left + border.right + border.left + padding.right + iwidth + conf.offset.left : conf.align == 'right' ? start.left + conf.offset.left + iwidth - lwidth : start.left + conf.offset.left,
speed = speedOveride ? 0 : conf.speed;
label.css({'line-height' : input.css('line-height')});
if (state == 'active') {
label
.css({'position':'absolute','text-align':conf.align,'display':'block'})
.animate({'top':top,'left':left,'font-size':nsize,'line-height' : input.css('line-height')}, speed, function() {
$(this)
.addClass(conf.active_class)
.removeClass(conf.inactive_class);
input
.addClass(conf.active_class)
.removeClass(conf.inactive_class);
});
} else {
label
.css({position:'absolute','text-align':input.css('text-align'),display:'block'})
.animate({'top':start.top,'left':start.left,'font-size':osize,'line-height' : input.css('line-height')}, speed, function() {
$(this)
.css('opacity',1)
.removeClass(conf.active_class)
.addClass(conf.inactive_class);
input
.removeClass(conf.active_class)
.addClass(conf.inactive_class);
});
}
}
function checkInputState($form,e,m) {
$e = $form.find('#'+$(e).attr('for'));
if (!$e.is('select') && $e.is(conf.input) && $e.is(filterDate) && !$e.is(':hidden')) {
$(e).css({'position':'absolute'});
if(!$e.is('[placeholder]') && $e.val().length == 0) {
if(conf.hideOnFocus) {
$(e).css('opacity',1);
}
setState($form,$e,'inactive',1);
}
else if($e.is('[placeholder]') || $e.val().length != 0) {
if(conf.hideOnFocus || conf.hideOnBlur) {
$(e).css('opacity',0);
}
setState($form,$e,'active',1);
}
}
else if($e.is(conf.input) && $e.is('select') && !$e.is(':hidden')) {
$(e).css({'position':'absolute'});
if($e.find(':selected').val() == '' && $e.find(':selected').text() == '') {
if(conf.hideOnFocus) {
$(e).css('opacity',1);
}
setState($form,$e,'inactive',1);
}
else if($e.find(':selected').val() != '' || $e.find(':selected').text() != '') {
if(conf.hideOnFocus || conf.hideOnBlur) {
$(e).css('opacity',0);
}
setState($form,$e,'active',1);
}
}
}
return this.each(function() {
$form = $(this);
$form.find(conf.input).filter(filterDate)
.focus(function() {
if(!$(this).is('[placeholder]')) {
if(conf.hideOnFocus) {
$form.find('label[for='+$(this).attr('id')+']').animate({opacity:0},(conf.speed)/2);
} else {
setState($form,$(this),'active');
}
}
if (conf.hideOnBlur) {
$form.find('label[for='+$(this).attr('id')+']').animate({opacity:1},conf.speed);
}
})
.blur(function() {
if(!$(this).is('select')) {
if($(this).val().length == 0 && !$(this).is('[placeholder]')) {
if(conf.hideOnFocus) {
$form.find('label[for='+$(this).attr('id')+']').animate({opacity:1},(conf.speed)/2);
}
setState($form,$(this),'inactive');
} else if ($(this).val().length != 0 && conf.hideOnBlur) {
$form.find('label[for='+$(this).attr('id')+']').animate({opacity:0},conf.speed);
}
} else {
if($(this).find(':selected').val() == '' && $(this).find(':selected').text() == '') {
if(conf.hideOnFocus) {
$form.find('label[for='+$(this).attr('id')+']').animate({opacity:1},(conf.speed)/2);
}
setState($form,$(this),'inactive');
} else if ($(this).find(':selected').val() != '' && conf.hideOnBlur) {
$form.find('label[for='+$(this).attr('id')+']').animate({opacity:0},conf.speed);
}
}
});
$form.find('label')
.click(function(e) {
$e = $form.find('#'+$(this).attr('for'));
if ($e.is(conf.input) && $e.is(filterDate)) {
e.preventDefault();
$form.find(conf.input).filter(function(){
if ($(this).attr('id') == $(e.target).attr('for')) {return true} else {return false}
})
.focus();
}
})
.each(function(){
checkInputState($form,this,1);
})
var currentHeight = $(window).height();
var currentWidth = $(window).width();
$(window).resize(function(){
var windowHeight = $(window).height();
var windowWidth = $(window).width();
// did the window really just resize? (I'm looking at your IE 8-)
if (currentHeight == undefined || currentHeight != windowHeight || currentWidth == undefined || currentWidth != windowWidth) {
$form.find('label')
.each(function(){
checkInputState($form,this,1);
})
}
currentHeight = windowHeight;
currentWidth = windowWidth;
})
});
};
})(jQuery);